Warning:

To protect the digital assets of creators and enhance system security and stability, the CyberBrick Multi-Function Core Board does not support firmware flashing with any third-party tools! If you need to restore the Multi-Function Core Board to its factory state, please wait for the official recovery tool to be released by CyberBrick. If you proceed to flash the firmware using third-party tools, the firmware of the Multi-Function Core Board will be permanently damaged and cannot be recovered. CyberBrick will not be responsible for any consequences resulting from such operations.

This documentation is adapted from the official MicroPython project. The CyberBrick team has extended the source code with custom interfaces and features to our hardware and application needs.

In addition to these enhancements, certain built-in MicroPython interfaces have been intentionally disabled to protect the system's integrity, and ensure the overall security and reliability of the device. This also facilitates content protection for creators' intellectual property, where applicable.

Portions of the content are derived from the official MicroPython documentation and have been included here under its open-source license to provide users with a consistent and enriched development experience tailored to the CyberBrick platform.

MotorsController

class bbl.motors.MotorsController(*args, **kwargs)[source]

Bases: object

A 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)
__init__()[source]

Initializes the MotorsController instance.

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:

int

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:

int

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:

int

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:
  • motor_idx (int) – Index of the motor (1 or 2).

  • val (int) – The forward speed, in the range [0, 100].

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:
  • motor_idx (int) – Index of the motor (1 or 2).

  • val (int) – The offset value, in the range [-100, 100].

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:
  • motor_idx (int) – Index of the motor (1 or 2).

  • val (int) – The reverse speed, in the range [0, 100].

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:
  • motor_idx (int) – Index of the motor (1 or 2).

  • speed (int) – Speed value to set for the motor, ranging from -2048 to 2048.

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