54#define motor ConnectorM0
60#define SerialPort ConnectorUsb
65double maxVelocity = 2000;
69double torqueLimit = 100.0;
70double torqueLimitAlternate = 10.0;
74double pwmDeadBand = 2.0;
79bool CommandVelocity(int32_t commandedVelocity);
80bool LimitTorque(
double limit);
86 Connector::CPM_MODE_A_PWM_B_PWM);
90 SerialPort.Mode(Connector::USB_CDC);
91 SerialPort.Speed(baudRate);
92 uint32_t timeout = 5000;
94 SerialPort.PortOpen();
95 while (!SerialPort &&
Milliseconds() - startTime < timeout) {
100 motor.EnableRequest(
true);
101 SerialPort.SendLine(
"Motor Enabled");
104 SerialPort.SendLine(
"Waiting for motor to reach speed...");
109 SerialPort.SendLine(
"Motor Ready");
113 CommandVelocity(100);
117 CommandVelocity(300);
122 CommandVelocity(-200);
126 CommandVelocity(-300);
130 CommandVelocity(100);
147bool CommandVelocity(int32_t commandedVelocity) {
148 if (abs(commandedVelocity) > maxVelocity) {
149 SerialPort.SendLine(
"Move rejected, invalid velocity requested.");
153 SerialPort.Send(
"Commanding velocity: ");
154 SerialPort.SendLine(commandedVelocity);
157 double rangeUnsigned = 127.5 - (pwmDeadBand / 100 * 255);
162 double scaleFactor = rangeUnsigned / maxVelocity;
166 if (commandedVelocity < 0) {
167 dutyRequest = 127.5 - (pwmDeadBand / 100 * 255)
168 + (commandedVelocity * scaleFactor);
170 else if (commandedVelocity > 0) {
171 dutyRequest = 127.5 + (pwmDeadBand / 100 * 255)
172 + (commandedVelocity * scaleFactor);
179 motor.MotorInBDuty(dutyRequest);
181 SerialPort.SendLine(
"Velocity Commanded");
198bool LimitTorque(
double limit) {
199 if (limit > torqueLimit || limit < torqueLimitAlternate) {
200 SerialPort.SendLine(
"Torque limiting rejected, invalid torque requested.");
203 SerialPort.Send(
"Limit torque to: ");
204 SerialPort.Send(limit);
205 SerialPort.SendLine(
"%.");
209 double scaleFactor = 255 / (torqueLimit - torqueLimitAlternate);
212 uint8_t dutyRequest = (torqueLimit - limit) * scaleFactor;
215 motor.MotorInADuty(dutyRequest);
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.