47#define motor ConnectorM0
53#define SerialPort ConnectorUsb
63#define HANDLE_ALERTS (0)
66int32_t velocityLimit = 10000;
67int32_t accelerationLimit = 100000;
71bool MoveDistance(int32_t distance);
82 Connector::CPM_MODE_STEP_AND_DIR);
85 motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
87 motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
90 motor.VelMax(velocityLimit);
93 motor.AccelMax(accelerationLimit);
97 SerialPort.Mode(Connector::USB_CDC);
98 SerialPort.Speed(baudRate);
99 uint32_t timeout = 5000;
101 SerialPort.PortOpen();
102 while (!SerialPort &&
Milliseconds() - startTime < timeout) {
107 motor.EnableRequest(
true);
108 SerialPort.SendLine(
"Motor Enabled");
111 SerialPort.SendLine(
"Waiting for HLFB...");
112 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
113 !motor.StatusReg().bit.AlertsPresent) {
118 if (motor.StatusReg().bit.AlertsPresent) {
119 SerialPort.SendLine(
"Motor alert detected.");
124 SerialPort.SendLine(
"Enable automatic alert handling by setting HANDLE_ALERTS to 1.");
126 SerialPort.SendLine(
"Enabling may not have completed as expected. Proceed with caution.");
127 SerialPort.SendLine();
129 SerialPort.SendLine(
"Motor Ready");
140 MoveDistance(-12800);
164bool MoveDistance(int32_t distance) {
167 if (motor.StatusReg().bit.AlertsPresent) {
168 SerialPort.SendLine(
"Motor alert detected.");
173 SerialPort.SendLine(
"Enable automatic alert handling by setting HANDLE_ALERTS to 1.");
175 SerialPort.SendLine(
"Move canceled.");
176 SerialPort.SendLine();
180 SerialPort.Send(
"Moving distance: ");
181 SerialPort.SendLine(distance);
184 motor.Move(distance);
187 SerialPort.SendLine(
"Moving.. Waiting for HLFB");
188 while ( (!motor.StepsComplete() || motor.HlfbState() != MotorDriver::HLFB_ASSERTED) &&
189 !motor.StatusReg().bit.AlertsPresent) {
194 if (motor.StatusReg().bit.AlertsPresent) {
195 SerialPort.SendLine(
"Motor alert detected.");
200 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_ALERTS to 1.");
202 SerialPort.SendLine(
"Motion may not have completed as expected. Proceed with caution.");
203 SerialPort.SendLine();
206 SerialPort.SendLine(
"Move Done");
226 SerialPort.SendLine(
"Alerts present: ");
227 if(motor.AlertReg().bit.MotionCanceledInAlert){
228 SerialPort.SendLine(
" MotionCanceledInAlert "); }
229 if(motor.AlertReg().bit.MotionCanceledPositiveLimit){
230 SerialPort.SendLine(
" MotionCanceledPositiveLimit "); }
231 if(motor.AlertReg().bit.MotionCanceledNegativeLimit){
232 SerialPort.SendLine(
" MotionCanceledNegativeLimit "); }
233 if(motor.AlertReg().bit.MotionCanceledSensorEStop){
234 SerialPort.SendLine(
" MotionCanceledSensorEStop "); }
235 if(motor.AlertReg().bit.MotionCanceledMotorDisabled){
236 SerialPort.SendLine(
" MotionCanceledMotorDisabled "); }
237 if(motor.AlertReg().bit.MotorFaulted){
238 SerialPort.SendLine(
" MotorFaulted ");
258 if(motor.AlertReg().bit.MotorFaulted){
260 SerialPort.SendLine(
"Faults present. Cycling enable signal to motor to clear faults.");
261 motor.EnableRequest(
false);
263 motor.EnableRequest(
true);
266 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 MotorInputClocking(MotorClockRates newRate)
Sets the output step rate for the motor step generators.
bool MotorModeSet(MotorPair motorPair, Connector::ConnectorModes newMode)
Sets the operational mode for the specified MotorDriver connectors.