Skip to content

Commit ba8cf73

Browse files
author
Dario Cammi
committed
Added examples
1 parent 18d8a6a commit ba8cf73

File tree

5 files changed

+330
-0
lines changed

5 files changed

+330
-0
lines changed

examples/active_motor.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import board
2+
import asyncio
3+
from buildhat.hat import Hat
4+
from buildhat.motors.speedunit import SpeedUnit
5+
from buildhat.motors.direction import Direction
6+
7+
motor_port = 3
8+
9+
# Pins for Waveshare RP2040-Zero.
10+
# Change the pins if you are using a different board
11+
tx_pin = board.TX
12+
rx_pin = board.RX
13+
reset_pin = board.GP23
14+
15+
buildhat = Hat(tx=tx_pin, rx=rx_pin, reset=reset_pin, debug=True)
16+
17+
stop = False
18+
async def buildhat_loop(hat):
19+
while not stop:
20+
hat.update()
21+
await asyncio.sleep(0)
22+
23+
async def data_loop(hat):
24+
motor = buildhat.get_device(motor_port)
25+
motor.data_update_interval = 10
26+
27+
while not stop:
28+
if motor.has_absolute_position:
29+
print(f"Speed: {motor.actual_speed} °/s, Pos: {motor.actual_position}°, APos: {motor.actual_absolute_position}°")
30+
else:
31+
print(f"Speed: {motor.actual_speed} °/s, Pos: {motor.actual_position}°")
32+
await asyncio.sleep(0.1)
33+
34+
async def motor_loop(hat):
35+
motor = buildhat.get_device(motor_port)
36+
37+
motor.power_limit = 1
38+
motor.release_after_run = True # The motor is free to spin after the movement is done
39+
40+
while not stop:
41+
motor.run_command_speed_unit = SpeedUnit.DGS # degrees per second
42+
motor.run_command_default_speed = 120 # Default speed when not specified in the run command
43+
await motor.run_for_rotations(2) # Use default speed (90 DGS)
44+
await motor.run_for_rotations(2, speed = 180) # Run at speed 4180 DGS
45+
await motor.run_for_degrees(90) # Turn 90 degree from current position
46+
await motor.run_for_degrees(-90, speed = 180) # Turn back 90 degree from current postion
47+
48+
motor.actual_position = 0 # Preset actual_position and change it to 0
49+
await motor.run_to_position(720, speed=360) # Run the motor at the position 720 degree
50+
51+
motor.run_command_speed_unit = SpeedUnit.RPM # revolutions per second
52+
if motor.has_absolute_position:
53+
# Not all motors have the absolute position
54+
# Absolute position cover one turn from -180 to 180 degree
55+
await motor.run_to_absolute_angle(0, speed = 60)
56+
await motor.run_to_absolute_angle(-30, speed = 120, direction=Direction.CW)
57+
await motor.run_to_absolute_angle(-170, speed = 90, direction=Direction.CCW)
58+
await motor.run_to_absolute_angle(170, speed = 120, direction=Direction.SHORTEST)
59+
60+
await motor.run_for_seconds(5) # Spin the motor for 5 seconds
61+
motor.start(speed = 120) # Start the motor to run indefinitely
62+
await asyncio.sleep(2)
63+
motor.stop() # Stop and leaving floating
64+
await asyncio.sleep(1)
65+
66+
motor.pwm(1) # Spin the motor open loop at max speed. All previous mode were close loop
67+
await asyncio.sleep(2)
68+
motor.coast() # Let the motor coast to a stop
69+
await asyncio.sleep(2)
70+
71+
motor.reverse_direction = not motor.reverse_direction
72+
73+
async def main():
74+
buildhat_loop_task = asyncio.create_task(buildhat_loop(buildhat))
75+
data_loop_task = asyncio.create_task(data_loop(buildhat))
76+
motor_loop_task = asyncio.create_task(motor_loop(buildhat))
77+
78+
await asyncio.gather(buildhat_loop_task, data_loop_task, motor_loop_task)
79+
80+
asyncio.run(main())

examples/color_distance_sensor.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'''
2+
Test code for Boost color & distance sensor
3+
Part number: 88007
4+
'''
5+
6+
import board
7+
import asyncio
8+
from buildhat.hat import Hat
9+
from buildhat.devices.color import Color
10+
11+
sensor_port = 1
12+
13+
# Pins for Waveshare RP2040-Zero.
14+
# Change the pins if you are using a different board
15+
tx_pin = board.TX
16+
rx_pin = board.RX
17+
reset_pin = board.GP23
18+
19+
buildhat = Hat(tx=tx_pin, rx=rx_pin, reset=reset_pin, debug=True)
20+
21+
stop = False
22+
async def buildhat_loop(hat):
23+
while not stop:
24+
hat.update()
25+
await asyncio.sleep(0)
26+
27+
async def read_loop(hat):
28+
sensor = buildhat.get_device(sensor_port)
29+
30+
while not stop:
31+
color = await sensor.get_color()
32+
print(f"Color: {color}")
33+
ambient_light = await sensor.get_ambient_light()
34+
print(f"Ambient light: {ambient_light}")
35+
reflected = await sensor.get_reflected_light()
36+
print(f"Reflected light: {reflected}")
37+
distance = await sensor.get_distance()
38+
print(f"Distance: {distance}")
39+
counter = await sensor.get_counter()
40+
print(f"Counter: {counter}")
41+
42+
sensor.off()
43+
print(f"Sensor off")
44+
await asyncio.sleep(1)
45+
sensor.on()
46+
print(f"Sensor on")
47+
await asyncio.sleep(1)
48+
49+
async def main():
50+
buildhat_loop_task = asyncio.create_task(buildhat_loop(buildhat))
51+
read_loop_task = asyncio.create_task(read_loop(buildhat))
52+
53+
await asyncio.gather(buildhat_loop_task, read_loop_task)
54+
55+
asyncio.run(main())

examples/distance_sensor.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'''
2+
Test code for Lego Technic Distance Sensor
3+
Part number: 6302968
4+
'''
5+
6+
import board
7+
import asyncio
8+
from buildhat.hat import Hat
9+
10+
sensor_port = 0
11+
12+
# Pins for Waveshare RP2040-Zero.
13+
# Change the pins if you are using a different board
14+
tx_pin = board.TX
15+
rx_pin = board.RX
16+
reset_pin = board.GP23
17+
18+
buildhat = Hat(tx=tx_pin, rx=rx_pin, reset=reset_pin, debug=True)
19+
20+
stop = False
21+
async def buildhat_loop(hat):
22+
while not stop:
23+
hat.update()
24+
await asyncio.sleep(0)
25+
26+
async def read_loop(hat):
27+
sensor = buildhat.get_device(sensor_port)
28+
29+
while not stop:
30+
d = sensor.distance
31+
if d > 0:
32+
print(f"Distance {d} mm")
33+
else:
34+
print("Please put an obstacle in front of the ultrasonic sensor")
35+
await asyncio.sleep(0.2)
36+
37+
async def eyes_loop(hat):
38+
sensor = buildhat.get_device(sensor_port)
39+
pause = 0.02
40+
41+
while not stop:
42+
# Drive all four eyes together
43+
for i in range(100):
44+
sensor.eyes(i)
45+
await asyncio.sleep(pause)
46+
47+
# Drive all four eyes one a the time
48+
for i in range(100):
49+
sensor.eyes(i, 0, 0, 0)
50+
await asyncio.sleep(pause)
51+
for i in range(100):
52+
sensor.eyes(0, i, 0, 0)
53+
await asyncio.sleep(pause)
54+
for i in range(100):
55+
sensor.eyes(0, 0, i, 0)
56+
await asyncio.sleep(pause)
57+
for i in range(100):
58+
sensor.eyes(0, 0, 0, i)
59+
await asyncio.sleep(pause)
60+
61+
async def main():
62+
buildhat_loop_task = asyncio.create_task(buildhat_loop(buildhat))
63+
read_loop_task = asyncio.create_task(read_loop(buildhat))
64+
eyes_loop_task = asyncio.create_task(eyes_loop(buildhat))
65+
66+
await asyncio.gather(buildhat_loop_task, read_loop_task, eyes_loop_task)
67+
68+
asyncio.run(main())

