50#define INPUT_A_B_FILTER 20
53#define motor ConnectorM0
59#define SerialPort ConnectorUsb
69#define HANDLE_MOTOR_FAULTS (0)
73bool RampToVelocitySelection(uint8_t velocityIndex);
74void HandleMotorFaults();
81 Connector::CPM_MODE_A_DIRECT_B_DIRECT);
84 motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
86 motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
90 motor.MotorInAState(
false);
91 motor.MotorInBState(
false);
95 SerialPort.Mode(Connector::USB_CDC);
96 SerialPort.Speed(baudRate);
97 uint32_t timeout = 5000;
99 SerialPort.PortOpen();
100 while (!SerialPort &&
Milliseconds() - startTime < timeout)
106 motor.EnableRequest(
true);
107 SerialPort.SendLine(
"Motor Enabled");
110 SerialPort.SendLine(
"Waiting for HLFB...");
111 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
112 !motor.StatusReg().bit.MotorInFault)
118 if (motor.StatusReg().bit.MotorInFault)
120 SerialPort.SendLine(
"Motor fault detected.");
121 if (HANDLE_MOTOR_FAULTS)
127 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
129 SerialPort.SendLine(
"Enabling may not have completed as expected. Proceed with caution.");
130 SerialPort.SendLine();
134 SerialPort.SendLine(
"Motor Ready");
141 RampToVelocitySelection(1);
145 RampToVelocitySelection(2);
147 RampToVelocitySelection(3);
149 RampToVelocitySelection(4);
178bool RampToVelocitySelection(uint8_t velocityIndex)
182 if (motor.StatusReg().bit.MotorInFault)
184 if (HANDLE_MOTOR_FAULTS)
186 SerialPort.SendLine(
"Motor fault detected. Move canceled.");
191 SerialPort.SendLine(
"Motor fault detected. Move canceled. Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
196 SerialPort.Send(
"Moving to Velocity Selection: ");
197 SerialPort.Send(velocityIndex);
199 switch (velocityIndex)
203 motor.MotorInAState(
false);
204 motor.MotorInBState(
false);
205 SerialPort.SendLine(
" (Inputs A Off/B Off)");
209 motor.MotorInAState(
true);
210 motor.MotorInBState(
false);
211 SerialPort.SendLine(
" (Inputs A On/B Off)");
215 motor.MotorInAState(
false);
216 motor.MotorInBState(
true);
217 SerialPort.SendLine(
" (Inputs A Off/B On)");
221 motor.MotorInAState(
true);
222 motor.MotorInBState(
true);
223 SerialPort.SendLine(
" (Inputs A On/B On)");
237 SerialPort.SendLine(
"Moving.. Waiting for HLFB");
238 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
239 !motor.StatusReg().bit.MotorInFault)
245 if (motor.StatusReg().bit.MotorInFault)
247 SerialPort.SendLine(
"Motor fault detected.");
248 if (HANDLE_MOTOR_FAULTS)
254 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
256 SerialPort.SendLine(
"Motion may not have completed as expected. Proceed with caution.");
257 SerialPort.SendLine();
262 SerialPort.SendLine(
"Move Done");
281void HandleMotorFaults()
283 SerialPort.SendLine(
"Handling fault: clearing faults by cycling enable signal to motor.");
284 motor.EnableRequest(
false);
286 motor.EnableRequest(
true);
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.