back to Index
Let's make a robot drive forward:
In [2]:
import lbot, time, IPython, sys, time
r = lbot.Robot()
r.drive(.1, .1) # range [-1.0 ... 1.0]
The robot should now be driving forward!
Stop the robot by running the cell below:
In [3]:
r.drive(0,0)
In the empty cell below, try making the robot spin to the right:
In [18]:
# Your code goes here!~
r.drive(.1,0)
Then, in the empty cell below, try to make the robot stop.
In [19]:
# Your code goes here!~
r.drive(0,0)
now, try making the robot drive backwards:
In [6]:
# Your code goes here!~
r.drive(-.1,-.1)
In [ ]:
These code blocks are called cells.
The menu and toolbar:
Try clicking on Insert
==> Insert Cell Below
.
By making new cells below this one, try commanding the robot to drive in different radius circles.
Make a cell below here that spins the robot in ~5 inch diameter circles. (Don't spend too much time tuning)
In [40]:
# Your code goes here!~
r.drive(.3,.1)
In [41]:
# Your code goes here!~
r.drive(0,0)
Make a cell below here that spins the robot in ~10 inch diameter circles. (Don't spend too much time tuning)
In [ ]:
# Your code goes here!~
In [ ]:
# Your code goes here!~
Commands can be strung together to make a robot follow paths.
Compare the behavior of the two cells, and try to record an explaination:
In [44]:
r.drive(0,.2)
r.drive(0,0)
What happened: It moved a little and stopped.
Why: First command told the robot to move forward and then the second command told it to stop completely.
In [48]:
r.drive(.15,.15)
time.sleep(.5)
r.drive(0,0)
What happened: The robot moved forward by .5 seconds.
Why: The first command told the robot to move forward. Second command set a time limit of .5 seconds. Third command told the robot to stop completely.
Try making the robot turn 90 degrees using what you've learned (hint: time.sleep might help!)
In [55]:
# Your code goes here!~
r.drive(.3,0)
time.sleep(1.0)
r.drive(0,0)
Try making the robot drive 5 inches.
In [52]:
# Your code goes here!~
r.drive(.2,.2)
time.sleep(1.0)
r.drive(0,0)
Now, use these together to make the robot drive forwards, turn 180 degrees, drive back, and then turn back to the original angle.
In [58]:
# Your code goes here!~
r.drive(.2,.2)
time.sleep(1.0)
r.drive(0,0)
r.drive(.3,0)
time.sleep(1.5)
r.drive(0,0)
r.drive(.2,.2)
time.sleep(1.0)
r.drive(0,0)
r.drive(.3,0)
time.sleep(1.5)
r.drive(0,0)
We can pass variables to functions:
In [60]:
left = .1
right = .1
r.drive(left, right)
We can also assign variables:
In [ ]:
left = .1
right = .1
print left
print right
left = left * 2
print left
print right
r.drive(left, right)
In [ ]:
r.drive(0,0)
In [61]:
for i in range(10):
value = i*5
print i
print value
Now, using the for loop, make a robot drive in a square:
In [67]:
# Your code goes here!~
for i in range(4):
r.drive(.2,.2)
time.sleep(1.0)
r.drive(.3,0)
time.sleep(1.0)
r.drive(0,0)
In [68]:
r.drive(0,0)
Run the cell below a couple times under various lighting conditions. For example, try:
In [71]:
reflectances = r.read_sensors()
print reflectances
print len(reflectances)
The output of read_sensors
is a tuple: a group of values.
In [72]:
tups = (12, -1)
print tups
print len(tups)
In [73]:
print tups[0]
print tups[1]
Use the examples above to print out the value of the third reflectance sensor.
In [74]:
# Your code goes here!~
print reflectances[2]
Use your knowlege of loops and tuples to print all the values of read_sensors.
In [87]:
# Your code goes here!~
for i in range (6):
print reflectances[i]
In [86]:
for i in range(5):
print i
time.sleep(1)
In [77]:
for i in range(5):
IPython.display.clear_output()
print i
sys.stdout.flush()
time.sleep(1)
Try this:
(To stop it remember that there is a stop button in the toolbar ar the top)
In [79]:
while True:
values = r.read_sensors()
IPython.display.clear_output()
for val in values:
print '='*int(30*val) + int(30*(1.-val))*' ' + str(val)
sys.stdout.flush()
time.sleep(1)
In [ ]:
5 < 4
In [ ]:
5 > 4
In [ ]:
5 >= 4
In [ ]:
4 >= 4
In [ ]:
4 <= 3
In [ ]:
5 == 4
In [ ]:
5 == 5.0
In [ ]:
5 != 4
In [ ]:
2 + 2 == 4
In [ ]:
2 >= 3
What do the following comparison operators do?
">":
"<":
">=":
"<=":
"!=":
See if the value read in by the first sensor is greater than or equal to .3
Store the result in variable boo
In [81]:
# Your code goes here!~
i = reflectances[0] >= .3
print i
Play with the code below to understand conditionals.
In [82]:
temp = 0
if temp > 8:
print "hello"
else:
if temp > 4:
print "bye"
else:
print "Your number is too small"
Try printing a tuple of Booleans (True or False) that indicates if a sensor is on the tape line.
In [129]:
# Your code goes here!~
reflectances = r.read_sensors()
i = reflectances[0] <= .71
print reflectances
print i
Try to do the following:
Set the robot to be perpendicular to some retro reflective tape a few inches away
Let the robot drive until the robot reaches the tape
Run the cell multiple times for varrying distance from the reflective tape.
Think about your tests under different lighting conditions and logical operators.
In [153]:
# Your code goes here!~
r.drive(.1,.1)
while True:
reflectances = r.read_sensors()
if reflectances[0] <.1:
r.drive(0,0)
In [ ]:
# Your code goes here!~
r.drive(0,0)
Now put the robot on reflective tape parallel to it and have it drive until it is off the reflective tape.
In [ ]:
# Your code goes here!~
Congratulations, you are on your way to follow a line!
Run the cell below, and try graphing the values using examples above.
You will need to jiggle the joysticks to read values.
In [ ]:
r.read_joysticks()
Try making the robot drive using joysticks.
In [ ]:
# Your code goes here!~