back to Index

base_lcm_driver_test

2014-08-07

Example student code. This needs to be run on the robot in order to work.


In [3]:
# Cross-notebook include shim
with open("nbinclude.ipynb") as nbinclude_f: # don't rename nbinclude_f
    import IPython.nbformat.current
    get_ipython().run_cell(IPython.nbformat.current.read(nbinclude_f, 'json').worksheets[0].cells[0].input)

In [6]:
import time, lcm
from python.forseti2 import piemos_cmd
from python.fearing import base_cmd, reflectance_scan, xbox_joystick_state
import python.fearing

In [ ]:
python.fearing.xbox_joystick_state

In [5]:
lc=lcm.LCM('udpm://239.255.76.67:7667?ttl=1')

In [4]:
cmd = base_cmd()
cmd.header = python.fearing.header()

In [5]:
cmd.left_cmd=0
cmd.right_cmd=0
lc.publish('robot0/cmd', cmd.encode())

In [30]:
%%file lbot.py
import time, lcm
from python.forseti2 import piemos_cmd
from python.fearing import base_cmd, reflectance_scan, xbox_joystick_state
import python.fearing
class Robot:
    def __init__(self):
        self.lcm=lcm.LCM('udpm://239.255.76.67:7667?ttl=1')
        self.msg=base_cmd()
        self.msg.header = python.fearing.header()
    def drive(self, l, r):
        self.msg.left_cmd=l
        self.msg.right_cmd=r
        self.lcm.publish('robot0/cmd', self.msg.encode())
    def read_sensors(self):
        class Container:
            values=None
        c=Container()
        lc=lcm.LCM('udpm://239.255.76.67:7667?ttl=1')
        def callback(chan, data):
            c.values=reflectance_scan.decode(data).reflectaces
        lc.subscribe('robot0/state', callback)
        while c.values is None:
            lc.handle()
        return c.values
    def read_joysticks(self):
        class Container:
            values=None
        c=Container()
        lc=lcm.LCM('udpm://239.255.76.67:7667?ttl=1')
        def callback(chan, data):
            c.values=xbox_joystick_state.decode(data).axes
        lc.subscribe('robot0/joy', callback)
        while c.values is None:
            lc.handle()
        return c.values


Overwriting lbot.py

In [30]:
class LCMBot:
    def __init__(self):
        self.lcm=lcm.LCM('udpm://239.255.76.67:7667?ttl=1')
        self.msg=base_cmd()
        self.msg.header = python.fearing.header()
    def drive(self, l, r):
        self.msg.left_cmd=l
        self.msg.right_cmd=r
        self.lcm.publish('robot0/cmd', self.msg.encode())
    def read_sensors(self):
        class Container:
            values=None
        c=Container()
        lc=lcm.LCM('udpm://239.255.76.67:7667?ttl=1')
        def callback(chan, data):
            c.values=reflectance_scan.decode(data).reflectaces
        lc.subscribe('robot0/state', callback)
        while c.values is None:
            lc.handle()
        return c.values
    def read_joysticks(self):
        class Container:
            values=None
        c=Container()
        lc=lcm.LCM('udpm://239.255.76.67:7667?ttl=1')
        def callback(chan, data):
            c.values=xbox_joystick_state.decode(data).axes
        lc.subscribe('robot0/joy', callback)
        while c.values is None:
            lc.handle()
        return c.values

In [31]:
lcmbot=LCMBot()

In [32]:
lcmbot.drive(0,.1)

In [33]:
lcmbot.drive(0,0)

In [34]:
lcmbot.read_sensors()


Out[34]:
(0.8327229022979736,
 0.6495726704597473,
 0.5724053978919983,
 0.6454212665557861,
 0.6080586314201355,
 0.6871795058250427)

In [7]:
def read_joysticks():
    class Container:
        values=None
    c=Container()
    lc=lcm.LCM('udpm://239.255.76.67:7667?ttl=1')
    def callback(chan, data):
        c.values=xbox_joystick_state.decode(data).axes
    lc.subscribe('robot0/joy', callback)
    while c.values is None:
        lc.handle()
    return c.values

In [24]:
%timeit read_joysticks()


10 loops, best of 3: 31.6 ms per loop

In [26]:
def read_sensors():
    class Container:
        values=None
    c=Container()
    lc=lcm.LCM('udpm://239.255.76.67:7667?ttl=1')
    def callback(chan, data):
        c.values=reflectance_scan.decode(data).reflectaces
    lc.subscribe('robot0/state', callback)
    while c.values is None:
        lc.handle()
    return c.values

In [27]:
%timeit read_sensors()


10 loops, best of 3: 45.1 ms per loop

In [14]:
read_sensors()


Out[14]:
(0.6691086888313293,
 0.2271062433719635,
 0.04395604506134987,
 0.04688645154237747,
 0.05274725705385208,
 0.3948718309402466)

In [110]:
import IPython, sys, time

In [119]:
while True:
    vals=read_sensors()
    IPython.display.clear_output()
    for val in vals:
        print '>'*int(100*val) + ' '*int(100*(1.0-val)) + str(val)
    sys.stdout.flush()


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                                 0.669108688831
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                                                                      0.299389511347
>>>>                                                                                               0.0495726540685
>>>>                                                                                               0.0485958531499
>>>>>                                                                                              0.0507936552167
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                                                                   0.326984137297
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-119-809fd1f97f5a> in <module>()
      1 while True:
----> 2     vals=read_sensors()
      3     IPython.display.clear_output()
      4     for val in vals:
      5         print '>'*int(100*val) + ' '*int(100*(1.0-val)) + str(val)

<ipython-input-72-897172fa8374> in read_sensors()
      9     lc.subscribe('robot0/state', callback)
     10     while c.values is None:
---> 11         lc.handle()
     12     return c.values

KeyboardInterrupt: 

In [ ]: