Skip to main content

Examples

Each page of the API section has examples of its feature. The examples below combine several features.

Hand detection​

The color of the LEDs depends on the side where a hand is detected, white for the top sensor.

import olem

print("Starting ir hand detection test")

start_timestamp = olem.get_time_ms()

while (olem.get_time_ms() - start_timestamp) < 20000:

olem.led_off(olem.LEDALL)

if (olem.ir_side_is_hand_present(olem.FRONT)):
olem.led_on(olem.LEDALL, "#FF0000")

if (olem.ir_side_is_hand_present(olem.RIGHT)):
olem.led_on(olem.LEDALL, "#00FF00")

if (olem.ir_side_is_hand_present(olem.BACK)):
olem.led_on(olem.LEDALL, "#0000FF")

if (olem.ir_side_is_hand_present(olem.LEFT)):
olem.led_on(olem.LEDALL, "#00FFFF")

if (olem.ir_top_is_hand_present()):
olem.led_on(olem.LEDALL, "#FFFFFF")

olem.delay_ms(100)

print("End ir hand detection test")

Extension module - GPIO & IR​

Pin P1 is HIGH while a hand is in front of OLEM, pin P2 while a hand is behind it.

import olem

print("Starting extension module gpio ir test")

olem.extmod_mode_set(olem.EXTMOD_GPIO)

timestamp_start = olem.get_time_ms()

while ((olem.get_time_ms() - timestamp_start) < 20000):

if (olem.ir_side_is_hand_present(olem.FRONT)):
olem.extmod_gpio_set(olem.EXTMOD_GPIO1, olem.HIGH)
else:
olem.extmod_gpio_set(olem.EXTMOD_GPIO1, olem.LOW)

if (olem.ir_side_is_hand_present(olem.BACK)):
olem.extmod_gpio_set(olem.EXTMOD_GPIO2, olem.HIGH)
else:
olem.extmod_gpio_set(olem.EXTMOD_GPIO2, olem.LOW)

olem.extmod_gpio_set(olem.EXTMOD_GPIO1, olem.LOW)
olem.extmod_gpio_set(olem.EXTMOD_GPIO2, olem.LOW)

print("End of extension module gpio ir test")

Use the force (To be fixed)​

Untitled

import olem


THRESHOLD = 500
DEADZONE = 100
MIN_VALUE = 150
STEP = 20
MAX_SPEED = 10 / 10

def get_translation(value):
translation = (value - THRESHOLD) / THRESHOLD
if abs(value - THRESHOLD) < DEADZONE:
return 0
else:
return translation

def set_speed(x, y):
if abs(x) > abs(y):
speed = abs(x)
else:
speed = abs(y)
speed = speed + 1 / 2

if (speed > MAX_SPEED):
olem.set_move_speed(MAX_SPEED)
else:
olem.set_move_speed(speed)

def force_loop():
x = 0
y = 0
front = olem.ir_side_get_sensor_value(olem.FRONT)
right = olem.ir_side_get_sensor_value(olem.RIGHT)
back = olem.ir_side_get_sensor_value(olem.BACK)
left = olem.ir_side_get_sensor_value(olem.LEFT)

# Get translations
if front > MIN_VALUE:
x -= get_translation(front)

if back > MIN_VALUE:
x += get_translation(back)

if right > MIN_VALUE:
y -= get_translation(right)

if left > MIN_VALUE:
y += get_translation(left)

# Update speed based on translations
set_speed(x, y)

if x != 0 or y != 0:
if (x != 0):
x = x / abs(x) * STEP
if (y != 0):
y = y / abs(y) * STEP
olem.move(x, y, 0)

#########################################################
#########################################################
#########################################################

olem.led_off(olem.LEDALL)

# Wait for button press
while olem.get_power_button_state() == 1:
olem.delay_ms(20)

# Display logo and turn on led
img = olem.display_img("/common/images/olem_logo.bin")
olem.led_on(olem.LEDALL, "#FFFFFF")

# Change filter param to allow for faster detection
olem.ir_side_set_filter(2)

while not olem.ir_top_is_hand_present():
force_loop()