added content for 3rd robot lesson
parent
4f8c2ba774
commit
092963c511
|
|
@ -1101,25 +1101,39 @@ robot.turn(0)
|
||||||
level: 'robot',
|
level: 'robot',
|
||||||
map: 'Level 3',
|
map: 'Level 3',
|
||||||
content: `
|
content: `
|
||||||
<p>Turning is very similar to moving, we use the <code>robot.turn(amount)</code> function.</p>
|
<p>We can't do much just by moving a robot using delays, so lets start using its sensors.</p>
|
||||||
<p>The <code>amount</code> parameter is a number between -1 and 1, where -1 is full left, 0 is no turn, and 1 is full right.</p>
|
<p>The <code>robot.get_distance_left()</code> function returns the length of the left sensor beam.</p>
|
||||||
|
<p>When there is something blocking it, it gets smaller, and you can use this to detect obstacles.</p>
|
||||||
|
|
||||||
|
<p>Try this code, and watch the console output:</p>
|
||||||
<pre><code>
|
<pre><code>
|
||||||
import robot
|
import robot
|
||||||
import time
|
import time
|
||||||
|
|
||||||
robot.turn(1)
|
robot.move(0.5)
|
||||||
time.sleep(2)
|
while True:
|
||||||
robot.turn(0)
|
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
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</br>
|
</br>
|
||||||
<p>This code causes the robot to turn right at max speed for 2 seconds, then stop.</p>
|
<p>Note how the distance changes as the robot moves.</p>
|
||||||
|
<p>We can use this to automatically steer around obstacles.</p>
|
||||||
|
<p>Add the following code AFTER the <code>print(distance)</code> line:</p>
|
||||||
|
<pre><code>
|
||||||
|
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
|
||||||
|
</code></pre>
|
||||||
|
</br>
|
||||||
|
<p>If you want to get the length of the right sensor, it's <code>robot.get_distance_right()</code>.</p>
|
||||||
|
|
||||||
<p>You'll need to combine moving, turning, and waiting to reach all the checkpoints.</p>
|
</br>
|
||||||
<p><strong>Note:</strong> The values for move, turn, and sleep can all be decimal numbers (floats). ie <code>time.sleep(0.5)</code> or <code>robot.move(0.8)</code></p>
|
<p><strong>Note:</strong> Having that short <code>time.sleep(0.1)</code> is very important, if the loop runs too fast it overwhelms the system and nothing will happen.</p>
|
||||||
`,
|
`,
|
||||||
objectives: [
|
objectives: [
|
||||||
"Reach the first checkpoint",
|
"Reach the checkpoint",
|
||||||
|
|
||||||
"Code should complete without errors"
|
"Code should complete without errors"
|
||||||
],
|
],
|
||||||
|
|
|
||||||
|
|
@ -122,7 +122,7 @@ class RobotModule:
|
||||||
deg = -1
|
deg = -1
|
||||||
elif deg > 1:
|
elif deg > 1:
|
||||||
deg = 1
|
deg = 1
|
||||||
deg = deg/250
|
deg = deg/150
|
||||||
send_to_main("turn", deg)
|
send_to_main("turn", deg)
|
||||||
|
|
||||||
robot = RobotModule()
|
robot = RobotModule()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue