Commit d0fce5bc authored by Martin Karlsson's avatar Martin Karlsson
Browse files

Added python scripts for receiving data from phone.

parent 91abe89b
#!/usr/bin/python
# Auto generated labcomm_data_format_lc
import labcomm
class script2extctrl(object):
signature = labcomm.sample('script2extctrl',
labcomm.struct([
('data', labcomm.array([3],
labcomm.FLOAT())),
('scalar', labcomm.DOUBLE())]))
class extctrl2script(object):
signature = labcomm.sample('extctrl2script',
labcomm.struct([
('data', labcomm.array([3],
labcomm.FLOAT())),
('scalar', labcomm.DOUBLE())]))
#!/bin/sh
PYTHONPATH=/opt/robot/ python testPhone.py $@
#!/bin/sh
PYTHONPATH=/opt/robot/ python test_phone_acc_gyro.py $@
#!/usr/bin/env python
'''
orca_labcomm_basic.py --- Created by Patrik Cairen, LTH, April 2014
call e.g., with:
./start_bolc turing 2000
where 'start_bolc' mainly sets up the PYTHON_PATH and passes on the
arugments to 'orca_labcomm_basic.py'
PYTHONPATH=/opt/robot/ python orca_labcomm_basic.py $@
'''
import threading
from orca.connection import writer
import labcomm_data_format_lc
import time
import sys
import labcomm
import orca
import time
import socket
class orca_data_struct(object):
'''
Variables in this class needs to match the layout of your struct in
the .lc-file.
This is to match the notation normally used by structs, e.g.,
orca_data_struct.scalar
orca_data_struct.data[3]
'''
def __init__(self, scalar, data):
self.scalar = scalar
self.data = data
class bolc( threading.Thread ):
'''
'''
def __init__(self, args=None):
threading.Thread.__init__(self)
self.host_addr = args[0]
self.host_port = int(args[1])
self.encoder = None
self.decoder = None
self._stop = threading.Event()
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.bind(("", 12345))
self.sSend = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# threading.Thread(target=self.print_time).start()
def setup_orca_and_labcomm(self):
'''
'''
try:
o = orca.connection(self.host_addr, self.host_port)
s2 = o.open_and_select(['script2extctrl'], 1, o._directory.input, o._select_input)
self.encoder = labcomm.Encoder(writer(s2))
self.encoder.add_decl(labcomm_data_format_lc.script2extctrl.signature)
self.decoder = o.open_output(['extctrl2script'])
return True
except:
return False
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
'''
Creats a dummy-data struct 'data'
'''
print "Connecting to: {host}/{port}".format(host=self.host_addr,port=self.host_port)
self.orca_conn_successfull = self.setup_orca_and_labcomm()
i = 0
count = 0;
#print "{connection}".format(connection=self.orca_conn_successfull)
count = 0;
# MAIN LOOP
while not self.stopped():
if self.orca_conn_successfull:
print "starting"
#if True:
dataFromPhone = self.s.recvfrom(1024) # buffer size is 1024 bytes
dataFromPhone=dataFromPhone[0]
lista=dataFromPhone.split(' ')
print lista
#for i in range(len(lista)):
# if lista[i]=='m':
# lista[i]='-';
# if lista[i]==',':
# lista[i]=' ';
#
# dataFromPhone="".join(lista)
# dataFromPhone=' '.join(dataFromPhone.split(','))
#
# dataFromPhone=dataFromPhone.split();
# #dataFromPhonePrevious=dataFromPhone;
#try:
# for x in range(0,6):
# #if dataFromPhone[x][1]=='-'||dataFromPhone[x][1]=='+'
#
# dataFromPhone[x]=float(dataFromPhone[x]);
# #dataFromPhonePrevious[x]=dataFromPhone[x];
#
#
# dataFromPhonePrevious=dataFromPhone;
#except:
# dataFromPhone=dataFromPhonePrevious;
#count = count + 1;
#print count
data2ext = orca_data_struct(scalar=2.5, data=[float(lista[2]),float(lista[1]),float(lista[0])])
#data = orca_data_struct(scalar=2.5,data=[1.25,2.2*(i%3),3.75])
self.encoder.encode(data2ext, labcomm_data_format_lc.script2extctrl.signature)
#(recv,_) = self.decoder.decode() # blocking, returning data +sender-IP (discarded with _)
# type for recv defined from .lc-file
#if i % 100 == 0 and recv <> None: # NOTE: returns None at first sample
# s2 = "input: {data:<20} output: {scalar}\r".format(data=data.data, scalar=recv.scalar)
# sys.stdout.write(s2), sys.stdout.flush() # NOTE: force flush of printout to be sure
#i+=1
else:
print "err: connection failed!"
self.stop()
if __name__ == "__main__":
b = bolc(sys.argv[1:]) # exclude own name and _init_
b.start() # run-method
while not b.stopped():
try:
time.sleep(0.1)
except KeyboardInterrupt:
b.stop()
sys.exit(0)
#!/usr/bin/env python
'''
orca_labcomm_basic.py --- Created by Patrik Cairen, LTH, April 2014
call e.g., with:
./start_bolc turing 2000
where 'start_bolc' mainly sets up the PYTHON_PATH and passes on the
arugments to 'orca_labcomm_basic.py'
PYTHONPATH=/opt/robot/ python orca_labcomm_basic.py $@
'''
import threading
from orca.connection import writer
import mt_imu
import time
import sys
import labcomm
import orca
import time
import socket
class orca_data_struct(object):
'''
Variables in this class needs to match the layout of your struct in
the .lc-file.
This is to match the notation normally used by structs, e.g.,
orca_data_struct.scalar
orca_data_struct.data[3]
'''
def __init__(self, scalar, data):
self.scalar = scalar
self.data = data
class bolc( threading.Thread ):
'''
'''
def __init__(self, args=None):
threading.Thread.__init__(self)
self.host_addr = args[0]
self.host_port = int(args[1])
self.encoder = None
self.decoder = None
self._stop = threading.Event()
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.bind(("", 12345))
self.sSend = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# threading.Thread(target=self.print_time).start()
def setup_orca_and_labcomm(self):
'''
'''
try:
o = orca.connection(self.host_addr, self.host_port)
s2 = o.open_and_select(['acc_gyro'], 1, o._directory.input, o._select_input)
self.encoder = labcomm.Encoder(writer(s2))
self.encoder.add_decl(mt_imu.acc_gyro.signature)
#self.decoder = o.open_output(['acc_gyro'])
return True
except:
return False
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
'''
Creats a dummy-data struct 'data'
'''
print "Connecting to: {host}/{port}".format(host=self.host_addr,port=self.host_port)
self.orca_conn_successfull = self.setup_orca_and_labcomm()
i = 0
count = 0;
#print "{connection}".format(connection=self.orca_conn_successfull)
count = 0;
# MAIN LOOP
while not self.stopped():
if self.orca_conn_successfull:
print "starting"
#if True:
dataFromPhone = self.s.recvfrom(1024) # buffer size is 1024 bytes
dataFromPhone=dataFromPhone[0]
lista=dataFromPhone.split(' ')
#print lista
#for i in range(len(lista)):
# if lista[i]=='m':
# lista[i]='-';
# if lista[i]==',':
# lista[i]=' ';
#
# dataFromPhone="".join(lista)
# dataFromPhone=' '.join(dataFromPhone.split(','))
#
# dataFromPhone=dataFromPhone.split();
# #dataFromPhonePrevious=dataFromPhone;
#try:
# for x in range(0,6):
# #if dataFromPhone[x][1]=='-'||dataFromPhone[x][1]=='+'
#
# dataFromPhone[x]=float(dataFromPhone[x]);
# #dataFromPhonePrevious[x]=dataFromPhone[x];
#
#
# dataFromPhonePrevious=dataFromPhone;
#except:
# dataFromPhone=dataFromPhonePrevious;
#count = count + 1;
#print count
data2ext = orca_data_struct(scalar=2.5, data=[float(lista[1]),float(lista[2]),float(lista[3]),float(lista[4]),float(lista[5]),float(lista[6]),float(lista[6]),float(lista[6]),float(lista[6])])
#print data2ext.data
#data = orca_data_struct(scalar=2.5,data=[1.25,2.2*(i%3),3.75])
self.encoder.encode(data2ext.data, mt_imu.acc_gyro.signature)
#(recv,_) = self.decoder.decode() # blocking, returning data +sender-IP (discarded with _)
# type for recv defined from .lc-file
#if i % 100 == 0 and recv <> None: # NOTE: returns None at first sample
# s2 = "input: {data:<20} output: {scalar}\r".format(data=data.data, scalar=recv.scalar)
# sys.stdout.write(s2), sys.stdout.flush() # NOTE: force flush of printout to be sure
#i+=1
else:
print "err: connection failed!"
self.stop()
if __name__ == "__main__":
b = bolc(sys.argv[1:]) # exclude own name and _init_
b.start() # run-method
while not b.stopped():
try:
time.sleep(0.1)
except KeyboardInterrupt:
b.stop()
sys.exit(0)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment