52#define motor ConnectorM0
55#define AnalogSensor ConnectorA9
59#define INPUT_A_FILTER 20
65#define SerialPort ConnectorUsb
75bool CommandVelocity(int32_t commandedVelocity);
79 AnalogSensor.Mode(Connector::INPUT_ANALOG);
84 Connector::CPM_MODE_A_DIRECT_B_PWM);
88 SerialPort.Mode(Connector::USB_CDC);
89 SerialPort.Speed(baudRate);
90 uint32_t timeout = 5000;
92 SerialPort.PortOpen();
93 while (!SerialPort &&
Milliseconds() - startTime < timeout) {
98 motor.EnableRequest(
true);
99 SerialPort.SendLine(
"Motor Enabled");
103 float analogVoltage = AnalogSensor.AnalogVoltage();
105 int32_t commandedVelocity =
106 static_cast<int32_t
>(round(analogVoltage / 10 * maxSpeed));
109 CommandVelocity(commandedVelocity);
125bool CommandVelocity(int32_t commandedVelocity) {
126 if (abs(commandedVelocity) >= abs(maxSpeed)) {
127 SerialPort.SendLine(
"Move rejected, requested velocity at or over the limit.");
132 if (motor.StatusReg().bit.AlertsPresent) {
133 SerialPort.SendLine(
"Motor status: 'In Alert'. Move Canceled.");
137 SerialPort.Send(
"Commanding velocity: ");
138 SerialPort.SendLine(commandedVelocity);
144 if (commandedVelocity >= 0) {
145 motor.MotorInAState(
false);
148 motor.MotorInAState(
true);
156 double scaleFactor = 255 / maxSpeed;
159 uint8_t dutyRequest = abs(commandedVelocity) * scaleFactor;
162 motor.MotorInBDuty(dutyRequest);
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.