Motor Control Task¶
Summary¶
This task implements the inner wheel-speed control loop for one motor. It reads the encoder, computes wheel speed, applies PI control, commands the motor driver, and optionally logs velocity data.
Important class¶
task_motor(mot, enc, goFlag, k_p, k_i, setpoint, dataValues, timeValues, pos_share=None, vel_share=None, invert_effort=False)Generator-based motor-control task for one wheel.
Important methods¶
run()Main task loop. It waits for an enable flag, zeros the encoder, enables the motor, computes measured speed, applies PI control, and sends effort to the driver.
_mps_to_radps(speed_mps)Converts linear wheel speed in meters per second to angular speed.
_counts_us_to_radps(count_per_us)Converts encoder count rate into angular speed.
_write_telemetry(meas_radps)Publishes velocity and position telemetry through shares.
Important state constants¶
S0_INITInitialization state.
S1_WAITIdle state waiting for a nonzero go flag.
S2_RUNActive closed-loop control state.
Important variables¶
self._motMotor-driver object used to command wheel effort.
self._encEncoder object used to measure wheel motion.
self._goFlagMode share controlling task behavior.
self._k_p/self._k_iPI gains for the inner speed loop.
self._setpointWheel speed command share in meters per second.
self._dataValues/self._timeValuesQueues used to log measured wheel speed.
self._pos_share/self._vel_shareOptional shares used to publish wheel position and wheel velocity.
self._invert_effortOptional sign inversion applied to the computed duty cycle.
self._integralIntegral state of the PI controller.
self._ref_velMost recent angular velocity setpoint.
self._battery_voltsBattery voltage used to convert requested volts into PWM duty cycle.
self._wheel_radius_mWheel radius used for unit conversions.
self._counts_per_wheel_revEncoder counts per wheel revolution.
self._two_piCached constant used in the conversion formulas.
Go-flag meaning¶
0Stop. The motor is disabled and the task returns to the wait state.
1Run closed-loop control and log data until the logging queues fill, then stop automatically.
2Run continuous closed-loop control without auto-stop logging behavior.
Control behavior¶
The task converts the setpoint from meters per second to radians per second, measures actual wheel speed from the encoder, and computes:
error = reference - measuredintegral += error * dteffort_volts = Kp * error + Ki * integral
The voltage command is converted into a duty cycle using the battery voltage
and clamped to the range -100 to 100 percent before being sent to the
motor driver.