58#define motor ConnectorM0
62#define TRIGGER_PULSE_TIME 25
66#define INPUT_A_B_FILTER 20
70#define POSITION_INCREMENT_1 750
71#define POSITION_INCREMENT_2 -750
72#define POSITION_INCREMENT_3 1666
73#define POSITION_INCREMENT_4 -1666
79#define SerialPort ConnectorUsb
89#define HANDLE_MOTOR_FAULTS (0)
93bool MoveIncrements(uint32_t NumberOfIncrements, int32_t PositionIncrement);
94void HandleMotorFaults();
101 Connector::CPM_MODE_A_DIRECT_B_DIRECT);
104 motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
106 motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
110 motor.MotorInAState(
false);
111 motor.MotorInBState(
false);
115 SerialPort.Mode(Connector::USB_CDC);
116 SerialPort.Speed(baudRate);
117 uint32_t timeout = 5000;
119 SerialPort.PortOpen();
120 while (!SerialPort &&
Milliseconds() - startTime < timeout)
127 motor.EnableRequest(
true);
128 SerialPort.SendLine(
"Motor Enabled");
131 SerialPort.SendLine(
"Waiting for HLFB...");
132 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
133 !motor.StatusReg().bit.MotorInFault)
139 if (motor.StatusReg().bit.MotorInFault)
141 SerialPort.SendLine(
"Motor fault detected.");
142 if (HANDLE_MOTOR_FAULTS)
148 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
150 SerialPort.SendLine(
"Enabling may not have completed as expected. Proceed with caution.");
151 SerialPort.SendLine();
155 SerialPort.SendLine(
"Motor Ready");
161 MoveIncrements(1, POSITION_INCREMENT_1);
167 MoveIncrements(1, POSITION_INCREMENT_2);
175 MoveIncrements(2, POSITION_INCREMENT_3);
179 MoveIncrements(2, POSITION_INCREMENT_4);
198bool MoveIncrements(uint32_t numberOfIncrements, int32_t positionIncrement)
202 if (motor.StatusReg().bit.MotorInFault)
204 if (HANDLE_MOTOR_FAULTS)
206 SerialPort.SendLine(
"Motor fault detected. Move canceled.");
211 SerialPort.SendLine(
"Motor fault detected. Move canceled. Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
216 SerialPort.Send(
"Moving ");
217 SerialPort.Send(numberOfIncrements);
218 SerialPort.Send(
" * ");
220 switch (positionIncrement)
222 case POSITION_INCREMENT_1:
224 SerialPort.SendLine(POSITION_INCREMENT_1);
225 motor.MotorInAState(
false);
226 motor.MotorInBState(
false);
228 case POSITION_INCREMENT_2:
230 SerialPort.SendLine(POSITION_INCREMENT_2);
231 motor.MotorInAState(
true);
232 motor.MotorInBState(
false);
234 case POSITION_INCREMENT_3:
236 SerialPort.SendLine(POSITION_INCREMENT_3);
237 motor.MotorInAState(
false);
238 motor.MotorInBState(
true);
240 case POSITION_INCREMENT_4:
242 SerialPort.SendLine(POSITION_INCREMENT_4);
243 motor.MotorInAState(
true);
244 motor.MotorInBState(
true);
256 motor.EnableTriggerPulse(numberOfIncrements, TRIGGER_PULSE_TIME,
true);
259 SerialPort.SendLine(
"Moving.. Waiting for HLFB");
260 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
261 !motor.StatusReg().bit.MotorInFault)
267 if (motor.StatusReg().bit.MotorInFault)
269 SerialPort.SendLine(
"Motor fault detected.");
270 if (HANDLE_MOTOR_FAULTS)
276 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
278 SerialPort.SendLine(
"Motion may not have completed as expected. Proceed with caution.");
279 SerialPort.SendLine();
284 SerialPort.SendLine(
"Move Done");
303void HandleMotorFaults()
305 SerialPort.SendLine(
"Handling fault: clearing faults by cycling enable signal to motor.");
306 motor.EnableRequest(
false);
308 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.