sensors.ForceSensor
- class sensors.ForceSensor(port: Optional[int] = None)
Sensor for measuring linear forces and rotational torques.
from controllables import TextScreen from sensors import ForceSensor sensor = ForceSensor() screen = TextScreen() screen.h_align = "L" screen.size = 90 while True: screen.text = f'force: {sensor.force() : 8.2f}' + "\n" + \ f'torque: {sensor.torque(): 8.2f}'
- force() Vector3
The force in newtons that the right (further x) surface is applying to the left surface from the left surface’s perspective. As a result of this, negative forces indicate compressive forces whereas positive forces indicate stretching forces.
The force is in the left (negative in the x axis) surface’s space meaning a value in the x axis is a force directly in the left surface’s x axis regardless of where the right surface’s x axis is pointed.
A slight filter is applied to smooth out values. For an unfiltered version see
force_stream()
For best results, avoid “chaining” joints together. The sensor will give more accurate results measuring collisions with objects that it is not connected to with a joint.
from controllables import TextScreen from sensors import ForceSensor from inputs import Input sensor = ForceSensor() screen = TextScreen() input_stream = Input.stream('t') weight_offset = 0 while True: weight = sensor.force().y screen.text = f'weight: {weight - weight_offset: 6.2f}' + '\n' + \ f'press T to recalibrate' for key in input_stream: if not key: weight_offset = sensor.force().y
- force_stream() Stream[Vector3]
Creates a stream of unfiltered
force()
values in newtons.from sensors import ForceSensor from controllables import TextScreen from time import time stream = ForceSensor().force_stream() force_threshold = 1500 screen = TextScreen() screen.text = '' time_of_last_hit = time() while True: for force in stream: if force.magnitude() > force_threshold: time_of_last_hit = time() screen.text = "We've been hit!" if time() - time_of_last_hit > 1: screen.text = ''
- torque() Vector3
The torque in newton meters that the right (further x) surface is applying to the left surface from the left surface’s perspective. As a result of this, negative values indicate the right surface rotating counter clockwise from the left surface’s perspective whereas positive values indicate a clockwise force.
The torque is in the left surface’s space meaning a value in the x axis is a torque directly around the left surface’s x axis regardless of where the right surface’s x axis is pointed.
A slight filter is applied to smooth out values. For an unfiltered version see
torque_stream()
If you are getting inconsistent results avoid “chaining” joints together with this part. The sensor will give more accurate results measuring collisions with objects that it is not connected to with a mechanical or rotatary joint.
from controllables import TextScreen from sensors import ForceSensor sensor = ForceSensor() screen = TextScreen() while True: screen.text = f'torque: {sensor.torque(): 4.0f}'
- torque_stream() Stream[Vector3]
Creates a stream of unfiltered
torque()
values in newton meters.from sensors import ForceSensor from controllables import TextScreen from time import time stream = ForceSensor().torque_stream() torque_limit = 300 screen = TextScreen() screen.text = '' time_of_last_hit = time() while True: for torque in stream: if torque.x > torque_limit: time_of_last_hit = time() screen.text = "We've been twisted!" 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())