-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
100 lines (73 loc) · 2.6 KB
/
client.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
"""
Simple demonstration of Mavlink communication using Pymavlink.
Gives a simple example of a Python thread which sends heartbeats to the server.
NOTE: Launch AFTER `server.py`
October 12th 2023
Eric Roth
"""
import os
import time
from threading import Thread
from pymavlink import mavutil
import pymavlink.dialects.v20.ardupilotmega as dialect
import keyboard_handler
# Set Mavlink2 as our default
os.environ['MAVLINK20'] = '1'
# File constants
HEARTBEAT_INTERVAL = 1
def heartbeat(client: mavutil.mavfile):
"""
Sends 1 [s] spaced heartbeats to the vehicle
"""
mav: dialect.MAVLink = client.mav
last_beat = 0
# 1 [s] loop
while True:
loop_time = time.perf_counter()
# Check to make sure it's actually time to send
if loop_time - last_beat >= HEARTBEAT_INTERVAL:
mav.heartbeat_send(dialect.MAV_TYPE_SUBMARINE, dialect.MAV_AUTOPILOT_INVALID, 0, 0, 0)
last_beat = time.perf_counter()
# Sleep until next heartbeat
time.sleep(HEARTBEAT_INTERVAL - (last_beat - loop_time))
def get_new_messages(client: mavutil.mavfile):
"""
Clears out the message buffer and returns a list of messages
"""
msgs = []
while True:
msg: dialect.MAVLink_message = client.recv_msg()
if msg is None:
break
msgs.append(msg)
return msgs
def main():
print("Establishing connection to the server...")
# Connect to the server on localhost port 14550
client: mavutil.mavfile = mavutil.mavlink_connection('tcp:127.0.0.1:14550', dialect="ardupilotmega", autoreconnect=True)
print("Connected!")
# Set up the heartbeat thread
heartbeat_thread = Thread(target=heartbeat, daemon=True, args=(client,))
heartbeat_thread.start()
# Set up the keyboard handler
keyboard_handler.initialize_keyboard()
# Client loop
print("Press [`] to quit the client!")
while True:
# Check any incoming messages
new_msgs = get_new_messages(client)
# Print any new messages to the terminal
for msg in new_msgs:
msg: dialect.MAVLink_message # Type hinting
print(f"Received message of type {msg.get_type()}...")
print(msg)
# Check any keyboard input
quit = keyboard_handler.get_quit()
if quit:
break
# Shut down the keyboard
keyboard_handler.destroy_keyboard()
# No need to join the heartbeat thread as we declared it a daemon thread earlier (i.e. it stops running when the main thread stops)
print("Shutting down...")
if __name__ == "__main__":
main()