ClearCore Library
Loading...
Searching...
No Matches
UserSeeksHome.cpp

Return to SDK Examples for Microchip Studio

1/*
2 * Title: UserSeeksHome
3 *
4 * Objective:
5 * This example demonstrates control of the "User Seeks Home" homing feature
6 * of a ClearPath-SD motor (this feature is also available in ClearPath-MCPV
7 * Pulse Burst Positioning Mode, but this example is only for Step and
8 * Direction mode). "User Seeks Home" should be used when more flexibility is
9 * required during a homing sequence (e.g. to move at multiple velocities,
10 * stopping to perform other tasks, manually exiting a homing sequence, or
11 * using sensor-based homing).
12 *
13 * Description:
14 * This example enables the motor, moves towards a hardstop at a velocity for
15 * 2 seconds, then with a slower velocity until clamping into the hardstop.
16 * An offset position move is then commanded away from the hardstop to
17 * complete the homing sequence. Homing status is printed to the USB serial
18 * port.
19 *
20 * Requirements:
21 * 1. A ClearPath motor must be connected to Connector M-0.
22 * 2. The connected ClearPath motor must be configured through the MSP software
23 * for Step and Direction mode (In MSP select Mode>>Step and Direction).
24 * 3. The ClearPath motor must be set to use the HLFB mode "ASG-Position
25 * w/Measured Torque" with a PWM carrier frequency of 482 Hz through the MSP
26 * software (select Advanced>>High Level Feedback [Mode]... then choose
27 * "ASG-Position w/Measured Torque" from the dropdown, make sure that 482 Hz
28 * is selected in the "PWM Carrier Frequency" dropdown, and hit the OK
29 * button).
30 * 4. The ClearPath must have homing enabled and configured. To configure, look
31 * under the "Homing" label on the MSP's main window, check the "Enabled"
32 * radial button, then click the "Setup..." button. Set the Homing Style set
33 * to "User seeks home; ClearPath ASG signals when homing is complete" then
34 * hit the OK button.
35 * 5. A hardstop for homing must be installed on your mechanics. Set the homing
36 * torque limit accordingly. This torque will be used to move toward and
37 * clamp up against the hardstop.
38 *
39 * ** IMPORTANT: This examples homes in the Positive (CCW) direction, assuming
40 * the hardstop is on the positive end of travel
41 *
42 * ** Note: Set the Input Resolution in MSP the same as your motor's Positioning
43 * Resolution spec if you'd like the pulses sent by ClearCore to command a
44 * move of the same number of Encoder Counts, a 1:1 ratio.
45 *
46 * Links:
47 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
48 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
49 * ** ClearPath Manual (DC Power): https://www.teknic.com/files/downloads/clearpath_user_manual.pdf
50 * ** ClearPath Manual (AC Power): https://www.teknic.com/files/downloads/ac_clearpath-mc-sd_manual.pdf
51 *
52 *
53 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
54 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
55 */
56
57#include "ClearCore.h"
58
59// Specify which motor to move.
60// Options are: ConnectorM0, ConnectorM1, ConnectorM2, or ConnectorM3.
61#define motor ConnectorM0
62
63// Selects the baud rate to match the target serial device
64#define baudRate 9600
65
66// Specify which serial connector to use: ConnectorUsb, ConnectorCOM0, or
67// ConnectorCOM1
68#define SerialPort ConnectorUsb
69
70int main() {
71 // Sets the input clocking rate. This normal rate is ideal for ClearPath
72 // step and direction applications.
73 MotorMgr.MotorInputClocking(MotorManager::CLOCK_RATE_NORMAL);
74
75 // Sets all motor connectors into step and direction mode.
76 MotorMgr.MotorModeSet(MotorManager::MOTOR_ALL,
77 Connector::CPM_MODE_STEP_AND_DIR);
78
79 // Set the motor's HLFB mode to bipolar PWM
80 motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
81 // Set the HFLB carrier frequency to 482 Hz
82 motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
83
84 // Sets the maximum velocity in step pulses/sec.
85 motor.VelMax(10000);
86 // Sets the maximum acceleration in step pulses/sec^2.
87 motor.AccelMax(100000);
88
89 // Sets up serial communication and waits up to 5 seconds for a port to open.
90 // Serial communication is not required for this example to run.
91 SerialPort.Mode(Connector::USB_CDC);
92 SerialPort.Speed(baudRate);
93 uint32_t timeout = 5000;
94 uint32_t startTime = Milliseconds();
95 SerialPort.PortOpen();
96 while (!SerialPort && Milliseconds() - startTime < timeout) {
97 continue;
98 }
99
100 // Enables the motor
101 motor.EnableRequest(true);
102 SerialPort.SendLine("Motor Enabled");
103
104 // Check if an alert would prevent motion
105 if (motor.StatusReg().bit.AlertsPresent) {
106 // In this case, we can't proceed with homing.
107 SerialPort.SendLine("Motor status: 'In Alert'. Move Canceled.");
108 // The end...
109 while (true) {
110 continue;
111 }
112 }
113
114 // Commands a speed of 5000 pulses/sec towards the hardstop for 2 seconds
115 SerialPort.SendLine("Moving toward hardstop... Waiting for HLFB");
116 motor.MoveVelocity(5000);
117 Delay_ms(2000);
118 // Then slows down to 1000 pulses/sec until clamping into the hard stop
119 motor.MoveVelocity(1000);
120
121 // Check if an alert occurred during motion
122 if (motor.StatusReg().bit.AlertsPresent) {
123 // In this case, we can't proceed with homing. Print the alert and bail.
124 SerialPort.SendLine("Motor alert occurred during motion. Homing canceled.");
125 // The end...
126 while (true) {
127 continue;
128 }
129 }
130
131
132 // Delay so HLFB has time to deassert
133 Delay_ms(20);
134 // Waits for HLFB to assert again, meaning the hardstop has been reached
135 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED) {
136 if (motor.StatusReg().bit.AlertsPresent) {
137 // In this case, we can't proceed with homing. Print the alert and bail.
138 SerialPort.SendLine("Motor alert detected. Homing canceled.");
139 // The end...
140 while (true) {
141 continue;
142 }
143 }
144 }
145
146 // Stop the velocity move now that the hardstop is reached
147 motor.MoveStopAbrupt();
148
149 // Move away from the hard stop. Any move away from the hardstop will
150 // conclude the homing sequence.
151 motor.Move(-1000);
152
153 // Delay so HLFB has time to deassert
154 Delay_ms(20);
155 // Waits for HLFB to assert, meaning homing is complete
156 SerialPort.SendLine("Moving away from hardstop... Waiting for HLFB");
157 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED) {
158 continue;
159 }
160
161 // Check if an alert occurred during offset move
162 if (motor.StatusReg().bit.AlertsPresent) {
163 // In this case, we can't proceed with homing. Print the alert and bail.
164 SerialPort.SendLine("Motor alert occurred during offset move. Homing canceled.");
165 // The end...
166 while (true) {
167 continue;
168 }
169 } else {
170 SerialPort.SendLine("Homing Complete. Motor Ready.");
171 }
172 // Zero the motor's reference position after homing to allow for accurate
173 // absolute position moves
174 motor.PositionRefSet(0);
175}
void Delay_ms(uint32_t ms)
Blocks operations for ms milliseconds.
Definition SysTiming.h:287
uint32_t Milliseconds(void)
Number of milliseconds since the ClearCore was initialized.
bool MotorInputClocking(MotorClockRates newRate)
Sets the output step rate for the motor step generators.
bool MotorModeSet(MotorPair motorPair, Connector::ConnectorModes newMode)
Sets the operational mode for the specified MotorDriver connectors.