43#define motor ConnectorM0
49#define SerialPort ConnectorUsb
59#define HANDLE_ALERTS (0)
62int32_t accelerationLimit = 100000;
66bool MoveAtVelocity(int32_t velocity);
77 Connector::CPM_MODE_STEP_AND_DIR);
80 motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
82 motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
85 motor.AccelMax(accelerationLimit);
89 SerialPort.Mode(Connector::USB_CDC);
90 SerialPort.Speed(baudRate);
91 uint32_t timeout = 5000;
93 SerialPort.PortOpen();
94 while (!SerialPort &&
Milliseconds() - startTime < timeout) {
99 motor.EnableRequest(
true);
100 SerialPort.SendLine(
"Motor Enabled");
103 SerialPort.SendLine(
"Waiting for HLFB...");
104 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
105 !motor.StatusReg().bit.AlertsPresent) {
110 if (motor.StatusReg().bit.AlertsPresent) {
111 SerialPort.SendLine(
"Motor alert detected.");
116 SerialPort.SendLine(
"Enable automatic alert handling by setting HANDLE_ALERTS to 1.");
118 SerialPort.SendLine(
"Enabling may not have completed as expected. Proceed with caution.");
119 SerialPort.SendLine();
121 SerialPort.SendLine(
"Motor Ready");
126 MoveAtVelocity(1000);
129 MoveAtVelocity(-5000);
132 MoveAtVelocity(10000);
135 MoveAtVelocity(-10000);
154bool MoveAtVelocity(int32_t velocity) {
157 if (motor.StatusReg().bit.AlertsPresent) {
158 SerialPort.SendLine(
"Motor alert detected.");
163 SerialPort.SendLine(
"Enable automatic alert handling by setting HANDLE_ALERTS to 1.");
165 SerialPort.SendLine(
"Move canceled.");
166 SerialPort.SendLine();
170 SerialPort.Send(
"Commanding velocity: ");
171 SerialPort.SendLine(velocity);
174 motor.MoveVelocity(velocity);
178 SerialPort.SendLine(
"Ramping to speed...");
179 while (!motor.StatusReg().bit.AtTargetVelocity) {
184 if (motor.StatusReg().bit.AlertsPresent) {
185 SerialPort.SendLine(
"Motor alert detected.");
190 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_ALERTS to 1.");
192 SerialPort.SendLine(
"Motion may not have completed as expected. Proceed with caution.");
193 SerialPort.SendLine();
196 SerialPort.SendLine(
"Move Done");
216 SerialPort.SendLine(
"Alerts present: ");
217 if(motor.AlertReg().bit.MotionCanceledInAlert){
218 SerialPort.SendLine(
" MotionCanceledInAlert "); }
219 if(motor.AlertReg().bit.MotionCanceledPositiveLimit){
220 SerialPort.SendLine(
" MotionCanceledPositiveLimit "); }
221 if(motor.AlertReg().bit.MotionCanceledNegativeLimit){
222 SerialPort.SendLine(
" MotionCanceledNegativeLimit "); }
223 if(motor.AlertReg().bit.MotionCanceledSensorEStop){
224 SerialPort.SendLine(
" MotionCanceledSensorEStop "); }
225 if(motor.AlertReg().bit.MotionCanceledMotorDisabled){
226 SerialPort.SendLine(
" MotionCanceledMotorDisabled "); }
227 if(motor.AlertReg().bit.MotorFaulted){
228 SerialPort.SendLine(
" MotorFaulted ");
248 if(motor.AlertReg().bit.MotorFaulted){
250 SerialPort.SendLine(
"Faults present. Cycling enable signal to motor to clear faults.");
251 motor.EnableRequest(
false);
253 motor.EnableRequest(
true);
256 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.