54#define motor ConnectorM0
60#define SerialPort ConnectorUsb
70#define HANDLE_MOTOR_FAULTS (0)
73double commandedVelocity = 0;
77int32_t maxVelocityCW = 1000;
78int32_t maxVelocityCCW = 1000;
83double velocityResolution = 2.0;
87bool MoveAtVelocity(
double velocity);
88void HandleMotorFaults();
94 Connector::CPM_MODE_A_DIRECT_B_DIRECT);
97 motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
99 motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
103 motor.MotorInAState(
false);
104 motor.MotorInBState(
false);
108 SerialPort.Mode(Connector::USB_CDC);
109 SerialPort.Speed(baudRate);
110 uint32_t timeout = 5000;
112 SerialPort.PortOpen();
113 while (!SerialPort &&
Milliseconds() - startTime < timeout) {
118 motor.EnableRequest(
true);
119 SerialPort.SendLine(
"Motor Enabled");
122 SerialPort.SendLine(
"Waiting for HLFB...");
123 SerialPort.SendLine(
"Waiting for HLFB...");
124 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
125 !motor.StatusReg().bit.MotorInFault) {
130 if (motor.StatusReg().bit.MotorInFault) {
131 SerialPort.SendLine(
"Motor fault detected.");
132 if(HANDLE_MOTOR_FAULTS){
135 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
137 SerialPort.SendLine(
"Enabling may not have completed as expected. Proceed with caution.");
138 SerialPort.SendLine();
140 SerialPort.SendLine(
"Motor Ready");
150 MoveAtVelocity(-100);
154 MoveAtVelocity(-750);
158 MoveAtVelocity(1000);
179bool MoveAtVelocity(
double velocity) {
181 if (velocity == commandedVelocity) {
186 if (velocity > maxVelocityCCW || velocity < -maxVelocityCW) {
187 SerialPort.Send(
"An invalid velocity of ");
188 SerialPort.Send(velocity);
189 SerialPort.SendLine(
" RPM has been requested.");
195 if (motor.StatusReg().bit.MotorInFault) {
196 if(HANDLE_MOTOR_FAULTS){
197 SerialPort.SendLine(
"Motor fault detected. Move canceled.");
200 SerialPort.SendLine(
"Motor fault detected. Move canceled. Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
205 SerialPort.Send(
"Commanding ");
206 SerialPort.Send(velocity);
207 SerialPort.SendLine(
" RPM");
213 int32_t currentVelocityRounded = round(commandedVelocity / velocityResolution);
214 int32_t targetVelocityRounded = round(velocity / velocityResolution);
215 int32_t velocityDifference = labs(targetVelocityRounded - currentVelocityRounded);
216 for (int32_t i = 0; i < velocityDifference; i++) {
217 if (velocity > commandedVelocity) {
219 motor.MotorInAState(
true);
222 motor.MotorInBState(
true);
224 motor.MotorInAState(
false);
226 motor.MotorInBState(
false);
230 motor.MotorInBState(
true);
232 motor.MotorInAState(
true);
234 motor.MotorInBState(
false);
236 motor.MotorInAState(
false);
242 commandedVelocity = velocity;
246 SerialPort.SendLine(
"Ramping Speed... Waiting for HLFB");
247 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
248 !motor.StatusReg().bit.MotorInFault) {
253 if (motor.StatusReg().bit.MotorInFault) {
254 SerialPort.SendLine(
"Motor fault detected.");
255 if(HANDLE_MOTOR_FAULTS){
258 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
260 SerialPort.SendLine(
"Motion may not have completed as expected. Proceed with caution.");
261 SerialPort.SendLine();
264 SerialPort.SendLine(
"Move Done");
268 SerialPort.SendLine(
"Target Velocity Reached");
286 void HandleMotorFaults(){
287 SerialPort.SendLine(
"Handling fault: clearing faults by cycling enable signal to motor.");
288 motor.EnableRequest(
false);
290 motor.EnableRequest(
true);
void Delay_ms(uint32_t ms)
Blocks operations for ms milliseconds.
Definition SysTiming.h:287
void Delay_us(uint32_t usec)
Blocks for operations usec microseconds.
Definition SysTiming.h:296
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.