#!/usr/bin/env bash

# The goal is tobe able to attach a u-blox NEO-6M gps module via a
# usb->serial converter and change its settings using ubxtools from
# gpsd-clients.
#
# See: https://gpsd.io/ubxtool-examples.html
#
# The minimal binary protocol allows for 10Hz update
#
sudo apt-get install -y gpsd gpsd-tools gpsd-clients

# A u-blox 6 assume protocol version 12
CURRENT_VERSION=$(ubxtool -p MON-VER)
echo "Current Version: ${CURRENT_VERSION}"

export UBXOPTS="-P 12"

# Reset to factory defaults
ubxtool -p RESET

# Enable binary messages
ubxtool -e BINARY

# Disable NMEA
ubxtool -d NMEA

# Rate settings - slow for low power, high for fast to compute fix in dynamic
# environments.

# Current rate:
echo "Before rate: $(ubxtool -p CFG_RATE)"

# Change to 10Hz measurement and fix rate (in milliseconds)
ubxtool -p CFG-RATE,100

# New rate:
echo "After rate: $(ubxtool -p CFG_RATE)"

# Check constellations (GPS only for 7 and below)
ubxtool -p MON_GNSS

# https://gist.githubusercontent.com/jnunyez/519f3f1a807d0f2b4dc8bd307e11c066/raw/003927f12a5ec4378b98e66b1dec392fe7129b3f/useful-commands-environment

# grep ubxtool /tmp/useful-commands-environment.txt
#
# ubxtool -g CFG-TMODE-SVIN_MIN_DUR -P 29.20
# ubxtool -t -p TIM-SVIN -P 29.20
# ubxtool -t -p NAV-CLOCK -P 29.20
# ubxtool -t -p TIM-SVIN -P 29.20
# ubxtool -t -P 29.20 -e SURVEYIN,86400,1000
(#ubxtool -t -p TIM-SVIN -P 29.20) meanV should decrease over time
# ubxtool -v 1 -P 29.20 -z CFG-TMODE-ECEF_X,<value>,5 -w 1
# ubxtool -v 1 -P 29.20 -z CFG-TMODE-ECEF_Y,<value>,5 -w 1
# ubxtool -v 1 -P 29.20 -z CFG-TMODE-ECEF_Z,<value>,5 -w 1
# ubxtool -v 1 -P 29.20 -z CFG-TMODE-MODE,2,5 -v 1 -w 1
# ubxtool -v 1 -P 29.20 -g CFG-TMODE-MODE,0 -v 1
# ubxtool -v 1 -P 29.20 -g CFG-TMODE-ECEF_X,2 -v 1
# ubxtool -v 1 -P 29.20 -g CFG-TMODE-ECEF_Y,2 -v 1
# ubxtool -v 1 -P 29.20 -g CFG-TMODE-ECEF_Z,2 -v 1
# ubxtool -P 29.20 -w 1 -v 3 -z CFG-NAVSPG-INFIL_NCNOTHRS,50,1
# ubxtool -P 29.20 -w 1 -v 3 -z CFG-NAVSPG-INFIL_NCNOTHRS,0,1
# ubxtool -g CFG-TP-ANT_CABLEDELAY -P 29.20
# ubxtool -v 1  -P 29.20 -z CFG-HW-ANT_CFG_VOLTCTRL,1
# ubxtool -v 1 -P 29.20 -p MON-RF
# ubxtool -p CFG-GNSS
# ubxtool -g CFG-TMODE-SVIN_MIN_DUR -P 29.20
# ubxtool -t -p NAV-CLOCK -P 29.20
# ubxtool -t -P 29.20 -v 1 -e SURVEYIN,60,20000
# ubxtool: enable SURVEYIN,60,20000
# ubxtool -t -p TIM-SVIN -P 29.20
# ubxtool -t -p NAV-STATUS -P 29.20
# ubxtool -p CFG-TP5
