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

Return to SDK Examples for Microchip Studio

1/*
2 * Title: MoveVelocity
3 *
4 * Objective:
5 * This example demonstrates control of a ClearPath motor in Step and
6 * Direction mode, making velocity moves.
7 *
8 * Description:
9 * This example enables a ClearPath then commands a series of repeating
10 * velocity 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: Set the Input Resolution in MSP the same as your motor's Positioning
25 * Resolution spec if you'd like the pulse frequency sent by ClearCore to
26 * command the same frequency in motor encoder counts/sec, a 1:1 ratio.
27 *
28 * Links:
29 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
30 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
31 * ** ClearPath Manual (DC Power): https://www.teknic.com/files/downloads/clearpath_user_manual.pdf
32 * ** ClearPath Manual (AC Power): https://www.teknic.com/files/downloads/ac_clearpath-mc-sd_manual.pdf
33 *
34 *
35 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
36 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
37 */
38
39#include "ClearCore.h"
40
41// Specifies which motor to move.
42// Options are: ConnectorM0, ConnectorM1, ConnectorM2, or ConnectorM3.
43#define motor ConnectorM0
44
45// Select the baud rate to match the target serial device
46#define baudRate 9600
47
48// Specify which serial to use: ConnectorUsb, ConnectorCOM0, or ConnectorCOM1.
49#define SerialPort ConnectorUsb
50
51// This example has built-in functionality to automatically clear motor alerts,
52// including motor shutdowns. Any uncleared alert will cancel and disallow motion.
53// WARNING: enabling automatic alert handling will clear alerts immediately when
54// encountered and return a motor to a state in which motion is allowed. Before
55// enabling this functionality, be sure to understand this behavior and ensure
56// your system will not enter an unsafe state.
57// To enable automatic alert handling, #define HANDLE_ALERTS (1)
58// To disable automatic alert handling, #define HANDLE_ALERTS (0)
59#define HANDLE_ALERTS (0)
60
61// Define the acceleration limit to be used for each move
62int32_t accelerationLimit = 100000; // pulses per sec^2
63
64// Declares user-defined helper functions.
65// The definition/implementations of these functions are at the bottom of the sketch.
66bool MoveAtVelocity(int32_t velocity);
67void PrintAlerts();
68void HandleAlerts();
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 // Set the maximum acceleration for each move
85 motor.AccelMax(accelerationLimit);
86
87 // Sets up serial communication and waits up to 5 seconds for a port to open.
88 // Serial communication is not required for this example to run.
89 SerialPort.Mode(Connector::USB_CDC);
90 SerialPort.Speed(baudRate);
91 uint32_t timeout = 5000;
92 uint32_t startTime = Milliseconds();
93 SerialPort.PortOpen();
94 while (!SerialPort && Milliseconds() - startTime < timeout) {
95 continue;
96 }
97
98 // Enables the motor; homing will begin automatically if enabled
99 motor.EnableRequest(true);
100 SerialPort.SendLine("Motor Enabled");
101
102 // Waits for HLFB to assert (waits for homing to complete if applicable)
103 SerialPort.SendLine("Waiting for HLFB...");
104 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
105 !motor.StatusReg().bit.AlertsPresent) {
106 continue;
107 }
108 // Check if motor alert occurred during enabling
109 // Clear alert if configured to do so
110 if (motor.StatusReg().bit.AlertsPresent) {
111 SerialPort.SendLine("Motor alert detected.");
112 PrintAlerts();
113 if(HANDLE_ALERTS){
114 HandleAlerts();
115 } else {
116 SerialPort.SendLine("Enable automatic alert handling by setting HANDLE_ALERTS to 1.");
117 }
118 SerialPort.SendLine("Enabling may not have completed as expected. Proceed with caution.");
119 SerialPort.SendLine();
120 } else {
121 SerialPort.SendLine("Motor Ready");
122 }
123
124 while (true) {
125 // Move at 1,000 steps/sec for 2000ms
126 MoveAtVelocity(1000);
127 Delay_ms(2000);
128 // Move at -5,000 steps/sec for 2000ms
129 MoveAtVelocity(-5000);
130 Delay_ms(2000);
131 // Move at 10,000 steps/sec for 2000ms
132 MoveAtVelocity(10000);
133 Delay_ms(2000);
134 // Move at -10,000 steps/sec for 2000ms
135 MoveAtVelocity(-10000);
136 Delay_ms(2000);
137 // Command a 0 steps/sec velocity to stop motion for 2000ms
138 MoveAtVelocity(0);
139 Delay_ms(2000);
140 }
141}
142
143/*------------------------------------------------------------------------------
144 * MoveAtVelocity
145 *
146 * Command the motor to move at the specified "velocity", in steps/second.
147 * Prints the move status to the USB serial port
148 *
149 * Parameters:
150 * int velocity - The velocity, in step steps/sec, to command
151 *
152 * Returns: None
153 */
154bool MoveAtVelocity(int32_t velocity) {
155 // Check if a motor alert is currently preventing motion
156 // Clear alert if configured to do so
157 if (motor.StatusReg().bit.AlertsPresent) {
158 SerialPort.SendLine("Motor alert detected.");
159 PrintAlerts();
160 if(HANDLE_ALERTS){
161 HandleAlerts();
162 } else {
163 SerialPort.SendLine("Enable automatic alert handling by setting HANDLE_ALERTS to 1.");
164 }
165 SerialPort.SendLine("Move canceled.");
166 SerialPort.SendLine();
167 return false;
168 }
169
170 SerialPort.Send("Commanding velocity: ");
171 SerialPort.SendLine(velocity);
172
173 // Command the velocity move
174 motor.MoveVelocity(velocity);
175
176 // Waits for the step command to ramp up/down to the commanded velocity.
177 // This time will depend on your Acceleration Limit.
178 SerialPort.SendLine("Ramping to speed...");
179 while (!motor.StatusReg().bit.AtTargetVelocity) {
180 continue;
181 }
182 // Check if motor alert occurred during move
183 // Clear alert if configured to do so
184 if (motor.StatusReg().bit.AlertsPresent) {
185 SerialPort.SendLine("Motor alert detected.");
186 PrintAlerts();
187 if(HANDLE_ALERTS){
188 HandleAlerts();
189 } else {
190 SerialPort.SendLine("Enable automatic fault handling by setting HANDLE_ALERTS to 1.");
191 }
192 SerialPort.SendLine("Motion may not have completed as expected. Proceed with caution.");
193 SerialPort.SendLine();
194 return false;
195 } else {
196 SerialPort.SendLine("Move Done");
197 return true;
198 }
199}
200//------------------------------------------------------------------------------
201
202
203/*------------------------------------------------------------------------------
204 * PrintAlerts
205 *
206 * Prints active alerts.
207 *
208 * Parameters:
209 * requires "motor" to be defined as a ClearCore motor connector
210 *
211 * Returns:
212 * none
213 */
214 void PrintAlerts(){
215 // report status of alerts
216 SerialPort.SendLine("Alerts present: ");
217 if(motor.AlertReg().bit.MotionCanceledInAlert){
218 SerialPort.SendLine(" MotionCanceledInAlert "); }
219 if(motor.AlertReg().bit.MotionCanceledPositiveLimit){
220 SerialPort.SendLine(" MotionCanceledPositiveLimit "); }
221 if(motor.AlertReg().bit.MotionCanceledNegativeLimit){
222 SerialPort.SendLine(" MotionCanceledNegativeLimit "); }
223 if(motor.AlertReg().bit.MotionCanceledSensorEStop){
224 SerialPort.SendLine(" MotionCanceledSensorEStop "); }
225 if(motor.AlertReg().bit.MotionCanceledMotorDisabled){
226 SerialPort.SendLine(" MotionCanceledMotorDisabled "); }
227 if(motor.AlertReg().bit.MotorFaulted){
228 SerialPort.SendLine(" MotorFaulted ");
229 }
230 }
231//------------------------------------------------------------------------------
232
233
234/*------------------------------------------------------------------------------
235 * HandleAlerts
236 *
237 * Clears alerts, including motor faults.
238 * Faults are cleared by cycling enable to the motor.
239 * Alerts are cleared by clearing the ClearCore alert register directly.
240 *
241 * Parameters:
242 * requires "motor" to be defined as a ClearCore motor connector
243 *
244 * Returns:
245 * none
246 */
247 void HandleAlerts(){
248 if(motor.AlertReg().bit.MotorFaulted){
249 // if a motor fault is present, clear it by cycling enable
250 SerialPort.SendLine("Faults present. Cycling enable signal to motor to clear faults.");
251 motor.EnableRequest(false);
252 Delay_ms(10);
253 motor.EnableRequest(true);
254 }
255 // clear alerts
256 SerialPort.SendLine("Clearing alerts.");
257 motor.ClearAlerts();
258 }
259//------------------------------------------------------------------------------
260
261
262
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.