53#define INPUT_A_B_FILTER 20
56#define motor ConnectorM0
59#define HomingSensor ConnectorDI6
65#define SerialPort ConnectorUsb
75#define HANDLE_MOTOR_FAULTS (0)
79void HomingSensorCallback();
80bool MoveToPosition(uint8_t positionNum);
81void HandleMotorFaults();
87 HomingSensor.Mode(Connector::INPUT_DIGITAL);
88 HomingSensor.InterruptHandlerSet(HomingSensorCallback, InputManager::CHANGE);
92 Connector::CPM_MODE_A_DIRECT_B_DIRECT);
95 motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
97 motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
100 motor.MotorInAState(
false);
102 motor.MotorInBState(HomingSensor.State());
106 SerialPort.Mode(Connector::USB_CDC);
107 SerialPort.Speed(baudRate);
108 uint32_t timeout = 5000;
110 SerialPort.PortOpen();
111 while (!SerialPort &&
Milliseconds() - startTime < timeout)
117 motor.EnableRequest(
true);
118 SerialPort.SendLine(
"Motor Enabled");
121 SerialPort.SendLine(
"Waiting for HLFB...");
122 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
123 !motor.StatusReg().bit.MotorInFault)
129 if (motor.StatusReg().bit.MotorInFault)
131 SerialPort.SendLine(
"Motor fault detected.");
132 if (HANDLE_MOTOR_FAULTS)
138 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
140 SerialPort.SendLine(
"Enabling may not have completed as expected. Proceed with caution.");
141 SerialPort.SendLine();
145 SerialPort.SendLine(
"Motor Ready");
173bool MoveToPosition(uint8_t positionNum)
177 if (motor.StatusReg().bit.MotorInFault)
179 if (HANDLE_MOTOR_FAULTS)
181 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.Send(positionNum);
198 motor.MotorInAState(
false);
199 SerialPort.SendLine(
" (Input A Off)");
203 motor.MotorInAState(
true);
204 SerialPort.SendLine(
" (Input A On)");
216 SerialPort.SendLine(
"Moving.. Waiting for HLFB");
217 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
218 !motor.StatusReg().bit.MotorInFault)
224 if (motor.StatusReg().bit.MotorInFault)
226 SerialPort.SendLine(
"Motor fault detected.");
227 if (HANDLE_MOTOR_FAULTS)
233 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
235 SerialPort.SendLine(
"Motion may not have completed as expected. Proceed with caution.");
236 SerialPort.SendLine();
241 SerialPort.SendLine(
"Move Done");
252void HomingSensorCallback()
257 motor.MotorInBState(HomingSensor.State());
274void HandleMotorFaults()
276 SerialPort.SendLine(
"Handling fault: clearing faults by cycling enable signal to motor.");
277 motor.EnableRequest(
false);
279 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.