Skip to content

Instantly share code, notes, and snippets.

@Lauszus
Created February 21, 2019 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lauszus/cfff920b9dd130bdcec4198121e44367 to your computer and use it in GitHub Desktop.
Save Lauszus/cfff920b9dd130bdcec4198121e44367 to your computer and use it in GitHub Desktop.
GPS NMEA string generator
#!/usr/bin/env python
import numpy as np
def main():
def ubx_checksum(data):
ck_a = 0
ck_b = 0
for byte in data:
ck_a = (ck_a + byte) & 0xFF
ck_b = (ck_b + ck_a) & 0xFF
return ck_a, ck_b
# UBX-CFG-RATE (0x06 0x08)
head0 = 0xB5
head1 = 0x62
Class = 0x06
ID = 0x08
length0 = 6
length1 = 0
# 10 Hz
measRate0 = 100 # 100 ms
measRate1 = 0
# One measurement for every sample (default)
navRate0 = 1
navRate1 = 0
# GPS time (default)
timeRef0 = 1
timeRef1 = 0
msg = np.array([Class, ID, length0, length1,
measRate0, measRate1, navRate0, navRate1, timeRef0, timeRef1])
assert((length1 << 8 | length0) == len(msg) - 4)
checksum = ubx_checksum(msg)
# Final message
rate_msg = np.append(np.append([head0, head1], msg), checksum)
print('uint8_t rate_msg[' + str(len(rate_msg)) + '] = { ' + ' '.join('0x{:02X},'.format(x) for x in rate_msg) + ' };')
# Set baudrate:
# gps_str = 'PUBX,41,1,0007,0002,38400,0'
# gps_str = 'PUBX,41,1,0007,0002,57600,0'
gps_str = 'PUBX,41,1,0007,0002,115200,0'
# gps_str = 'PUBX,41,1,0007,0002,230400,0'
# gps_str = 'PUBX,41,1,0007,0002,460800,0'
# inRtcm - RTCM2 protocol (bit 2)
# inNmea - NMEA protocol (bit 1)
# inUbx - UBX protocol (bit 0)
# inProtoMask = 7 (All)
# outNmea - NMEA protocol (bit 1)
# outUbx - UBX protocol (bit 0)
# outProtoMask = 2 (NMEA only)
checksum = 0
for char in gps_str:
checksum ^= ord(char)
print('$' + gps_str + '*' + '{:02X}'.format(checksum) + '\\r\\n')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment