controllables.DCMotor

class controllables.DCMotor(port: Optional[int] = None)

Reference to a DC motor part. Base class: MotorBase.

In addition to the methods and properties in the MotorBase, DCMotor provides the brake_force() and brake_time() properties.

The following code block assumes that a DC motors are assigned to ports 0-3 with the first 2 on the left and the last 2 on the right. It also assumes that none of the motors are flipped. For best performance make sure that the wasd keys are not assigned to those motors.

from controllables import DCMotor
from inputs import Input

# get the motors
left_motors = [DCMotor(0),DCMotor(1)]
right_motors = [DCMotor(2),DCMotor(3)]

def go_forward():
    for motor in left_motors:
        motor.spin(-1)
    for motor in right_motors:
        motor.spin(1)

def go_backward():
    for motor in left_motors:
        motor.spin(1)
    for motor in right_motors:
        motor.spin(-1)

def go_left():
    for motor in left_motors + right_motors:
        motor.spin(.5)

def go_right():
    for motor in left_motors + right_motors:
        motor.spin(-.5)

def stop():
    for motor in left_motors + right_motors:
        motor.stop()

# a class to hold the information for each button
class Control():
    def __init__(self,key,onpress):
        self.stream = Input.stream(key)
        self.onpress = onpress

# the list of our controls
controls = [
    Control("a",go_left),
    Control("d",go_right),
    Control("w",go_forward),
    Control("s",go_backward)
    ]

# keep the pressed controls in a stack so that
# we can remember which was pressed most recently
stack : list[Control] = []

while True:
    # for each control
    for control in controls:
        # listen to its key events
        for event in control.stream:
            # if the key was pressed
            if event:
                # call its onpress
                control.onpress()
                # and add it to the stack
                stack.append(control)
            else:
                # remove the control from the stack
                if control in stack:
                    stack.remove(control)
                # if there are any keys still being pressed
                if len(stack) > 0:
                    # call their onpress function
                    stack[-1].onpress()
                else:
                    # otherwise, stop
                    stop()
property brake_force: float

The brake force property (in newton meters over radians per second) of the motor available in the properties tab. The torque applied to brake the motor is higher when the motor is spinning faster.

In the below example, the property is displayed on a TextScreen. It assumes that the screen is on port 0 and the dc motor is assigned to port 1.

from controllables import TextScreen, DCMotor

screen = TextScreen(0)
motor = DCMotor(1)

while True:
    screen.text = f'motor.brake_force: {motor.brake_force: 0.1f}'

In the next example the property is set to 0 on start up. It assumes that the dc motor is assigned to port 0.

from controllables import DCMotor

DCMotor(0).brake_force = 0
property brake_time: float

The brake time (in seconds) property of the motor avaliable in the properties tab.

In the below example, the property is displayed on a TextScreen. It assumes that the screen is on port 0 and the dc motor is assigned to port 1.

from controllables import TextScreen, DCMotor

screen = TextScreen(0)
motor = DCMotor(1)

while True:
    screen.text = f'motor.brake_time: {motor.brake_time: 0.1f}'

In the next example the property is set to 0 on start up. It assumes that the dc motor is assigned to port 0.

from controllables import DCMotor

DCMotor(0).brake_time = 0
property acceleration_time: float

The acceleration time (in seconds) property of the motor avaliable in the properties tab.

In the below example, the property is displayed on a TextScreen. It assumes that the screen is on port 0 and the motor is assigned to port 1.

from controllables import TextScreen, MotorBase

screen = TextScreen(0)
motor = MotorBase(1)

while True:
    screen.text = f'motor.acceleration_time: {motor.acceleration_time: 0.1f}'

In the next example the property is set to 0 on start up. It assumes that the motor is assigned to port 0.

from controllables import MotorBase

MotorBase(0).acceleration_time = 0
angle() float

Returns the angle of the motor in radians.

In the below example, the angle is displayed on a TextScreen. It assumes that the screen is on port 0 and the motor is assigned to port 1.

from controllables import TextScreen, MotorBase
from math import tau

screen = TextScreen(0)
motor = MotorBase(1)

