MotorsController¶
- class bbl.motors.MotorsController(*args, **kwargs)[source]¶
Bases:
objectA singleton class to control two DC motors using PWM signals.
This class provides a simple interface to control the speed and direction of two DC motors. It initializes the PWM pins for each motor channel and provides methods to set the motor speed, stop the motors, and configure the forward/reverse speed and offset parameters for each motor. .. rubric:: Example
>>> motors = MotorsController() >>> # Set motor 1 to forward at half speed >>> motors.set_speed(1, 1024) >>> # Set motor 2 to reverse at quarter speed >>> motors.set_speed(2, -512) >>> # Stop motor 1 >>> motors.stop(1)
- get_forward_rate(motor_idx)[source]¶
Gets the maximum forward speed for the specified motor.
- Parameters:
motor_idx (int) – Index of the motor (1 or 2).
- Returns:
The forward speed of the motor, in the range [0, 100]. If the motor index is invalid, returns None.
- Return type:
Example
>>> # Returns 100 (default forward speed for motor 1) >>> motors.get_forward_rate(1) >>> # Returns 80 (if motor 2's forward speed is set to 80) >>> motors.get_forward_rate(2)
- get_offset(motor_idx)[source]¶
Gets the offset value for the specified motor.
- Parameters:
motor_idx (int) – Index of the motor (1 or 2).
- Returns:
The offset value of the motor, in the range [-100, 100].
- Return type:
Example
>>> # Returns 0 (default offset for motor 1) >>> motors.get_offset(1) >>> # Returns 10 (if motor 2's offset is set to 10) >>> motors.get_offset(2)
- get_reverse_rate(motor_idx)[source]¶
Gets the maximum reverse speed for the specified motor.
- Parameters:
motor_idx (int) – Index of the motor (1 or 2).
invalid (If the motor index is)
None. (returns)
- Returns:
The reverse speed of the motor, in the range [0, 100].
- Return type:
Example
>>> # Returns 100 (default reverse speed for motor 1) >>> motors.get_reverse_rate(1) >>> # Returns 80 (if motor 2's reverse speed is set to 80) >>> motors.get_reverse_rate(2)
- motors_period_cb()[source]¶
Updates the duty cycles of the motors based on the current motor duty. This method simulates PWM by turning the motors on and off in intervals, adjusting the duty cycles based on the PWM timing signal.
This method should be called periodically to update motor speed. .. rubric:: Example
>>> motors.motors_period_cb() # Periodically update motor speed
- set_forward_rate(motor_idx, val)[source]¶
Sets the maximum forward speed for the specified motor. The speed value must be between 0 and 100, where 100 indicates maximum speed.
- Parameters:
- Raises:
ValueError – If the value is outside the acceptable range.
Example
>>> # Set motor 1 to 80% forward speed >>> motors.set_forward_rate(1, 80)
- set_offset(motor_idx, val)[source]¶
Sets the offset for the specified motor.
- Parameters:
- Raises:
ValueError – If the value is outside the acceptable range.
Example
>>> motors.set_offset(1, 20) # Set motor 1 offset to 20
- set_reverse_rate(motor_idx, val)[source]¶
Sets the maximum reverse speed for the specified motor.
- Parameters:
- Raises:
ValueError – If the value is outside the acceptable range.
Example
>>> # Set motor 2 to 50% reverse speed >>> motors.set_reverse_rate(2, 50)
- set_speed(motor_idx, speed)[source]¶
Sets the speed of a motor.
This method sets the speed of a specified motor by adjusting the duty cycles of its channels. The speed is controlled using a simulated PWM (Pulse Width Modulation) signal, where the duty cycle determines the effective speed of the motor. The speed value can range from -2048 to 2048, where positive values indicate forward movement, negative values indicate reverse movement, and zero indicates no movement.
- Parameters:
- Returns:
None
Example
>>> # Set motor 1 to move forward at half speed >>> set_speed(1, 1024) >>> # Set motor 2 to move reverse at a quarter speed >>> set_speed(2, -512)
- stop(motor_idx)[source]¶
Stops a motor by setting its duty cycles to 0.
- Parameters:
motor_idx (int) – Index of the motor (1 or 2).
- Raises:
ValueError – If the motor index is not 1 or 2.
Example
>>> motors.stop(1) # Stop motor 1 >>> motors.stop(2) # Stop motor 2