back to Index

linefollow_test

2014-07-26


In [1]:
# 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 [2]:
import sys, IPython, time

In [3]:
nbinclude('robot')

In [4]:
r=Robot()

In [5]:
r.enable()

In [6]:
r.cmd(.2,.2)

In [7]:
r.cmd(0,0)

In [14]:
mbed=SerialRPC('/dev/ttyACM0', 115200)

In [15]:
ains=[]
for i in (p20,p19,p18,p17,p16,p15):
#     AnalogIn(mbed, 'p{0}'.format(i))
    a=AnalogIn(mbed, i)
    print a.read()
    ains.append(AnalogIn(mbed, i))


0.91355317831
0.834432303905
0.732356548309
0.0500610545278
0.0466422475874
0.0525030568242

In [16]:
for ain in ains:
    print ain.read()


0.911843776703
0.830525100231
0.733089148998
0.0495726540685
0.0493284538388
0.0566544607282

In [17]:
def read(ain): return ain.read()

In [18]:
def line_pos(ains):
    vals=map(read, ains)
    wsum=0
    for i in range(len(vals)):
        wsum+= (i+1.0)*vals[i]
    return wsum/sum(vals)

In [61]:
line_pos(ains)


Out[61]:
2.4184905336223506

Lets measure the maximum controller frequency (bounded by the polling rate):


In [62]:
%timeit line_pos(ains)


10 loops, best of 3: 32.7 ms per loop

In [63]:
1000/32.7


Out[63]:
30.58103975535168

Apparently: we can run the control loop at 30hz.


In [64]:
while True:
    IPython.display.clear_output()
    pos= line_pos(ains)
    out=(3.0-pos)*.2
    
    print pos, out
    r.cmd(.2-out, .2+out)
    sys.stdout.flush()
    time.sleep(.01)


---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-64-a335bf38ea49> in <module>()
      1 while True:
      2     IPython.display.clear_output()
----> 3     pos= line_pos(ains)
      4     out=(3.0-pos)*.2
      5 

<ipython-input-18-910a08d3c41e> in line_pos(ains)
      1 def line_pos(ains):
----> 2     vals=map(read, ains)
      3     wsum=0
      4     for i in range(len(vals)):
      5         wsum+= (i+1.0)*vals[i]

<ipython-input-17-0f96c652a2c2> in read(ain)
----> 1 def read(ain): return ain.read()

/home/ajc/topgear/odroid/topgear/python/mbedrpc.pyc in read(self)
    132 
    133     def read(self):
--> 134         r = self.mbed.rpc(self.name, "read", [])
    135         return float(r)
    136 

/home/ajc/topgear/odroid/topgear/python/mbedrpc.pyc in rpc(self, name, method, args)
     86     def rpc(self, name, method, args):
     87         self.ser.write("/" + name + "/" + method + " " + " ".join(args) + "\n")
---> 88         return self.ser.readline().strip()
     89 
     90 class HTTPRPC(mbed):

/usr/lib/python2.7/dist-packages/serial/serialposix.pyc in read(self, size)
    440         read = bytearray()
    441         while len(read) < size:
--> 442             ready,_,_ = select.select([self.fd],[],[], self._timeout)
    443             # If select was used with a timeout, and the timeout occurs, it
    444             # returns with empty lists -> thus abort read operation.

KeyboardInterrupt: 

In [94]:


In [ ]: