controllables.ServoMotor
- class controllables.ServoMotor(port: Optional[int] = None)
Reference to a servo motor part. Base class:
MotorBase
.The follow code block assumes that a servo is assigned to port
0
.from controllables import ServoMotor from inputs import Input from math import tau motor = ServoMotor(0) left = Input.stream("a") right = Input.stream("d") rotation = 0 stepsize = tau / 12. while True: for event in left: if event: rotation -= stepsize for event in right: if event: rotation += stepsize motor.spin_to(rotation)
- 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 ServoMotor from time import sleep motor = ServoMotor(0) motor.spin() sleep(1) motor.spin(-1) sleep(1) motor.stop()
- spin_to(angle: float)
Sends a signal to spin the motor towards
angle
in the clockwise direction.angle
should be provided in radians. A negative value forangle
will spin the motor counter clockwise.from controllables import ServoMotor from math import tau # get the motor reference motor = ServoMotor(0) # spin the motor a quater turn to the right. motor.spin_to(tau / 4.)
- spin_to_degrees(angle: float)
Sends a signal to spin the motor towards
angle
in the clockwise direction.angle
should be provided in degrees. A negative value forangle
will spin the motor counter clockwise.from controllables import ServoMotor # get the motor reference motor = ServoMotor(0) # spin the motor a quater turn to the right. motor.spin_to_degrees(90)
- stop()
Stops running the motor.
Note
If a transmitter is spinning the motor, this will not stop it.
from controllables import ServoMotor from time import sleep from math import tau motor = ServoMotor(0) # set the motor to move to a quarter turn motor.spin_to(tau / 4.) sleep(2) # release control of the motor so it can be moved by transmitters # or the "return to origin" property motor.stop()
- property limits: Tuple[float, float]
A tuple with two positive radian components representing the counterclockwise and clockwise rotational limits of the servo motor respectively.
To set the limits to their max do the following:
from controllables import ServoMotor from math import tau motor = ServoMotor(0) # set the limits in each direction to half # the way around the circle motor.limits = (tau / 2.,tau / 2.)
- property limits_degrees: Tuple[float, float]
The limits (in degrees) property of the servo avaliable in the properties tab.
To set the limits to their max do the following:
from controllables import ServoMotor from math import tau motor = ServoMotor(0) # set the limits in each direction to half # the way around the circle motor.limits = (180,180)
- property oscillate: bool
The oscillate property of the servo avaliable in the properties tab.
In the below example, the property is displayed on a
TextScreen
. It assumes that the screen is on port0
and the servo is assigned to port1
.from controllables import TextScreen, ServoMotor screen = TextScreen(0) motor = ServoMotor(1) while True: screen.text = f'motor.oscillate: {motor.oscillate}'
In the next example the property is set to True on start up. It assumes that the servo is assigned to port
0
.from controllables import ServoMotor ServoMotor(0).oscillate = True
- property return_to_origin: bool
The return to origin property of the servo in the properties tab.
In the below example, the property is displayed on a
TextScreen
. It assumes that the screen is on port0
and the servo is assigned to port1
.from controllables import TextScreen, ServoMotor screen = TextScreen(0) motor = ServoMotor(1) while True: screen.text = f'motor.return_to_origin: {motor.return_to_origin}'
In the next example the property is set to True on start up. It assumes that the servo is assigned to port
0
.from controllables import ServoMotor ServoMotor(0).return_to_origin = True
- property map_to_input: bool
The map to input property of the servo in the properties tab.
In the below example, the property is displayed on a
TextScreen
. It assumes that the screen is on port0
and the servo is assigned to port1
.from controllables import TextScreen, ServoMotor screen = TextScreen(0) motor = ServoMotor(1) while True: screen.text = f'motor.map_to_input: {motor.map_to_input}'
In the next example the property is set to True on start up. It assumes that the servo is assigned to port
0
.from controllables import ServoMotor ServoMotor(0).map_to_input = True
- 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 port0
and the motor is assigned to port1
.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 port0
and the motor is assigned to port1
.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 port0
and the motor is assigned to port1
.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 port0
and the motor is assigned to port1
.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 port0
and the motor is assigned to port1
.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 port0
and the motor is assigned to port1
.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 port0
and the motor is assigned to port1
.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 port0
and the motor is assigned to port1
.from controllables import TextScreen, MotorBase screen = TextScreen(0) motor = MotorBase(1) while True: screen.text = f'motor.rpm(): {motor.rpm(): 0.1f}'
- 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 port0
and the motor is assigned to port1
.from controllables import TextScreen, MotorBase screen = TextScreen(0) motor = MotorBase(1) while True: screen.text = f'motor.velocity(): {motor.velocity(): 0.1f}'