gps: test_gps

File test_gps, 6.9 KB (added by Ryan Erbstoesser, 7 years ago)
Line 
1#!/bin/sh
2#
3# see http://aprs.gids.nl/nmea
4
5# DEV=${1:-/dev/ttymxc0}
6DEV=${1:-/dev/ttymxc4}
7BAUD=${2:-4800} #might need to go to 9600?
8
9[ $VERBOSE ] || VERBOSE=0
10
11#Estimated Moving Avgerage: EMA = EMA * (1 – APLHA) + NEWAVG * ALPHA
12#Variables for calculating moving averages:
13#SRN_EMA: EMA for sattellite signal strength
14#SAT_EMA: EMA for number of visible sattellites
15SRN_AVG=0 #srn_sum / srn_cnt
16ALPHA=$(echo ${3:-10} | awk '{alph = 1/$1; print alph}') #default of last 10
17
18echo "Device:$DEV"
19echo "Baud :$BAUD"
20
21stty -F $DEV $BAUD cs8
22
23debug() {
24 [ $VERBOSE -ge $1 ] && echo $2
25}
26
27# GPS DOP and active satellites
28gsa() {
29 local i x sats type fix pdop hdop vdop
30
31 #debug 1 "gsa: $1"
32 oIFS=${IFS}
33 IFS=','
34 i=0
35 sats=
36 for x in $1; do
37 case $i in
38 1) type=$x;;
39 2) fix=$x;;
40 3) sats="$sats$x ";;
41 4) sats="$sats$x ";;
42 5) sats="$sats$x ";;
43 6) sats="$sats$x ";;
44 7) sats="$sats$x ";;
45 8) sats="$sats$x ";;
46 9) sats="$sats$x ";;
47 10) sats="$sats$x ";;
48 11) sats="$sats$x ";;
49 12) sats="$sats$x ";;
50 13) sats="$sats$x ";;
51 14) sats="$sats$x ";;
52 15) pdop=$x;;
53 16) hdop=$x;;
54 17) vdop=$x;;
55 esac
56 i=$((i+1))
57 done
58
59 case $type in
60 M) type=manual;;
61 A) type=automatic;;
62 esac
63 case $fix in
64 2) fix=2;; #2D
65 3) fix=3;; #3D
66 esac
67
68 [ ${fix} -ne 1 ] && echo "GSA: $fix $type $sats"
69 IFS=${oIFS}
70}
71
72# Fix data
73gga() {
74 local i x time lat lon type svs prec alt altm
75
76 #debug 1 "gga: $1"
77 oIFS=${IFS}
78 IFS=','
79 i=0
80 for x in $1; do
81 case $i in
82 1) time=$x;;
83 2) lat=$x;;
84 3) lat=$lat$x;;
85 4) lon=$x;;
86 5) lon=$lon$x;;
87 6) type=$x;;
88 7) svs=$x;;
89 8) prec=$x;;
90 9) alt=$x;;
91 esac
92 i=$((i+1))
93 done
94
95 case $type in
96 1) type=1;; #GPS
97 2) type=2;; #DGPS
98 esac
99
100 [ ${type} -ne 0 ] && echo "$qual Fix: $time: $svs $lat/$lon/$alt"
101 IFS=${oIFS}
102}
103
104gsv() {
105 local i x s msgtot msgnum inview crc line
106 local prn1 elev1 az1 snr1
107 local prn2 elev2 az2 snr2
108 local prn3 elev3 az3 snr3
109 local prn4 elev4 az4 snr4
110
111 line=${1%%\**}
112 oIFS=${IFS}
113 IFS=','
114 i=0
115 for x in $line; do
116 case $i in
117 1) msgtot=$x;;
118 2) msgnum=$x
119 [ $msgnum -eq 1 ] && {
120 echo "GPS DATA BEGINS:"
121 srn_cnt=0
122 srn_sum=0
123 sat_cnt=0
124 }
125 ;;
126 3) inview=$x;;
127 4) prn1=$x;;
128 5) elev1=$x;;
129 6) sz1=$x;;
130 7) snr1=$x;;
131 8) prn2=$x;;
132 9) elev2=$x;;
133 10) sz2=$x;;
134 11) snr2=$x;;
135 12) prn3=$x;;
136 13) elev3=$x;;
137 14) sz3=$x;;
138 15) snr3=$x;;
139 16) prn4=$x;;
140 17) elev4=$x;;
141 18) sz4=$x;;
142 19) snr4=${x%%\**}; crc=${x##*\*};;
143 esac
144 case $i in
145 7|11|15|19)
146 [ $i -eq 19 ] && x=$snr4
147 #echo "i is $i, X is $x"
148 [ "$x" ] && [ $x -gt 0 ] && {
149 srn_cnt=$(( srn_cnt + 1 ))
150 srn_sum=$(( srn_sum + x ))
151 };;
152 esac
153 i=$((i+1))
154 done
155
156
157 s=$(( ((msgnum-1)) *4))
158 echo "Message $msgnum of $msgtot"
159 debug 1 "gsv: $1"
160 [ "$snr1" -o "$snr2" -o "$snr3" -o "$snr4" ] || echo "-- No Satellites --"
161 # [ "$snr1" -o "$snr2" -o "$snr3" -o "$snr4" ] && echo "----BEGIN GPS DATA----"
162 [ "$snr1" ] && printf "%02d:%s:%s:%s:%s\n" $((s+1)) $prn1 $elev1 $sz1 $snr1 && sat_cnt=$((sat_cnt+1))
163 [ "$snr2" ] && printf "%02d:%s:%s:%s:%s\n" $((s+2)) $prn2 $elev2 $sz2 $snr2 && sat_cnt=$((sat_cnt+1))
164 [ "$snr3" ] && printf "%02d:%s:%s:%s:%s\n" $((s+3)) $prn3 $elev3 $sz3 $snr3 && sat_cnt=$((sat_cnt+1))
165 [ "$snr4" ] && printf "%02d:%s:%s:%s:%s\n" $((s+4)) $prn4 $elev4 $sz4 $snr4 && sat_cnt=$((sat_cnt+1))
166 # [ "$snr1" -o "$snr2" -o "$snr3" -o "$snr4" ] && echo "-----END GPS DATA-----"
167
168 [ $msgnum -eq $msgtot ] && {
169 # echo "srn_cnt is $srn_cnt"
170 [ "$srn_cnt" -eq 0 ] && SRN_AVG=0
171 [ "$srn_cnt" -ne 0 ] && SRN_AVG=$(echo "$srn_sum $srn_cnt" | awk '{avg = $1/$2; print avg}')
172 [ "$SRN_EMA" ] || SRN_EMA=$SRN_AVG
173 SRN_EMA=$(echo "$SRN_AVG $SRN_EMA $ALPHA" | awk '{EMA = $2 * (1 - $3) + $1 * $3; print EMA}')
174 [ "$SAT_EMA" ] || SAT_EMA=$srn_cnt
175 SAT_EMA=$(echo "$srn_cnt $SAT_EMA $ALPHA" | awk '{EMA = $2 * (1 - $3) + $1 * $3; print EMA}')
176 printf "SRN average: %s, Visible Satellite Average: %s,\
177 Current Satellites in view: %s\n\n" $SRN_EMA $SAT_EMA $sat_cnt
178 }
179
180 IFS=${oIFS}
181}
182
183oIFS=${IFS}
184
185while [ 1 ]; do
186 IFS='' read -r line
187 debug 2 "${#line}: $line"
188
189 # use case statement for easy regex
190 case "$line" in
191 *GSV,*) gsv "$line";;
192 *GSA,*) gsa "$line";;
193 *GGA,*) gga "$line";;
194 esac
195done < $DEV
196
197IFS=$oIFS
198
199########### END ##############
200
201
202### from OSP to NMEA script
203# #!/bin/sh
204
205# # Make sure we talk to the device at its current configured data format
206# # (assuming power-on default of 4800 8N1)
207# stty -F /dev/gpsdevice 4800 cs8
208
209# # change to mode=0 (OSP/SiRF binary) 9600bd 8N1
210# echo "\$PSRF100,0,9600,8,1,0*0C" > /dev/gpsdevice
211# echo "\$PSRF100,1,9600,8,1,0*0D" > /dev/gpsdevice
212
213# # set new tty baudrate for future communication
214# stty -F /dev/gpsdevice 9600
215
216## changing baud rate of NMEA
217#stty -F /dev/ttymxc4 4800 cs8
218#echo "\$PSRF100,1,9600,8,1,0*0D" > /dev/ttymxc4
219#stty -F /dev/ttymxc4 9600