#include "ClearCore.h"
#define motor ConnectorM0
#define TRIGGER_PULSE_TIME 25
#define baudRate 9600
#define SerialPort ConnectorUsb
#define HANDLE_ALERTS (0)
bool MoveDistance(int32_t pulseNum);
void PrintAlerts();
void HandleAlerts();
int main() {
Connector::CPM_MODE_STEP_AND_DIR);
motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
motor.VelMax(INT32_MAX);
motor.AccelMax(INT32_MAX);
SerialPort.Mode(Connector::USB_CDC);
SerialPort.Speed(baudRate);
uint32_t timeout = 5000;
SerialPort.PortOpen();
while (!SerialPort &&
Milliseconds() - startTime < timeout) {
continue;
}
motor.EnableRequest(true);
SerialPort.SendLine("Motor Enabled");
SerialPort.SendLine("Waiting for HLFB...");
while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
!motor.StatusReg().bit.AlertsPresent) {
continue;
}
if (motor.StatusReg().bit.AlertsPresent) {
SerialPort.SendLine("Motor alert detected.");
PrintAlerts();
if(HANDLE_ALERTS){
HandleAlerts();
} else {
SerialPort.SendLine("Enable automatic alert handling by setting HANDLE_ALERTS to 1.");
}
SerialPort.SendLine("Enabling may not have completed as expected. Proceed with caution.");
SerialPort.SendLine();
} else {
SerialPort.SendLine("Motor Ready");
}
while (true) {
MoveDistance(6400);
MoveDistance(19200);
motor.EnableTriggerPulse(1, TRIGGER_PULSE_TIME, true);
MoveDistance(-12800);
MoveDistance(-6400);
MoveDistance(-6400);
}
}
bool MoveDistance(int32_t distance) {
if (motor.StatusReg().bit.AlertsPresent) {
SerialPort.SendLine("Motor alert detected.");
PrintAlerts();
if(HANDLE_ALERTS){
HandleAlerts();
} else {
SerialPort.SendLine("Enable automatic alert handling by setting HANDLE_ALERTS to 1.");
}
SerialPort.SendLine("Move canceled.");
SerialPort.SendLine();
return false;
}
SerialPort.Send("Commanding ");
SerialPort.Send(distance);
SerialPort.SendLine(" pulses");
motor.Move(distance);
SerialPort.SendLine("Moving.. Waiting for HLFB");
while ( (!motor.StepsComplete() || motor.HlfbState() != MotorDriver::HLFB_ASSERTED) &&
!motor.StatusReg().bit.AlertsPresent) {
continue;
}
if (motor.StatusReg().bit.AlertsPresent) {
SerialPort.SendLine("Motor alert detected.");
PrintAlerts();
if(HANDLE_ALERTS){
HandleAlerts();
} else {
SerialPort.SendLine("Enable automatic fault handling by setting HANDLE_ALERTS to 1.");
}
SerialPort.SendLine("Motion may not have completed as expected. Proceed with caution.");
SerialPort.SendLine();
return false;
} else {
SerialPort.SendLine("Move Done");
return true;
}
}
void PrintAlerts(){
SerialPort.SendLine("Alerts present: ");
if(motor.AlertReg().bit.MotionCanceledInAlert){
SerialPort.SendLine(" MotionCanceledInAlert "); }
if(motor.AlertReg().bit.MotionCanceledPositiveLimit){
SerialPort.SendLine(" MotionCanceledPositiveLimit "); }
if(motor.AlertReg().bit.MotionCanceledNegativeLimit){
SerialPort.SendLine(" MotionCanceledNegativeLimit "); }
if(motor.AlertReg().bit.MotionCanceledSensorEStop){
SerialPort.SendLine(" MotionCanceledSensorEStop "); }
if(motor.AlertReg().bit.MotionCanceledMotorDisabled){
SerialPort.SendLine(" MotionCanceledMotorDisabled "); }
if(motor.AlertReg().bit.MotorFaulted){
SerialPort.SendLine(" MotorFaulted ");
}
}
void HandleAlerts(){
if(motor.AlertReg().bit.MotorFaulted){
SerialPort.SendLine("Faults present. Cycling enable signal to motor to clear faults.");
motor.EnableRequest(false);
motor.EnableRequest(true);
}
SerialPort.SendLine("Clearing alerts.");
motor.ClearAlerts();
}