brix5 / applications / sliposc / serialtoosc.py @ 4d6aa081
History | View | Annotate | Download (3.704 KB)
1 |
#!/usr/bin/env python3
|
---|---|
2 |
#vim:ts=2:sw=2:expandtab:autoindent
|
3 |
#v 0.1
|
4 |
#dumb router
|
5 |
#echos serial from serial port to a udp socket
|
6 |
#
|
7 |
# BUG: Corrupted messages, even crash sclang
|
8 |
# [ /sensors/, ms, 19300, eX, -7.875, eY, 16, eZ, 0, aX, 2.7000000476837, aY, 9.3199996948242, aZ, -1.3700000047684, gX, 0, gY, 0, gZ, 0.0625, calSys, 0, calAcc, 3, calGyr, 1, calMag, 0, Pa, 94696.84375 ]
|
9 |
#nil
|
10 |
#acc -2/-2/-2 gyro -2/-2/-2 euler -2/-2/-2
|
11 |
#bnoX/Y 0.05 0.05 -1.5 0.02 bnoX/Y 0.05 0.05 -1.5 0.02 [ /sensors/, ms, 20300, eX, -7.875, eY, 7.1728720664978, ��V��, 0, @0��, 4.286162823218e-39, �, 1.4349296274686e-42, , 2.7451219558716, �, 5.0638562171084e+19, �, 5.0638562171084e+19, �, 2.3509887016446e-38, , 8.00048828125, , 2.7292528152466, �, 7.1746481373431e-43, , 0, , 9.9746769275256e-15 ]
|
12 |
#nil
|
13 |
#acc -2/-2/-2 gyro -2/-2/-2 euler -2/-2/-2
|
14 |
#bnoX/Y 0.05 0.05 -1.5 0.02 [ /sensors/, ms, 26300, eX, -7.875, eY, 16, eZ, 0, aX, 2.6900000572205, aY, 9.3599996566772, aZ, -1.3600000143051, gX, -0.0625, gY, -0.125, gZ, 0, calSys, 0, calAcc, 3, calGyr, 1, calMag, 0, Pa, 94695.546875 ]
|
15 |
#nil
|
16 |
#acc -2/-2/-2 gyro -2/-2/-2 euler -2/-2/-2
|
17 |
#bnoX/Y 0.05 0.05 -1.5 0.02 ERROR: OSC messages must have type tags. lSys
|
18 |
#[ /sensors/, ms, 29300, eX, -7.875, eY, 16, eZ, 0, aX, 2.6600000858307, aY, 9.3500003814697, aZ, -1.3400000333786, gX, 0, gY, -0.0625, gZ, 0, calSys, 0, calAcc, 3, calGyr, 1, calMag, 0, Pa, 94696.6875 ]
|
19 |
#nil
|
20 |
#acc -2/-2/-2 gyro -2/-2/-2 euler -2/-2/-2
|
21 |
#bnoX/Y 0.05 0.05 -1.5 0.02 [ /sensors/, ms, 30300, eX, -7.875, eY, 7.1728839874268, ��[��, 0, @0��, 4.286162823218e-39, �, 1.4349296274686e-42, , 2.7451219558716, �, 5.0638562171084e+19, �, 5.0638562171084e+19, �, 2.3509887016446e-38, , 8.00048828125, , 2.7292528152466, �, 7.1746481373431e-43, , 0, , 9.9746769275256e-15 ]
|
22 |
#nil
|
23 |
#acc -2/-2/-2 gyro -2/-2/-2 euler -2/-2/-2
|
24 |
#bnoX/Y 0.05 0.05 -1.5 0.02 [ /sensors/, ms, 33290, eX, -7.875, eY, 16, eZ, 0, aX, 2.6900000572205, aY, 9.3599996566772, aZ, -1.3500000238419, gX, 0, gY, 0.125, gZ, 0.125, calSys, 0, calAcc, 3, calGyr, 1, calMag, 0, Pa, 94694.421875 ]
|
25 |
#nil
|
26 |
#
|
27 |
#TODO: OSC client as well
|
28 |
#TODO: OSCQuery everything
|
29 |
from IPython import embed |
30 |
|
31 |
# local packages
|
32 |
import os, sys |
33 |
path = os.path.realpath(__file__).split('/')
|
34 |
package_path = path[:-1]
|
35 |
#package_path.append('python-packages')
|
36 |
package_path.append('SlipLib/src')
|
37 |
sys.path.append('/'.join(package_path))
|
38 |
|
39 |
import serial |
40 |
import sliplib |
41 |
import socket |
42 |
import socketserver |
43 |
|
44 |
from time import sleep |
45 |
#import threading
|
46 |
|
47 |
DEBUG = False
|
48 |
|
49 |
if(len(sys.argv) > 1 and sys.argv[1] == '-v'): |
50 |
DEBUG = True
|
51 |
|
52 |
TARGET_HOST="127.0.0.1"
|
53 |
TARGET_PORTS=[1234, 57120, 57121, 57122]; |
54 |
TARGET_PORT=1234
|
55 |
|
56 |
#RECV_PORT=9999
|
57 |
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) |
58 |
#sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # default socket
|
59 |
#slipsocket = sliplib.SlipSocket(sock)
|
60 |
#slipsocket.create_connection((TARGET_HOST, TARGET_PORT))
|
61 |
|
62 |
#TODO:autodetect serialport
|
63 |
ser = serial.Serial('/dev/ttyACM0',9600) |
64 |
drv = sliplib.Driver() |
65 |
messages = [] |
66 |
|
67 |
#embed()
|
68 |
while True: |
69 |
# slipsocket.driver
|
70 |
#TODO: handle drv errors on improper packet
|
71 |
messages = drv.receive(ser.read(ser.inWaiting())) |
72 |
|
73 |
if DEBUG:
|
74 |
print(len(messages), " messages in read") |
75 |
|
76 |
# embed()
|
77 |
for msg in messages: |
78 |
if DEBUG:
|
79 |
print(len, msg)
|
80 |
try:
|
81 |
if not sliplib.is_valid(msg): |
82 |
for port in TARGET_PORTS: |
83 |
sock.sendto(msg, (TARGET_HOST, port)) |
84 |
|
85 |
except sliplib.ProtocolError:
|
86 |
if DEBUG:
|
87 |
print ("Invalid Packet received") |
88 |
|
89 |
print("---");
|
90 |
sleep(0.1)
|
91 |
slipsocket.close() |
92 |
sock.close() |
93 |
|
94 |
#import subprocess, shlex
|
95 |
#sc_port_cmd = "lsof -p `pidof sclang`|grep UDP|awk '{ print $9}'|cut -d: -f2"
|
96 |
#sp = subprocess.Popen(sc_port_cmd, stdout=subprocess.PIPE, shell=True)
|
97 |
#print(sp.returncode)
|
98 |
|