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

Return to SDK Examples for Microchip Studio

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