#include "ClearCore.h"
#define motor ConnectorM0
#define baudRate 9600
#define SerialPort ConnectorUsb
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(10000);
motor.AccelMax(100000);
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");
if (motor.StatusReg().bit.AlertsPresent) {
SerialPort.SendLine("Motor status: 'In Alert'. Move Canceled.");
while (true) {
continue;
}
}
SerialPort.SendLine("Moving toward hardstop... Waiting for HLFB");
motor.MoveVelocity(5000);
motor.MoveVelocity(1000);
if (motor.StatusReg().bit.AlertsPresent) {
SerialPort.SendLine("Motor alert occurred during motion. Homing canceled.");
while (true) {
continue;
}
}
while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED) {
if (motor.StatusReg().bit.AlertsPresent) {
SerialPort.SendLine("Motor alert detected. Homing canceled.");
while (true) {
continue;
}
}
}
motor.MoveStopAbrupt();
motor.Move(-1000);
SerialPort.SendLine("Moving away from hardstop... Waiting for HLFB");
while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED) {
continue;
}
if (motor.StatusReg().bit.AlertsPresent) {
SerialPort.SendLine("Motor alert occurred during offset move. Homing canceled.");
while (true) {
continue;
}
} else {
SerialPort.SendLine("Homing Complete. Motor Ready.");
}
motor.PositionRefSet(0);
}