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

Return to SDK Examples for Microchip Studio

1/*
2 * Title: MovePositionRelative
3 *
4 * Objective:
5 * This example demonstrates control of a ClearPath motor in Step and
6 * Direction mode.
7 *
8 * Description:
9 * This example enables a ClearPath then commands a series of repeating
10 * moves to the motor.
11 *
12 * Requirements:
13 * 1. A ClearPath motor must be connected to Connector M-0.
14 * 2. The connected ClearPath motor must be configured through the MSP software
15 * for Step and Direction mode (In MSP select Mode>>Step and Direction).
16 * 3. The ClearPath motor must be set to use the HLFB mode "ASG-Position
17 * w/Measured Torque" with a PWM carrier frequency of 482 Hz through the MSP
18 * software (select Advanced>>High Level Feedback [Mode]... then choose
19 * "ASG-Position w/Measured Torque" from the dropdown, make sure that 482 Hz
20 * is selected in the "PWM Carrier Frequency" dropdown, and hit the OK
21 * button).
22 * 4. Set the Input Format in MSP for "Step + Direction".
23 *
24 * ** Note: Homing is optional, and not required in this operational mode or in
25 * this example. This example makes its first move in the positive direction,
26 * assuming any homing move occurs in the negative direction.
27 *
28 * ** Note: Set the Input Resolution in MSP the same as your motor's Positioning
29 * Resolution spec if you'd like the pulses sent by ClearCore to command a
30 * move of the same number of Encoder Counts, a 1:1 ratio.
31 *
32 * Links:
33 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
34 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
35 * ** ClearPath Manual (DC Power): https://www.teknic.com/files/downloads/clearpath_user_manual.pdf
36 * ** ClearPath Manual (AC Power): https://www.teknic.com/files/downloads/ac_clearpath-mc-sd_manual.pdf
37 *
38 *
39 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
40 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
41 */
42
43#include "ClearCore.h"
44
45// Specifies which motor to move.
46// Options are: ConnectorM0, ConnectorM1, ConnectorM2, or ConnectorM3.
47#define motor ConnectorM0
48
49// Select the baud rate to match the target serial device
50#define baudRate 9600
51
52// Specify which serial to use: ConnectorUsb, ConnectorCOM0, or ConnectorCOM1.
53#define SerialPort ConnectorUsb
54
55// This example has built-in functionality to automatically clear motor alerts,
56// including motor shutdowns. Any uncleared alert will cancel and disallow motion.
57// WARNING: enabling automatic alert handling will clear alerts immediately when
58// encountered and return a motor to a state in which motion is allowed. Before
59// enabling this functionality, be sure to understand this behavior and ensure
60// your system will not enter an unsafe state.
61// To enable automatic alert handling, #define HANDLE_ALERTS (1)
62// To disable automatic alert handling, #define HANDLE_ALERTS (0)
63#define HANDLE_ALERTS (0)
64
65// Define the velocity and acceleration limits to be used for each move
66int32_t velocityLimit = 10000; // pulses per sec
67int32_t accelerationLimit = 100000; // pulses per sec^2
68
69// Declares user-defined helper functions.
70// The definition/implementations of these functions are at the bottom of the sketch.
71bool MoveDistance(int32_t distance);
72void PrintAlerts();
73void HandleAlerts();
74
75int main() {
76 // Sets the input clocking rate. This normal rate is ideal for ClearPath
77 // step and direction applications.
78 MotorMgr.MotorInputClocking(MotorManager::CLOCK_RATE_NORMAL);
79
80 // Sets all motor connectors into step and direction mode.
81 MotorMgr.MotorModeSet(MotorManager::MOTOR_ALL,
82 Connector::CPM_MODE_STEP_AND_DIR);
83
84 // Set the motor's HLFB mode to bipolar PWM
85 motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
86 // Set the HFLB carrier frequency to 482 Hz
87 motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
88
89 // Sets the maximum velocity for each move
90 motor.VelMax(velocityLimit);
91
92 // Set the maximum acceleration for each move
93 motor.AccelMax(accelerationLimit);
94
95 // Sets up serial communication and waits up to 5 seconds for a port to open.
96 // Serial communication is not required for this example to run.
97 SerialPort.Mode(Connector::USB_CDC);
98 SerialPort.Speed(baudRate);
99 uint32_t timeout = 5000;
100 uint32_t startTime = Milliseconds();
101 SerialPort.PortOpen();
102 while (!SerialPort && Milliseconds() - startTime < timeout) {
103 continue;
104 }
105
106 // Enables the motor; homing will begin automatically if enabled
107 motor.EnableRequest(true);
108 SerialPort.SendLine("Motor Enabled");
109
110 // Waits for HLFB to assert (waits for homing to complete if applicable)
111 SerialPort.SendLine("Waiting for HLFB...");
112 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
113 !motor.StatusReg().bit.AlertsPresent) {
114 continue;
115 }
116 // Check if motor alert occurred during enabling
117 // Clear alert if configured to do so
118 if (motor.StatusReg().bit.AlertsPresent) {
119 SerialPort.SendLine("Motor alert detected.");
120 PrintAlerts();
121 if(HANDLE_ALERTS){
122 HandleAlerts();
123 } else {
124 SerialPort.SendLine("Enable automatic alert handling by setting HANDLE_ALERTS to 1.");
125 }
126 SerialPort.SendLine("Enabling may not have completed as expected. Proceed with caution.");
127 SerialPort.SendLine();
128 } else {
129 SerialPort.SendLine("Motor Ready");
130 }
131
132 while (true) {
133 // Move 6400 counts (positive direction), then wait 2000ms
134 MoveDistance(6400);
135 Delay_ms(2000);
136 // Move 19200 counts farther positive, then wait 2000ms
137 MoveDistance(19200);
138 Delay_ms(2000);
139 // Move back 12800 counts (negative direction), then wait 2000ms
140 MoveDistance(-12800);
141 Delay_ms(2000);
142 // Move back 6400 counts (negative direction), then wait 2000ms
143 MoveDistance(-6400);
144 Delay_ms(2000);
145 // Move back to the start (negative 6400 pulses), then wait 2000ms
146 MoveDistance(-6400);
147 Delay_ms(2000);
148 }
149}
150
151/*------------------------------------------------------------------------------
152 * MoveDistance
153 *
154 * Command "distance" number of step pulses away from the current position
155 * Prints the move status to the USB serial port
156 * Returns when HLFB asserts (indicating the motor has reached the commanded
157 * position)
158 *
159 * Parameters:
160 * int distance - The distance, in step pulses, to move
161 *
162 * Returns: True/False depending on whether the move was successfully triggered.
163 */
164bool MoveDistance(int32_t distance) {
165 // Check if a motor alert is currently preventing motion
166 // Clear alert if configured to do so
167 if (motor.StatusReg().bit.AlertsPresent) {
168 SerialPort.SendLine("Motor alert detected.");
169 PrintAlerts();
170 if(HANDLE_ALERTS){
171 HandleAlerts();
172 } else {
173 SerialPort.SendLine("Enable automatic alert handling by setting HANDLE_ALERTS to 1.");
174 }
175 SerialPort.SendLine("Move canceled.");
176 SerialPort.SendLine();
177 return false;
178 }
179
180 SerialPort.Send("Moving distance: ");
181 SerialPort.SendLine(distance);
182
183 // Command the move of incremental distance
184 motor.Move(distance);
185
186 // Waits for HLFB to assert (signaling the move has successfully completed)
187 SerialPort.SendLine("Moving.. Waiting for HLFB");
188 while ( (!motor.StepsComplete() || motor.HlfbState() != MotorDriver::HLFB_ASSERTED) &&
189 !motor.StatusReg().bit.AlertsPresent) {
190 continue;
191 }
192 // Check if motor alert occurred during move
193 // Clear alert if configured to do so
194 if (motor.StatusReg().bit.AlertsPresent) {
195 SerialPort.SendLine("Motor alert detected.");
196 PrintAlerts();
197 if(HANDLE_ALERTS){
198 HandleAlerts();
199 } else {
200 SerialPort.SendLine("Enable automatic fault handling by setting HANDLE_ALERTS to 1.");
201 }
202 SerialPort.SendLine("Motion may not have completed as expected. Proceed with caution.");
203 SerialPort.SendLine();
204 return false;
205 } else {
206 SerialPort.SendLine("Move Done");
207 return true;
208 }
209}
210//------------------------------------------------------------------------------
211
212
213/*------------------------------------------------------------------------------
214 * PrintAlerts
215 *
216 * Prints active alerts.
217 *
218 * Parameters:
219 * requires "motor" to be defined as a ClearCore motor connector
220 *
221 * Returns:
222 * none
223 */
224 void PrintAlerts(){
225 // report status of alerts
226 SerialPort.SendLine("Alerts present: ");
227 if(motor.AlertReg().bit.MotionCanceledInAlert){
228 SerialPort.SendLine(" MotionCanceledInAlert "); }
229 if(motor.AlertReg().bit.MotionCanceledPositiveLimit){
230 SerialPort.SendLine(" MotionCanceledPositiveLimit "); }
231 if(motor.AlertReg().bit.MotionCanceledNegativeLimit){
232 SerialPort.SendLine(" MotionCanceledNegativeLimit "); }
233 if(motor.AlertReg().bit.MotionCanceledSensorEStop){
234 SerialPort.SendLine(" MotionCanceledSensorEStop "); }
235 if(motor.AlertReg().bit.MotionCanceledMotorDisabled){
236 SerialPort.SendLine(" MotionCanceledMotorDisabled "); }
237 if(motor.AlertReg().bit.MotorFaulted){
238 SerialPort.SendLine(" MotorFaulted ");
239 }
240 }
241//------------------------------------------------------------------------------
242
243
244/*------------------------------------------------------------------------------
245 * HandleAlerts
246 *
247 * Clears alerts, including motor faults.
248 * Faults are cleared by cycling enable to the motor.
249 * Alerts are cleared by clearing the ClearCore alert register directly.
250 *
251 * Parameters:
252 * requires "motor" to be defined as a ClearCore motor connector
253 *
254 * Returns:
255 * none
256 */
257 void HandleAlerts(){
258 if(motor.AlertReg().bit.MotorFaulted){
259 // if a motor fault is present, clear it by cycling enable
260 SerialPort.SendLine("Faults present. Cycling enable signal to motor to clear faults.");
261 motor.EnableRequest(false);
262 Delay_ms(10);
263 motor.EnableRequest(true);
264 }
265 // clear alerts
266 SerialPort.SendLine("Clearing alerts.");
267 motor.ClearAlerts();
268 }
269//------------------------------------------------------------------------------
270
271
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.