back to Index

Quickstart-Robot

Let's make a robot drive forward:

Click on the cell below, and press `SHIFT` + `ENTER` to run

In [1]:
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 [6]:
r.drive(0,0)

Robot Commands

In the empty cell below, try making the robot spin to the right:

Click on the cell below ("Your code goes here!~") and start typing code!

In [8]:
r.drive(0,0)

Then, in the empty cell below, try to make the robot stop.


In [ ]:


In [ ]:


In [ ]:
# Your code goes here!~

now, try making the robot drive backwards:


In [7]:
r.drive(-0.2,-0.2)

How to use this interface

These code blocks are called cells.

The menu and toolbar:

Try clicking on Insert ==> Insert Cell Below.

Fancy Robot Commands

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 [18]:
r.drive(0.2,0.05)

In [ ]:
# Your code goes here!~

Make a cell below here that spins the robot in ~10 inch diameter circles. (Don't spend too much time tuning)


In [20]:
r.drive(0.2, 0.02)

In [ ]:
# Your code goes here!~

Robot Trajectories

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 [22]:
r.drive(0,.2)
r.drive(0,0)
Double click on the cell below ("What happened: Why:") to record your thoughts. Press `SHIFT` + `ENTER` to render the text.

What happened: IT moved a little bit and then stopped. Why: It was programmed to stop right after movement. There was nothing telling it to run for whatever amount of time.


In [23]:
r.drive(.15,.15)
time.sleep(.5)
r.drive(0,0)

What happened: It moved at speed .15 for .5 secs.

Why: The sleep determines the amount of time the robot runs before it stops.

Using Robot Trajectories with Sleep

Try making the robot turn 90 degrees using what you've learned (hint: time.sleep might help!)


In [29]:
r.drive(.2,-.2)
time.sleep(3.6)
r.drive(0,0)

In [36]:
r.drive(.1, .1)
time.sleep(4.4)
r.drive(0,0)

Try making the robot drive 5 inches.

Now, use these together to make the robot drive forwards, turn 180 degrees, drive back, and then turn back to the original angle.


In [39]:
r.drive(.1, .1)
time.sleep(4.4)
r.drive(.2,-.2)
time.sleep(7.1)
r.drive(.1, .1)
time.sleep(4.4)
r.drive(.2,-.2)
time.sleep(7.1)
r.drive(0,0)

Variables

We can pass variables to functions:


In [40]:
left = .1
right = .1

r.drive(left, right)

We can also assign variables:


In [42]:
left = .1
right = .1

print left
print right

left = left * 2

print left
print right

r.drive(left, right)


0.1
0.1
0.2
0.1

In [181]:
r.drive(0,0)

In [44]:
for i in range(10):
    value = i*5
    print i
    print value


0
0
1
5
2
10
3
15
4
20
5
25
6
30
7
35
8
40
9
45

Using Robot Trajectories With loops

Now, using the for loop, make a robot drive in a square:


In [51]:
for i in range(4):
    r.drive(.1, .1)
    time.sleep(5)
    r.drive(.2,-.2)
    time.sleep(3.4)
r.drive(0,0)

Sensors

Run the cell below a couple times under various lighting conditions. For example, try:

  • facing up
  • on retro-reflective tape
  • on carpet
  • on the table surface
Press `CTRL` + `ENTER` to run a cell repeatedly while the cell is selected

In [83]:
reflectances = r.read_sensors()
print reflectances
print len(reflectances)


(0.696703314781189, 0.40293043851852417, 0.3094017207622528, 0.40390723943710327, 0.3719169795513153, 0.5526251792907715)
6

The output of read_sensors is a tuple: a group of values.


In [84]:
tups = (12, -1)
print tups
print len(tups)


(12, -1)
2

In [ ]:
print tups[0]
print tups[1]

Use the examples above to print out the value of the third reflectance sensor.


In [85]:
reflectances = r.read_sensors()
print reflectances[2]


0.507936537266

Use your knowlege of loops and tuples to print all the values of read_sensors.


In [86]:
reflectances = r.read_sensors()
for i in range(5):
    print reflectances[i]


0.787301659584
0.446153879166
0.0520146563649
0.0444444455206
0.044200245291

Streaming data


In [87]:
for i in range(5):
    print i
    time.sleep(1)


0
1
2
3
4

In [88]:
for i in range(5):
    IPython.display.clear_output()
    print i
    sys.stdout.flush()
    time.sleep(1)


4

Streaming the reflectance values

Try this:

(To stop it remember that there is a stop button in the toolbar ar the top)

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(.01)

Let's explore comparisons!


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?

  1. ">":

  2. "<":

  3. ">=":

  4. "<=":

  5. "!=":

Playing with logicals

See if the value read in by the first sensor is greater than or equal to .3

Store the result in variable boo


In [93]:
reflectances = r.read_sensors()
boo = reflectances[0]>=.3
print boo


False

Conditionals

Play with the code below to understand conditionals.


In [ ]:
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 [128]:
reflectances = r.read_sensors()
list = []
for i in range(5):
    if 0.035 < reflectances[i] < 0.06:
        list.append(True)
    else:
        list.append(False)
print list


[True, True, True, True, True]

Now let's use sensors while driving the robot

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 [179]:
def is_on_line(values):
    r = 0
    for i in range(5):
        if values[i] < 0.6:
            r = r + 1
    if r>2:
        return True
    else:
        return False
while is_on_line(r.read_sensors()) == False:
    r.drive(0.1,0.1)
    if is_on_line(r.read_sensors()) == True:
        r.drive(0,0)
    time.sleep(0.01)

In [155]:
def drive(left,right): r.drive(left,right)

In [149]:
from IPython.html.widgets import interact

In [156]:
interact(drive, 
        left=(-1., 1., .1),
        right=(-1., 1., .1))

In [188]:
r.drive(0,0)
# Your code goes here!~

Now put the robot on reflective tape parallel to it and have it drive until it is off the reflective tape.


In [190]:
def is_on_line(values):
    r = 0
    for i in range(5):
        if values[i] < 0.6:
            r = r + 1
    if r>2:
        return True
    else:
        return False
while is_on_line(r.read_sensors()) == True:
    r.drive(0.1,0.1)
    time.sleep(0.01)
r.drive(0,0)

Congratulations, you are on your way to follow a line!

Reading Joysticks

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!~