58#define motor ConnectorM0
61#define LockSensor ConnectorDI6
64#define AnalogSensor ConnectorA9
70#define SerialPort ConnectorUsb
80#define HANDLE_MOTOR_FAULTS (0)
84double positionZeroPWM = 0;
85double positionMaxPWM = 10000;
86void HandleMotorFaults();
91void LockSensorCallback();
92bool CommandPosition(int32_t commandedPosition);
96 AnalogSensor.Mode(Connector::INPUT_ANALOG);
101 Connector::CPM_MODE_A_DIRECT_B_PWM);
104 motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
106 motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
110 LockSensor.Mode(Connector::INPUT_DIGITAL);
111 LockSensor.InterruptHandlerSet(LockSensorCallback, InputManager::CHANGE,
true);
113 motor.MotorInAState(LockSensor.State());
117 SerialPort.Mode(Connector::USB_CDC);
118 SerialPort.Speed(baudRate);
119 uint32_t timeout = 5000;
121 SerialPort.PortOpen();
122 while (!SerialPort &&
Milliseconds() - startTime < timeout) {
127 motor.EnableRequest(
true);
128 SerialPort.SendLine(
"Motor Enabled");
131 SerialPort.SendLine(
"Waiting for HLFB...");
132 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
133 !motor.StatusReg().bit.MotorInFault) {
138 if (motor.StatusReg().bit.MotorInFault) {
139 SerialPort.SendLine(
"Motor fault detected.");
140 if(HANDLE_MOTOR_FAULTS){
143 SerialPort.SendLine(
"Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
145 SerialPort.SendLine(
"Enabling may not have completed as expected. Proceed with caution.");
146 SerialPort.SendLine();
148 SerialPort.SendLine(
"Motor Ready");
153 float analogVoltage = AnalogSensor.AnalogVoltage();
155 int32_t commandedPosition =
156 static_cast<int32_t
>(round(analogVoltage / 10 * positionMaxPWM));
157 CommandPosition(commandedPosition);
172bool CommandPosition(int32_t commandedPosition) {
173 if (abs(commandedPosition) > abs(positionMaxPWM) ||
174 abs(commandedPosition) < abs(positionZeroPWM)) {
175 SerialPort.SendLine(
"Move rejected, invalid position requested");
181 if (motor.StatusReg().bit.MotorInFault) {
182 if(HANDLE_MOTOR_FAULTS){
183 SerialPort.SendLine(
"Motor fault detected. Move canceled.");
186 SerialPort.SendLine(
"Motor fault detected. Move canceled. Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
191 SerialPort.Send(
"Moving to position: ");
192 SerialPort.SendLine(commandedPosition);
196 double scaleFactor = 255 / abs(positionMaxPWM - positionZeroPWM);
199 uint8_t dutyRequest = abs(commandedPosition - positionZeroPWM) * scaleFactor;
202 motor.MotorInBDuty(dutyRequest);
218void LockSensorCallback() {
222 motor.MotorInAState(LockSensor.State());
239 void HandleMotorFaults(){
240 SerialPort.SendLine(
"Handling fault: clearing faults by cycling enable signal to motor.");
241 motor.EnableRequest(
false);
243 motor.EnableRequest(
true);
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 MotorModeSet(MotorPair motorPair, Connector::ConnectorModes newMode)
Sets the operational mode for the specified MotorDriver connectors.