motor

class motor.Motor(pins, pwm_channel, gains)

Bases: object

A class to control a DC motor using PWM and direction signals.

pwm_pin

Pin object for the PWM signal controlling motor speed.

Type:

Pin

dir_pin

Pin object for setting the motor direction.

Type:

Pin

slp_pin

Pin object for enabling or disabling the motor.

Type:

Pin

pwm_timer

Timer object configured for PWM signal generation.

Type:

Timer

pwm_channel

Timer channel used for generating the PWM signal.

Type:

Timer.channel

kp

Proportional gain for the PID controller.

Type:

float

ki

Integral gain for the PID controller.

Type:

float

integral

Integral term accumulation for the PID controller.

Type:

float

prev_time

Timestamp of the previous PID update.

Type:

int

del_time

Time difference between PID updates.

Type:

int

now

Current timestamp for PID calculations.

Type:

int

prev_error

Previous error value used in the PID controller.

Type:

float

disable()

Disables the motor by setting the sleep pin low.

enable()

Enables the motor by setting sleep pin high while keeping PWM at 0.

forward(speed)

Moves the motor forward at a specified speed.

Parameters:

speed (int) – Speed percentage (0-100) to apply to the motor.

pid(des, act, vel, mode)

Implements a PID controller to adjust motor speed based on error.

Parameters:
  • des (float) – Desired position or velocity.

  • act (float) – Actual measured position or velocity.

  • vel (int) – Base velocity adjustment value.

  • mode (int) – Control mode (0 for standard PID control, 1 for inverse correction).

reverse(speed)

Moves the motor in reverse at a specified speed.

Parameters:

speed (int) – Speed percentage (0-100) to apply to the motor.

stop()

Stops the motor by setting PWM to 0 while keeping it enabled.