Page MenuHomec4science

boat
No OneTemporary

File Metadata

Created
Thu, Jun 26, 18:06
#!/usr/bin/env python
#
# Boat simulator
#
# Send RMC position each second
# When receiving RMB from OpenCPN, jump to the WP
#
import threading, socket, time, sys, pynmea2, datetime
# Define start position
lat='4301.000'
latNS='N'
lon='00617.000'
lonEW='E'
def boatThread(nb, nom = ''):
prev_lat = '0'
prev_lon = '0'
while 1:
global lat, lon, quit
if (prev_lat != lat) or (prev_lon != lon) :
quit = 0;
else :
quit += 1
if quit > 20 :
print("Timeout reach: no new RMB received.")
client.close()
sys.exit()
# $GPRMC,165905.56,A,4228.634,N,00610.659,E,6.0,183.0,210816,-nan,E,*1B
msg = pynmea2.RMC( 'GP', 'RMC', (
datetime.datetime.now().strftime('%H%M%S'), # TIMESTAMP hhmmss
'A',
lat,
latNS,
lon,
lonEW,
'5.9', # SOG
'183.0', # Bearing
datetime.datetime.now().strftime('%d%m%y'), # DATE ddmmyy
'0.0', # Magnetic variation
'E'
))
try:
client.send(bytes(str(msg),'ASCII'))
except OSError as errormsg:
print("Error while sending RMC: %s" % errormsg)
sys.exit()
# Sleep 1 second
time.sleep(1)
prev_lat = lat
prev_lon = lon
# $ECRMB,A,0.214,L,001,002,4301.550,N,00617.937,E,0.880,51.213,6.000,V*2A
def fromOpenCPN(sentence):
global lat, lon
try :
msg = pynmea2.parse(sentence)
if msg.sentence_type == 'RMB':
print("Parsed: %s" % msg)
lat = msg.data[5]
latNS = msg.data[6]
lon = msg.data[7]
lonEW = msg.data[8]
print('Changed to '+lat+' '+lon)
else:
print("Ignored: %s" % msg.sentence_type)
except :
print('???')
###### main #####
# Connect to multiplexer
client = socket.socket()
host = socket.gethostname()
client.connect(('0.0.0.0', 39150))
# Starts boat thread (for $GPRMC)
a = threading.Thread(None, boatThread, None, (200,), {'nom':'thread a'})
a.start()
while 1:
try:
resp = client.recv(255).decode()
except:
print
print("Killing boat...")
global quit
quit = 1000;
sys.exit();
if not resp:
break
sentences = resp.splitlines()
for sentence in sentences :
fromOpenCPN(sentence)

Event Timeline