Skip to content

Commit 23b2f06

Browse files
authored
feat(robot): calibrate the color sensor of the robot (#36)
* calibration * README * updated readme
1 parent 3c3f7d9 commit 23b2f06

File tree

2 files changed

+107
-40
lines changed

2 files changed

+107
-40
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,22 @@ The project is composed by two components:
2424
6. Copy the printed MAC address
2525
7. `make robot-upload` to upload the code into the alvik
2626

27-
NOTE: the PATCH steps `2` and `3` are no more necessary when the [Arduino_AlvikCarrier](https://github.com/arduino-libraries/Arduino_AlvikCarrier) and the [Arduino-Alvik-mpy](https://github.com/arduino/arduino-alvik-mpy) are merged to master and release ad an official release.
27+
NOTE: the PATCH steps `2` and `3` are no more necessary when the [Arduino_AlvikCarrier](https://github.com/arduino-libraries/Arduino_AlvikCarrier) and the [Arduino-Alvik-mpy](https://github.com/arduino/arduino-alvik-mpy) are merged to master and released.
28+
29+
#### Color calibration mode
30+
The robot exposes a `color calibration mode` that can be activated by pressing the `ok` button.
31+
The robot guides the calibration using different lights code showed on the Modulino Pixels.
32+
The user must move the robot on WHITE and BLACK color surfaces when a specific lights code is shown.
33+
34+
Steps to calibrate the robot
35+
0. Prepare a WHITE and BLACK surface
36+
1. Switch on the robot
37+
2. Press the `ok` button to enter into the `calibration mode`. The mode has the following light codes
38+
- WHITE leds countdown: indicate that the robot must be put on WHITE color. At the end of the countdown, the robot calibrates the WHITE color.
39+
- GREEN leds for 2 seconds: white calibration terminated
40+
- BLUE leds countdown: indicate that the robot must be put on BLACK color. At the end of the countdown, the robot calibrates the BLACK color
41+
- GREEN leds for 2 seconds: black calibration terminated.
42+
- the robot automatically reset itself.
2843

2944
### Configure the controller
3045
1. Connect the controller

sketches/robot/main.py

Lines changed: 91 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import struct
44
import random
55
import sys
6+
import machine
67

78
from arduino_alvik import ArduinoAlvik
89
from time import sleep_ms, ticks_add, ticks_diff, ticks_ms
@@ -125,54 +126,105 @@ def map_value(value, from_low, from_high, to_low, to_high):
125126
)
126127

127128

129+
def countdown_color(color, ledoff_every_tick=2):
130+
for i in range(7, 0, -ledoff_every_tick):
131+
pixels.set_range_color(0, i, color)
132+
pixels.show()
133+
sleep_ms(500)
134+
pixels.clear_all()
135+
pixels.show()
136+
sleep_ms(500)
137+
pixels.clear_all()
138+
pixels.show()
139+
140+
def calibrate_color():
141+
print("Reading white color")
142+
countdown_color(ModulinoColor.WHITE)
143+
a.color_calibration("white")
144+
145+
pixels.set_all_color(ModulinoColor.GREEN, 15)
146+
pixels.show()
147+
sleep_ms(2000)
148+
149+
print("Reading black color")
150+
countdown_color(ModulinoColor.BLUE)
151+
a.color_calibration("black")
152+
153+
pixels.set_all_color(ModulinoColor.GREEN, 15)
154+
pixels.show()
155+
sleep_ms(2000)
156+
157+
pixels.clear_all()
158+
pixels.show()
159+
160+
# hard-reset the board to refresh the calibration (read again the color from the file)
161+
machine.reset()
162+
163+
164+
STATE_SETUP = -1
128165
STATE_INIT = 0
129166
STATE_PLAY = 1
130167

131168
state = STATE_INIT
132169

133170
deadline = 0
134171
while True:
135-
if state == STATE_INIT:
136-
a.drive(0, 0)
137-
showEndAnimation()
138-
if a.get_color_label() is not "BLACK":
139-
showReadyToPlayLeds()
140-
state = STATE_PLAY
141-
142-
elif state == STATE_PLAY:
143-
receiveAndExecuteFromEspNow()
144-
color = a.get_color_label()
145-
if color == "BLACK":
146-
state = STATE_INIT
147-
elif color == "YELLOW":
148-
pixels.set_all_color(ModulinoColor.YELLOW, 15)
149-
pixels.show()
150-
deg = random.choice([30.0, 45.0, 90.0, 130.0, 150.0, 180.0, 275.0, 360.0])
151-
a.rotate(deg, "deg")
152-
showReadyToPlayLeds()
153-
elif color == "BLUE":
172+
try:
173+
if state == STATE_SETUP:
174+
calibrate_color()
175+
176+
if state == STATE_INIT:
154177
a.drive(0, 0)
155-
for x in range(0, FREEZE_FOR_SECONDS):
156-
sleep_ms(500)
157-
pixels.set_all_color(ModulinoColor.BLUE, 15)
178+
showEndAnimation()
179+
if a.get_touch_ok():
180+
state = STATE_SETUP
181+
if a.get_color_label() is not "BLACK":
182+
showReadyToPlayLeds()
183+
state = STATE_PLAY
184+
185+
elif state == STATE_PLAY:
186+
receiveAndExecuteFromEspNow()
187+
188+
if a.get_touch_ok():
189+
state = STATE_SETUP
190+
191+
color = a.get_color_label()
192+
if color == "BLACK":
193+
state = STATE_INIT
194+
elif color == "YELLOW":
195+
pixels.set_all_color(ModulinoColor.YELLOW, 15)
158196
pixels.show()
159-
sleep_ms(500)
197+
deg = random.choice([30.0, 45.0, 90.0, 130.0, 150.0, 180.0, 275.0, 360.0])
198+
a.rotate(deg, "deg")
199+
showReadyToPlayLeds()
200+
elif color == "BLUE":
201+
a.drive(0, 0)
202+
for x in range(0, FREEZE_FOR_SECONDS):
203+
sleep_ms(500)
204+
pixels.set_all_color(ModulinoColor.BLUE, 15)
205+
pixels.show()
206+
sleep_ms(500)
207+
pixels.clear_all()
208+
pixels.show()
209+
showReadyToPlayLeds()
210+
elif color == "GREEN" or color == "LIGHT GREEN":
211+
if not isPlayingReverted:
212+
deadline = ticks_add(ticks_ms(), REVERT_CONTROLLER_FOR_SECONDS * 1000)
213+
isPlayingReverted = True
214+
215+
if isPlayingReverted:
216+
elapsed = ticks_diff(deadline, ticks_ms())
217+
mapped = map_value(elapsed, 0, REVERT_CONTROLLER_FOR_SECONDS * 1000, 0, 7)
160218
pixels.clear_all()
219+
pixels.set_range_color(0, mapped, ModulinoColor.VIOLET)
161220
pixels.show()
162-
showReadyToPlayLeds()
163-
elif color == "GREEN" or color == "LIGHT GREEN":
164-
if not isPlayingReverted:
165-
deadline = ticks_add(ticks_ms(), REVERT_CONTROLLER_FOR_SECONDS * 1000)
166-
isPlayingReverted = True
167-
168-
if isPlayingReverted:
169-
elapsed = ticks_diff(deadline, ticks_ms())
170-
mapped = map_value(elapsed, 0, REVERT_CONTROLLER_FOR_SECONDS * 1000, 0, 7)
171-
pixels.clear_all()
172-
pixels.set_range_color(0, mapped, ModulinoColor.VIOLET)
173-
pixels.show()
174-
if elapsed < 0:
175-
showReadyToPlayLeds()
176-
isPlayingReverted = False
177-
221+
if elapsed < 0:
222+
showReadyToPlayLeds()
223+
isPlayingReverted = False
224+
except AssertionError:
225+
print("AssertionError")
226+
# If calibration is not done correctly, the _limit() function can raise an AssertError
227+
# See https://github.com/arduino/arduino-alvik-mpy/blob/80e66561a2ae06c69adddb3adc21ca88f91a57dd/arduino_alvik/arduino_alvik.py#L724
228+
state = STATE_SETUP
229+
continue
178230
sleep_ms(50)

0 commit comments

Comments
 (0)