Ticket #533: waypoint.sh

File waypoint.sh, 5.3 KB (added by kapiteined, 11 years ago)
Line 
1#!/bin/bash
2PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
3export DISPLAY=:0.0
4
5# 100103  12:25 script to feed navit a list of destinations, one at the time
6# and set the next waypoint as destination if you are near the current waypoint
7# start it from cron every minute
8# * * * * * if test -s /home/navit/scripts/waypoint.sh; then /home/navit/scripts/waypoint.sh; fi
9
10LANG=POSIX # needed to prevent lat/lon strings beeing formatted into 1,234,567.89
11SOURCE_FILE=~/trip.csv
12WAYPOINTS=~/itinerary.csv
13DEST_SET_FILE=/dev/shm/destination.set # a switch to see if the route was set already.
14
15# cp "${SOURCE_FILE}" "${WAYPOINTS}" # do this by hand at the start of your trip
16FIELD_SEPERATOR=';'
17MAX_DELTA=100 # number to indicate how close you want to be to your current waypoint before taking the next waypoint as destination
18
19# for example, with max_delta=10000 and the waypoint at lat=52.100 and long 5.100 then any location between 52.000,5.000 and 52.200,5.200
20# would be "close" (100 * 100 = 10000)
21#                                               52.100,5.100 and 52.100,5.100
22# with a max_delta of 1024 any location between 52.068,5.068 and 52.132,5.132 would be close ( 32 * 32 = 1024 ) -> aprox 10   Km radius at this latitude
23# with a max_delta of  100 any location between 52.090,5.090 and 52.110,5.110 would be close ( 10 * 10 =  100 ) -> aprox  2   Km radius at this latitude
24# with a max_delta of    9 any location between 52.097,5.097 and 52.103,5.103 would be close (  3 *  3 =    9 ) -> aprox  0.1 Km radius at this latitude
25# if this script is run every minute and your max speed is 120 Km/h (=2 Km/min) you don't want to go below max_delta=100
26
27if test -s "${WAYPOINTS}"; then
28 GO_AHEAD=1
29else
30 exit 2
31fi
32
33if gpsbabel -V >/dev/null 2>&1; then
34 GO_AHEAD=1
35else
36 echo 'gpsbabel not found in path, exiting'
37 exit 3
38fi
39
40if gpspipe -V >/dev/null 2>&1; then
41 GO_AHEAD=1
42else
43 echo 'gpspipe not found in path, exiting'
44 exit 4
45fi
46
47OLD_IFS="${IFS}"
48IFS=$'\n'
49
50N=1
51for WAYPOINT in `cat "${WAYPOINTS}" | tr ',' '.' `; do
52 DEST_LAT[${N}]=`echo "${WAYPOINT}" | awk -F "${FIELD_SEPERATOR}" '{print $1}'`
53 DEST_LON[${N}]=`echo "${WAYPOINT}" | awk -F "${FIELD_SEPERATOR}" '{print $2}'`
54 N=$(( ${N} + 1 ))
55done
56
57IFS="${OLD_IFS}"
58
59NUMBER_OF_WAYPOINTS="${N}"
60
61# set the first waypoint as destination
62N=1
63DEST_LAT=${DEST_LAT[${N}]}
64DEST_LON=${DEST_LON[${N}]}
65
66# "${DEST_SET_FILE}" is a switch whether we have set our destination already
67if test -f "${DEST_SET_FILE}"; then
68 GO_AHEAD=1
69else
70 dbus-send --print-reply --dest=org.navit_project.navit /org/navit_project/navit/default_navit org.navit_project.navit.navit.set_destination string:"${DEST_LON} ${DEST_LAT}" string:"comment"
71 touch "${DEST_SET_FILE}"
72fi
73
74
75get_current_lat_lon()
76{
77
78# read lines of nmea data from gpsd
79NOW=`date +%s`
80START_TIME="${NOW}"
81STOP_TIME=$(( ${START_TIME} + 55 )) \
82# if it takes more then 55 seconds to collect the nmea data, don't use it.
83# if this script is started every minute, another run is on it's way or already running.
84
85N=1
86IFS=$'\n'
87for NMEA_LINE in `gpspipe -r -n 100 2>/dev/null`; do
88 NMEA_DATA[${N}]="${NMEA_LINE}"
89 N=$(( N + 1 ))
90done
91IFS="${OLD_IFS}"
92
93NOW=`date +%s`
94if test "${NOW}" -gt "${STOP_TIME}"; then exit 5; fi
95
96# convert the nmea data into lat/long data and cut out bad locations (90,0)
97CURRENT_LAT=`\
98echo "${NMEA_DATA[@]}" |\
99gpsbabel -i nmea -f - -o gpx -F - |\
100grep '<trkpt' |\
101grep -v 90.000000000 |\
102grep -v 0.000000000  |\
103awk -F'"' '{print $2}' |\
104tail -1`
105
106CURRENT_LON=`\
107echo "${NMEA_DATA[@]}" |\
108gpsbabel -i nmea -f - -o gpx -F - |\
109grep '<trkpt' |\
110grep -v 90.000000000 |\
111grep -v 0.000000000  |\
112awk -F'"' '{print $4}' |\
113tail -1`
114}
115
116get_current_lat_lon
117
118################################################################################
119## test block to set the current lat/lon manually                             ##
120################################################################################
121#CURRENT_LAT=38.773276
122#CURRENT_LON=-120.814478
123################################################################################
124
125if test "${CURRENT_LAT}"; then
126 ROUNDED_CURRENT_LAT=`printf "%'.3f" "${CURRENT_LAT}" | tr -d '.'`
127else
128 exit 6
129fi
130
131if test "${CURRENT_LON}"; then
132 ROUNDED_CURRENT_LON=`printf "%'.3f" "${CURRENT_LON}" | tr -d '.'`
133else
134 exit 7
135fi
136
137
138if test "${DEST_LAT}"; then
139 ROUNDED_DEST_LAT=`printf "%'.3f" "${DEST_LAT}" | tr -d '.'`
140else
141 exit 8
142fi
143
144if test "${DEST_LON}"; then
145 ROUNDED_DEST_LON=`printf "%'.3f" "${DEST_LON}" | tr -d '.'`
146else
147 exit 9
148fi
149
150DIFF_LAT=$(( ${ROUNDED_CURRENT_LAT} - ${ROUNDED_DEST_LAT} ))
151DIFF_LON=$(( ${ROUNDED_CURRENT_LON} - ${ROUNDED_DEST_LON} ))
152
153# abs2 is a way to always have a positive delta (-12345 * -12345 = positive)
154ABS2_DIFF_LAT=$(( ${DIFF_LAT} * ${DIFF_LAT} ))
155ABS2_DIFF_LON=$(( ${DIFF_LON} * ${DIFF_LON} ))
156
157if test "${ABS2_DIFF_LAT}" -lt "${MAX_DELTA}" -a "${ABS2_DIFF_LON}" -lt "${MAX_DELTA}"; then
158 # we are close to our waypoint, so generate a new waypoints file, starting with the next waypoint (n=2)
159 rm "${WAYPOINTS}"
160 N=2
161 while test "${N}" -lt "${NUMBER_OF_WAYPOINTS}"; do
162  echo "${DEST_LAT[${N}]}"';'"${DEST_LON[${N}]}" >> "${WAYPOINTS}"
163  N=$(( N + 1 ))
164 done
165
166 # set the next waypoint as destination
167 N=2
168 DEST_LAT=${DEST_LAT[${N}]}
169 DEST_LON=${DEST_LON[${N}]}
170 dbus-send --print-reply --dest=org.navit_project.navit /org/navit_project/navit/default_navit org.navit_project.navit.navit.set_destination string:"${DEST_LON} ${DEST_LAT}" string:"comment"
171fi