-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnm3example.py
More file actions
517 lines (401 loc) · 16.2 KB
/
nm3example.py
File metadata and controls
517 lines (401 loc) · 16.2 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#! /usr/bin/env python
#
# Example Usage of the Python Driver for NM V3
#
# This file is part of NM3 Python Driver. https://github.com/bensherlock/nm3-python-driver
#
#
# MIT License
#
# Copyright (c) 2019 Benjamin Sherlock <benjamin.sherlock@ncl.ac.uk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
"""Example program for using the Nm3 driver. """
import time
import serial
from nm3driver import Nm3
from nm3driver import MessagePacket
def example_query_status(nm3_modem):
"""Example: $? - Query Status."""
print('Example: Query Status')
addr_int, voltage, version_string, build_date_string = nm3_modem.query_status()
print(' Modem Address={:03d}'.format(addr_int))
print(' Battery Voltage={:.2f}V'.format(voltage))
print(' Version=' + version_string)
print(' Build Date=' + build_date_string)
def example_set_address(nm3_modem, new_address):
"""Example: $A - Set Address."""
print('Example: Set Address')
print(' Query Current Status')
ret = nm3_modem.query_status()
if ret == -1:
print(' Error')
else:
addr_int, voltage, version_string, build_date_string = ret
print(' Initial Modem Address={:03d}'.format(addr_int))
addr_new_int = nm3_modem.set_address(new_address)
print(' Set Modem Address={:03d}'.format(addr_new_int))
print(' Query Current Status')
ret = nm3_modem.query_status()
if ret == -1:
print(' Error')
else:
addr_int, voltage, version_string, build_date_string = ret
print(' Current Modem Address={:03d}'.format(addr_int))
def example_broadcast_data(nm3_modem, message):
"""Example: $B - Broadcast Data."""
print('Example: Broadcast Data')
sent_bytes_count = nm3_modem.send_broadcast_message(message)
if sent_bytes_count == -1:
print(' Error')
else:
# Pause for the modem to finish the transmission
time.sleep(4.0)
print(' Bytes Transmitted={:02d}'.format(sent_bytes_count))
def example_channel_impulse_response(nm3_modem, remote_address, plot_results=False):
"""Example: $C - Channel Impulse Response."""
print('Example: Channel Impulse Response')
print('Magnitudes: Remote Address={:03d}'.format(remote_address))
ret = nm3_modem.send_ping_for_channel_impulse_response(remote_address, 'M')
if ret == -1:
print(' Error')
else:
timeofarrival, data_count, data_values = ret
print(' Time of Arrival={:.6f} seconds'.format(timeofarrival))
print(' Data Count={:04d}'.format(data_count))
if plot_results:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_title("Channel Impulse Response (Magnitude)")
ax.set_xlabel("Delay (s)")
ax.set_ylabel("Magnitude")
ax.grid(True, which='both')
time_values = [(float(b-100) / 16e3) for b in range(data_count)]
ax.plot(time_values, data_values)
plt.show()
print('Complex: Remote Address={:03d}'.format(remote_address))
ret = nm3_modem.send_ping_for_channel_impulse_response(remote_address, 'C')
if ret == -1:
print(' Error')
else:
timeofarrival, data_count, data_values = ret
print(' Time of Arrival={:.6f} seconds'.format(timeofarrival))
print(' Data Count={:04d}'.format(data_count))
real_data_values = data_values[0::2]
imaginary_data_values = data_values[1::2]
if plot_results:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_title("Channel Impulse Response (Complex)")
ax.set_xlabel("Delay (s)")
ax.set_ylabel("Correlation Value")
ax.grid(True, which='both')
time_values = [(float(b-50) / 16e3) for b in range(int(data_count/2))]
ax.plot(time_values, real_data_values, label='Real')
ax.plot(time_values, imaginary_data_values, label='Imaginary')
plt.show()
def example_unicast_data_with_ack(nm3_modem, remote_address, message):
"""Example: $M - Unicast Data with Ack."""
print('Example: Unicast Data with Ack')
ret = nm3_modem.send_unicast_message_with_ack(remote_address, message)
if ret == -1:
print(' Error')
else:
print(' Time of Arrival={:.6f} seconds'.format(ret))
def example_unicast_data(nm3_modem, remote_address, message):
"""Example: $U - Unicast Data."""
print('Example: Unicast Data')
sent_bytes_count = nm3_modem.send_unicast_message(remote_address, message)
if sent_bytes_count == -1:
print(' Error')
else:
# Pause for the modem to finish the transmission
time.sleep(4.0)
print(' Bytes Transmitted={:02d}'.format(sent_bytes_count))
def example_noise_measurement(nm3_modem):
"""Example: $N - Noise Measurement"""
print('Example: Noise Measurement')
ret = nm3_modem.measure_local_ambient_noise()
if ret == -1:
print(' Error')
else:
rms_int, p2p_int, mm_int = ret
print(' RMS={:05d}'.format(rms_int))
print(' P2P={:05d}'.format(p2p_int))
print(' Mean Magnitude={:05d}'.format(mm_int))
def example_ping(nm3_modem, remote_address):
"""Example: $P - Ping"""
print('Example: Ping')
ret = nm3_modem.send_ping(remote_address)
if ret == -1:
print(' Error')
else:
print(' Time of Arrival={:.6f} seconds'.format(ret))
def example_spectrum_measurement(nm3_modem, plot_results=False):
"""Example: $S - Spectrum Measurement"""
print('Example: Spectrum Measurement')
ret = nm3_modem.measure_local_noise_spectrum()
if ret == -1:
print(' Error')
else:
data_count, data_values = ret
print(' Data Count={:04d}'.format(data_count))
if plot_results:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_title("FFT")
ax.set_xlabel("Frequency (Hz)")
ax.set_ylabel("Magnitude")
ax.grid(True, which='both')
bin_values = range(data_count)
fs = 160.0e3
nfft = (data_count-1)*2.0
bin_step_frequency = fs/nfft
bin_frequencies = [ (float(f)*bin_step_frequency) for f in bin_values ]
ax.bar(bin_frequencies, data_values, width=bin_step_frequency, linewidth=0.0)
#ax.plot(bin_frequencies, data_values)
plt.show()
def example_system_timer(nm3_modem):
"""Example: $XT - System Timer"""
print('Example: System Timer')
print(' Get System Timer Status')
ret = nm3_modem.get_system_timer_status()
if ret == -1:
print(' Error')
else:
enabled_flag, time_count_int = ret
if enabled_flag:
print(' Enabled=True')
else:
print(' Enabled=False')
print(' Time Count={:014d}'.format(time_count_int))
print(' Enable System Timer')
ret = nm3_modem.enable_system_timer()
if ret == -1:
print(' Error')
else:
enabled_flag, time_count_int = ret
if enabled_flag:
print(' Enabled=True')
else:
print(' Enabled=False')
print(' Time Count={:014d}'.format(time_count_int))
print(' Get System Timer Status')
ret = nm3_modem.get_system_timer_status()
if ret == -1:
print(' Error')
else:
enabled_flag, time_count_int = ret
if enabled_flag:
print(' Enabled=True')
else:
print(' Enabled=False')
print(' Time Count={:014d}'.format(time_count_int))
print(' Get System Timer Status')
ret = nm3_modem.get_system_timer_status()
if ret == -1:
print(' Error')
else:
enabled_flag, time_count_int = ret
if enabled_flag:
print(' Enabled=True')
else:
print(' Enabled=False')
print(' Time Count={:014d}'.format(time_count_int))
print(' Clear System Timer')
ret = nm3_modem.clear_system_timer()
if ret == -1:
print(' Error')
else:
enabled_flag, time_count_int = ret
if enabled_flag:
print(' Enabled=True')
else:
print(' Enabled=False')
print(' Time Count={:014d}'.format(time_count_int))
print(' Get System Timer Status')
ret = nm3_modem.get_system_timer_status()
if ret == -1:
print(' Error')
else:
enabled_flag, time_count_int = ret
if enabled_flag:
print(' Enabled=True')
else:
print(' Enabled=False')
print(' Time Count={:014d}'.format(time_count_int))
print(' Disable System Timer')
ret = nm3_modem.disable_system_timer()
if ret == -1:
print(' Error')
else:
enabled_flag, time_count_int = ret
if enabled_flag:
print(' Enabled=True')
else:
print(' Enabled=False')
print(' Time Count={:014d}'.format(time_count_int))
print(' Get System Timer Status')
ret = nm3_modem.get_system_timer_status()
if ret == -1:
print(' Error')
else:
enabled_flag, time_count_int = ret
if enabled_flag:
print(' Enabled=True')
else:
print(' Enabled=False')
print(' Time Count={:014d}'.format(time_count_int))
def example_delayed_transmission(nm3_modem, remote_address, message):
"""Example: Delayed Transmission"""
print('Example: Delayed Transmission')
time_count_int = 0
# Enable System Timer
print(' Enable System Timer')
ret = nm3_modem.enable_system_timer()
if ret == -1:
print(' Error')
else:
enabled_flag, time_count_int = ret
if enabled_flag:
print(' Enabled=True')
else:
print(' Enabled=False')
print(' Time Count={:014d}'.format(time_count_int))
print(' Send Broadcast Message')
transmit_time_count_int = time_count_int + 1000000 # 1 second
sent_bytes_count = nm3_modem.send_broadcast_message(message, transmit_time_count_int)
if sent_bytes_count == -1:
print(' Error')
else:
# Pause for the modem to finish the transmission
time.sleep(2.0)
print(' Bytes Transmitted={:02d}'.format(sent_bytes_count))
ret = nm3_modem.get_system_timer_status()
if ret == -1:
print(' Error')
else:
enabled_flag, time_count_int = ret
if enabled_flag:
print(' Enabled=True')
else:
print(' Enabled=False')
print(' Time Count={:014d}'.format(time_count_int))
print(' Send Unicast Message')
transmit_time_count_int = time_count_int + 1000000 # 1 second
sent_bytes_count = nm3_modem.send_unicast_message(remote_address, message, transmit_time_count_int)
if sent_bytes_count == -1:
print(' Error')
else:
# Pause for the modem to finish the transmission
time.sleep(2.0)
print(' Bytes Transmitted={:02d}'.format(sent_bytes_count))
ret = nm3_modem.get_system_timer_status()
if ret == -1:
print(' Error')
else:
enabled_flag, time_count_int = ret
if enabled_flag:
print(' Enabled=True')
else:
print(' Enabled=False')
print(' Time Count={:014d}'.format(time_count_int))
print(' Send Unicast with Ack Message')
transmit_time_count_int = time_count_int + 1000000 # 1 second
ret = nm3_modem.send_unicast_message_with_ack(remote_address, message, 5.0, transmit_time_count_int)
if ret == -1:
print(' Error')
else:
# Pause for the modem to finish the transmission
time.sleep(2.0)
print(' Time of Arrival={:.6f} seconds'.format(ret))
def example_receiver(nm3_modem, remote_address, timeout_seconds):
"""Example: Receiver"""
print('Example: Receiver')
# Enable System Timer
print(' Enable System Timer')
ret = nm3_modem.enable_system_timer()
if ret == -1:
print(' Error')
else:
enabled_flag, time_count_int = ret
if enabled_flag:
print(' Enabled=True')
else:
print(' Enabled=False')
print(' Time Count={:014d}'.format(time_count_int))
# Send Test Message Request
now_time = time.time()
end_time = now_time + timeout_seconds
# Receiving unicast and broadcast messages
while time.time() < end_time:
# Periodically poll the serial port for bytes
nm3_modem.poll_receiver()
# Periodically process any bytes received
nm3_modem.process_incoming_buffer()
# Periodically check for received packets
while nm3_modem.has_received_packet():
message_packet = nm3_modem.get_received_packet()
payload_as_string = bytes(message_packet.packet_payload).decode('utf-8')
print('Received a message packet: ')
print(' serial_string: ' + message_packet.serial_string)
print(' type: ' + MessagePacket.PACKETTYPE_NAMES[message_packet.packet_type])
print(' src: ' + str(message_packet.source_address))
print(' data: ' + payload_as_string)
print(' lqi: ' + str(message_packet.packet_lqi))
print(' doppler: ' + str(message_packet.packet_doppler))
print(' timestamp_count: ' + str(message_packet.packet_timestamp_count))
def main():
"""Main Program Entry."""
## Create the Modem
# Serial Port is opened with a 100ms timeout for reading.
serial_port = serial.Serial('COM8', 9600, 8, serial.PARITY_NONE, serial.STOPBITS_ONE, 0.1)
nm3_modem = Nm3(input_stream=serial_port, output_stream=serial_port)
## Parameters used for the examples
local_address = 255
remote_address = 122
message = b'Hello'
## Run Through Examples
# $? - Query Status
example_query_status(nm3_modem=nm3_modem)
# $A - Set Address
example_set_address(nm3_modem=nm3_modem, new_address=local_address)
# $B - Broadcast Data
example_broadcast_data(nm3_modem=nm3_modem, message=message)
# $C - Channel Impulse Response
example_channel_impulse_response(nm3_modem=nm3_modem, remote_address=remote_address, plot_results=True)
# $N - Ambient Noise Measurement
example_noise_measurement(nm3_modem=nm3_modem)
# $P - Ping
example_ping(nm3_modem=nm3_modem, remote_address=remote_address)
# $S - Spectrum Measurement
example_spectrum_measurement(nm3_modem=nm3_modem, plot_results=True)
# $XT - System Timer
example_system_timer(nm3_modem=nm3_modem)
# Delayed Transmission
example_delayed_transmission(nm3_modem=nm3_modem, remote_address=remote_address, message=message)
# Receiver
example_receiver(nm3_modem=nm3_modem, remote_address=remote_address, timeout_seconds=20)
return
if __name__ == '__main__':
main()