From 092963c511a4850cd9542cffb7b25724991421ac Mon Sep 17 00:00:00 2001 From: Jake Date: Sun, 6 Jul 2025 18:44:13 +0800 Subject: [PATCH] added content for 3rd robot lesson --- data/lessons.js | 32 +++++++++++++++++++++++--------- pyodide-worker.js | 2 +- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/data/lessons.js b/data/lessons.js index 153411b..cca8f76 100644 --- a/data/lessons.js +++ b/data/lessons.js @@ -1101,25 +1101,39 @@ robot.turn(0) level: 'robot', map: 'Level 3', content: ` -

Turning is very similar to moving, we use the robot.turn(amount) function.

-

The amount parameter is a number between -1 and 1, where -1 is full left, 0 is no turn, and 1 is full right.

+

We can't do much just by moving a robot using delays, so lets start using its sensors.

+

The robot.get_distance_left() function returns the length of the left sensor beam.

+

When there is something blocking it, it gets smaller, and you can use this to detect obstacles.

+

Try this code, and watch the console output:


 import robot 
 import time
 
-robot.turn(1)
-time.sleep(2) 
-robot.turn(0)
+robot.move(0.5)
+while True:
+    distance = robot.get_distance_left()  # Get the distance from the left sensor
+    print(distance)  # Print the distance
+    time.sleep(0.1)  # Wait for on tenth of a second
 

-

This code causes the robot to turn right at max speed for 2 seconds, then stop.

+

Note how the distance changes as the robot moves.

+

We can use this to automatically steer around obstacles.

+

Add the following code AFTER the print(distance) line:

+

+if distance < 50:  # If the distance is less than 0.5 meters
+    robot.turn(1)  # Turn right at max speed
+else:
+    robot.turn(0)  # Stop turning
+
+
+

If you want to get the length of the right sensor, it's robot.get_distance_right().

-

You'll need to combine moving, turning, and waiting to reach all the checkpoints.

-

Note: The values for move, turn, and sleep can all be decimal numbers (floats). ie time.sleep(0.5) or robot.move(0.8)

+
+

Note: Having that short time.sleep(0.1) is very important, if the loop runs too fast it overwhelms the system and nothing will happen.

`, objectives: [ - "Reach the first checkpoint", + "Reach the checkpoint", "Code should complete without errors" ], diff --git a/pyodide-worker.js b/pyodide-worker.js index dbe920a..7385022 100644 --- a/pyodide-worker.js +++ b/pyodide-worker.js @@ -122,7 +122,7 @@ class RobotModule: deg = -1 elif deg > 1: deg = 1 - deg = deg/250 + deg = deg/150 send_to_main("turn", deg) robot = RobotModule()