Now that all the testing is complete we can move the RoboCar to the floor and see what it and your Team are able to do.
First of all go to the file library in Mu and load the file RoboCar.py.
from gpiozero import Robot import time roboCar = Robot(left=(7,8), right=(9,10)) roboCar.forward() time.sleep(2) roboCar.stop() time.sleep(1) roboCar.backward() time.sleep(2) roboCar.stop() time.sleep(1) roboCar.left() time.sleep(1) roboCar.stop() time.sleep(1) roboCar.right() time.sleep(1) roboCar.stop()
This is your basic operating program. You can load it at any time through Mu. When you run it your RoboCar will go forward a little, back a little and then spin in a circle. Now the challenge.
The Obstacle Course Challenge
Set you RoboCar anywhere within the START AREA on the MakerPlace floor.
Go over and take a look at it and observe what happens when you run the program. Now change the program so that your RoboCar goes forward, around the obstacle and comes to a stop in the STOP AREA on the MakerPlace floor. You will have to keep your RoboCar within the boundary markers, also shown on the floor.
Your changed program will likely require making the RoboCar go forward a number of times and make at least two turns. You will also have to run the course a number of times before you are successful. When you are ready for a “timed lap”, ask for one of the Course Leaders to come and observe your run. This time will be recorded on the board.
Please use the following tips in writing your RoboCar program
The six basic commands you need to operate your RoboCar are:
If you do not put a value between the parentheses (), the motor will run at full speed. If you put in a decimal value less that one, it will run at a slower speed. For example:
The RoboCar will go forward at half speed. (A half (1/2) in decimal is written as 0.5)
time.sleep() function tells the command in the line
above it how long to operate. The sleep function understands that the
value within the parentheses is the time you want in seconds. That
time can be a large number (
time.sleep(1000)) or a very
small number (
time.sleep(0.2)). You can use different time intervals to control your RoboCar’s actions.
Use the “Cut and Paste” shortcut to limit the amount of code you have to type into your program. It’s simple:/p>
- Step One – Highlight the code lines you wish to copy
- Step Two – While highlighted, press both Ctrl and C
- Step three – Move the cursor to where you want the copied code to go. Remember to right-click the mouse.
- Step Four – Press both Ctrl and V, and it’s done
- Refer to the Copy and Paste handout sheet in your kit to help you.
If you have made changes to the RoboCar.py program and want to reload the original code again, open the RoboBackup.py file.
Have fun and if you get stuck, ask for help.