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 thebrake_force()
andbrake_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.
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.
- property brake_time: float
The brake time (in seconds) property of the motor avaliable in the properties tab.
- property acceleration_time: float
The acceleration time (in seconds) property of the motor avaliable in the properties tab.
- angle() float
Returns the angle of the motor in radians.
- degrees() float
Returns the angle of the motor in degrees.
- property flipped: bool
The flip property of the motor avaliable in the properties tab.
- property max_rpm: float
The max rpm property of the motor avaliable in the properties tab.
- property max_torque: float
The max torque (in newton meters) property of the motor avaliable in the properties tab.
- property max_velocity: float
The maximum angular velocity of the motor in radians per second.
- name() str
Returns the user editable name of the controllable as found in the properties tab of the game.
from ports import PortReference print(PortReference(0).name())
- rpm() float
Returns the angular velocity of the motor in rotations per minute.
- spin(power: float = 1)
Sends a signal to spin the motor.
power
should be set to afloat
from-1
to1
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 running the motor.
Note
If a transmitter is running 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.