Skip to content

Commit 0e213ed

Browse files
committed
return left sensor first when reading sensors. Add Light sensor documentation
1 parent 2df7a95 commit 0e213ed

File tree

4 files changed

+150
-12
lines changed

4 files changed

+150
-12
lines changed

docs/source/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ This library is meant to be used with the `Mu editor <https://codewith.mu/>`_ ,
1313

1414
For step by step installation instructions and how to get started, please see the main `GiggleBot website <https://www.gigglebot.io/pages/program-the-gigglebot-robot-micropython>`_
1515

16+
For an introduction to MicroPython on the Micro:Bit, see the `official tutorial <https://microbit-micropython.readthedocs.io/en/latest/index.html>`_.
1617

1718

1819
.. toctree::
1920
:maxdepth: 2
2021
:caption: Contents:
2122

2223
tutorial
24+
sensors
2325
code
2426

2527

docs/source/sensors.rst

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
Sensors tutorial
2+
================
3+
4+
The GiggleBot comes with a couple of onboard sensors:
5+
6+
#. two light sensors, near the front eyes LEDs,
7+
#. two line sensors, on the underside of the line follower.
8+
9+
Light Sensors
10+
-------------
11+
12+
The GiggleBot comes with two light sensors right in front of the LED on each eye. They are very small and easy to miss.
13+
However, they are more versatile than the Micro:Bit's light sensor as they can be read together to detect which side is receiving more light.
14+
15+
The main method to query the light sensors is :py:meth:`~gigglebot.read_sensor()`. The same method is used for both light sensors and line sensors.
16+
17+
Here's how to read both light sensors in one call:
18+
19+
.. code::
20+
21+
left, right = read_sensor(LIGHT_SENSOR, BOTH)
22+
23+
And here's how to read just one side at a time:
24+
25+
.. code::
26+
27+
right = read_sensor(LIGHT_SENSOR, RIGHT)
28+
29+
30+
Chase the Light
31+
^^^^^^^^^^^^^^^
32+
33+
This tutorial will turn the GiggleBot into a cat, following a spotlight on the
34+
floor. Many cats do that when you shine a flashlight in front of them, they will
35+
try to hunt the light spot. When running this code, you will be able to guide
36+
your GiggleBot by using a flashlight.
37+
38+
This is how to use this project:
39+
40+
#. start GiggleBot and wait for sleepy face to appear on the Micro:Bit,
41+
#. press button A to start the Chase the Light game (heart will replace the sleepy face),
42+
#. chase the light as long as you want,
43+
#. press button B to stop the GiggleBot and display sleepy face again.
44+
45+
First, a bit of explanation on the algorithm being used here.
46+
47+
On starting the GiggleBot, the Micro:Bit will:
48+
49+
#. assign a value to the `diff` variable (here it is using 10),
50+
#. display a sleepy face image,
51+
#. resets whatever readings from the buttons it might have had,
52+
#. then start a forever loop.
53+
54+
This forever loop only waits for one thing:
55+
for the user to press button A, and that's when the light chasing begins.
56+
57+
As soon as button A is pressed, the Micro:Bit will display a heart as it's
58+
quite happy to be active! And then it starts a second forever loop! This second
59+
forever loop is the actual Light Chasing game. It will end when button B is
60+
pressed by the user.
61+
62+
How to chase a light:
63+
64+
#. take reading from both light sensors,
65+
#. compare the readings, using `diff` to allow for small variations. You will most likely get absolutely identical readings, even if the light is mostly equal. Using a differential value helps stabilize the behavior. You can adapt to your own lighting conditions by changing this value.
66+
#. if the right sensor reads more than the left sensor plus the `diff` value, then we know it's brighter to the right. Turn right.
67+
#. if the left sensor reads more than the right sensor plus the `diff` value, then it's brighter to the left. Turn left.
68+
#. if there isn't that much of a difference between the two sensors, go straight.
69+
#. if button B gets pressed at any time, stop the robot, change sleepy face, and get out of this internal loop. The code will fall back to the first loop, ready for another game.
70+
71+
72+
73+
.. code::
74+
75+
from gigglebot import *
76+
# value for the differential between the two sensors.
77+
# you can change this value to make it more or less sensitive.
78+
diff = 10
79+
# display sleepy face
80+
microbit.display.show(microbit.Image.ASLEEP)
81+
# the following two lines resets the 'was_pressed' info
82+
# and discards any previous presses
83+
microbit.button_a.was_pressed()
84+
microbit.button_b.was_pressed()
85+
86+
# start first loop, waiting for user input
87+
while True:
88+
# test for user input
89+
if microbit.button_a.was_pressed():
90+
# game got started! Display much love
91+
microbit.display.show(microbit.Image.HEART)
92+
93+
# start game loop
94+
while True:
95+
# read both sensors
96+
left, right = read_sensor(LIGHT_SENSOR, BOTH)
97+
98+
# test if it's brighter to the right
99+
if right > left+diff:
100+
turn(RIGHT)
101+
102+
# test if it's brighter to the left
103+
elif left > right+diff:
104+
turn(LEFT)
105+
106+
# both sides being equal, go straight
107+
else:
108+
drive(FORWARD)
109+
110+
# oh no, the game got interrupted
111+
if microbit.button_b.is_pressed():
112+
stop()
113+
microbit.display.show(microbit.Image.ASLEEP)
114+
115+
# this line here gets us out of the game loop
116+
break
117+
118+
What else can be done with the light sensors?
119+
120+
You could modify this code to turn the GiggleBot into a night insect? Those would
121+
avoid light instead of chasing it.
122+
123+
You could detect when it gets dark or bright. Imagine the GiggleBot inside your
124+
closet. When someone opens the door, the sudden light can be detected. The GiggleBot
125+
can let you know someone went through your things while you were away.
126+

