initial commit
[ETG_Helmet] / SOFTWARE / OMEGA2 / gps.sh
1 #/bin/bash
2
3 if [ "$TMPFS" == "" ]; then 
4         . "common.sh"
5 fi
6
7 echo $$ > $PID/$0
8
9 echo "---------- START GPS -------------"
10
11 GPSenable
12
13 : > $CURRENTGPS
14
15 prevGPS=''
16 PREVGPSTMP=''
17
18 prevtimeGPSmess=0
19 prevtime=0
20 prevtimeNav=0
21
22 while [ 1 ]; do
23
24 #       nmea=$(str=''; read -t 3 str < /dev/ttyS1; echo $str | grep -E 'GPRMC|GNRMC' | awk -F',' '{print $4 " " $5 " " $6 " " $7; exit}' | head -1)
25 #       nmea=$(head -10 $GPSTTY | grep -E 'GPRMC|GNRMC' | awk -F',' '{print $4 " " $5 " " $6 " " $7; exit}' | head -1 ) # tr -d '\040\011\012\015'
26
27         NMEA=$(head -10 $GPSTTY | grep -E 'GPRMC|GNRMC') 
28         nmea=$(echo $NMEA | awk -F',' '{print $4 " " $5 " " $6 " " $7; exit}' | head -1 )
29         GPSvalid=$(echo $NMEA | awk -F'[,*]' '{print $13; exit}' | head -1 )
30
31 #       echo $nmea, $GPSdate, $GPStime, $GPSvalid
32
33 #       A = Autonomous
34 #       D = Differential
35 #       E = Estimated
36 #       N = Data not valid 
37         
38         nmeaTrim=$(echo $nmea | tr -d '\040\011\012\015')
39
40         time=$(date +%s)
41
42         if [ "$nmeaTrim" != "" ] && ([ "$GPSvalid" == "A" ] || [ "$GPSvalid" == "D" ] || [ "$GPSvalid" == "E" ]); then 
43
44                 # save first fixation NMEA time
45
46                 if [ "$firstFix" == "" ]; then 
47
48                         firstFix=1
49
50                         GPSdate=$(echo $NMEA | awk -F',' '{print $10; exit}' | head -1 )
51                         GPStime=$(echo $NMEA | awk -F'[,.]' '{print $2; exit}' | head -1 )
52
53                         echo "$GPSdate|$GPStime" > $TIMENMEA &
54                 fi 
55
56                 # mesure time from previous correct probe
57
58                 tdiff=$(( $time - $prevtime ))
59                 currentGPS=$nmea
60
61                 echo "$nmea|$prevGPS|$prevtime" > $CURRENTGPS
62
63                 if [ "$tdiff" -gt "$STANDARD_DISTANCE_TIME" ]; then 
64
65                         prevGPS=$PREVGPSTMP
66                         PREVGPSTMP=$nmea
67                         prevtime=$time
68
69                         led main slow
70
71                 fi 
72         fi 
73
74         # -------------- GPS dependent messages -------------
75
76         tdiff=$(( $time - $prevtime ))
77
78         if [ "$tdiff" -lt "$MAX_GPS_AGE_THRESHOLD" ]; then # gps is ok 
79
80                 # -------------- navigation -----------------
81
82                 if [ -s "$navOn" ]; then # if navigation turned on
83
84                         tdiffNav=$(( $time - $prevtimeNav ))
85                         
86                         if [ "$tdiffNav" -gt "$NAV_MESSAGE_EACH_SEC" ]; then    # time to infrom user
87
88                                 prevtimeNav=$time
89
90                                 distance=0 # TODO: CALCULATE DISTANCE HERE BETWEEN $prevGPS AND $currentGPS
91
92                                 if [ "$distance" -ge "$MIN_DISTANCE" ]; then    # motion detected
93
94                                         # direction determination 
95
96                                         targetGPS=$(cat $navMemo)
97
98                                         angle=0 # CALCULATE ANGLE HERE BY 3 POINTS $prevGPS $currentGPS $targetGPS                      
99
100                                         NMESSang=$(sayDir $angle)
101
102                                         # distance between current and memorized points
103
104                                         navdDstanceOk=0 # TODO: CALCULATE DISTANCE HERE BETWEEN $targetGPS AND $currentGPS
105
106                                         km=$(echo $navdDstanceOk | awk '{print $1; exit}')
107                                         kmPfx=$(echo $navdDstanceOk | awk '{print "$" $2; exit}')
108                                         md=$(echo $navdDstanceOk | awk '{print $3; exit}')
109                                         m=$(echo $navdDstanceOk | awk '{print $4; exit}')
110                                         mPfx=$(echo $navdDstanceOk | awk '{print "$" $5; exit}')
111
112                                         # km
113
114                                         currentL=$(cat $TMPFS/$currentLang)
115                 
116                                         if [ "$km" -gt "0" ]; then
117                                                 number=$(ls -p $CONTENT/$currentL/$SYSMESSAGES/$NAV_NUMBERS_1 | grep -v '/' | awk -v a=$km 'NR == a+1 {print; exit}')
118
119                                                 NMESSkmNum="$NAV_NUMBERS_1/$number"
120                                                 NMESSkm=$(eval "echo $kmPfx")
121                                         fi 
122
123                                         # m 
124
125                                         if [ "$md" -gt "0" ]; then
126                                                 number=$(ls -p $CONTENT/$currentL/$SYSMESSAGES/$NAV_NUMBERS_2_100 | grep -v '/' | awk -v a=$md 'NR == a+1 {print; exit}')
127                                                 NMESSmNum="$NAV_NUMBERS_2_100/$number"
128                                         fi 
129
130                                         if [ "$m" -gt "0" ]; then
131                                                 number=$(ls -p $CONTENT/$currentL/$SYSMESSAGES/$NAV_NUMBERS_2 | grep -v '/' | awk -v a=$m 'NR == a+1 {print; exit}')
132
133                                                 NMESSm2Num="$NAV_NUMBERS_2/$number"
134                                                 NMESSm=$(eval "echo $mPfx")
135                                         fi
136
137                                         echo "$NMESSang|$NAV_DISTANCE_IS|$NMESSkmNum|$NMESSkm|$NMESSmNum|$NMESSm2Num|$NMESSm" > $TMPFS/$message
138
139                                 else 
140                                         # no direction detected 
141
142                                         echo $NAV_NO_DIR > $TMPFS/$message
143                                 fi 
144                         fi                      
145                 fi 
146         else 
147                 # can't determine position 
148
149                 led main midd
150
151                 # -------------- no GPS position message -------------
152
153                 timeGPSmess=$time
154
155                 if [ "$prevtimeGPSmess" == "0" ]; then 
156                         prevtimeGPSmess=$timeGPSmess; 
157                         tdiffGPSmess=0;
158                 else
159                         tdiffGPSmess=$(( $timeGPSmess - $prevtimeGPSmess ))
160                 fi
161
162                 if [ "$tdiffGPSmess" -gt "$NOGPS_MESSAGE_EACH_SEC" ]; then 
163                         prevtimeGPSmess=$timeGPSmess
164
165                         echo "GPS lost" | myLog
166
167                         echo "$DEV_JINGLE|$DEV_NO_COORD" > $TMPFS/$message
168                 fi 
169         fi 
170
171         sleep $GPSREADINTERVAL # maybe not needed since NMEA has 1 sec period # look like NEEDED
172         
173 done
174
Contact me: dev (at) shalnoff (dot) com
PGP fingerprint: A6B8 3B23 6013 F18A 0C71 198B 83D8 C64D 917A 5717