-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserialtest.py
198 lines (131 loc) · 4.1 KB
/
serialtest.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#! /usr/bin/env python
from microkopter import *
from serial.tools import list_ports
serial_usb_port = [port[0] for port in list_ports.grep('usbserial')]
if len(serial_usb_port) == 0:
print "Couldn't find the serial interface. Is it plugged in?"
sys.exit()
# TODO : add this later to handle exceptions
comPort = serial_usb_port[0]
comPort = '/dev/tty.usbserial-A40078OI'
comm = MkComm()
comm.printDebugMsg = True
comm.open(comPort = comPort, timeout = 0.5)
print "Sending FC->NC forwarding"
comm.sendNCRedirectUartFromFC()
# Version message request
# msg = VersionRequestMsg()
# comm.sendMsg(msg)
# returned_msg = comm.waitForMsg(VersionMsg.command)
# print returned_msg
# print
msg = ThreeDDataRequestMsg()
comm.sendMsg(msg)
returned_msg = comm.waitForMsg(ThreeDDataMsg.command)
print returned_msg
print
# Read Pulse Position Modulation (PPM) channels
# Returns s16 PPM-Array[11]
# print 'Reading PPM channels'
# msg = MkMsg(address=MkComm.ADDRESS_FC, cmd='p', data=[])
# comm.sendMsg(msg)
# returned_msg = comm.waitForMsg('P')
# print returned_msg
# Trying to get GPS data
# samplePeriod = 0.05
# nbSamples = 20
# result = []
# comm.serPort.flushInput()
# msg = MkMsg(address=MkComm.ADDRESS_FC, cmd='o', data=[int(samplePeriod*100)])
# comm.sendMsg(msg)
# returned_msg = comm.waitForMsg('O')
# print returned_msg
# samplePeriod = 0.05
# msgs = []
# for k in range(50):
# comm.serPort.flushInput()
# msg = MkMsg(address=MkComm.ADDRESS_FC, cmd='a', data=[k])
# comm.sendMsg(msg)
# returned_msg = comm.waitForMsg('A')
# msgs.append(returned_msg)
# for msg in msgs:
# print msg.data[0], ''.join([chr(c) for c in msg.data[1:]])
# print 'Read waypoints'
# comm.serPort.flushInput()
# msg = MkMsg(address=MkComm.ADDRESS_FC, cmd='x', data=[0])
# comm.sendMsg(msg)
# returned_msg = comm.waitForMsg('X')
# print returned_msg
# All on screen options
# msgs = []
# for k in range(32):
# msg = MkMsg(address=MkComm.ADDRESS_NC, cmd='l', data=[k])
# comm.sendMsg(msg)
# returned_msg = comm.waitForMsg('L')
# msgs.append(returned_msg)
# for msg in msgs:
# for c in msg.data:
# sys.stdout.write(chr(c))
# sys.stdout.write('\n')
# READING A BUNCH OF PARAMS (NC-Parameter)
# msgs = []
# for k in range(256):
# msg = MkMsg(address=MkComm.ADDRESS_NC, cmd='j', data=[0, k])
# comm.sendMsg(msg)
# returned_msg = comm.waitForMsg('J')
# msgs.append(returned_msg)
# for msg in msgs:
# print msg
# msg = MkMsg(address=MkComm.ADDRESS_NC, cmd='x', data=[0])
# comm.sendMsg(msg)
# returned_msg = comm.waitForMsg('X')
# print returned_msg
# samplePeriod = 0.05
# comm.serPort.flushInput()
# msg = MkMsg(address=MkComm.ADDRESS_NC, cmd='l', data=[16])
# comm.sendMsg(msg)
# returned_msg = comm.waitForMsg('L')
# print ''.join([chr(c) for c in returned_msg.data])
# print 'Reading OSD data'
# msg = MkMsg(address=MkComm.ADDRESS_NC, cmd='w', data=[])
# comm.sendMsg(msg)
# returned_msg = comm.waitForMsg('W')
# print returned_msg
# print 'Collecting 1 second of samples'
# samplePeriod = 0.05
# nbSamples = 20
# result = []
# comm.serPort.flushInput()
# msg = MkMsg(address=0, cmd='d', data=[int(samplePeriod*100)])
# comm.sendMsg(msg)
# for i in range(nbSamples):
# returned_msg = comm.waitForMsg('D')
# debug_msg = DebugDataMsg(returned_msg)
# print debug_msg
# result.append(debug_msg)
# print 'Getting settings messages'
# msg = comm.getSettingsMsg()
# print msg
# print 'Requesting BL stuff'
# ret = []
# k = range(256)
# for i in k:
# msg = MkMsg(address=MkComm.ADDRESS_FC, cmd='u', data=[i])
# comm.serPort.flushInput()
# comm.sendMsg(msg)
# returned_msg = comm.waitForMsg('U')
# ret.append( returned_msg )
# for r in ret:
# print r
# msg.setSetting(SettingsMsg.IDX_STICK_P, msg.getSetting(SettingsMsg.IDX_STICK_P)+1)
# comm.sendSettings(msg.getSettings())
# messages = comm.recordDbgMsg(0.05, 20)
# for msg in messages:
# print msg.getAngleRoll()
# except Exception,e:
# print
# print "An error occured: ", e
# print
# traceback.print_exc()
# print
# raw_input("Press ENTER, the application will close")