53#define motor ConnectorM0
58#define TRIGGER_PULSE_TIME 25
64#define SerialPort ConnectorUsb
74#define HANDLE_ALERTS (0)
78bool MoveDistance(int32_t pulseNum);
90 Connector::CPM_MODE_STEP_AND_DIR);
93 motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
95 motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
100 motor.VelMax(INT32_MAX);
101 motor.AccelMax(INT32_MAX);
105 SerialPort.Mode(Connector::USB_CDC);
106 SerialPort.Speed(baudRate);
107 uint32_t timeout = 5000;
109 SerialPort.PortOpen();
110 while (!SerialPort &&
Milliseconds() - startTime < timeout) {
116 motor.EnableRequest(
true);
117 SerialPort.SendLine(
"Motor Enabled");
120 SerialPort.SendLine(
"Waiting for HLFB...");
121 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
122 !motor.StatusReg().bit.AlertsPresent) {
127 if (motor.StatusReg().bit.AlertsPresent) {
128 SerialPort.SendLine(
"Motor alert detected.");
133 SerialPort.SendLine(
"Enable automatic alert handling by setting HANDLE_ALERTS to 1.");
135 SerialPort.SendLine(
"Enabling may not have completed as expected. Proceed with caution.");
136 SerialPort.SendLine();
138 SerialPort.SendLine(
"Motor Ready");
151 motor.EnableTriggerPulse(1, TRIGGER_PULSE_TIME,
true);
154 MoveDistance(-12800);
178bool MoveDistance(int32_t distance) {
181 if (motor.StatusReg().bit.AlertsPresent) {
182 SerialPort.SendLine(
"Motor alert detected.");
187 SerialPort.SendLine(
"Enable automatic alert handling by setting HANDLE_ALERTS to 1.");
189 SerialPort.SendLine(
"Move canceled.");
190 SerialPort.SendLine();
194 SerialPort.Send(
"Commanding ");
195 SerialPort.Send(distance);
196 SerialPort.SendLine(
" pulses");
199 motor.Move(distance);
205 SerialPort.SendLine(
"Moving.. Waiting for HLFB");
206 while ( (!motor.StepsComplete() || motor.HlfbState() != MotorDriver::HLFB_ASSERTED) &&
207 !motor.StatusReg().bit.AlertsPresent) {
212 if (motor.StatusReg().bit.AlertsPresent) {
213 SerialPort.SendLine(
"Motor alert detected.");
218 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_ALERTS to 1.");
220 SerialPort.SendLine(
"Motion may not have completed as expected. Proceed with caution.");
221 SerialPort.SendLine();
224 SerialPort.SendLine(
"Move Done");
244 SerialPort.SendLine(
"Alerts present: ");
245 if(motor.AlertReg().bit.MotionCanceledInAlert){
246 SerialPort.SendLine(
" MotionCanceledInAlert "); }
247 if(motor.AlertReg().bit.MotionCanceledPositiveLimit){
248 SerialPort.SendLine(
" MotionCanceledPositiveLimit "); }
249 if(motor.AlertReg().bit.MotionCanceledNegativeLimit){
250 SerialPort.SendLine(
" MotionCanceledNegativeLimit "); }
251 if(motor.AlertReg().bit.MotionCanceledSensorEStop){
252 SerialPort.SendLine(
" MotionCanceledSensorEStop "); }
253 if(motor.AlertReg().bit.MotionCanceledMotorDisabled){
254 SerialPort.SendLine(
" MotionCanceledMotorDisabled "); }
255 if(motor.AlertReg().bit.MotorFaulted){
256 SerialPort.SendLine(
" MotorFaulted ");
276 if(motor.AlertReg().bit.MotorFaulted){
278 SerialPort.SendLine(
"Faults present. Cycling enable signal to motor to clear faults.");
279 motor.EnableRequest(
false);
281 motor.EnableRequest(
true);
284 SerialPort.SendLine(
"Clearing alerts.");
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.