examples/light_matrix.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'''
2+
Test code for Lego Technic 3x3 Color Light Matrix
3+
Part number: 45608
4+
'''
5+
6+
import board
7+
import asyncio
8+
from buildhat.hat import Hat
9+
from buildhat.devices.matrixcolor import MatrixColor
10+
from buildhat.devices.matrixtransition import MatrixTransition
11+
12+
matrix_port = 2
13+
14+
# Pins for Waveshare RP2040-Zero.
15+
# Change the pins if you are using a different board
16+
tx_pin = board.TX
17+
rx_pin = board.RX
18+
reset_pin = board.GP23
19+
20+
buildhat = Hat(tx=tx_pin, rx=rx_pin, reset=reset_pin, debug=True)
21+
22+
stop = False
23+
async def buildhat_loop(hat):
24+
while not stop:
25+
hat.update()
26+
await asyncio.sleep(0)
27+
28+
async def matrix_loop(hat):
29+
while not stop:
30+
matrix = buildhat.get_device(matrix_port)
31+
32+
matrix.display_single_color(MatrixColor.YELLOW)
33+
await asyncio.sleep(1)
34+
35+
for i in range(10):
36+
matrix.display_level_bar(i)
37+
await asyncio.sleep(0.3)
38+
39+
matrix.set_display_image_transition(MatrixTransition.SWIPE_RTL)
40+
matrix.display_single_color(MatrixColor.YELLOW)
41+
await asyncio.sleep(2)
42+
matrix.display_single_color(MatrixColor.BLUE)
43+
await asyncio.sleep(2)
44+
matrix.display_single_color(MatrixColor.RED)
45+
await asyncio.sleep(2)
46+
47+
matrix.set_display_image_transition(MatrixTransition.FADE_IN_OUT)
48+
matrix.fill_pixels(MatrixColor.YELLOW, 5)
49+
matrix.set_pixel(1, 1, MatrixColor.BLUE, 10)
50+
matrix.display_pixels()
51+
await asyncio.sleep(2)
52+
matrix.fill_pixels(MatrixColor.YELLOW, 5)
53+
matrix.set_pixel(0, 0, MatrixColor.BLUE, 10)
54+
matrix.set_pixel(2, 2, MatrixColor.BLUE, 10)
55+
matrix.display_pixels()
56+
await asyncio.sleep(2)
57+
matrix.fill_pixels(MatrixColor.YELLOW, 5)
58+
matrix.set_pixel(0, 0, MatrixColor.BLUE, 10)
59+
matrix.set_pixel(1, 1, MatrixColor.BLUE, 10)
60+
matrix.set_pixel(2, 2, MatrixColor.BLUE, 10)
61+
matrix.display_pixels()
62+
await asyncio.sleep(2)
63+
matrix.set_display_image_transition(MatrixTransition.NONE)
64+
matrix.fill_pixels(MatrixColor.YELLOW, 5)
65+
matrix.set_pixel(0, 0, MatrixColor.BLUE, 10)
66+
matrix.set_pixel(0, 2, MatrixColor.BLUE, 10)
67+
matrix.set_pixel(2, 2, MatrixColor.BLUE, 10)
68+
matrix.set_pixel(2, 0, MatrixColor.BLUE, 10)
69+
matrix.display_pixels()
70+
await asyncio.sleep(2)
71+
72+
async def main():
73+
buildhat_loop_task = asyncio.create_task(buildhat_loop(buildhat))
74+
matrix_loop_task = asyncio.create_task(matrix_loop(buildhat))
75+
76+
await asyncio.gather(buildhat_loop_task, matrix_loop_task)
77+
78+
asyncio.run(main())

examples/test_color_sensor.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'''
2+
Test code for color sensor
3+
Part number: 6217705
4+
'''
5+
6+
import board
7+
import asyncio
8+
from buildhat.hat import Hat
9+
10+
sensor_port = 0
11+
12+
# Pins for Waveshare RP2040-Zero.
13+
# Change the pins if you are using a different board
14+
tx_pin = board.TX
15+
rx_pin = board.RX
16+
reset_pin = board.GP23
17+
18+
buildhat = Hat(tx=tx_pin, rx=rx_pin, reset=reset_pin, debug=True)
19+
20+
stop = False
21+
async def buildhat_loop(hat):
22+
while not stop:
23+
hat.update()
24+
await asyncio.sleep(0)
25+
26+
async def read_loop(hat):
27+
sensor = buildhat.get_device(sensor_port)
28+
29+
while not stop:
30+
color = await sensor.get_color()
31+
print(f"Color: {color}")
32+
ambient_light = await sensor.get_ambient_light()
33+
print(f"Ambient light: {ambient_light}")
34+
reflected = await sensor.get_reflected_light()
35+
print(f"Reflected light: {reflected}")
36+
sensor.off()
37+
print(f"Sensor off")
38+
await asyncio.sleep(1)
39+
sensor.on()
40+
print(f"Sensor on")
41+
await asyncio.sleep(1)
42+
43+
async def main():
44+
buildhat_loop_task = asyncio.create_task(buildhat_loop(buildhat))
45+
read_loop_task = asyncio.create_task(read_loop(buildhat))
46+
47+
await asyncio.gather(buildhat_loop_task, read_loop_task)
48+
49+
asyncio.run(main())

0 commit comments

Comments
 (0)