Skip to content

Commit 04f498a

Browse files
authored
Merge pull request #9 from arduino/dev
0.2.0
2 parents a8ca070 + 8036b51 commit 04f498a

18 files changed

+579
-109
lines changed

arduino_alvik.py

Lines changed: 326 additions & 70 deletions
Large diffs are not rendered by default.

constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11

22
# COLOR SENSOR
33
COLOR_FULL_SCALE = 4097
4+
WHITE_CAL = [444, 342, 345]
5+
BLACK_CAL = [153, 135, 123]

conversions.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# MEASUREMENT UNITS CONVERSION #
2+
3+
from math import pi
4+
5+
6+
def conversion_method(func):
7+
def wrapper(*args, **kwargs):
8+
try:
9+
return func(*args, **kwargs)
10+
except KeyError:
11+
raise ConversionError(f'Cannot {func.__name__} from {args[1]} to {args[2]}')
12+
return wrapper
13+
14+
15+
@conversion_method
16+
def convert_rotational_speed(value: float, from_unit: str, to_unit: str) -> float:
17+
"""
18+
Converts a rotational speed value from one unit to another
19+
:param value:
20+
:param from_unit: unit of input value
21+
:param to_unit: unit of output value
22+
:return:
23+
"""
24+
speeds = {'rpm': 1.0, 'deg/s': 1/6, 'rad/s': 60/(2*pi), 'rev/s': 60}
25+
return value * speeds[from_unit.lower()] / speeds[to_unit.lower()]
26+
27+
28+
@conversion_method
29+
def convert_angle(value: float, from_unit: str, to_unit: str) -> float:
30+
"""
31+
Converts an angle value from one unit to another
32+
:param value:
33+
:param from_unit: unit of input value
34+
:param to_unit: unit of output value
35+
:return:
36+
"""
37+
angles = {'deg': 1.0, 'rad': 180/pi, 'rev': 360, 'revolution': 360, '%': 3.6, 'perc': 3.6}
38+
return value * angles[from_unit.lower()] / angles[to_unit.lower()]
39+
40+
41+
@conversion_method
42+
def convert_distance(value: float, from_unit: str, to_unit: str) -> float:
43+
"""
44+
Converts a distance value from one unit to another
45+
:param value:
46+
:param from_unit: unit of input value
47+
:param to_unit: unit of output value
48+
:return:
49+
"""
50+
distances = {'cm': 1.0, 'mm': 0.1, 'm': 100, 'inch': 2.54, 'in': 2.54}
51+
return value * distances[from_unit.lower()] / distances[to_unit.lower()]
52+
53+
54+
@conversion_method
55+
def convert_speed(value: float, from_unit: str, to_unit: str) -> float:
56+
"""
57+
Converts a distance value from one unit to another
58+
:param value:
59+
:param from_unit: unit of input value
60+
:param to_unit: unit of output value
61+
:return:
62+
"""
63+
distances = {'cm/s': 1.0, 'mm/s': 0.1, 'm/s': 100, 'inch/s': 2.54, 'in/s': 2.54}
64+
return value * distances[from_unit.lower()] / distances[to_unit.lower()]
65+
66+
67+
class ConversionError(Exception):
68+
pass

examples/set_pid.py renamed to examples/hand_follower.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44

55
alvik = ArduinoAlvik()
66
alvik.begin()
7-
speed = 0
7+
8+
reference = 10.0
89

910
while True:
1011
try:
11-
alvik.left_wheel.set_pid_gains(10.0, 1.3, 4.2)
12-
sleep_ms(100)
13-
alvik.right_wheel.set_pid_gains(4.0, 13, 1.9)
12+
L, CL, C, CR, R = alvik.get_distance()
13+
print(f'C: {C}')
14+
error = C - reference
15+
alvik.set_wheels_speed(error*10, error*10)
1416
sleep_ms(100)
1517
except KeyboardInterrupt as e:
1618
print('over')
1719
alvik.stop()
18-
sys.exit()
20+
sys.exit()

examples/message_reader.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
if alvik.begin() < 0:
77
sys.exit()
88

9-
speed = 0
10-
119
while True:
1210
try:
1311
print(f'VER: {alvik.version}')

examples/pose_example.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,56 +8,48 @@
88
while True:
99
try:
1010

11-
alvik.move(100.0)
11+
alvik.move(100.0, 'mm')
1212
print("on target after move")
1313

14-
alvik.move(50.0)
14+
alvik.move(50.0, 'mm')
1515
print("on target after move")
1616

17-
alvik.rotate(90.0)
17+
alvik.rotate(90.0, 'deg')
1818
print("on target after rotation")
1919

20-
alvik.rotate(-45.00)
20+
alvik.rotate(-45.00, 'deg')
2121
print("on target after rotation")
2222

2323
x, y, theta = alvik.get_pose()
24-
print(f'Current pose is x={x}, y={y} ,theta={theta}')
24+
print(f'Current pose is x(cm)={x}, y(cm)={y}, theta(deg)={theta}')
2525

2626
alvik.reset_pose(0, 0, 0)
2727

2828
x, y, theta = alvik.get_pose()
29-
print(f'Updated pose is x={x}, y={y} ,theta={theta}')
29+
print(f'Updated pose is x(cm)={x}, y(cm)={y}, theta(deg)={theta}')
3030
sleep_ms(500)
3131

3232
print("___________NON-BLOCKING__________________")
3333

34-
alvik.move(50.0, blocking=False)
35-
while not alvik.is_target_reached():
36-
print(f"Not yet on target received:{alvik.last_ack}")
34+
alvik.move(50.0, 'mm', blocking=False)
3735
print("on target after move")
3836

39-
alvik.rotate(45.0, blocking=False)
40-
while not alvik.is_target_reached():
41-
print(f"Not yet on target received:{alvik.last_ack}")
37+
alvik.rotate(45.0, 'deg', blocking=False)
4238
print("on target after rotation")
4339

44-
alvik.move(100.0, blocking=False)
45-
while not alvik.is_target_reached():
46-
print(f"Not yet on target received:{alvik.last_ack}")
40+
alvik.move(100.0, 'mm', blocking=False)
4741
print("on target after move")
4842

49-
alvik.rotate(-90.00, blocking=False)
50-
while not alvik.is_target_reached():
51-
print(f"Not yet on target received:{alvik.last_ack}")
43+
alvik.rotate(-90.00, 'deg', blocking=False)
5244
print("on target after rotation")
5345

5446
x, y, theta = alvik.get_pose()
55-
print(f'Current pose is x={x}, y={y} ,theta={theta}')
47+
print(f'Current pose is x(cm)={x}, y(cm)={y}, theta(deg)={theta}')
5648

5749
alvik.reset_pose(0, 0, 0)
5850

5951
x, y, theta = alvik.get_pose()
60-
print(f'Updated pose is x={x}, y={y} ,theta={theta}')
52+
print(f'Updated pose is x={x}, y={y}, theta(deg)={theta}')
6153
sleep_ms(500)
6254

6355
alvik.stop()

examples/read_color_sensor.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
alvik = ArduinoAlvik()
66
alvik.begin()
7-
speed = 0
87

98
while True:
109
try:
11-
r, g, b = alvik.get_color_raw()
12-
print(f'RED: {r}, Green: {g}, Blue: {b}')
10+
r, g, b = alvik.get_color()
11+
h, s, v = alvik.get_color('hsv')
12+
print(f'RED: {r}, Green: {g}, Blue: {b}, HUE: {h}, SAT: {s}, VAL: {v}')
13+
print(f'COLOR LABEL: {alvik.get_color_label(h, s, v)}')
1314
sleep_ms(100)
1415
except KeyboardInterrupt as e:
1516
print('over')

examples/read_imu.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
alvik = ArduinoAlvik()
66
alvik.begin()
7-
speed = 0
87

98
while True:
109
try:

examples/read_tof.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from arduino_alvik import ArduinoAlvik
2+
from time import sleep_ms
3+
import sys
4+
5+
alvik = ArduinoAlvik()
6+
alvik.begin()
7+
8+
while True:
9+
try:
10+
L, CL, C, CR, R = alvik.get_distance()
11+
T = alvik.get_distance_top()
12+
B = alvik.get_distance_bottom()
13+
print(f'T: {T} | B: {B} | L: {L} | CL: {CL} | C: {C} | CR: {CR} | R: {R}')
14+
sleep_ms(100)
15+
except KeyboardInterrupt as e:
16+
print('over')
17+
alvik.stop()
18+
sys.exit()

examples/read_touch.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
alvik = ArduinoAlvik()
66
alvik.begin()
7-
speed = 0
87

98
while True:
109
try:

0 commit comments

Comments
 (0)