gigglebot.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
motor_power_left=50
88
motor_power_right=50
99
neopixelstrip=None
10-
GET_LINE_SENSORS=5
11-
GET_LIGHT_SENSORS=6
10+
LINE_SENSOR=5
11+
LIGHT_SENSOR=6
1212
_GET_VOLTAGE_BATTERY=4
1313
_SET_MOTOR_POWERS=10
1414
def _read(reg,size=8,repeat=False):
@@ -79,8 +79,11 @@ def servo_off(which):
7979
if which==LEFT or which==BOTH:microbit.pin14.write_digital(0)
8080
if which==RIGHT or which==BOTH:microbit.pin13.write_digital(0)
8181
def read_sensor(which_sensor,which_side):
82-
if(which_side==LEFT):return _get_sensors(which_sensor)[0]
83-
elif(which_side==RIGHT):return _get_sensors(which_sensor)[1]
84-
else:return _get_sensors(which_sensor)
82+
right,left=_get_sensors(which_sensor)
83+
if(which_side==LEFT):return left
84+
elif(which_side==RIGHT):return right
85+
else:return(left,right)
8586
def volt():
8687
return(_read(_GET_VOLTAGE_BATTERY,size=16)/1000)
88+
# Created by pyminifier (https://github.com/liftoff/pyminifier)
89+

gigglebot_with_docs.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
neopixelstrip = None
2222

2323
#: I2C command to read the line sensors.
24-
GET_LINE_SENSORS = 5
24+
LINE_SENSOR = 5
2525
#: I2C command to read the light sensors.
26-
GET_LIGHT_SENSORS = 6
26+
LIGHT_SENSOR = 6
2727
#: I2C command to query voltage level of the battery.
2828
_GET_VOLTAGE_BATTERY = 4
2929
#: I2C command to write new power values to the motor.
@@ -207,15 +207,22 @@ def read_sensor(which_sensor, which_side):
207207
"""
208208
Reads the GiggleBot onboard sensors, light or line sensors.
209209
210-
:param int which_sensor: reads the light sensors GET_LIGHT_SENSORS (6), or the line sensors GET_LINE_SENSORS (5). Values are from 0 to 1023.
210+
:param int which_sensor: reads the light sensors LIGHT_SENSOR (6), or the line sensors LINE_SENSOR (5). Values are from 0 to 1023.
211211
:param int which_side: reads LEFT (0), RIGHT (1), or BOTH (2) sensors. When reading both sensors, an array will be returned.
212212
213-
:returns: either an integer or an array of integers.
213+
:returns: either an integer or an array of integers (left, then right)
214+
215+
You can read the sensors this way:
216+
217+
.. code::
218+
219+
left, right = read_sensor(LIGHT_SENSOR, BOTH)
214220
215221
"""
216-
if (which_side == LEFT): return _get_sensors(which_sensor)[0]
217-
elif (which_side == RIGHT): return _get_sensors(which_sensor)[1]
218-
else: return _get_sensors(which_sensor)
222+
right, left = _get_sensors(which_sensor)
223+
if(which_side==LEFT): return left
224+
elif(which_side==RIGHT): return right
225+
else: return (left, right)
219226

220227

221228
def volt():

0 commit comments

Comments
 (0)