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

Return to SDK Examples for Microchip Studio

1/*
2 * Title: MovePositionAbsolute
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 * absolute position 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 positive absolute position moves,
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 MoveAbsolutePosition(int32_t position);
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 to +10000 counts (positive direction), then wait 2000ms
134 MoveAbsolutePosition(10000);
135 Delay_ms(2000);
136 // Move to 19200 counts, then wait 2000ms
137 MoveAbsolutePosition(19200);
138 Delay_ms(2000);
139 // Move to 12800 counts, then wait 2000ms
140 MoveAbsolutePosition(12800);
141 Delay_ms(2000);
142 // Move back to "home", then wait 2000ms
143 MoveAbsolutePosition(0);
144 Delay_ms(2000);
145 }
146}
147
148/*------------------------------------------------------------------------------
149 * MoveAbsolutePosition
150 *
151 * Command step pulses to move the motor's current position to the absolute
152 * position specified by "position"
153 * Prints the move status to the USB serial port
154 * Returns when HLFB asserts (indicating the motor has reached the commanded
155 * position)
156 *
157 * Parameters:
158 * int position - The absolute position, in step pulses, to move to
159 *
160 * Returns: True/False depending on whether the move was successfully triggered.
161 */
162bool MoveAbsolutePosition(int32_t position) {
163 // Check if a motor alert is currently preventing motion
164 // Clear alert if configured to do so
165 if (motor.StatusReg().bit.AlertsPresent) {
166 SerialPort.SendLine("Motor alert detected.");
167 PrintAlerts();
168 if(HANDLE_ALERTS){
169 HandleAlerts();
170 } else {
171 SerialPort.SendLine("Enable automatic alert handling by setting HANDLE_ALERTS to 1.");
172 }
173 SerialPort.SendLine("Move canceled.");
174 SerialPort.SendLine();
175 return false;
176 }
177
178 SerialPort.Send("Moving to absolute position: ");
179 SerialPort.SendLine(position);
180
181 // Command the move of absolute distance
182 motor.Move(position, MotorDriver::MOVE_TARGET_ABSOLUTE);
183
184 // Waits for HLFB to assert (signaling the move has successfully completed)
185 SerialPort.SendLine("Moving.. Waiting for HLFB");
186 while ( (!motor.StepsComplete() || motor.HlfbState() != MotorDriver::HLFB_ASSERTED) &&
187 !motor.StatusReg().bit.AlertsPresent) {
188 continue;
189 }
190 // Check if motor alert occurred during move
191 // Clear alert if configured to do so
192 if (motor.StatusReg().bit.AlertsPresent) {
193 SerialPort.SendLine("Motor alert detected.");
194 PrintAlerts();
195 if(HANDLE_ALERTS){
196 HandleAlerts();
197 } else {
198 SerialPort.SendLine("Enable automatic fault handling by setting HANDLE_ALERTS to 1.");
199 }
200 SerialPort.SendLine("Motion may not have completed as expected. Proceed with caution.");
201 SerialPort.SendLine();
202 return false;
203 } else {
204 SerialPort.SendLine("Move Done");
205 return true;
206 }
207}
208//------------------------------------------------------------------------------
209
210
211/*------------------------------------------------------------------------------
212 * PrintAlerts
213 *
214 * Prints active alerts.
215 *
216 * Parameters:
217 * requires "motor" to be defined as a ClearCore motor connector
218 *
219 * Returns:
220 * none
221 */
222 void PrintAlerts(){
223 // report status of alerts
224 SerialPort.SendLine("Alerts present: ");
225 if(motor.AlertReg().bit.MotionCanceledInAlert){
226 SerialPort.SendLine(" MotionCanceledInAlert "); }
227 if(motor.AlertReg().bit.MotionCanceledPositiveLimit){
228 SerialPort.SendLine(" MotionCanceledPositiveLimit "); }
229 if(motor.AlertReg().bit.MotionCanceledNegativeLimit){
230 SerialPort.SendLine(" MotionCanceledNegativeLimit "); }
231 if(motor.AlertReg().bit.MotionCanceledSensorEStop){
232 SerialPort.SendLine(" MotionCanceledSensorEStop "); }
233 if(motor.AlertReg().bit.MotionCanceledMotorDisabled){
234 SerialPort.SendLine(" MotionCanceledMotorDisabled "); }
235 if(motor.AlertReg().bit.MotorFaulted){
236 SerialPort.SendLine(" MotorFaulted ");
237 }
238 }
239//------------------------------------------------------------------------------
240
241
242/*------------------------------------------------------------------------------
243 * HandleAlerts
244 *
245 * Clears alerts, including motor faults.
246 * Faults are cleared by cycling enable to the motor.
247 * Alerts are cleared by clearing the ClearCore alert register directly.
248 *
249 * Parameters:
250 * requires "motor" to be defined as a ClearCore motor connector
251 *
252 * Returns:
253 * none
254 */
255 void HandleAlerts(){
256 if(motor.AlertReg().bit.MotorFaulted){
257 // if a motor fault is present, clear it by cycling enable
258 SerialPort.SendLine("Faults present. Cycling enable signal to motor to clear faults.");
259 motor.EnableRequest(false);
260 Delay_ms(10);
261 motor.EnableRequest(true);
262 }
263 // clear alerts
264 SerialPort.SendLine("Clearing alerts.");
265 motor.ClearAlerts();
266 }
267//------------------------------------------------------------------------------
268
269
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.