62#define motor ConnectorM0
66#define TRIGGER_PULSE_TIME 25
70#define INPUT_A_B_FILTER 20
74#define POSITION_INCREMENT_1 1000
75#define POSITION_INCREMENT_2 -1000
78#define HomingSensor ConnectorDI6
84#define SerialPort ConnectorUsb
94#define HANDLE_MOTOR_FAULTS (0)
98void HomingSensorCallback();
99bool MoveIncrements(uint32_t numberOfIncrements, int32_t positionIncrement);
100void HandleMotorFaults();
107 Connector::CPM_MODE_A_DIRECT_B_DIRECT);
110 motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
112 motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
115 motor.MotorInAState(
false);
116 motor.MotorInBState(
false);
120 HomingSensor.Mode(Connector::INPUT_DIGITAL);
121 HomingSensor.InterruptHandlerSet(HomingSensorCallback, InputManager::CHANGE);
123 motor.MotorInBState(HomingSensor.State());
127 SerialPort.Mode(Connector::USB_CDC);
128 SerialPort.Speed(baudRate);
129 uint32_t timeout = 5000;
131 SerialPort.PortOpen();
132 while (!SerialPort &&
Milliseconds() - startTime < timeout)
139 motor.EnableRequest(
true);
140 SerialPort.SendLine(
"Motor Enabled");
143 SerialPort.SendLine(
"Waiting for HLFB...");
144 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
145 !motor.StatusReg().bit.MotorInFault)
151 if (motor.StatusReg().bit.MotorInFault)
153 SerialPort.SendLine(
"Motor fault detected.");
154 if (HANDLE_MOTOR_FAULTS)
160 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
162 SerialPort.SendLine(
"Enabling may not have completed as expected. Proceed with caution.");
163 SerialPort.SendLine();
167 SerialPort.SendLine(
"Motor Ready");
174 MoveIncrements(1, POSITION_INCREMENT_1);
179 MoveIncrements(1, POSITION_INCREMENT_2);
187 MoveIncrements(4, POSITION_INCREMENT_1);
191 MoveIncrements(4, POSITION_INCREMENT_2);
210bool MoveIncrements(uint32_t numberOfIncrements, int32_t positionIncrement)
214 if (motor.StatusReg().bit.MotorInFault)
216 if (HANDLE_MOTOR_FAULTS)
218 SerialPort.SendLine(
"Motor fault detected. Move canceled.");
223 SerialPort.SendLine(
"Motor fault detected. Move canceled. Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
228 SerialPort.Send(
"Moving ");
229 SerialPort.Send(numberOfIncrements);
230 SerialPort.Send(
" * ");
232 switch (positionIncrement)
234 case POSITION_INCREMENT_1:
236 SerialPort.SendLine(POSITION_INCREMENT_1);
237 motor.MotorInAState(
false);
239 case POSITION_INCREMENT_2:
241 SerialPort.SendLine(POSITION_INCREMENT_2);
242 motor.MotorInAState(
true);
254 motor.EnableTriggerPulse(numberOfIncrements, TRIGGER_PULSE_TIME,
true);
256 SerialPort.SendLine(
"Moving.. Waiting for HLFB");
257 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
258 !motor.StatusReg().bit.MotorInFault)
264 if (motor.StatusReg().bit.MotorInFault)
266 SerialPort.SendLine(
"Motor fault detected.");
267 if (HANDLE_MOTOR_FAULTS)
273 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
275 SerialPort.SendLine(
"Motion may not have completed as expected. Proceed with caution.");
276 SerialPort.SendLine();
281 SerialPort.SendLine(
"Move Done");
285 SerialPort.SendLine(
"Move Done");
295void HomingSensorCallback()
300 motor.MotorInBState(HomingSensor.State());
317void HandleMotorFaults()
319 SerialPort.SendLine(
"Handling fault: clearing faults by cycling enable signal to motor.");
320 motor.EnableRequest(
false);
322 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.