Statistics
| Branch: | Revision:

brix5 / applications / sliposc / serialtoosc.py @ 4d6aa081

History | View | Annotate | Download (3.704 KB)

1 5ef3477f Jan
#!/usr/bin/env python3
2 edc6c2e7 Jan
#vim:ts=2:sw=2:expandtab:autoindent
3 5ef3477f Jan
#v 0.1
4
#dumb router
5
#echos serial from serial port to a udp socket
6
#
7 4d6aa081 Jan
# 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 5ef3477f Jan
#TODO: OSC client as well
28
#TODO: OSCQuery everything
29 d83f65fc Jan
from IPython import embed
30 5ef3477f Jan
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 d83f65fc Jan
import sliplib
41 5ef3477f Jan
import socket
42
import socketserver
43 d83f65fc Jan
44 5ef3477f Jan
from time import sleep
45 d83f65fc Jan
#import threading
46 5ef3477f Jan
47 edc6c2e7 Jan
DEBUG = False
48
49
if(len(sys.argv) > 1 and sys.argv[1] == '-v'):
50
    DEBUG = True
51
52 d83f65fc Jan
TARGET_HOST="127.0.0.1"
53
TARGET_PORTS=[1234, 57120, 57121, 57122];
54
TARGET_PORT=1234
55 edc6c2e7 Jan
56 5ef3477f Jan
#RECV_PORT=9999
57
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
58 d83f65fc Jan
#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 5ef3477f Jan
62
#TODO:autodetect serialport
63
ser = serial.Serial('/dev/ttyACM0',9600)
64 edc6c2e7 Jan
drv = sliplib.Driver()
65 d83f65fc Jan
messages = []
66 5ef3477f Jan
67 d83f65fc Jan
#embed()
68 5ef3477f Jan
while True:
69 d83f65fc Jan
#  slipsocket.driver
70 5ef3477f Jan
  #TODO: handle drv errors on improper packet
71
  messages = drv.receive(ser.read(ser.inWaiting()))
72 edc6c2e7 Jan
73
  if DEBUG:
74
    print(len(messages), " messages in read")
75
76 d83f65fc Jan
#  embed()
77 5ef3477f Jan
  for msg in messages:
78 d83f65fc Jan
    if DEBUG:
79
      print(len, msg)
80 edc6c2e7 Jan
    try:
81 d83f65fc Jan
      if not sliplib.is_valid(msg):
82
        for port in TARGET_PORTS:
83
          sock.sendto(msg, (TARGET_HOST, port))
84
85 edc6c2e7 Jan
    except sliplib.ProtocolError:
86
      if DEBUG:
87
        print ("Invalid Packet received")
88
89
  print("---");
90 d83f65fc Jan
  sleep(0.1)
91
slipsocket.close()
92 5ef3477f Jan
sock.close()
93 d83f65fc Jan
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)