Skip to content

Latest commit

 

History

History
420 lines (274 loc) · 7.58 KB

07_SENSORS.md

File metadata and controls

420 lines (274 loc) · 7.58 KB

Sensors Blocks

In word blocks, there is a quite big difference of how sensors behave as those azure sensors blocks, compared to yellow events blocks. When you are using them as events trigger, sensor will not retriger the event, until the sensor values changes to falsy and then back to truthy. In sensor block variant, if sensor stays true in for example while loop, it will keep triggering the event indefinitely.

In pythonBlocks, you can achieve both behaviors, but here we are going to cover the 'sensor block' variants.

Table of contents

  1. color sensor is
  2. color sensor (value)
  3. color sensor reflection is
  4. color sensor reflection (value)
  5. color sensor RGBI
  6. force sensor is
  7. prassure in
  8. distance sensor is
  9. distance sensor (value)
  10. is tilted
  11. when is up
  12. when gesture is
  13. gesture (value)
  14. angle (value)
  15. set yaw angle to 0
  16. acceleration (value)
  17. angular velocity (value)
  18. set sensor orientation to
  19. orientation (value)
  20. is button pressed
  21. timer (value)
  22. reset timer

color sensor is

alt text

color_sensor.color(port.A) == color.RED

usage:

if color_sensor.color(port.A) == color.RED:
    pass #your code here

color sensor (value)

alt text

color_sensor.color(port.A)

usage:

color_variable = color_sensor.color(port.A)

color sensor reflection is

alt text

color_sensor.reflection(port.A) < 50
  • You can use any comparison operator comparison operator here.
  • Value is in range 0 (black, no light reflected) to 100 (white, all light reflected).

color sensor reflection (value)

alt text

color_sensor.reflection(port.A)

color sensor RGBI

alt text

There is one more mode, that is not present in word blocks, and that is RGBI mode:

color_sensor.rgbi(port.A)

which returns tuple [red: int, green: int, blue: int, intensity: int]. So, if you want just red value, you can do:

color_sensor.rgbi(port.A)[0]

force sensor is

alt text

pressed:

force_sensor.pressed(port.A) == 1

released:

force_sensor.pressed(port.A) == 0

hard pressed:

force_sensor.force(port.A) > 50

prassure in

alt text

percentage:

force_sensor.force(port.A)

newton:

round(force_sensor.force(port.A) / 10)

distance sensor is

alt text

closer than:

percentage:

_distanceSensor.distance_percentage(port.A) < 15

cm:

_distanceSensor.distance_cm(port.A) < 15

inch:

_distanceSensor.distance_inch(port.A) < 15

farther than:

percentage:

_distanceSensor.distance_percentage(port.A) > 15

cm:

_distanceSensor.distance_cm(port.A) > 15

inch:

_distanceSensor.distance_inch(port.A) > 15

exactly at:

percentage:

_distanceSensor.distance_percentage(port.A) == 15

cm:

_distanceSensor.distance_cm(port.A) == 15

inch:

_distanceSensor.distance_inch(port.A) == 15

distance sensor (value)

alt text

percentage:

_distanceSensor.distance_percentage(port.A)

cm:

_distanceSensor.distance_cm(port.A)

inch:

_distanceSensor.distance_inch(port.A)

mm:

distanceSensor.distance(port.A)

is tilted

alt text

forward:

_motion_sensor.tilt_angles(1) > 130

backward:

_motion_sensor.tilt_angles(1) < -130

right:

_motion_sensor.tilt_angles(2) > 130

left:

_motion_sensor.tilt_angles(2) < -130

tilted (any direction):

_motion_sensor.tilted(0) == True

not tilted (any direction):

_motion_sensor.tilted(0) == False

when is up

note: basically, this is the same as when tilted *somewhere*, but breakpoint value is in the middle, which is 450 out of 900 instead of 130. There is one more option, not covered by when tilted which is when bottom is up, which you can implement like this:

alt text

_motion_sensor.upside_down(0) == True

when gesture is

alt text

shaken:

motion_sensor.gesture() = motion_sensor.SHAKEN

other gestures:

other possible values are: motion_sensor.TAPPED, motion_sensor.DOUBLE_TAPPED, motion_sensor.FALLING and motion_sensor.UNKNOWN which corresponds to 'no gesture'

gesture (value)

alt text

motion_sensor.gesture()

angle (value)

alt text

pitch:

_motion_sensor.tilt_angles(1)

roll:

_motion_sensor.tilt_angles(2)

yaw:

_motion_sensor.tilt_angles(0)

set yaw angle to 0

alt text

motion_sensor.reset_yaw(0)

acceleration (value)

alt text

motion_sensor.acceleration(False) # tuple of 3 values
motion_sensor.acceleration(False)[0] # x value
motion_sensor.acceleration(False)[1] # y value
motion_sensor.acceleration(False)[2] # z value

pass True as argument to get acceleration as raw, unfiltred values

angular velocity (value)

alt text

motion_sensor.angular_velocity(False) # tuple of 3 values
motion_sensor.angular_velocity(False)[0] # x value
motion_sensor.angular_velocity(False)[1] # y value
motion_sensor.angular_velocity(False)[2] # z value

pass True as argument to get angular velocity as raw, unfiltred values

set sensor orientation to

alt text

motion_sensor.set_yaw_face(motion_sensor.FRONT)

possible values are: motion_sensor.FRONT, motion_sensor.BACK, motion_sensor.LEFT, motion_sensor.RIGHT, motion_sensor.TOP and motion_sensor.BOTTOM

orientation (value)

alt text

motion_sensor.get_yaw_face()

is button pressed

alt text

left:

pressed:

button.pressed(button.LEFT) > 0

released:

button.pressed(button.LEFT) == 0

right:

pressed:

button.pressed(button.RIGHT) > 0

released:

button.pressed(button.RIGHT) == 0

timer (value)

alt text

returns time in milliseconds (in word blocks it is in seconds)

timer.get_time_ms()

reset timer

alt text

timer.reset_timer()