back to Index
reading fieldcontrol messages
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)
nbinclude('netrobot')
nbinclude('simplekernel')
import time, lcm
from python.forseti2 import piemos_cmd
import IPython.display
import sys
import threading
    
In [2]:
    
r=NetRobot()
    
In [3]:
    
r.cmd(1,1)
    
In [4]:
    
r.cmd(1,0)
    
In [5]:
    
r.cmd(0,1)
    
In [6]:
    
r.cmd(0,0)
    
In [7]:
    
class RobotRunner:
    def __init__(self, robot):
        self.robot=robot
        
    def cmd_handler(self, channel, data):
        msg=piemos_cmd.decode(data)
#         IPython.display.clear_output()
#         print('forseti2.piemos_cmd:'
#             + 'channel='
#             + str(channel)
#             + ', header.seq='
#             + str(msg.header.seq)
#             + ', header.time='
#             + str(msg.header.time)
#             + ', auton=' 
#             + str(msg.auton)
#             + ', enabled=' 
#             + str(msg.enabled)
#             + ', is_blue=' 
#             + str(msg.is_blue)
#             + ', game_time=' 
#             + str(msg.game_time))
        if msg.enabled:
            self.robot.enable()
        else:
            self.robot.disable()
#         sys.stdout.flush()
        
    def lcm_run(self):
        lc=lcm.LCM('udpm://239.255.76.67:7667?ttl=1')
        sub=lc.subscribe("piemos0/cmd", self.cmd_handler)
        while True:
            completed=False
            try:
                lc.handle()
                completed=True
            except KeyboardInterrupt as e:
                print e
            yield completed
    
In [8]:
    
xs=np.zeros(shape=[100], dtype=np.float32)
    
In [9]:
    
times=np.zeros(shape=[100], dtype=np.float32)
    
In [10]:
    
import numpy as np
def go():
    while True:
        cmds=np.arange(-.5, .5, .05)
        cmds1=np.arange(.5, -.5, -.05)
        for cmd in cmds:
            r.cmd(cmd, -cmd)
            yield
        for cmd in cmds1:
            r.cmd(cmd, -cmd)
            yield
def modulate():
    while True:
        r.disable()
        yield
        r.enable()
        yield
def time_test():
    i=0
    last_time=time.time()
    while True:
        now=time.time()
        
        xs[i]=i
        times[i]=now - last_time
        i +=1
        last_time=now
        print i
        if i >= 100:
            yield False
        yield
    
In [11]:
    
m1=modulate()
g1=go()
tt=time_test()
    
In [12]:
    
rr=RobotRunner(r)
lcm_run_gen=rr.lcm_run()
    
lcm_run_gen=rr.lcm_run()
In [13]:
    
r.enable()
    
In [14]:
    
next(g1)
    
In [15]:
    
next(lcm_run_gen)
    
    Out[15]:
In [16]:
    
sk=SimpleKernel()
    
In [17]:
    
# sk.add_coroutine(lcm_run_gen, .01)
    
In [18]:
    
# sk.add_coroutine(g1, .1)
    
In [19]:
    
sk.add_coroutine(tt, .01)
    
In [20]:
    
# sk.add_coroutine(m1, .01)
    
In [21]:
    
sk.tick()
    
    
    Out[21]:
In [22]:
    
# next(lcm_run_gen)
    
In [23]:
    
sk.run()
    
    
In [24]:
    
%matplotlib inline
    
In [25]:
    
import matplotlib.pyplot as plt
    
In [26]:
    
plt.plot(xs[14:],times[14:])
    
    Out[26]:
    
In [27]:
    
fft_times = np.fft.fft(times)
    
In [28]:
    
n=len(fft_times)
    
In [29]:
    
plt.plot(np.abs(fft_times))
    
    Out[29]:
    
In [29]: