sensors.TouchSensor

class sensors.TouchSensor(port: Optional[int] = None)

Sensor for detecting small linear contact forces.

from sensors import TouchSensor
from controllables import Piston
from time import sleep

# requires exactly 1 touch sensor on any port
sensor = TouchSensor()
# requires exactly 1 piston on any port
piston = Piston()

# The idea is that the sensor is on the tip
# of the piston, so that the piston reacts
# to being touched by pushing whatever touched
# it away. If you want multiple of these mechanisms
# consider using multiple microcontrollers

# runs continously
while True:
        # if the sensor senses a touch
        if sensor.pressed():
                # it pushes the piston out
                piston.move_to(1)
                # waits for half a second
                sleep(.5)
                # pushes it out again
                piston.move_to(0)
                # and then waits before starting over again
                sleep(.5)
pressed() bool

Whether the button is more than half way depressed.

from sensors import TouchSensor
from controllables import TextScreen

# requires exactly 1 touch sensor on any port
sensor = TouchSensor()
# requires exactly 1 text screen on any port
screen = TextScreen()

# runs continuously
while True:
        # writing the pressed status to the text screen
        screen.text = f'pressed: {sensor.pressed()}'
pressed_stream() Stream[bool]

A stream of pressed() values.

from sensors import TouchSensor
from controllables import TextScreen
from time import time

stream = TouchSensor().pressed_stream()

screen = TextScreen()

screen.text = ''
time_of_last_press = time()

while True:
        for press in stream:
                if press:
                        time_of_last_press = time()
                        screen.text = "We've been hit!"
        if time() - time_of_last_press > 1:
                screen.text = ''
force()

The force in newtons that the tip of the button is applying to the base.

A slight filter is applied. For an unfiltered version see force_stream().

from sensors import TouchSensor
from controllables import TextScreen

# requires exactly 1 touch sensor on any port
sensor = TouchSensor()
# requires exactly 1 text screen on any port
screen = TextScreen()

# runs continuously
while True:
        # writing the pressed status to the text screen
        screen.text = f'force: {sensor.force(): 5.0f}'
        # ': 5.0f' means
        #       show a space instead of a + sign
        #       show atleast five symbols
        #       don't show any decimal places
        #       display as a float number
force_stream()

A stream of unfiltered force() values.

from sensors import TouchSensor
from controllables import TextScreen
from time import time

stream = TouchSensor().force_stream()

force_threshold = 25

screen = TextScreen()

screen.text = ''
time_of_last_hit = time()

while True:
        for force in stream:
                if force > force_threshold:
                        time_of_last_hit = time()
                        screen.text = "We've been hit!"
        if time() - time_of_last_hit > 1:
                screen.text = ''
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())