52#define motor0 ConnectorM0
53#define motor1 ConnectorM1
60#define SerialPort ConnectorUsb
70#define HANDLE_ALERTS (0)
73int32_t velocityLimit = 10000;
74int32_t accelerationLimit = 100000;
78bool SynchronizedMove(int32_t distance);
89 Connector::CPM_MODE_STEP_AND_DIR);
93 motor0.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
94 motor1.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
97 motor0.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
98 motor1.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
101 motor0.VelMax(velocityLimit);
102 motor1.VelMax(velocityLimit);
105 motor0.AccelMax(accelerationLimit);
106 motor1.AccelMax(accelerationLimit);
110 SerialPort.Mode(Connector::USB_CDC);
111 SerialPort.Speed(baudRate);
112 uint32_t timeout = 5000;
114 SerialPort.PortOpen();
115 while (!SerialPort &&
Milliseconds() - startTime < timeout) {
120 motor0.EnableRequest(
true);
121 SerialPort.SendLine(
"Motor 0 Enabled");
122 motor1.EnableRequest(
true);
123 SerialPort.SendLine(
"Motor 1 Enabled");
127 SerialPort.SendLine(
"Waiting for HLFB...");
128 while ( (motor0.HlfbState() != MotorDriver::HLFB_ASSERTED ||
129 motor1.HlfbState() != MotorDriver::HLFB_ASSERTED) &&
130 !motor0.StatusReg().bit.AlertsPresent &&
131 !motor1.StatusReg().bit.AlertsPresent) {
134 SerialPort.SendLine(
"Waiting for HLFB to assert on both motors");
140 if (motor0.StatusReg().bit.AlertsPresent ||
141 motor1.StatusReg().bit.AlertsPresent) {
142 SerialPort.SendLine(
"Motor alert detected.");
147 SerialPort.SendLine(
"Enable automatic alert handling by setting HANDLE_ALERTS to 1.");
149 SerialPort.SendLine(
"Enabling may not have completed as expected. Proceed with caution.");
150 SerialPort.SendLine();
152 SerialPort.SendLine(
"Motor Ready");
157 SynchronizedMove(6400);
160 SynchronizedMove(19200);
163 SynchronizedMove(-12800);
166 SynchronizedMove(-6400);
169 SynchronizedMove(-6400);
188bool SynchronizedMove(int32_t distance) {
191 if (motor0.StatusReg().bit.AlertsPresent || motor1.StatusReg().bit.AlertsPresent ) {
192 SerialPort.SendLine(
"Motor alert detected.");
197 SerialPort.SendLine(
"Enable automatic alert handling by setting HANDLE_ALERTS to 1.");
199 SerialPort.SendLine(
"Move canceled.");
200 SerialPort.SendLine();
204 SerialPort.Send(
"Moving distance: ");
205 SerialPort.SendLine(distance);
208 motor0.Move(distance);
209 motor1.Move(distance);
212 SerialPort.SendLine(
"Waiting for HLFB to assert on both motors");
216 while ( (!motor0.StepsComplete() || motor0.HlfbState() != MotorDriver::HLFB_ASSERTED ||
217 !motor1.StepsComplete() || motor1.HlfbState() != MotorDriver::HLFB_ASSERTED) &&
218 !motor0.StatusReg().bit.AlertsPresent && !motor1.StatusReg().bit.AlertsPresent ){
224 if (motor0.StatusReg().bit.AlertsPresent || motor1.StatusReg().bit.AlertsPresent){
225 motor0.MoveStopAbrupt();
226 motor1.MoveStopAbrupt();
227 SerialPort.SendLine(
"Motor alert detected.");
232 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_ALERTS to 1.");
234 SerialPort.SendLine(
"Motion may not have completed as expected. Proceed with caution.");
235 SerialPort.SendLine();
238 SerialPort.SendLine(
"Move Done");
257 SerialPort.SendLine(
"Alerts present on motor0: ");
258 if(motor0.AlertReg().bit.MotionCanceledInAlert){
259 SerialPort.SendLine(
" MotionCanceledInAlert "); }
260 if(motor0.AlertReg().bit.MotionCanceledPositiveLimit){
261 SerialPort.SendLine(
" MotionCanceledPositiveLimit "); }
262 if(motor0.AlertReg().bit.MotionCanceledNegativeLimit){
263 SerialPort.SendLine(
" MotionCanceledNegativeLimit "); }
264 if(motor0.AlertReg().bit.MotionCanceledSensorEStop){
265 SerialPort.SendLine(
" MotionCanceledSensorEStop "); }
266 if(motor0.AlertReg().bit.MotionCanceledMotorDisabled){
267 SerialPort.SendLine(
" MotionCanceledMotorDisabled "); }
268 if(motor0.AlertReg().bit.MotorFaulted){
269 SerialPort.SendLine(
" MotorFaulted ");
273 SerialPort.SendLine(
"Alerts present on motor1: ");
274 if(motor1.AlertReg().bit.MotionCanceledInAlert){
275 SerialPort.SendLine(
" MotionCanceledInAlert "); }
276 if(motor1.AlertReg().bit.MotionCanceledPositiveLimit){
277 SerialPort.SendLine(
" MotionCanceledPositiveLimit "); }
278 if(motor1.AlertReg().bit.MotionCanceledNegativeLimit){
279 SerialPort.SendLine(
" MotionCanceledNegativeLimit "); }
280 if(motor1.AlertReg().bit.MotionCanceledSensorEStop){
281 SerialPort.SendLine(
" MotionCanceledSensorEStop "); }
282 if(motor1.AlertReg().bit.MotionCanceledMotorDisabled){
283 SerialPort.SendLine(
" MotionCanceledMotorDisabled "); }
284 if(motor1.AlertReg().bit.MotorFaulted){
285 SerialPort.SendLine(
" MotorFaulted ");
306 if(motor0.AlertReg().bit.MotorFaulted){
307 SerialPort.SendLine(
"Faults present on motor0. Cycling enable signal to motor to clear faults.");
308 motor0.EnableRequest(
false);
310 motor0.EnableRequest(
true);
312 if(motor1.AlertReg().bit.MotorFaulted){
313 SerialPort.SendLine(
"Faults present on motor1. Cycling enable signal to motor to clear faults.");
314 motor1.EnableRequest(
false);
316 motor1.EnableRequest(
true);
319 SerialPort.SendLine(
"Clearing alerts on both motors.");
320 motor0.ClearAlerts();
321 motor1.ClearAlerts();
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.