Browse Source

Add ugv_to_ground.py

master
Ryan Nemiroff 6 years ago
parent
commit
ee0ff57484
  1. 2
      tools/requirements.txt
  2. 5
      tools/ugv.py
  3. 7
      tools/ugv_cmd.py
  4. 54
      tools/ugv_to_ground.py

2
tools/requirements.txt

@ -0,0 +1,2 @@ @@ -0,0 +1,2 @@
python-socketio==4.0.1
PyYAML==5.1

5
tools/ugv.py

@ -19,8 +19,9 @@ class UGVComms: @@ -19,8 +19,9 @@ class UGVComms:
MAX_WRITE_RETRY = 5
RETRY_TIME = 3.0
def __init__(self, serial_port: serial.Serial):
def __init__(self, serial_port: serial.Serial, on_msg_received=None):
self.ser = serial_port
self.on_msg_received = on_msg_received
self.msg_acks = []
self.ack_cv = threading.Condition()
self.next_command_id = 1
@ -84,6 +85,8 @@ class UGVComms: @@ -84,6 +85,8 @@ class UGVComms:
if msg is None:
return
log.debug("received UGV message: %s", msg)
if self.on_msg_received:
self.on_msg_received(msg)
if msg.HasField("command_ack"):
with self.ack_cv:
self.msg_acks.append(msg.command_ack)

7
tools/ugv_cmd.py

@ -48,7 +48,8 @@ def cli_cmd(names=None, description=""): @@ -48,7 +48,8 @@ def cli_cmd(names=None, description=""):
class UGV_CLI:
def __init__(self):
def __init__(self, on_msg_received=None):
self.on_msg_received = on_msg_received
self.is_running = False
self.last_state = messages.STATE_IDLE
self.commands = {
@ -137,7 +138,7 @@ class UGV_CLI: @@ -137,7 +138,7 @@ class UGV_CLI:
self.last_status()
@cli_cmd(names=["ping", "p"], description="Ping the UGV")
def get_status(self):
def ping(self):
cmd = messages.GroundCommand()
cmd.type = messages.CMD_PING
self.ugv.write_command(cmd)
@ -172,7 +173,7 @@ class UGV_CLI: @@ -172,7 +173,7 @@ class UGV_CLI:
readline.parse_and_bind("tab: complete")
readline.set_completer(self.complete_command)
self.ugv = UGVComms(ser)
self.ugv = UGVComms(ser, self.on_msg_received)
self.ugv.start()
time.sleep(0.2)
last_line = None

54
tools/ugv_to_ground.py

@ -0,0 +1,54 @@ @@ -0,0 +1,54 @@
#!/usr/bin/env python3
import socketio
import signal
import logging
from base64 import b64decode, b64encode
from ugv_cmd import UGV_CLI
import messages_pb2 as messages
from google.protobuf.message import Message
def encode_msg(msg: Message):
data = msg.SerializeToString()
return b64encode(data)
if __name__ == "__main__":
server_ip = 'localhost'
sio = socketio.Client()
def on_msg_received(msg: messages.UGV_Message):
sio.emit('UGV_MESSAGE', encode_msg(msg), namespace='/ugv')
ugv_cli = UGV_CLI(on_msg_received)
@sio.on('connect', namespace='/ugv')
def on_connect():
print("connected to ground server")
@sio.on('disconnect', namespace='/ugv')
def on_disconnect():
print("disconnected from ground server!")
@sio.on('SET_TARGET', namespace='/ugv')
def set_target(msg):
print("Setting UGV target")
ugv_cli.set_target(msg['lat'], msg['lng'])
@sio.on('DRIVE_TO_TARGET', namespace='/ugv')
def drive_to_target():
print("Driving to target!")
ugv_cli.drive_to_target()
while True:
try:
sio.connect('http://'+server_ip+':8081', namespaces=['/ugv'], transports='websocket')
break
except:
print("Can't connect to ground server. Retrying in 2 seconds...")
sio.sleep(2)
logging.basicConfig(format='%(asctime)s [%(name)s] %(levelname)s: %(message)s', datefmt='%Y-%b-%d %H:%M:%S')
logging.getLogger().setLevel(logging.INFO)
ugv_cli.start()
Loading…
Cancel
Save