controllables.Piston
- class controllables.Piston(port: Optional[int] = None)
Reference to a piston part
The following code block assumes that a piston is assigned to port
0
and aTextScreen
is assigned to port1
.from controllables import Piston, TextScreen from time import sleep piston = Piston(0) screen = TextScreen(1) # the notes to mary had a little lamb notes = [5,4,3,4, 5,5,5, 4,4,4, 5,7,7, 5,4,3,4, 5,5,5,5, 4,4,5,4, 3,] # the lengths of each note lengths = [1,1,1,1, 1,1,2, 1,1,2, 1,1,2, 1,1,1,1, 1,1,1,1, 1,1,1,1, 4] # the words for each note lyrics = ["mary", "mary", "had", "a", "little", "little", "lamb", "little", "little", "lamb", "little", "little", "lamb", "mary", "mary", "had", "a", "little", "little", "lamb", "its", "fleece", "was", "white", "as", "snow"] while True: # play each note for i in range(0,len(notes)): screen.text = lyrics[i] # move the piston the the height of the note piston.move_to(notes[i] / 8.) # hold it for it's length sleep(lengths[i]) # lift the note piston.move_to(0) sleep(.05) # rest for a measure before repeating sleep(4)
- move(power: float = 1)
Sends a signal to move the piston shaft.
power
should be set to afloat
from-1
to1
representing the proportion of max velocity at which the shaft will extend. Negative values retract the shaft instead of extending it.from controllables import Piston import time piston = Piston(0) piston.move() time.sleep(1) piston.stop()
- stop()
Stops moving the piston.
Note
This doesn’t prevent transmitters or properties from moving the piston.
from controllables import Piston import time piston = Piston(0) piston.move() time.sleep(1) piston.stop()
- move_to(position: float)
Sends a signal to move the piston shaft to the position
position
between0
and1
with0
being fully retracted and1
being fully extended.from controllables import Piston import time piston = Piston(0) # set the target position to 50% piston.move_to(.5) time.sleep(2) # release the target position piston.stop()
- position() float
A
float
from0
to1
representing how far extended the shaft is with0
being fully retracted and1
being fully extended.from controllables import Piston import time piston = Piston(0) print(f'position: {piston.position() : 0.2f}') time.sleep(1) piston.move_to(.5) time.sleep(1) print(f'position: {piston.position() : 0.2f}') piston.move_to(1) time.sleep(1) print(f'position: {piston.position() : 0.2f}') piston.stop() time.sleep(1) print(f'position: {piston.position() : 0.2f}')
- property oscillate: bool
The oscillate property of the piston 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 piston is assigned to port1
.from controllables import TextScreen, Piston screen = TextScreen(0) motor = Piston(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 piston is assigned to port
0
.from controllables import Piston Piston(0).oscillate = True
- property return_to_origin: bool
The return to origin property of the piston 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 piston is assigned to port1
.from controllables import TextScreen, Piston screen = TextScreen(0) motor = Piston(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 piston is assigned to port
0
.from controllables import Piston Piston(0).return_to_origin = True
- property max_velocity: float
The maximum velocity (in meters per second) property of the piston 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 piston is assigned to port1
.from controllables import TextScreen, Piston screen = TextScreen(0) motor = Piston(1) while True: screen.text = f'motor.max_velocity: {motor.max_velocity : 0.0f}'
In the next example the property is set to 0 on start up. It assumes that the piston is assigned to port
0
.from controllables import Piston Piston(0).max_velocity = 0
- property acceleration_time: float
The acceleration time (in seconds) property of the piston 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 piston is assigned to port1
.from controllables import TextScreen, Piston screen = TextScreen(0) motor = Piston(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 piston is assigned to port
0
.from controllables import Piston Piston(0).acceleration_time = 0
- property max_force: float
The maximum force (in newtons) property of the piston 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 piston is assigned to port1
.from controllables import TextScreen, Piston screen = TextScreen(0) motor = Piston(1) while True: screen.text = f'motor.max_force: {motor.max_force : 0.0f}'
In the next example the property is set to 0 on start up. It assumes that the piston is assigned to port
0
.from controllables import Piston Piston(0).max_force = 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())