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

Return to SDK Examples for Microchip Studio

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