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

Return to SDK Examples for Microchip Studio

1/*
2 * Title: FollowDigitalVelocityWithVariableTorque
3 *
4 * Objective:
5 * This example demonstrates control of the ClearPath-MC operational mode
6 * Follow Digital Velocity Command, Bipolar PWM Command with Variable Torque.
7 *
8 * Description:
9 * This example enables a ClearPath motor and executes a repeating pattern of
10 * bidirectional velocity moves and torque limits. During operation, various
11 * move statuses are written to the USB serial port.
12 * The resolution for PWM outputs is 8-bit, meaning only 256 discrete speeds
13 * and torque limits can be commanded. The motor's actual commanded speed
14 * and torque limit may differ slightly from what you input below because
15 * of this.
16 * Consider using Manual Velocity Control mode if greater velocity command
17 * resolution is required, or if HLFB is needed for "move done/at speed"
18 * status feedback.
19 *
20 * Requirements:
21 * 1. A ClearPath motor must be connected to Connector M-0.
22 * 2. The connected ClearPath motor must be configured through the MSP software
23 * for Follow Digital Velocity Command, Bipolar PWM Command with Variable
24 * Torque mode (In MSP select Mode>>Velocity>>Follow Digital Velocity
25 * Command, then with "Bipolar PWM Command w/ Variable Torque")
26 * 3. The ClearPath must have a defined Max Speed configured through the MSP
27 * software (On the main MSP window fill in the "Max Speed (RPM)" box with
28 * your desired maximum speed). Ensure the value of maxSpeed below matches
29 * this Max Speed.
30 * 4. Set the PWM Deadband in MSP to 2.
31 * 5. In MSP, ensure the two checkboxes for "Invert Torque PWM Input" and
32 * "Invert Speed PWM Input" are unchecked.
33 * 6. A primary Torque Limit and Alternate Torque Limit must be defined using
34 * the Torque Limit setup window through the MSP software (To configure,
35 * click the "Setup..." button found under the "Torque Limit" label. Then
36 * fill in the textbox labeled "Alt Torque Limit (% of max)" and hit the
37 * Apply button). Use only symmetric limits. These limits must match the
38 * "torqueLimit" and "torqueLimitAlternate" variables defined below.
39 *
40 * Links:
41 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
42 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
43 * ** ClearPath Manual (DC Power): https://www.teknic.com/files/downloads/clearpath_user_manual.pdf
44 * ** ClearPath Manual (AC Power): https://www.teknic.com/files/downloads/ac_clearpath-mc-sd_manual.pdf
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// Defines the motor's connector as ConnectorM0
54#define motor ConnectorM0
55
56// Select the baud rate to match the target device.
57#define baudRate 9600
58
59// Specify which serial to use: ConnectorUsb, ConnectorCOM0, or ConnectorCOM1.
60#define SerialPort ConnectorUsb
61
62// This is the commanded speed limit (must match the MSP value). This speed
63// cannot actually be commanded, so use something slightly higher than your real
64// max speed here and in MSP.
65double maxVelocity = 2000;
66
67// Defines the default torque limit and the alternate torque limit
68// (must match MSP values)
69double torqueLimit = 100.0;
70double torqueLimitAlternate = 10.0;
71
72// A PWM deadband of 2% prevents signal jitter from effecting a 0 RPM command
73// (must match MSP value)
74double pwmDeadBand = 2.0;
75
76// Declares our user-defined helper functions, which are used to command
77// velocity and limit torque. The definitions/implementations of these functions
78// are at the bottom of the sketch.
79bool CommandVelocity(int32_t commandedVelocity);
80bool LimitTorque(double limit);
81
82int main() {
83 // Sets all motor connectors to the correct mode for Follow Digital
84 // Velocity, Bipolar PWM mode.
85 MotorMgr.MotorModeSet(MotorManager::MOTOR_ALL,
86 Connector::CPM_MODE_A_PWM_B_PWM);
87
88 // Sets up serial communication and waits up to 5 seconds for a port to open.
89 // Serial communication is not required for this example to run.
90 SerialPort.Mode(Connector::USB_CDC);
91 SerialPort.Speed(baudRate);
92 uint32_t timeout = 5000;
93 uint32_t startTime = Milliseconds();
94 SerialPort.PortOpen();
95 while (!SerialPort && Milliseconds() - startTime < timeout) {
96 continue;
97 }
98
99 // Enables the motor
100 motor.EnableRequest(true);
101 SerialPort.SendLine("Motor Enabled");
102
103 // Waits for 5 seconds to allow the motor to come up to speed
104 SerialPort.SendLine("Waiting for motor to reach speed...");
105 startTime = Milliseconds();
106 while (Milliseconds() - startTime < timeout) {
107 continue;
108 }
109 SerialPort.SendLine("Motor Ready");
110
111 while (true) {
112 // Move at +100 RPM (CCW).
113 CommandVelocity(100); // See below for the detailed function definition.
114 // Wait 5000ms
115 Delay_ms(5000);
116
117 CommandVelocity(300); // Move at +300 RPM (CCW).
118 Delay_ms(5000);
119
120 // Limit the torque to 70%.
121 LimitTorque(70); // See below for the detailed function definition.
122 CommandVelocity(-200); // Move at -200 RPM (CW).
123 Delay_ms(5000);
124
125 LimitTorque(15); // Limit the torque to 15%.
126 CommandVelocity(-300); // Move at -300 RPM (CW).
127 Delay_ms(5000);
128
129 LimitTorque(100); // Increase torque limit to 100%.
130 CommandVelocity(100); // Move at +100 RPM (CCW).
131 Delay_ms(5000);
132 }
133}
134
135/*------------------------------------------------------------------------------
136 * CommandVelocity
137 *
138 * Command the motor to move using a velocity of commandedVelocity
139 * Prints the move status to the USB serial port
140 *
141 * Parameters:
142 * int commandedVelocity - The velocity to command
143 *
144 * Returns: True/False depending on whether the velocity was successfully
145 * commanded.
146 */
147bool CommandVelocity(int32_t commandedVelocity) {
148 if (abs(commandedVelocity) > maxVelocity) {
149 SerialPort.SendLine("Move rejected, invalid velocity requested.");
150 return false;
151 }
152
153 SerialPort.Send("Commanding velocity: ");
154 SerialPort.SendLine(commandedVelocity);
155
156 // If there is a deadband defined, the range of the PWM scale is reduced.
157 double rangeUnsigned = 127.5 - (pwmDeadBand / 100 * 255);
158
159 // Find the scaling factor of our velocity range mapped to the PWM duty cycle
160 // range (the PWM to the ClearPath is bipolar, so the range starts at a 50%
161 // duty cycle).
162 double scaleFactor = rangeUnsigned / maxVelocity;
163
164 // Scale the velocity command to our duty cycle range.
165 double dutyRequest;
166 if (commandedVelocity < 0) {
167 dutyRequest = 127.5 - (pwmDeadBand / 100 * 255)
168 + (commandedVelocity * scaleFactor);
169 }
170 else if (commandedVelocity > 0) {
171 dutyRequest = 127.5 + (pwmDeadBand / 100 * 255)
172 + (commandedVelocity * scaleFactor);
173 }
174 else {
175 dutyRequest = 128.0;
176 }
177
178 // Command the move.
179 motor.MotorInBDuty(dutyRequest);
180
181 SerialPort.SendLine("Velocity Commanded");
182 return true;
183}
184//------------------------------------------------------------------------------
185
186/*------------------------------------------------------------------------------
187 * LimitTorque
188 *
189 * Command the motor to limit the maximum applied torque to (limit)%
190 * Prints the move status to the USB serial port
191 *
192 * Parameters:
193 * int limit - The torque level to command
194 *
195 * Returns: True/False depending on whether the torque limit was successfully
196 * commanded.
197 */
198bool LimitTorque(double limit) {
199 if (limit > torqueLimit || limit < torqueLimitAlternate) {
200 SerialPort.SendLine("Torque limiting rejected, invalid torque requested.");
201 return false;
202 }
203 SerialPort.Send("Limit torque to: ");
204 SerialPort.Send(limit);
205 SerialPort.SendLine("%.");
206
207 // Find the scaling factor of our torque range mapped to the PWM duty cycle
208 // range (255 is the max duty cycle).
209 double scaleFactor = 255 / (torqueLimit - torqueLimitAlternate);
210
211 // Scale the torque limit command to our duty cycle range.
212 uint8_t dutyRequest = (torqueLimit - limit) * scaleFactor;
213
214 // Command the new torque limit.
215 motor.MotorInADuty(dutyRequest);
216
217 return true;
218}
219//------------------------------------------------------------------------------
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.