53#define INPUT_A_FILTER 20
56#define motor ConnectorM0
62#define SerialPort ConnectorUsb
66double maxTorque = 100;
70bool CommandTorque(int8_t commandedTorque);
76 Connector::CPM_MODE_A_DIRECT_B_PWM);
80 SerialPort.Mode(Connector::USB_CDC);
81 SerialPort.Speed(baudRate);
82 uint32_t timeout = 5000;
84 SerialPort.PortOpen();
85 while (!SerialPort &&
Milliseconds() - startTime < timeout) {
90 motor.EnableRequest(
true);
91 SerialPort.SendLine(
"Motor Enabled");
94 SerialPort.SendLine(
"Waiting for HLFB...");
95 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED) {
98 SerialPort.SendLine(
"Motor Ready");
134bool CommandTorque(int8_t commandedTorque) {
135 if (abs(commandedTorque) > abs(maxTorque)) {
136 SerialPort.SendLine(
"Move rejected, invalid torque requested");
141 if (motor.StatusReg().bit.AlertsPresent) {
142 SerialPort.SendLine(
"Motor status: 'In Alert'. Move Canceled.");
146 SerialPort.Send(
"Commanding torque: ");
147 SerialPort.SendLine(commandedTorque);
151 double scaleFactor = 255 / maxTorque;
154 uint8_t dutyRequest = abs(commandedTorque) * scaleFactor;
157 if (commandedTorque < 0) {
158 motor.MotorInAState(
true);
161 motor.MotorInAState(
false);
169 motor.MotorInBDuty(dutyRequest);
172 SerialPort.SendLine(
"Moving... Waiting for HLFB");
175 while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED) {
179 SerialPort.SendLine(
"Move Done");
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.