47#define motor ConnectorM0
53#define SerialPort ConnectorUsb
63#define HANDLE_ALERTS (0)
66int32_t velocityLimit = 10000;
67int32_t accelerationLimit = 100000;
71bool MoveAbsolutePosition(int32_t position);
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");
134 MoveAbsolutePosition(10000);
137 MoveAbsolutePosition(19200);
140 MoveAbsolutePosition(12800);
143 MoveAbsolutePosition(0);
162bool MoveAbsolutePosition(int32_t position) {
165 if (motor.StatusReg().bit.AlertsPresent) {
166 SerialPort.SendLine(
"Motor alert detected.");
171 SerialPort.SendLine(
"Enable automatic alert handling by setting HANDLE_ALERTS to 1.");
173 SerialPort.SendLine(
"Move canceled.");
174 SerialPort.SendLine();
178 SerialPort.Send(
"Moving to absolute position: ");
179 SerialPort.SendLine(position);
182 motor.Move(position, MotorDriver::MOVE_TARGET_ABSOLUTE);
185 SerialPort.SendLine(
"Moving.. Waiting for HLFB");
186 while ( (!motor.StepsComplete() || motor.HlfbState() != MotorDriver::HLFB_ASSERTED) &&
187 !motor.StatusReg().bit.AlertsPresent) {
192 if (motor.StatusReg().bit.AlertsPresent) {
193 SerialPort.SendLine(
"Motor alert detected.");
198 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_ALERTS to 1.");
200 SerialPort.SendLine(
"Motion may not have completed as expected. Proceed with caution.");
201 SerialPort.SendLine();
204 SerialPort.SendLine(
"Move Done");
224 SerialPort.SendLine(
"Alerts present: ");
225 if(motor.AlertReg().bit.MotionCanceledInAlert){
226 SerialPort.SendLine(
" MotionCanceledInAlert "); }
227 if(motor.AlertReg().bit.MotionCanceledPositiveLimit){
228 SerialPort.SendLine(
" MotionCanceledPositiveLimit "); }
229 if(motor.AlertReg().bit.MotionCanceledNegativeLimit){
230 SerialPort.SendLine(
" MotionCanceledNegativeLimit "); }
231 if(motor.AlertReg().bit.MotionCanceledSensorEStop){
232 SerialPort.SendLine(
" MotionCanceledSensorEStop "); }
233 if(motor.AlertReg().bit.MotionCanceledMotorDisabled){
234 SerialPort.SendLine(
" MotionCanceledMotorDisabled "); }
235 if(motor.AlertReg().bit.MotorFaulted){
236 SerialPort.SendLine(
" MotorFaulted ");
256 if(motor.AlertReg().bit.MotorFaulted){
258 SerialPort.SendLine(
"Faults present. Cycling enable signal to motor to clear faults.");
259 motor.EnableRequest(
false);
261 motor.EnableRequest(
true);
264 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.