Normal Differential Shade Values for Roaming
To make the cyber:bot roam toward light, the code's job is to get from the raw sensor values to a norm_diff_shade value that can be used to adjust the servo speeds. We want cyber:bot turn a little—or a lot—when the contrast between the light detected on each side is a little—or a lot. The following equation in code makes the norm_diff_shade value to integer between -100 and 100.
norm_diff_shade = (200 * qt_right) / (qt_right + qt_left + 1) - 100
Let's confirm this strategy works before using it in a navigation script.
ERROR FALSE ALARM! On line 12 of the script test_integer_shade_value, you may see a compiler error warning "Operator "+" not supported for this combination of types." This is just a false alarm. The script actually works just fine, so go ahead and send it to the micro:bit.
Example script: test_integer_shade_value
- Use the micro:bit Python Editor to open a previous cyber:bot project, like norm_diff_shade_display or cyberbot-template-with-blink.
- Set the project's name to test_integer_shade_value, update the script so that it matches the one below, and then click Save.
- Click Send to micro:bit.
# test_integer_shade_value from cyberbot import * while True: bot(8).write_digital(1) qt_left = bot(8).rc_time(1) bot(6).write_digital(1) qt_right = bot(6).rc_time(1) norm_diff_shade = (200 * qt_right) / (qt_right + qt_left + 1) - 100 display.scroll(norm_diff_shade)
- Cover any windows letting in direct light, so the sun doesn't overwhelm the phototransistors.
- Set the cyber:bot board's PWR switch to 1.
- Cast shade over each phototransistor, and also try a flashlight if you have it, and watch the values on the micro:bit display.
- Set the cyber:bot board's PWR switch to 0.
Make sure the micro:bit displays values between -100 and 100 depending on which sides the light and shadow are on. The values do not necessarily have to reach the -100 and 100 end points, but they should get close.
Shady Navigation Decisions
Now, using this new strategy, we can manipulate the servos to drive based on which direction the light is hitting the cyber:bot. The light-seeking script and some ideas for adapting it are included below. How this script works is on the next page.
ERROR FALSE ALARM! On line 12 of the script seek_bright_light, you may see a compiler error warning "Operator "+" not supported for this combination of types." This is just a false alarm. The script actually works just fine, so go ahead and send it to the micro:bit.
- Use the micro:bit Python Editor to open test_integer_shade_value.
- Set the project's name to seek_bright_light, update the script so that it matches the one below, and then click Save.
- Click Send to micro:bit.
# seek_bright_light from cyberbot import * while True: bot(8).write_digital(1) qt_left = bot(8).rc_time(1) bot(6).write_digital(1) qt_right = bot(6).rc_time(1) norm_diff_shade = (200 * qt_right) / (qt_right + qt_left + 1) - 100 if norm_diff_shade > 0: left_speed = 75 - norm_diff_shade right_speed = -75 else: left_speed = 75 right_speed = -75 - norm_diff_shade bot(18).servo_speed(left_speed) bot(19).servo_speed(right_speed)
- Unplug the USB cable from the micro:bit, set the cyber:bot robot on the floor, and set its PWR switcht to position 2.
- The cyber:bot should start moving towards the brightest area of the room.
- Cast shade over one phototransistor, then over the other. The cyber:bot should turn away from from the shaded side.
- Use a flashlight to drive the cyber:bot and verify that the cyber:bot is navigating towards the bright light.
- If it is not working, check the script and the wiring to make sure everything is correct.
- Remember to set the cyber:bot robot's PWR switch to 0 when it does not need to run a script, and make sure to unplug the battery holder's plug from the cyber:bot board's barrel jack when you are done for the day.
Try This: Changing Responsiveness
You can change the cyber:bot's responsiveness by changing the value 200 in this line of the script:
norm_diff_shade = (200 * qt_right) / (qt_right + qt_left + 1) - 100
- To increase responsiveness to light, change 200 to a larger value and re-flash the script. Does the cyber:bot's behavior change?
- To decrease responsiveness, change 200 to a smaller value and re-flash the script. Now how does the cyber:bot behave?
Your Turn
Here are several more light-sensing navigation ideas for your cyber:bot that can be made with adjustments to the while True: function:
- To make your cyber:bot follow shade instead of light, place norm_diff_shade = -norm_diff_shade right above the if statement.
- End roaming under a bright light or in a dark cubby by detecting very bright or very dark conditions. Add qt_left and qt_right together, and compare the result to either a really high (dark) threshold value or a really low (bright) threshold value.
- Make your cyber:bot function as a light compass by remaining stationary and turning toward bright light sources.