← Back to Main Portfolio

task_motor.py

Purpose

This file implements the motor-control task for a single wheel. It combines encoder updates, PI velocity control, optional logging, and publication of observer-related signals such as motor voltage and wheel displacement in meters.

Main Role

The task provides low-level closed-loop actuation for one wheel. Two instances of this task are created in main.py, one for the left motor and one for the right motor. This is what makes commanded wheel speeds repeatable and robust to disturbances such as battery variation or friction changes.

Key Responsibilities

Important Attributes / Shares / Variables

NameDescription
_motMotor-driver object used to apply PWM effort to the wheel.
_encEncoder object used to measure wheel position and velocity.
_goFlagShare that enables or disables active motor control.
_vRefShare containing the desired wheel velocity.
_Kp, _KiPI controller gain Shares.
_dataValues, _timeValues, _effortValuesQueues used for logging speed, timestamp, and control effort.
_u_shareShare used to publish motor voltage for the observer.
_s_shareShare used to publish wheel displacement in meters.
_VbattBattery voltage scale factor used to convert effort percentage to volts.
_RWheel radius in meters.
_CPRCounts per revolution used for displacement conversion.
_m_per_countDistance-per-count conversion factor.
_signDirection sign chosen so forward motion is treated as positive.
_v_filtFiltered velocity state used in control.
_e_intIntegral-of-error accumulator for PI control.

Important Methods

MethodDescription
_sat()Saturates the control effort to a bounded range.
_clear_queue()Clears queue contents before new logging sessions.
_update_encoder_and_publish_s()Always updates encoder position and publishes wheel displacement, even when the motor-go flag is false. This is important for observer behavior when the robot is moved by hand.
run()Main generator task implementing initialization, wait, and run behavior.
Click to view representative PI-control concept
error = vRef - velocity
effort = Kp * error + Ki * integral_error
Click to view engineering notes

A subtle but very good design choice in this file is that encoder updates continue even when the motor is disabled. That means displacement Shares still reflect the physical motion of the robot, which is valuable for state estimation and debugging.

Why This File Matters

This file closes the loop around each wheel. Without it, the robot would behave much more like an open-loop platform and would be less stable and repeatable during line following, turns, and obstacle-course maneuvers.

Open raw task_motor.py file