-
Notifications
You must be signed in to change notification settings - Fork 0
/
canreceiver.py
executable file
·137 lines (105 loc) · 4 KB
/
canreceiver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
##############################################################################
#
# SkyLines Tracker is a location tracking client for the SkyLines platform <www.skylines-project.org>.
# Copyright (C) 2019 Andreas Lüthi
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import argparse
import socket
import struct
import can
from skylines import create_fix_message
parser = argparse.ArgumentParser(description='Read position updates from can-bus and sent it to SkyLines')
parser.add_argument('trackingkey', metavar='key', type=lambda x: int(x, 16), help='Your live tracking key')
parser.add_argument('-interval', metavar='interval', type=int, default=5,
help='Tracking interval in seconds, default=5')
parser.add_argument('-channel', metavar='channel', type=str, default='vcan0', help='Canbus, default=can0')
args = parser.parse_args()
tracking_key = args.trackingkey
tracking_interval = args.interval
channel = args.channel
CAN_SFF_MASK = 0x000007FF
HOST_PORT = ("skylines.aero", 5597)
bus = can.interface.Bus(channel=channel, bustype='socketcan')
bus.set_filters(
[{"can_id": 316, "can_mask": CAN_SFF_MASK},
{"can_id": 322, "can_mask": CAN_SFF_MASK},
{"can_id": 354, "can_mask": CAN_SFF_MASK},
{"can_id": 1036, "can_mask": CAN_SFF_MASK},
{"can_id": 1037, "can_mask": CAN_SFF_MASK},
{"can_id": 1039, "can_mask": CAN_SFF_MASK},
{"can_id": 1040, "can_mask": CAN_SFF_MASK},
{"can_id": 1200, "can_mask": CAN_SFF_MASK},
{"can_id": 1506, "can_mask": CAN_SFF_MASK}])
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
def getFloat():
return struct.unpack('>f', canMsg.data[4:8])[0]
def getDoubleL():
return struct.unpack('>l', canMsg.data[4:8])[0] / 1E7
def getUshort():
return struct.unpack('>H', canMsg.data[4:6])[0]
def getChar4():
return struct.unpack('4b', canMsg.data[4:])
last_s = 0
def initParams():
global lat, lon, gs, tt, tas, alt, vario, enl
lat = None
lon = None
gs = None
tt = None
tas = None
alt = None
vario = None
enl = None
initParams()
for canMsg in bus:
if canMsg.arbitration_id == 1200: # UTC
utc = getChar4()
now_s = (utc[0] * 3600) + (utc[1] * 60) + utc[2]
if now_s >= last_s + tracking_interval:
last_s = now_s
skylinesMsg = create_fix_message(
tracking_key,
now_s * 1000,
latitude=lat,
longitude=lon,
track=tt,
ground_speed=gs,
airspeed=tas,
altitude=alt,
vario=vario,
enl=enl)
sock.sendto(skylinesMsg, (HOST_PORT[0], HOST_PORT[1]))
initParams()
elif canMsg.arbitration_id == 316:
tas = getFloat()
elif canMsg.arbitration_id == 322:
alt = getFloat()
elif canMsg.arbitration_id == 354:
vario = getFloat()
elif canMsg.arbitration_id == 1036:
lat = getDoubleL()
elif canMsg.arbitration_id == 1037:
lon = getDoubleL()
elif canMsg.arbitration_id == 1039:
gs = getFloat()
elif canMsg.arbitration_id == 1040:
tt = getFloat()
elif canMsg.arbitration_id == 1506:
enl = getUshort()
else:
print(canMsg.arbitration_id, canMsg.data)