summaryrefslogtreecommitdiff
path: root/recipes-navigation/gpsd/gpsd/gpsd-default
blob: 4c92ea2476bc9e29a4bc758b7edfd83a468ffdf2 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
ENABLED="yes"

# GPSD control socket for sending GPSD commands.
# GPSD_SOCKET="/var/run/gpsd.sock"

GPSD_OPTIONS="-n -D 0" 

# Conduit 0.1 GPS devices
GPS_LINE=/dev/gps0
GPS_PPS=/dev/pps0

# Highest speed permitted by uBlox.
GPS_BAUD=115200

# Use this if you must use a specific pps
# that does not exist prior to gpsd running.
# GPS_DEVICES="/dev/gps1 /dev/pps1"

GPS_FIXFILE=/var/run/gpsfix

# GPSD Json TPV object, mode field.
# See: http://www.catb.org/gpsd/gpsd_json.html
#
# 0 Unknown
# 1 No fix
# 2 2D fix
# 3 3D fix
GPSFIX="3"

# U-Blox defaults to 9600.  If the
# default speed is not set before changing
# the baud rate, this does not work
# except right after a boot when the speed
# is 9600 baud.
#
# 115200 baud is preferred to reduce the CPU
# load.  Also, u-blox mode is preferred to
# nmea, as it is more efficient.
#
# We check for U-Blox first, because
# we do not set the baud rate of other
# GPS types.
# 
# This gpsctl does not always work, because
# the line speed might be wrong, so if we
# fail, we change the baud rate and try again.
#
# There should only be two speeds, 115200
# or 9600.
#
# If the line speed is already 115200, we
# should not need to change the baud rate.
#
function SET_GPS_SPEED {

((maxtries = 20))
if [[ $(cat /run/config/gpstype) == u-blox ]] ; then
    # If the U-Blox is connected via USB, it shows
    # up as using the ACM serial driver.  This greatly
    # simplifies setting up the U-Blox GNSS.
    if [[ $(readlink $GPS_LINE) =~ ^ttyACM ]] ; then
        stty -F $GPS_LINE 115200
        return 0
    fi
    while ((maxtries >= 0)) ; do
        ((maxtries--))
        if ((maxtries % 5 == 0)) ; then
            # Not going well, so reset the GPS.
            logger -s -t 'gpsd info' -p daemon.alert "u-blox is unresponsive, so reset it"
            # reset tty to defaults
            stty -F "$GPS_LINE" '500:5:cbd:8a3b:3:1c:7f:15:4:0:1:0:11:13:1a:0:12:f:17:16:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0' 
            mts-io-sysfs store gnss-reset 0
            usleep 100
            mts-io-sysfs store gnss-reset 1
            sleep 5
        fi  # Reset at maxtries == 5
        
        # Set the baud rate to default speed if having issues
        if ((maxtries < 8)) && ((maxtries % 2 == 1)) ; then
            stty -F $GPS_LINE 9600
        fi
            
        # Set the baud rate.  Works better with ntp at a higher baud rate.
        gpsctlout=$(gpsctl -T 20 -t 'u-blox' -s $GPS_BAUD -b  -f $GPS_LINE 2>&1)
        result=$?
        if [[ $gpsctlout =~ u-blox.*at[[:space:]]115200 ]] ; then
            break
        fi

        if ((result != 0)) ; then
            echo $gpsctlout
            echo 'WARNING: Unable to set GPS serial port speed.'
        else
            # The next line is needed due to a bug in gpsctl.
            # We will go back to the default baud rate if we don't do this step.
            gpsctlout=$(gpsctl -T 2 -f $GPS_LINE 2>&1)
            if [[ $gpsctlout =~ u-blox.*at[[:space:]]115200 ]] ; then
               break
            fi
            stty -F $GPS_LINE $GPS_BAUD
        fi
        speed=$(stty -F $GPS_LINE speed 2>&1)
        if [[ $speed = 115200 ]] ; then
            result=$(gpsctl -T 2 -f $GPS_LINE 2>&1)
            if [[ $result =~ u-blox[[:space:]]at[[:space:]]115200[[:space:]] ]] ; then
                break
            fi # gpsctl has a good status
        fi # gpsd serial port speed is correct
    done # Loop until maxtries

    if ((maxtries == 0)) && [[ -n $result ]] ; then
        logger -s -t 'gpsd info' -p daemon.alert "gpsctl -T 2 -f $GPS_LINE"
        logger -s -t 'gpsd info' -p daemon.alert "$GPS_LINE: $(stty -F $GPS_LINE speed) baud"
        logger -s -t 'gpsd info' -p daemon.alert "$result"
    elif [[ $(mts-io-sysfs show hw-version) =~ ^MTHS- ]] ; then
		 if gpsctl -t 'u-blox' -D 5 -x '\x06\x41\x00\x00\x03\x1F\x90\x47\x4F\xB1\xFF\xFF\xEA\xFF' $GPS_LINE ; then
		    logger -s -t 'gpsd info' -p daemon.info "GPS LNA set high"
		 else
		    logger -s -t 'gpsd info' -p daemon.alert "ERROR: GPS LNA not set"
		 fi
    fi #
fi # If U-Blox GPS type
}