55#define INPUT_A_B_FILTER 20
58#define motor ConnectorM0
64#define SerialPort ConnectorUsb
74#define HANDLE_MOTOR_FAULTS (0)
78bool MoveToPosition(uint8_t positionNum);
79void HandleMotorFaults();
85 Connector::CPM_MODE_A_DIRECT_B_DIRECT);
88 motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
90 motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
94 motor.MotorInAState(
false);
95 motor.MotorInBState(
false);
99 SerialPort.Mode(Connector::USB_CDC);
100 SerialPort.Speed(baudRate);
101 uint32_t timeout = 5000;
103 SerialPort.PortOpen();
104 while (!SerialPort &&
Milliseconds() - startTime < timeout)
110 motor.EnableRequest(
true);
111 SerialPort.SendLine(
"Motor Enabled");
114 SerialPort.SendLine(
"Waiting for HLFB...");
115 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
116 !motor.StatusReg().bit.MotorInFault)
122 if (motor.StatusReg().bit.MotorInFault)
124 SerialPort.SendLine(
"Motor fault detected.");
125 if (HANDLE_MOTOR_FAULTS)
131 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
133 SerialPort.SendLine(
"Enabling may not have completed as expected. Proceed with caution.");
134 SerialPort.SendLine();
138 SerialPort.SendLine(
"Motor Ready");
179bool MoveToPosition(uint8_t positionNum)
183 if (motor.StatusReg().bit.MotorInFault)
185 if (HANDLE_MOTOR_FAULTS)
187 SerialPort.SendLine(
"Motor fault detected. Move canceled.");
192 SerialPort.SendLine(
"Motor fault detected. Move canceled. Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
197 SerialPort.Send(
"Moving to position: ");
198 SerialPort.Send(positionNum);
204 motor.MotorInAState(
false);
205 motor.MotorInBState(
false);
206 SerialPort.SendLine(
" (Inputs A Off/B Off)");
210 motor.MotorInAState(
true);
211 motor.MotorInBState(
false);
212 SerialPort.SendLine(
" (Inputs A On/B Off)");
216 motor.MotorInAState(
false);
217 motor.MotorInBState(
true);
218 SerialPort.SendLine(
" (Inputs A Off/B On)");
222 motor.MotorInAState(
true);
223 motor.MotorInBState(
true);
224 SerialPort.SendLine(
" (Inputs A On/B On)");
236 SerialPort.SendLine(
"Moving.. Waiting for HLFB");
237 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
238 !motor.StatusReg().bit.MotorInFault)
244 if (motor.StatusReg().bit.MotorInFault)
246 SerialPort.SendLine(
"Motor fault detected.");
247 if (HANDLE_MOTOR_FAULTS)
253 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
255 SerialPort.SendLine(
"Motion may not have completed as expected. Proceed with caution.");
256 SerialPort.SendLine();
261 SerialPort.SendLine(
"Move Done");
280void HandleMotorFaults()
282 SerialPort.SendLine(
"Handling fault: clearing faults by cycling enable signal to motor.");
283 motor.EnableRequest(
false);
285 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.