back to Index

Important Information

The API:

r.drive(left, right) makes the motors move. The values range from -1 to 1.

Quickstart-Robot

Let's make a robot drive forward:

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

In [85]:
import lbot, time, IPython, sys, time
r = lbot.Robot()
r.drive(.1, .1)

The robot should now be driving forward!

Stop the robot by running the cell below:


In [86]:
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 [87]:
import lbot, time, IPython, sys, time
r = lbot.Robot()
r.drive(.1, .05)

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


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

now, try making the robot drive backwards:


In [89]:
r.drive(-.1, -.1)

How to use this interface

These code blocks are called cells.

The menu and toolbar:

Try clicking on Insert ==> Insert Cell Below.


In [91]:
r.drive(.1, .05)

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 [98]:
r.drive(.05, .15)

In [99]:
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 [77]:
# Your code goes here!~

In [89]:
# 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 [100]:
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: The robot moved a little then stopped

Why: The lines got executed one after another


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

What happened: THe robot moved quite a bit then stopped

Why: the time.sleep line allowed the code above to last for a longer amount of time so it continued driving lonver

Using Robot Trajectories with Sleep

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


In [105]:
r.drive(.3,-.3)
time.sleep(.5)
r.drive(0,0)

Try making the robot drive 5 inches.


In [84]:
# Your code goes here!~

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


In [87]:
# Your code goes here!~

Variables

We can pass variables to functions:


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

r.drive(left, right)

We can also assign variables:


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

In [109]:
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 [111]:
for i in range(4):
    r.drive(-.3,.3)
    time.sleep(.5)
    r.drive(0,0)
    r.drive(.2,.2)
    time.sleep(.7)
    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 [121]:
reflectances = r.read_sensors()
print reflectances
print len(reflectances)


(0.6039072275161743, 0.18119658529758453, 0.06275946646928787, 0.11672772467136383, 0.07399267703294754, 0.3672771751880646)
6

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


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


(12, -1)
2

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


12
-1

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


In [122]:
print reflectances[2]


0.0627594664693

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


In [124]:
for i in range(6):
    print reflectances[i]


0.603907227516
0.181196585298
0.0627594664693
0.116727724671
0.0739926770329
0.367277175188

Streaming data


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


0
1
2
3
4

In [126]:
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)


In [183]:
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)


===========                  0.391941428185
=                            0.0393162406981
=                            0.0415140427649
=                            0.0468864515424
==                           0.0727716758847
===========                  0.378510415554
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-183-1682ec94e1c0> in <module>()
      5         print '='*int(30*val) + int(30*(1.-val))*' ' + str(val)
      6     sys.stdout.flush()
----> 7     time.sleep(.01)

KeyboardInterrupt: 

Let's explore comparisons!


In [130]:
5 < 4


Out[130]:
False

In [131]:
5 > 4


Out[131]:
True

In [132]:
5 >= 4


Out[132]:
True

In [133]:
4 >= 4


Out[133]:
True

In [134]:
4 <= 3


Out[134]:
False

In [135]:
5 == 4


Out[135]:
False

In [136]:
5 == 5.0


Out[136]:
True

In [137]:
5 != 4


Out[137]:
True

In [138]:
2 + 2 == 4


Out[138]:
True

In [139]:
2 >= 3


Out[139]:
False

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 [154]:
reflectances = r.read_sensors()
boo = (reflectances[1] >= .3)
print reflectances[1]
print boo


0.0556776598096
False

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 [187]:
beforeline = True
while beforeline:
    reflectances = r.read_sensors()
    boo = 0
    r.drive(.2,.2)
    for i in range (6):
        if reflectances[i] < .5:
            boo = boo + 1
    if boo > 5:
        r.drive(0,0)
        beforeline = False

In [186]:
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 [180]:
online = True
while online:
    reflectances = r.read_sensors()
    boo = 0
    r.drive(.1,.1)
    for i in range (6):
        if reflectances[i] > .05:
            boo = boo + 1
    if boo > 3:
        r.drive(0,0)
        beforeline = False


---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-180-c42c82c2429f> in <module>()
      1 online = True
      2 while online:
----> 3     reflectances = r.read_sensors()
      4     boo = 0
      5     r.drive(.1,.1)

/home/ajc/topgear/odroid/topgear/lbot.pyc in read_sensors(self)
     21         lc.subscribe('robot0/state', callback)
     22         while c.values is None:
---> 23             lc.handle()
     24         return c.values

KeyboardInterrupt: 

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

Conditionals

The final step to make to follow lines is learning about conditionals


In [59]:
temp = 0

if temp > 8:
    print "hello"
else:
    if temp > 4:
        print "bye"
    else:
        print "Your number is too small"


Your number is too small

In [ ]: