In this challenge, students will modify their existing code so that their robot moves in a perfect circle with an approximate diameter of 1500mm and then comes to a stop.
My robot moves in circle and comes to a stop.
- Complete Module 2: Calculations and Turtle! to learn about mathematical calculations in the Python language.
- Complete Blockly Level 3 to apply the run once algorithm visually.
flowchart TD
A[Start Program] --> B[Setup Robot]
B --> C[Initialize variables]
C --> D{Is counter less than 1?}
D -->|Yes| E[Drive Backward until time ends]
E --> F[Apply brake to stop robot]
F --> G[Increment counter by 1]
G --> D
D -->|No| H[End Program]
style A fill:#e1f5fe,color:#000000
style B fill:#000000, color:#ffffff
style C fill:#000000, color:#ffffff
style D fill:#ffecb3,color:#000000
style E fill:#e8f5e8,color:#000000
style F fill:#ffcdd2,color:#000000
style G fill:#fff3e0,color:#000000
style H fill:#e1f5fe,color:#000000
- Make sure your battery power switch is off.
- Navigate to https://lab-micropython.arduino.cc/.
- Sign in with Google (use your @education.nsw.gov.au account).
- Follow these instructions to connect, code and save:
Because the while True: control loop runs forever, we need to add a logic control structure, so it only runs once then stops.
from aidriver import AIDriver, hold_state
import aidriver
aidriver.DEBUG_AIDRIVER = True
my_robot = AIDriver()
my_counter = 0
wheel_speed = 180
speed_adjust = 0
move_time = 0
while my_counter < 1:
my_robot.drive_backward(wheel_speed - speed_adjust, wheel_speed + speed_adjust)
hold_state(move_time)
my_robot.brake()
hold_state(1)
my_counter = my_counter + 1Use hold_state(seconds) any time you want the robot to keep doing the same thing for a short time and record that pause in event_log.txt.
If you see an error while editing this code, check Common_Errors.md.
Your challenge is to:
- Review the math behind my_robot.drive_forward(L, R) and modify the values assigned to wheel_speed and speed_adjust so your robot moves in the desired circle size.
- Update the value of move_time so the robot travels for approximately the time needed to complete one full circle.
- Then further modify your code to:
- Drive a circle forward.
- Drive a clockwise and anti-clockwise circle.
- Drive 2, 3, 4 circles
- Drive a figure 8.
Caution
To avoid damaging your computer or robot, first save your main.py file. Next, disconnect your robot from your computer, then place it on the floor in an area with enough space for it to move safely before powering it on.
-
Change one variable at a time (e.g.
speed_adjustormove_time) and test. -
Run your code after every small change instead of writing everything at once.
-
If you are unsure whether the loop is running, add a temporary line:
print("Loop running")
-
Start from your working Challenge 1 solution and then extend it, rather than rewriting everything.
- Copy all your code from
main.py. - Paste it in your portfolio under "Challenge 2".
