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

Return to SDK Examples for Microchip Studio

1/*
2 * Title: FollowDigitalTorque
3 *
4 * Objective:
5 * This example demonstrates control of the ClearPath-MC operational mode
6 * Follow Digital Torque Command, Unipolar PWM Command.
7 *
8 * Description:
9 * This example enables and then commands a ClearPath motor to output various
10 * torques. in both the clockwise and counterclockwise directions.
11 * During operation, various move statuses are written to the USB serial
12 * port.
13 *
14 * Requirements:
15 * 1. A ClearPath motor must be connected to Connector M-0.
16 * 2. The connected ClearPath motor must be configured through the MSP software
17 * for Follow Digital Torque Command, Unipolar PWM Command mode (In MSP
18 * select Mode>>Torque>>Follow Digital Torque Command, then with
19 * "Unipolar PWM Command" selected hit the OK button).
20 * 3. The ClearPath motor must be set to use the HLFB mode "ASG-Torque"
21 * through the MSP software (select Advanced>>High Level Feedback [Mode]...
22 * then choose "All Systems Go (ASG) - Torque" from the dropdown and hit
23 * the OK button).
24 * 4. The ClearPath must have a defined Max Torque Command configured through
25 * the MSP software. This value must match the "maxTorque" variable defined
26 * below.
27 * 5. Ensure the Input A filter in MSP is set to 20ms (In MSP select
28 * Advanced>>Input A, B Filtering... then in the Settings box fill in the
29 * textbox labeled "Input A Filter Time Constant (msec)" then hit the OK
30 * button).
31 * 6. Set your Max Speed and Over-Speed Timeout in MSP according to your
32 * mechanical system. Note you may notice any of the following if this max
33 * speed is reached: motor shutdown, speed limit cycling at the max speed,
34 * HLFB not signaling ASG/move done.
35 * 7. Ensure the "Invert PWM Input" box is unchecked in MSP.
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/#OpMode9
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_FILTER must match the Input A filter setting in
52// MSP (Advanced >> Input A, B Filtering...)
53#define INPUT_A_FILTER 20
54
55// Defines the motor's connector as ConnectorM0
56#define motor ConnectorM0
57
58// Select the baud rate to match the target device.
59#define baudRate 9600
60
61// Specify which serial to use: ConnectorUsb, ConnectorCOM0, or ConnectorCOM1.
62#define SerialPort ConnectorUsb
63
64// Defines the limit of the torque command, as a percent of the motor's peak
65// torque rating (must match the value used in MSP)
66double maxTorque = 100;
67
68// Declares our user-defined helper function, which is used to command torque.
69// The definition/implementation of this function is at the bottom of the sketch.
70bool CommandTorque(int8_t commandedTorque);
71
72int main() {
73 // Sets all motor connectors to the correct mode for Follow Digital
74 // Torque mode.
75 MotorMgr.MotorModeSet(MotorManager::MOTOR_ALL,
76 Connector::CPM_MODE_A_DIRECT_B_PWM);
77
78 // Sets up serial communication and waits up to 5 seconds for a port to open.
79 // Serial communication is not required for this example to run.
80 SerialPort.Mode(Connector::USB_CDC);
81 SerialPort.Speed(baudRate);
82 uint32_t timeout = 5000;
83 uint32_t startTime = Milliseconds();
84 SerialPort.PortOpen();
85 while (!SerialPort && Milliseconds() - startTime < timeout) {
86 continue;
87 }
88
89 // Enables the motor
90 motor.EnableRequest(true);
91 SerialPort.SendLine("Motor Enabled");
92
93 // Waits for HLFB to assert
94 SerialPort.SendLine("Waiting for HLFB...");
95 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED) {
96 continue;
97 }
98 SerialPort.SendLine("Motor Ready");
99
100 while (true) {
101 // Output 15% of the motor's peak torque in the positive (CCW) direction.
102 CommandTorque(15); // See below for the detailed function definition.
103 // Wait 2000ms.
104 Delay_ms(2000);
105
106 CommandTorque(-75); // Output 75% peak torque in the negative (CW) direction.
107 Delay_ms(2000);
108
109 CommandTorque(5); // Output 5% peak torque in the positive (CCW) direction.
110 Delay_ms(2000);
111
112 CommandTorque(-35); // Output 35% peak torque in the negative (CW) direction.
113 Delay_ms(2000);
114
115 CommandTorque(10); // Output 10% peak torque in the positive (CCW) direction.
116 Delay_ms(2000);
117 }
118}
119
120/*------------------------------------------------------------------------------
121 * CommandTorque
122 *
123 * Command the motor to move using a torque of commandedTorque
124 * Prints the move status to the USB serial port
125 * Returns when HLFB asserts (indicating the motor has reached the commanded
126 * torque)
127 *
128 * Parameters:
129 * int commandedTorque - The torque to command
130 *
131 * Returns: True/False depending on whether the torque was successfully
132 * commanded.
133 */
134bool CommandTorque(int8_t commandedTorque) {
135 if (abs(commandedTorque) > abs(maxTorque)) {
136 SerialPort.SendLine("Move rejected, invalid torque requested");
137 return false;
138 }
139
140 // Check if an alert is currently preventing motion
141 if (motor.StatusReg().bit.AlertsPresent) {
142 SerialPort.SendLine("Motor status: 'In Alert'. Move Canceled.");
143 return false;
144 }
145
146 SerialPort.Send("Commanding torque: ");
147 SerialPort.SendLine(commandedTorque);
148
149 // Find the scaling factor of our torque range mapped to the PWM duty cycle
150 // range (255 is the max duty cycle).
151 double scaleFactor = 255 / maxTorque;
152
153 // Scale the torque command to our duty cycle range.
154 uint8_t dutyRequest = abs(commandedTorque) * scaleFactor;
155
156 // Set input A to match the direction of torque.
157 if (commandedTorque < 0) {
158 motor.MotorInAState(true);
159 }
160 else {
161 motor.MotorInAState(false);
162 }
163
164 // Ensures this delay is at least 20ms longer than the Input A filter
165 // setting in MSP
166 Delay_ms(20 + INPUT_A_FILTER);
167
168 // Command the move
169 motor.MotorInBDuty(dutyRequest);
170
171 // Waits for HLFB to assert (signaling a successful new torque output)
172 SerialPort.SendLine("Moving... Waiting for HLFB");
173 // Allow some time for HLFB to transition.
174 Delay_ms(1);
175 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED) {
176 continue;
177 }
178
179 SerialPort.SendLine("Move Done");
180 return true;
181}
182//------------------------------------------------------------------------------
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.