| 1 | #!/bin/bash |
|---|
| 2 | PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin |
|---|
| 3 | export 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 | |
|---|
| 10 | LANG=POSIX # needed to prevent lat/lon strings beeing formatted into 1,234,567.89 |
|---|
| 11 | SOURCE_FILE=~/trip.csv |
|---|
| 12 | WAYPOINTS=~/itinerary.csv |
|---|
| 13 | DEST_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 |
|---|
| 16 | FIELD_SEPERATOR=';' |
|---|
| 17 | MAX_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 | |
|---|
| 27 | if test -s "${WAYPOINTS}"; then |
|---|
| 28 | GO_AHEAD=1 |
|---|
| 29 | else |
|---|
| 30 | exit 2 |
|---|
| 31 | fi |
|---|
| 32 | |
|---|
| 33 | if gpsbabel -V >/dev/null 2>&1; then |
|---|
| 34 | GO_AHEAD=1 |
|---|
| 35 | else |
|---|
| 36 | echo 'gpsbabel not found in path, exiting' |
|---|
| 37 | exit 3 |
|---|
| 38 | fi |
|---|
| 39 | |
|---|
| 40 | if gpspipe -V >/dev/null 2>&1; then |
|---|
| 41 | GO_AHEAD=1 |
|---|
| 42 | else |
|---|
| 43 | echo 'gpspipe not found in path, exiting' |
|---|
| 44 | exit 4 |
|---|
| 45 | fi |
|---|
| 46 | |
|---|
| 47 | OLD_IFS="${IFS}" |
|---|
| 48 | IFS=$'\n' |
|---|
| 49 | |
|---|
| 50 | N=1 |
|---|
| 51 | for 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 )) |
|---|
| 55 | done |
|---|
| 56 | |
|---|
| 57 | IFS="${OLD_IFS}" |
|---|
| 58 | |
|---|
| 59 | NUMBER_OF_WAYPOINTS="${N}" |
|---|
| 60 | |
|---|
| 61 | # set the first waypoint as destination |
|---|
| 62 | N=1 |
|---|
| 63 | DEST_LAT=${DEST_LAT[${N}]} |
|---|
| 64 | DEST_LON=${DEST_LON[${N}]} |
|---|
| 65 | |
|---|
| 66 | # "${DEST_SET_FILE}" is a switch whether we have set our destination already |
|---|
| 67 | if test -f "${DEST_SET_FILE}"; then |
|---|
| 68 | GO_AHEAD=1 |
|---|
| 69 | else |
|---|
| 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}" |
|---|
| 72 | fi |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | get_current_lat_lon() |
|---|
| 76 | { |
|---|
| 77 | |
|---|
| 78 | # read lines of nmea data from gpsd |
|---|
| 79 | NOW=`date +%s` |
|---|
| 80 | START_TIME="${NOW}" |
|---|
| 81 | STOP_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 | |
|---|
| 85 | N=1 |
|---|
| 86 | IFS=$'\n' |
|---|
| 87 | for NMEA_LINE in `gpspipe -r -n 100 2>/dev/null`; do |
|---|
| 88 | NMEA_DATA[${N}]="${NMEA_LINE}" |
|---|
| 89 | N=$(( N + 1 )) |
|---|
| 90 | done |
|---|
| 91 | IFS="${OLD_IFS}" |
|---|
| 92 | |
|---|
| 93 | NOW=`date +%s` |
|---|
| 94 | if 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) |
|---|
| 97 | CURRENT_LAT=`\ |
|---|
| 98 | echo "${NMEA_DATA[@]}" |\ |
|---|
| 99 | gpsbabel -i nmea -f - -o gpx -F - |\ |
|---|
| 100 | grep '<trkpt' |\ |
|---|
| 101 | grep -v 90.000000000 |\ |
|---|
| 102 | grep -v 0.000000000 |\ |
|---|
| 103 | awk -F'"' '{print $2}' |\ |
|---|
| 104 | tail -1` |
|---|
| 105 | |
|---|
| 106 | CURRENT_LON=`\ |
|---|
| 107 | echo "${NMEA_DATA[@]}" |\ |
|---|
| 108 | gpsbabel -i nmea -f - -o gpx -F - |\ |
|---|
| 109 | grep '<trkpt' |\ |
|---|
| 110 | grep -v 90.000000000 |\ |
|---|
| 111 | grep -v 0.000000000 |\ |
|---|
| 112 | awk -F'"' '{print $4}' |\ |
|---|
| 113 | tail -1` |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | get_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 | |
|---|
| 125 | if test "${CURRENT_LAT}"; then |
|---|
| 126 | ROUNDED_CURRENT_LAT=`printf "%'.3f" "${CURRENT_LAT}" | tr -d '.'` |
|---|
| 127 | else |
|---|
| 128 | exit 6 |
|---|
| 129 | fi |
|---|
| 130 | |
|---|
| 131 | if test "${CURRENT_LON}"; then |
|---|
| 132 | ROUNDED_CURRENT_LON=`printf "%'.3f" "${CURRENT_LON}" | tr -d '.'` |
|---|
| 133 | else |
|---|
| 134 | exit 7 |
|---|
| 135 | fi |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | if test "${DEST_LAT}"; then |
|---|
| 139 | ROUNDED_DEST_LAT=`printf "%'.3f" "${DEST_LAT}" | tr -d '.'` |
|---|
| 140 | else |
|---|
| 141 | exit 8 |
|---|
| 142 | fi |
|---|
| 143 | |
|---|
| 144 | if test "${DEST_LON}"; then |
|---|
| 145 | ROUNDED_DEST_LON=`printf "%'.3f" "${DEST_LON}" | tr -d '.'` |
|---|
| 146 | else |
|---|
| 147 | exit 9 |
|---|
| 148 | fi |
|---|
| 149 | |
|---|
| 150 | DIFF_LAT=$(( ${ROUNDED_CURRENT_LAT} - ${ROUNDED_DEST_LAT} )) |
|---|
| 151 | DIFF_LON=$(( ${ROUNDED_CURRENT_LON} - ${ROUNDED_DEST_LON} )) |
|---|
| 152 | |
|---|
| 153 | # abs2 is a way to always have a positive delta (-12345 * -12345 = positive) |
|---|
| 154 | ABS2_DIFF_LAT=$(( ${DIFF_LAT} * ${DIFF_LAT} )) |
|---|
| 155 | ABS2_DIFF_LON=$(( ${DIFF_LON} * ${DIFF_LON} )) |
|---|
| 156 | |
|---|
| 157 | if 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" |
|---|
| 171 | fi |
|---|