Skip to content

Commit 6c2c1fe

Browse files
committed
Fixed a few details in documentation. Added Sensors tutorial
1 parent f645d3c commit 6c2c1fe

File tree

4 files changed

+57
-44
lines changed

4 files changed

+57
-44
lines changed
98.2 KB
Loading

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@
176176
# dir menu entry, description, category)
177177
texinfo_documents = [
178178
(master_doc, 'GigglebotMicropythonLibrary', 'Gigglebot Micropython Library Documentation',
179-
author, 'GigglebotMicropythonLibrary', 'One line description of project.',
179+
author, 'GigglebotMicropythonLibrary', 'Gigglebot Microbit Robot.',
180180
'Miscellaneous'),
181181
]
182182

docs/source/sensors.rst

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ Sensors tutorial
33

44
The GiggleBot comes with a couple of onboard sensors:
55

6-
#. two light sensors, near the front eyes LEDs,
7-
#. two line sensors, on the underside of the line follower.
6+
#. Two light sensors, near the front eyes LEDs.
7+
#. Two line sensors, on the underside of the line follower.
88

99
Light Sensors
1010
-------------
@@ -37,19 +37,19 @@ your GiggleBot by using a flashlight.
3737

3838
This is how to use this project:
3939

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.
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.
4444

4545
First, a bit of explanation on the algorithm being used here.
4646

4747
On starting the GiggleBot, the Micro:Bit will:
4848

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.
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+
#. Start a forever loop.
5353

5454
This forever loop only waits for one thing:
5555
for the user to press button A, and that's when the light chasing begins.
@@ -59,14 +59,14 @@ quite happy to be active! And then it starts a second forever loop! This second
5959
forever loop is the actual Light Chasing game. It will end when button B is
6060
pressed by the user.
6161

62-
How to chase a light:
62+
How to Chase a Light:
6363

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.
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.
7070

7171

7272

@@ -132,17 +132,26 @@ It contains two line sensors. You can spot them from the top of the line
132132
follower by two white dots. And from the bottom, they are identified as *R* and
133133
*L* (for *right* and *left*)
134134

135+
.. figure:: _static/images/GigglebotLineFollowingSensors.jpg
136+
:align: center
137+
:alt: sensors underneath the line follower
138+
139+
*photo courtesy of Les Pounder*
140+
141+
135142
The easiest way of reading the sensors is as follow:
136143

137-
.. code:
144+
.. code::
145+
146+
from gigglebot import *
138147
left, right = read_sensor(LINE_SENSOR, BOTH)
139148
140149
The lower the number, the darker it is reading. Values can go from 0 to 1023
141150
and depend a lot on your environment. If you want to write a line follower
142151
robot, it is best to take a few readings first, to get a good idea of what
143152
numbers will represent a black line, and what numbers represent a white line.
144153

145-
Calibrating the line follower
154+
Calibrating the Line Follower
146155
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147156

148157
Calibrating the line follower means figuring out which numbers get returned
@@ -156,11 +165,11 @@ The following code will display the values onto the microbit leds when you
156165
press button A, allowing you to manually position your robot around your
157166
circuit and take readings.
158167

159-
.. code:
168+
.. code::
169+
160170
from gigglebot import *
161171
# reset all previous readings of button_a
162-
# strictly speaking this is not necessary
163-
# it is just a safety thing
172+
# strictly speaking this is not necessary, it is just a safety thing
164173
microbit.button_a.was_pressed()
165174
while True:
166175
if microbit.button_a.is_pressed():
@@ -180,20 +189,25 @@ in other words, if both sensors detect they're over the background color.
180189

181190
The logic will be as follow:
182191

