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

Return to SDK Examples for Microchip Studio

1/*
2 * Title: RampUpDownToSelectedVelocity
3 *
4 * Objective:
5 * This example demonstrates control of the ClearPath-MC operational mode
6 * Ramp Up/Down To Selected Velocity.
7 *
8 * Description:
9 * This example enables and then moves a ClearPath motor between
10 * preprogrammed velocity selections as defined in the MSP software. During
11 * operation, various move statuses are written to the USB serial port.
12 *
13 * Requirements:
14 * 1. A ClearPath motor must be connected to Connector M-0.
15 * 2. The connected ClearPath motor must be configured through the MSP software
16 * for Ramp Up/Down to Selected Velocity mode (In MSP
17 * select Mode>>Velocity>>Ramp Up/Down to Selected Velocity, then hit the OK
18 * button).
19 * 3. The ClearPath motor must be set to use the HLFB mode "ASG-Velocity
20 * w/Measured Torque" with a PWM carrier frequency of 482 Hz through the MSP
21 * software (select Advanced>>High Level Feedback [Mode]... then choose
22 * "ASG-Velocity w/Measured Torque" from the dropdown, make sure that 482 Hz
23 * is selected in the "PWM Carrier Frequency" dropdown, and hit the OK
24 * button).
25 * 4. The ClearPath must have defined Velocity Selections through the MSP
26 * software (On the main MSP window check the "Velocity Selection Setup
27 * (RPM)" box and fill in the four text boxes labeled "A off B off", "A on B
28 * off", "A off B on", and "A on B on").
29 * 5. Ensure the Input A & B filters in MSP are both set to 20ms (In MSP
30 * select Advanced>>Input A, B Filtering... then in the Settings box fill in
31 * the textboxes labeled "Input A Filter Time Constant (msec)" and "Input B
32 * Filter Time Constant (msec)" then hit the OK button).
33 *
34 * Links:
35 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
36 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
37 * ** ClearPath Manual (DC Power): https://www.teknic.com/files/downloads/clearpath_user_manual.pdf
38 * ** ClearPath Manual (AC Power): https://www.teknic.com/files/downloads/ac_clearpath-mc-sd_manual.pdf
39 * ** ClearPath Mode Informational Video: https://www.teknic.com/watch-video/#OpMode5
40 *
41 *
42 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
43 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
44 */
45
46#include "ClearCore.h"
47
48// The INPUT_A_B_FILTER must match the Input A, B filter setting in
49// MSP (Advanced >> Input A, B Filtering...)
50#define INPUT_A_B_FILTER 20
51
52// Defines the motor's connector as ConnectorM0
53#define motor ConnectorM0
54
55// Select the baud rate to match the target device.
56#define baudRate 9600
57
58// Specify which serial to use: ConnectorUsb, ConnectorCOM0, or ConnectorCOM1.
59#define SerialPort ConnectorUsb
60
61// This example has built-in functionality to automatically clear motor faults.
62// Any uncleared fault will cancel and disallow motion.
63// WARNING: enabling automatic fault handling will clear faults immediately when
64// encountered and return a motor to a state in which motion is allowed. Before
65// enabling this functionality, be sure to understand this behavior and ensure
66// your system will not enter an unsafe state.
67// To enable automatic fault handling, #define HANDLE_MOTOR_FAULTS (1)
68// To disable automatic fault handling, #define HANDLE_MOTOR_FAULTS (0)
69#define HANDLE_MOTOR_FAULTS (0)
70
71// Declares user-defined helper functions.
72// The definition/implementations of these functions are at the bottom of the sketch.
73bool RampToVelocitySelection(uint8_t velocityIndex);
74void HandleMotorFaults();
75
76int main()
77{
78 // Sets all motor connectors to the correct mode for Ramp Up/Down to
79 // Selected Velocity mode.
80 MotorMgr.MotorModeSet(MotorManager::MOTOR_ALL,
81 Connector::CPM_MODE_A_DIRECT_B_DIRECT);
82
83 // Set the motor's HLFB mode to bipolar PWM
84 motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
85 // Set the HFLB carrier frequency to 482 Hz
86 motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
87
88 // Enforces the state of the motor's A and B inputs before enabling
89 // the motor.
90 motor.MotorInAState(false);
91 motor.MotorInBState(false);
92
93 // Sets up serial communication and waits up to 5 seconds for a port to open.
94 // Serial communication is not required for this example to run.
95 SerialPort.Mode(Connector::USB_CDC);
96 SerialPort.Speed(baudRate);
97 uint32_t timeout = 5000;
98 uint32_t startTime = Milliseconds();
99 SerialPort.PortOpen();
100 while (!SerialPort && Milliseconds() - startTime < timeout)
101 {
102 continue;
103 }
104
105 // Enables the motor
106 motor.EnableRequest(true);
107 SerialPort.SendLine("Motor Enabled");
108
109 // Waits for HLFB to assert
110 SerialPort.SendLine("Waiting for HLFB...");
111 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
112 !motor.StatusReg().bit.MotorInFault)
113 {
114 continue;
115 }
116 // Check if a motor faulted during enabling
117 // Clear fault if configured to do so
118 if (motor.StatusReg().bit.MotorInFault)
119 {
120 SerialPort.SendLine("Motor fault detected.");
121 if (HANDLE_MOTOR_FAULTS)
122 {
123 HandleMotorFaults();
124 }
125 else
126 {
127 SerialPort.SendLine("Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
128 }
129 SerialPort.SendLine("Enabling may not have completed as expected. Proceed with caution.");
130 SerialPort.SendLine();
131 }
132 else
133 {
134 SerialPort.SendLine("Motor Ready");
135 }
136
137 while (true)
138 {
139 // Move to Velocity 1 defined in MSP (Inputs A off, B off).
140 // See below for the detailed function definition.
141 RampToVelocitySelection(1);
142
143 // Wait 1000ms.
144 Delay_ms(1000);
145 RampToVelocitySelection(2); // Inputs A on, B off
146 Delay_ms(1000);
147 RampToVelocitySelection(3); // Inputs A off, B on
148 Delay_ms(1000);
149 RampToVelocitySelection(4); // Inputs A on, B on
150 Delay_ms(1000);
151
152 // Alternatively, if you'd like to control the ClearPath motor's inputs
153 // directly using ClearCore inputs consider doing something like this:
154 /*
155 // Sets ClearPath's InA to DI6's state
156 motor.MotorInAState(ConnectorDI6.State());
157
158 // Sets ClearPath's InB to DI7's state
159 motor.MotorInBState(ConnectorDI7.State());
160 */
161 }
162}
163
164/*------------------------------------------------------------------------------
165 * RampToVelocitySelection
166 *
167 * Move to Velocity Selection number velocityIndex (defined in MSP)
168 * Prints the move status to the USB serial port
169 * Returns when HLFB asserts (indicating the motor has reached the target
170 * velocity)
171 *
172 * Parameters:
173 * int velocityIndex - The velocity number to command (defined in MSP)
174 *
175 * Returns: True/False depending on whether the velocity selection was
176 * successfully commanded.
177 */
178bool RampToVelocitySelection(uint8_t velocityIndex)
179{
180 // Check if a motor fault is currently preventing motion
181 // Clear fault if configured to do so
182 if (motor.StatusReg().bit.MotorInFault)
183 {
184 if (HANDLE_MOTOR_FAULTS)
185 {
186 SerialPort.SendLine("Motor fault detected. Move canceled.");
187 HandleMotorFaults();
188 }
189 else
190 {
191 SerialPort.SendLine("Motor fault detected. Move canceled. Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
192 }
193 return false;
194 }
195
196 SerialPort.Send("Moving to Velocity Selection: ");
197 SerialPort.Send(velocityIndex);
198
199 switch (velocityIndex)
200 {
201 case 1:
202 // Sets Input A and B for velocity 1
203 motor.MotorInAState(false);
204 motor.MotorInBState(false);
205 SerialPort.SendLine(" (Inputs A Off/B Off)");
206 break;
207 case 2:
208 // Sets Input A and B for velocity 2
209 motor.MotorInAState(true);
210 motor.MotorInBState(false);
211 SerialPort.SendLine(" (Inputs A On/B Off)");
212 break;
213 case 3:
214 // Sets Input A and B for velocity 3
215 motor.MotorInAState(false);
216 motor.MotorInBState(true);
217 SerialPort.SendLine(" (Inputs A Off/B On)");
218 break;
219 case 4:
220 // Sets Input A and B for velocity 4
221 motor.MotorInAState(true);
222 motor.MotorInBState(true);
223 SerialPort.SendLine(" (Inputs A On/B On)");
224 break;
225 default:
226 // If this case is reached then an incorrect velocityIndex was
227 // entered
228 return false;
229 }
230
231 // Ensures this delay is at least 20ms longer than the Input A, B filter
232 // setting in MSP
233 Delay_ms(20 + INPUT_A_B_FILTER);
234
235 // Waits for HLFB to assert (signaling the move has successfully reached its
236 // target velocity)
237 SerialPort.SendLine("Moving.. Waiting for HLFB");
238 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
239 !motor.StatusReg().bit.MotorInFault)
240 {
241 continue;
242 }
243 // Check if a motor faulted during move
244 // Clear fault if configured to do so
245 if (motor.StatusReg().bit.MotorInFault)
246 {
247 SerialPort.SendLine("Motor fault detected.");
248 if (HANDLE_MOTOR_FAULTS)
249 {
250 HandleMotorFaults();
251 }
252 else
253 {
254 SerialPort.SendLine("Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
255 }
256 SerialPort.SendLine("Motion may not have completed as expected. Proceed with caution.");
257 SerialPort.SendLine();
258 return false;
259 }
260 else
261 {
262 SerialPort.SendLine("Move Done");
263 return true;
264 }
265}
266//------------------------------------------------------------------------------
267
268/*------------------------------------------------------------------------------
269 * HandleMotorFaults
270 *
271 * Clears motor faults by cycling enable to the motor.
272 * Assumes motor is in fault
273 * (this function is called when motor.StatusReg.MotorInFault == true)
274 *
275 * Parameters:
276 * requires "motor" to be defined as a ClearCore motor connector
277 *
278 * Returns:
279 * none
280 */
281void HandleMotorFaults()
282{
283 SerialPort.SendLine("Handling fault: clearing faults by cycling enable signal to motor.");
284 motor.EnableRequest(false);
285 Delay_ms(10);
286 motor.EnableRequest(true);
287 Delay_ms(100);
288}
289//------------------------------------------------------------------------------
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 MotorModeSet(MotorPair motorPair, Connector::ConnectorModes newMode)
Sets the operational mode for the specified MotorDriver connectors.