49#define motor ConnectorM0
55#define SerialPort ConnectorUsb
65#define HANDLE_MOTOR_FAULTS (0)
69bool MoveToPosition(uint8_t positionNum);
70void HandleMotorFaults();
75 Connector::CPM_MODE_A_DIRECT_B_DIRECT);
78 motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
80 motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
84 motor.MotorInAState(
false);
85 motor.MotorInBState(
false);
89 SerialPort.Mode(Connector::USB_CDC);
90 SerialPort.Speed(baudRate);
91 uint32_t timeout = 5000;
93 SerialPort.PortOpen();
94 while (!SerialPort &&
Milliseconds() - startTime < timeout) {
99 motor.EnableRequest(
true);
100 SerialPort.SendLine(
"Motor Enabled");
103 SerialPort.SendLine(
"Waiting for HLFB...");
104 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
105 !motor.StatusReg().bit.MotorInFault) {
110 if (motor.StatusReg().bit.MotorInFault) {
111 SerialPort.SendLine(
"Motor fault detected.");
112 if(HANDLE_MOTOR_FAULTS){
115 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
117 SerialPort.SendLine(
"Enabling may not have completed as expected. Proceed with caution.");
118 SerialPort.SendLine();
120 SerialPort.SendLine(
"Motor Ready");
178bool MoveToPosition(uint8_t positionNum) {
181 if (motor.StatusReg().bit.MotorInFault) {
182 if(HANDLE_MOTOR_FAULTS){
183 SerialPort.SendLine(
"Motor fault detected. Move canceled.");
186 SerialPort.SendLine(
"Motor fault detected. Move canceled. Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
191 SerialPort.Send(
"Moving to position: ");
192 SerialPort.SendLine(positionNum);
194 if (positionNum < 17 && positionNum > 0) {
196 for (uint8_t i = 0; i < positionNum; i++) {
197 motor.MotorInBState(
true);
199 motor.MotorInBState(
false);
204 motor.MotorInAState(
true);
206 motor.MotorInAState(
false);
215 SerialPort.SendLine(
"Moving.. Waiting for HLFB");
216 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
217 !motor.StatusReg().bit.MotorInFault) {
222 if (motor.StatusReg().bit.MotorInFault) {
223 SerialPort.SendLine(
"Motor fault detected.");
224 if(HANDLE_MOTOR_FAULTS){
227 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
229 SerialPort.SendLine(
"Motion may not have completed as expected. Proceed with caution.");
230 SerialPort.SendLine();
233 SerialPort.SendLine(
"Move Done");
252 void HandleMotorFaults(){
253 SerialPort.SendLine(
"Handling fault: clearing faults by cycling enable signal to motor.");
254 motor.EnableRequest(
false);
256 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.