183-
#. if both sensors detect a black line, forge straight ahead.
184-
#. if neither sensor detects a black line, give up and stop.
185-
#. if the right sensor detects a black line but not the left sensor, then steer to the right.
186-
#. if the left sensor detects a black line but not the right sensor, then steer to the left.
192+
#. If both sensors detect a black line, forge straight ahead.
193+
#. If neither sensor detects a black line, give up and stop.
194+
#. If the right sensor detects a black line but not the left sensor, then steer to the right.
195+
#. If the left sensor detects a black line but not the right sensor, then steer to the left.
196+
197+
We are also using the LEDs on the LED smile to indicate what is going on while
198+
we follow the line.
199+
200+
.. code::
187201
188-
.. code:
189202
from gigglebot import *
190-
# reset all previous readings of button_a
191-
# strictly speaking this is not necessary
192-
# it is just a safety thing
203+
# reset all previous readings of button_a, and button_b
204+
# strictly speaking this is not necessary, it is just a safety thing
193205
microbit.button_a.was_pressed()
206+
microbit.buttom_b.was_pressed()
194207
microbit.display.show(microbit.Image.YES)
195208
strip=init()
196-
# speed needs to be set according to your line and battery level
209+
# speed needs to be set according to your line and battery level.
210+
# do not go too fast though.
197211
set_speed(60, 60)
198212
# threshold is a little over the highest number you got that indicates a
199213
# black line.
@@ -234,5 +248,4 @@ The logic will be as follow:
234248
strip[8]=(0,255,0)
235249
strip.show()
236250
turn(LEFT)
237-
microbit.sleep(50)
238251
stop()

gigglebot_with_docs.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ def set_smile(R=25,G=0,B=0):
119119
"""
120120
Controls the color of the smile neopixels, all together.
121121
122-
:param int R = 25: the red component of the color, from 0 to 254
123-
:param int G = 0: the green component of the color, from 0 to 254
124-
:param int B = 0: the blue component of the color, from 0 to 254
122+
:param int R = 25: Red component of the color, from 0 to 254.
123+
:param int G = 0: Green component of the color, from 0 to 254.
124+
:param int B = 0: Blue component of the color, from 0 to 254.
125125
126126
"""
127127
neopix = range(2,9)
@@ -132,10 +132,10 @@ def set_eyes(which=BOTH, R=0, G=0, B=10):
132132
"""
133133
Controls the color of the two eyes, each one individually or both together.
134134
135-
:param int which = BOTH: either LEFT (0), RIGHT (1), or BOTH (2)
136-
:param int R = 0: the red component of the color, from 0 to 254
137-
:param int G = 0: the green component of the color, from 0 to 254
138-
:param int B = 10: the blue component of the color, from 0 to 254
135+
:param int which = BOTH: either LEFT (0), RIGHT (1), or BOTH (2).
136+
:param int R = 0: Red component of the color, from 0 to 254.
137+
:param int G = 0: Green component of the color, from 0 to 254.
138+
:param int B = 10: Blue component of the color, from 0 to 254.
139139
"""
140140
if which != LEFT: neopixelstrip[0]=(R,G,B)
141141
if which != RIGHT: neopixelstrip[1]=(R,G,B)
@@ -165,7 +165,7 @@ def pixels_off():
165165

166166
def set_servo(which=LEFT, degrees=90):
167167
"""
168-
:param int which: Which servo to control: LEFT (0), RIGHT (1), or BOTH (2)
168+
:param int which: Which servo to control: LEFT (0), RIGHT (1), or BOTH (2).
169169
:param int degrees: Position of the servo, from 0 to 180.
170170
171171
.. note::
@@ -197,7 +197,7 @@ def servo_off(which):
197197
"""
198198
Removes power from the servo.
199199
200-
:param int which: determines which servo, LEFT (0), RIGHT (1), BOTH (2)
200+
:param int which: Determines which servo, LEFT (0), RIGHT (1), BOTH (2).
201201
"""
202202
if which == LEFT or which == BOTH: microbit.pin14.write_digital(0)
203203
if which == RIGHT or which == BOTH: microbit.pin13.write_digital(0)
@@ -207,8 +207,8 @@ 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 LIGHT_SENSOR (6), or the line sensors LINE_SENSOR (5). Values are from 0 to 1023.
211-
:param int which_side: reads LEFT (0), RIGHT (1), or BOTH (2) sensors. When reading both sensors, an array will be returned.
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.
211+
:param int which_side: Reads LEFT (0), RIGHT (1), or BOTH (2) sensors. When reading both sensors, an array will be returned.
212212
213213
:returns: either an integer or an array of integers (left, then right)
214214

0 commit comments

Comments
 (0)