back to Index
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))
In [16]:
for ain in ains:
print ain.read()
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]:
Lets measure the maximum controller frequency (bounded by the polling rate):
In [62]:
%timeit line_pos(ains)
In [63]:
1000/32.7
Out[63]:
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)
In [94]:
In [ ]: