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

Return to SDK Examples for Microchip Studio

1/*
2 * Title: 16PositionsHomeToHardStop
3 *
4 * Objective:
5 * This example demonstrates control of the ClearPath-MCPV operational mode
6 * Move To Absolute Position, 16 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, 16 Positions (Home to Hard Stop) mode (In
17 * MSP select Mode>>Position>>Move to Absolute Position, then with "16
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 fill in the textboxes labeled
27 * 1-16 found under "Position Selection Setup (cnts)").
28 * 5. Homing must be configured in the MSP software for your mechanical
29 * system (e.g. homing direction, torque limit, etc.). To configure, click
30 * the "Setup..." button found under the "Homing" label on the MSP's main
31 * window.
32 *
33 * Links:
34 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
35 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
36 * ** ClearPath Manual (DC Power): https://www.teknic.com/files/downloads/clearpath_user_manual.pdf
37 * ** ClearPath Manual (AC Power): https://www.teknic.com/files/downloads/ac_clearpath-mc-sd_manual.pdf
38 * ** ClearPath Mode Informational Video: https://www.teknic.com/watch-video/#OpMode14
39 *
40 *
41 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
42 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
43 */
44
45#include "ClearCore.h"
46
47
48// Defines the motor's connector as ConnectorM0
49#define motor ConnectorM0
50
51// Select the baud rate to match the target device.
52#define baudRate 9600
53
54// Specify which serial to use: ConnectorUsb, ConnectorCOM0, or ConnectorCOM1.
55#define SerialPort ConnectorUsb
56
57// This example has built-in functionality to automatically clear motor faults.
58// Any uncleared fault will cancel and disallow motion.
59// WARNING: enabling automatic fault handling will clear faults immediately when
60// encountered and return a motor to a state in which motion is allowed. Before
61// enabling this functionality, be sure to understand this behavior and ensure
62// your system will not enter an unsafe state.
63// To enable automatic fault handling, #define HANDLE_MOTOR_FAULTS (1)
64// To disable automatic fault handling, #define HANDLE_MOTOR_FAULTS (0)
65#define HANDLE_MOTOR_FAULTS (0)
66
67// Declares user-defined helper functions.
68// The definition/implementations of these functions are at the bottom of the sketch.
69bool MoveToPosition(uint8_t positionNum);
70void HandleMotorFaults();
71
72int main() {
73 // Sets all motor connectors to the correct mode for Absolute Position mode
74 MotorMgr.MotorModeSet(MotorManager::MOTOR_ALL,
75 Connector::CPM_MODE_A_DIRECT_B_DIRECT);
76
77 // Set the motor's HLFB mode to bipolar PWM
78 motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
79 // Set the HFLB carrier frequency to 482 Hz
80 motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
81
82 // Enforces the state of the motor's A and B inputs before enabling
83 // the motor.
84 motor.MotorInAState(false);
85 motor.MotorInBState(false);
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
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.MotorInFault) {
106 continue;
107 }
108 // Check if a motor faulted during enabling
109 // Clear fault if configured to do so
110 if (motor.StatusReg().bit.MotorInFault) {
111 SerialPort.SendLine("Motor fault detected.");
112 if(HANDLE_MOTOR_FAULTS){
113 HandleMotorFaults();
114 } else {
115 SerialPort.SendLine("Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
116 }
117 SerialPort.SendLine("Enabling may not have completed as expected. Proceed with caution.");
118 SerialPort.SendLine();
119 } else {
120 SerialPort.SendLine("Motor Ready");
121 }
122
123 while (true) {
124 // Move to Position 1 defined in MSP.
125 MoveToPosition(1); // See below for the detailed function definition.
126 // Wait 1000ms.
127 Delay_ms(1000);
128 MoveToPosition(2);
129 Delay_ms(1000);
130 MoveToPosition(3);
131 Delay_ms(1000);
132 MoveToPosition(4);
133 Delay_ms(1000);
134
135 MoveToPosition(5);
136 Delay_ms(1000);
137 MoveToPosition(6);
138 Delay_ms(1000);
139 MoveToPosition(7);
140 Delay_ms(1000);
141 MoveToPosition(8);
142 Delay_ms(1000);
143
144 MoveToPosition(9);
145 Delay_ms(1000);
146 MoveToPosition(10);
147 Delay_ms(1000);
148 MoveToPosition(11);
149 Delay_ms(1000);
150 MoveToPosition(12);
151 Delay_ms(1000);
152
153 MoveToPosition(13);
154 Delay_ms(1000);
155 MoveToPosition(14);
156 Delay_ms(1000);
157 MoveToPosition(15);
158 Delay_ms(1000);
159 MoveToPosition(16);
160 Delay_ms(1000);
161 }
162}
163
164/*------------------------------------------------------------------------------
165 * MoveToPosition
166 *
167 * Move to position number positionNum (defined in MSP)
168 * Prints the move status to the USB serial port
169 * Returns when HLFB asserts (indicating the motor has reached the commanded
170 * position)
171 *
172 * Parameters:
173 * int positionNum - The position number to command (defined in MSP)
174 *
175 * Returns: True/False depending on whether the position was successfully
176 * commanded.
177 */
178bool MoveToPosition(uint8_t positionNum) {
179 // Check if a motor fault is currently preventing motion
180 // Clear fault if configured to do so
181 if (motor.StatusReg().bit.MotorInFault) {
182 if(HANDLE_MOTOR_FAULTS){
183 SerialPort.SendLine("Motor fault detected. Move canceled.");
184 HandleMotorFaults();
185 } else {
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.SendLine(positionNum);
193
194 if (positionNum < 17 && positionNum > 0) {
195 // Sends pulses on Input B based on positionNum
196 for (uint8_t i = 0; i < positionNum; i++) {
197 motor.MotorInBState(true);
198 Delay_ms(1);
199 motor.MotorInBState(false);
200 Delay_ms(1);
201 }
202
203 // Triggers the command
204 motor.MotorInAState(true);
205 Delay_ms(1);
206 motor.MotorInAState(false);
207 }
208 else {
209 // If an invalid positionNum has been entered, returns a failure
210 return false;
211 }
212
213 // Waits for HLFB to assert (signaling the move has successfully completed)
214 Delay_ms(2);
215 SerialPort.SendLine("Moving.. Waiting for HLFB");
216 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
217 !motor.StatusReg().bit.MotorInFault) {
218 continue;
219 }
220 // Check if a motor faulted during move
221 // Clear fault if configured to do so
222 if (motor.StatusReg().bit.MotorInFault) {
223 SerialPort.SendLine("Motor fault detected.");
224 if(HANDLE_MOTOR_FAULTS){
225 HandleMotorFaults();
226 } else {
227 SerialPort.SendLine("Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
228 }
229 SerialPort.SendLine("Motion may not have completed as expected. Proceed with caution.");
230 SerialPort.SendLine();
231 return false;
232 } else {
233 SerialPort.SendLine("Move Done");
234 return true;
235 }
236}
237//------------------------------------------------------------------------------
238
239/*------------------------------------------------------------------------------
240 * HandleMotorFaults
241 *
242 * Clears motor faults by cycling enable to the motor.
243 * Assumes motor is in fault
244 * (this function is called when motor.StatusReg.MotorInFault == true)
245 *
246 * Parameters:
247 * requires "motor" to be defined as a ClearCore motor connector
248 *
249 * Returns:
250 * none
251 */
252 void HandleMotorFaults(){
253 SerialPort.SendLine("Handling fault: clearing faults by cycling enable signal to motor.");
254 motor.EnableRequest(false);
255 Delay_ms(10);
256 motor.EnableRequest(true);
257 Delay_ms(100);
258 }
259//------------------------------------------------------------------------------
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.