while True:
    message = f'motor.angle: {motor.angle(): 0.2f}'
    message += '\n'
    message += f'percentage: {motor.angle() / tau}'
    screen.text = message
degrees() float

Returns the angle of the motor in degrees.

In the below example, the angle in degrees is displayed on a TextScreen. It assumes that the screen is on port 0 and the motor is assigned to port 1.

from controllables import TextScreen, MotorBase

screen = TextScreen(0)
motor = MotorBase(1)

while True:
    screen.text = f'motor.degrees(): {motor.degrees(): 0.0f}'
property flipped: bool

The flip property of the motor avaliable in the properties tab.

In the below example, the property is displayed on a TextScreen. It assumes that the screen is on port 0 and the motor is assigned to port 1.

from controllables import TextScreen, MotorBase

screen = TextScreen(0)
motor = MotorBase(1)

while True:
    screen.text = f'motor.flipped: {motor.flipped}'

In the next example the property is set to True on start up. It assumes that the motor is assigned to port 0.

from controllables import MotorBase

MotorBase(0).flipped = True
property max_rpm: float

The max rpm property of the motor avaliable in the properties tab.

In the below example, the property is displayed on a TextScreen. It assumes that the screen is on port 0 and the motor is assigned to port 1.

from controllables import TextScreen, MotorBase

screen = TextScreen(0)
motor = MotorBase(1)

while True:
    screen.text = f'motor.max_rpm: {motor.max_rpm: 0.0f}'

In the next example the property is set to 0 on start up. It assumes that the motor is assigned to port 0.

from controllables import MotorBase

MotorBase(0).max_rpm = 0
property max_torque: float

The max torque (in newton meters) property of the motor avaliable in the properties tab.

In the below example, the property is displayed on a TextScreen. It assumes that the screen is on port 0 and the motor is assigned to port 1.

from controllables import TextScreen, MotorBase

screen = TextScreen(0)
motor = MotorBase(1)

while True:
    screen.text = f'motor.max_torque: {motor.max_torque: 0.0f}'

In the next example the property is set to 0 on start up. It assumes that the motor is assigned to port 0.

from controllables import MotorBase

MotorBase(0).max_torque = 0
property max_velocity: float

The maximum angular velocity of the motor in radians per second.

In the below example, the property is displayed on a TextScreen. It assumes that the screen is on port 0 and the motor is assigned to port 1.

from controllables import TextScreen, MotorBase

screen = TextScreen(0)
motor = MotorBase(1)

while True:
    screen.text = f'motor.max_velocity: {motor.max_velocity: 0.1f}'

In the next example the property is set to 0 on start up. It assumes that the motor is assigned to port 0.

from controllables import MotorBase

MotorBase(0).max_velocity = 0
name() str

Returns the user editable name of the controllable as found in the properties tab of the game.

The following example assumes that any controllable or sensor is assigned to port 0 and prints out its name.

from ports import PortReference

print(PortReference(0).name())
rpm() float

Returns the angular velocity of the motor in rotations per minute.

In the below example, the rpm is displayed on a TextScreen. It assumes that the screen is on port 0 and the motor is assigned to port 1.

from controllables import TextScreen, MotorBase

screen = TextScreen(0)
motor = MotorBase(1)

while True:
    screen.text = f'motor.rpm(): {motor.rpm(): 0.1f}'
spin(power: float = 1)

Sends a signal to spin the motor. power should be set to a float from -1 to 1 representing the proportion of max velocity at which the motor will run. Negative values run the motor backwards.

from controllables import MotorBase
import time

motor = MotorBase(0)

motor.spin()
time.sleep(1)
motor.stop()
stop()

Stops spinning the motor.

Note

If a transmitter is spinning the motor, this will not stop it.

from controllables import MotorBase
import time

motor = MotorBase(0)

motor.spin()
time.sleep(1)
motor.stop()
velocity() float

Returns the angular velocity of the motor in radians per second.

In the below example, the velocity is displayed on a TextScreen. It assumes that the screen is on port 0 and the motor is assigned to port 1.

from controllables import TextScreen, MotorBase

screen = TextScreen(0)
motor = MotorBase(1)

while True:
    screen.text = f'motor.velocity(): {motor.velocity(): 0.1f}'