gpx.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #include <string.h>
  2. #include <avr/pgmspace.h>
  3. #include "xprintf.h"
  4. #include "math.h"
  5. #include "main.h"
  6. #include "gpx.h"
  7. #include "ff.h"
  8. #include "settings.h"
  9. #include "timec.h"
  10. #define KALMAN_Q 8.5e-6
  11. #define KALMAN_R 4e-5
  12. #define KALMAN_ERR_MAX 6e-4
  13. __flash const char xml_header[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  14. "<gpx xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.topografix.com/GPX/1/1\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\" version=\"1.1\" creator=\"k4be\">\n";
  15. __flash const char xml_trk_start[] = "\t<trk>\n";
  16. __flash const char xml_trkseg_end[] = "\t\t</trkseg>\n";
  17. __flash const char xml_trkseg_start[] = "\t\t<trkseg>\n";
  18. FIL gpx_file;
  19. static char buf[sizeof(xml_header)+sizeof(xml_trk_start)+2];
  20. struct kalman_s {
  21. unsigned char initialized;
  22. float x_est_last;
  23. float P_last;
  24. float Q;
  25. float R;
  26. float K;
  27. };
  28. #define PREV_POINTS_LENGTH 4
  29. #define AVG_COUNT 3
  30. #define MIN_DIST_DELTA 2.0
  31. struct prev_points_s {
  32. struct location_s data[PREV_POINTS_LENGTH];
  33. unsigned char start;
  34. unsigned char count;
  35. };
  36. struct avg_store_s {
  37. float lat;
  38. float lon;
  39. time_t time;
  40. };
  41. static struct gpx_s {
  42. struct prev_points_s prev_points;
  43. unsigned char avg_count;
  44. unsigned char paused;
  45. unsigned char point_count;
  46. struct avg_store_s avg_store;
  47. struct location_s last_saved;
  48. struct kalman_s kalman[2];
  49. } gpx;
  50. float kalman_predict(struct kalman_s *k, float data);
  51. void kalman_init(struct kalman_s *k);
  52. float distance(struct location_s *pos1, struct location_s *pos2);
  53. void prev_points_append(struct location_s *new){
  54. gpx.prev_points.data[(gpx.prev_points.start + gpx.prev_points.count)%PREV_POINTS_LENGTH] = *new;
  55. if(++gpx.prev_points.count > PREV_POINTS_LENGTH){
  56. gpx.prev_points.count--;
  57. gpx.prev_points.start++;
  58. gpx.prev_points.start %= PREV_POINTS_LENGTH;
  59. }
  60. }
  61. struct location_s *prev_points_get(unsigned char index){
  62. unsigned char i, addr = gpx.prev_points.start;
  63. for(i=0; i<index; i++){
  64. addr++;
  65. }
  66. addr %= PREV_POINTS_LENGTH;
  67. return &gpx.prev_points.data[addr];
  68. }
  69. unsigned char gpx_init(FIL *file) {
  70. unsigned int bw;
  71. kalman_init(&gpx.kalman[0]);
  72. kalman_init(&gpx.kalman[1]);
  73. gpx.prev_points.count = 0;
  74. gpx.avg_count = 0;
  75. gpx.last_saved.lon = 0;
  76. gpx.last_saved.lat = 0;
  77. gpx.last_saved.time = 0;
  78. gpx.paused = 1; /* make it add a <trkseg> tag */
  79. strcpy_P(buf, xml_header);
  80. strcat_P(buf, xml_trk_start);
  81. return f_write(file, buf, strlen(buf), &bw);
  82. }
  83. void gpx_save_single_point(struct location_s *loc) {
  84. FIL gpx;
  85. UINT bw;
  86. unsigned char err = 0;
  87. char *time = get_iso_time(loc->time, 1);
  88. iso_time_to_filename(time);
  89. xsprintf(buf, PSTR("%s-POINT.GPX"), time);
  90. xprintf(PSTR("Writing single point in %s\r\n"), buf);
  91. if ((err = f_open(&gpx, buf, FA_WRITE | FA_OPEN_ALWAYS))) {
  92. f_close(&gpx);
  93. // System.status = STATUS_FILE_OPEN_ERROR;
  94. xputs_P(PSTR("File open error\r\n"));
  95. return; /* Failed to open file */
  96. }
  97. strcpy_P(buf, xml_header);
  98. err |= f_write(&gpx, buf, strlen(buf), &bw);
  99. xsprintf(buf, PSTR("\t<wpt lat=\"%.8f\" lon=\"%.8f\"></wpt>\n</gpx>\n"), loc->lat, loc->lon);
  100. err |= f_write(&gpx, buf, strlen(buf), &bw);
  101. err |= f_close(&gpx);
  102. if (err) {
  103. /* TODO */
  104. }
  105. }
  106. unsigned char is_paused(void) {
  107. return gpx.paused;
  108. }
  109. unsigned char gpx_write(struct location_s *loc, FIL *file) {
  110. unsigned int bw;
  111. const char *time;
  112. unsigned char paused = System.tracking_paused || System.tracking_auto_paused;
  113. if (paused) {
  114. if (!gpx.paused) {
  115. strcpy_P(buf, xml_trkseg_end);
  116. gpx.paused = 1;
  117. gpx.point_count = 0;
  118. System.current_pause_start = utc;
  119. } else {
  120. return 0; /* nothing to store */
  121. }
  122. } else {
  123. if (gpx.paused) {
  124. strcpy_P(buf, xml_trkseg_start);
  125. f_write(file, buf, strlen(buf), &bw);
  126. gpx.paused = 0;
  127. if (System.current_pause_start)
  128. System.pause_time += utc - System.current_pause_start;
  129. }
  130. time = get_iso_time(loc->time, 0);
  131. xsprintf(buf, PSTR("\t\t\t<trkpt lat=\"%.8f\" lon=\"%.8f\">\n\t\t\t\t<time>%s</time>\n"), loc->lat, loc->lon, time);
  132. /* alt */
  133. strcat_P(buf, PSTR("\t\t\t</trkpt>\n"));
  134. }
  135. return f_write(file, buf, strlen(buf), &bw);
  136. }
  137. unsigned char gpx_close(FIL *file) {
  138. unsigned int bw;
  139. buf[0] = '\0';
  140. if (!gpx.paused)
  141. strcpy_P(buf, xml_trkseg_end);
  142. strcat_P(buf, PSTR("\t</trk>\n</gpx>\n"));
  143. f_write(file, buf, strlen(buf), &bw);
  144. return f_close(file);
  145. }
  146. void gpx_process_point(struct location_s *loc, FIL *file){
  147. float lon_est, lon_err, lat_est, lat_err, dist = NAN;
  148. struct location_s *ptr;
  149. struct location_s nloc;
  150. if (gpx.point_count < System.conf.skip_points) { /* Skipping initial points */
  151. gpx.point_count++;
  152. return;
  153. }
  154. if (get_flag(CONFFLAG_DISABLE_FILTERS)) {
  155. xputs_P(PSTR("Write with filters disabled\r\n"));
  156. prev_points_append(loc);
  157. if(gpx.prev_points.count == PREV_POINTS_LENGTH){
  158. dist = distance(prev_points_get(2), prev_points_get(3));
  159. }
  160. gpx_write(loc, file);
  161. } else {
  162. lat_est = kalman_predict(&gpx.kalman[0], loc->lat);
  163. lon_est = kalman_predict(&gpx.kalman[1], loc->lon);
  164. lat_err = fabs(loc->lat - lat_est);
  165. lon_err = fabs(loc->lon - lon_est);
  166. // xprintf(PSTR("lat_err: %e, lon_err: %e, limit: %e\r\n"), lat_err, lon_err, (float)KALMAN_ERR_MAX);
  167. if(lat_err > KALMAN_ERR_MAX || lon_err > KALMAN_ERR_MAX){
  168. xputs_P(PSTR("KALMAN REJECT\r\n"));
  169. return;
  170. }
  171. loc->lat = lat_est;
  172. loc->lon = lon_est;
  173. prev_points_append(loc);
  174. if(gpx.prev_points.count == PREV_POINTS_LENGTH){
  175. float dist12 = distance(prev_points_get(0), prev_points_get(1));
  176. float dist34 = distance(prev_points_get(2), prev_points_get(3));
  177. float dist32 = distance(prev_points_get(2), prev_points_get(1));
  178. xprintf(PSTR("New distance: %fm\r\n"), dist32);
  179. if(dist34 > dist12 && dist32 > dist12){
  180. xputs_P(PSTR("DISTANCE DIFF REJECT\r\n"));
  181. return;
  182. }
  183. ptr = prev_points_get(PREV_POINTS_LENGTH - 2);
  184. dist = dist34;
  185. } else {
  186. if(gpx.prev_points.count >= PREV_POINTS_LENGTH-2){
  187. ptr = prev_points_get(gpx.prev_points.count - 2);
  188. xputs_P(PSTR("NEW\r\n"));
  189. } else {
  190. return;
  191. }
  192. }
  193. if(distance(&gpx.last_saved, ptr) < MIN_DIST_DELTA){
  194. xputs_P(PSTR("Too small position change REJECT\r\n"));
  195. return;
  196. }
  197. xputs_P(PSTR("ACCEPT\r\n"));
  198. gpx.avg_store.lat += ptr->lat;
  199. gpx.avg_store.lon += ptr->lon;
  200. if(gpx.avg_count == AVG_COUNT/2)
  201. gpx.avg_store.time = ptr->time;
  202. if(++gpx.avg_count == AVG_COUNT){
  203. nloc.lat = gpx.avg_store.lat / AVG_COUNT;
  204. nloc.lon = gpx.avg_store.lon / AVG_COUNT;
  205. nloc.time = gpx.avg_store.time;
  206. gpx.avg_count = 0;
  207. gpx.avg_store.lat = 0;
  208. gpx.avg_store.lon = 0;
  209. gpx.avg_store.time = 0;
  210. gpx.last_saved = nloc;
  211. gpx_write(&nloc, file);
  212. }
  213. }
  214. if (System.time_start == 0)
  215. System.time_start = utc;
  216. if (isnan(dist))
  217. return;
  218. /* FIXME distance is always calculated from unfiltered data and never paused! */
  219. add_distance(dist);
  220. }
  221. void kalman_init(struct kalman_s *k){
  222. k->initialized = 0;
  223. k->P_last = 0;
  224. //the noise in the system
  225. k->Q = KALMAN_Q; // process variance
  226. k->R = KALMAN_R; // measurement variance
  227. k->K = 0;
  228. }
  229. float kalman_predict(struct kalman_s *k, float data){
  230. if(!k->initialized){
  231. //initial values for the kalman filter
  232. k->x_est_last = data;
  233. k->initialized = 1;
  234. return data;
  235. }
  236. //do a prediction
  237. float x_temp_est = k->x_est_last;
  238. float P_temp = k->P_last + k->Q;
  239. //calculate the Kalman gain
  240. k->K = P_temp * (1.0/(P_temp + k->R));
  241. //correct
  242. float x_est = x_temp_est + k->K * (data - x_temp_est);
  243. k->P_last = (1 - k->K) * P_temp;
  244. k->x_est_last = x_est;
  245. return x_est;
  246. }
  247. #define R_EARTH 6371e3 // m
  248. float distance(struct location_s *pos1, struct location_s *pos2){
  249. float lat1 = pos1->lat * M_PI / 180.0;
  250. float lat2 = pos2->lat * M_PI / 180.0;
  251. float dlat = (pos2->lat - pos1->lat) * M_PI / 180.0;
  252. float dlon = (pos2->lon - pos1->lon) * M_PI / 180.0;
  253. float a = sinf(dlat/2.0) * sinf(dlat/2.0) + cosf(lat1) * cosf(lat2) * sinf(dlon/2.0) * sinf(dlon/2.0);
  254. float c = 2 * atan2f(sqrtf(a), sqrtf(1-a));
  255. float ret = R_EARTH * c;
  256. return ret;
  257. }
  258. void add_distance(float dist) {
  259. if (!gpx.paused)
  260. System.distance += (dist+0.005)*100.0;
  261. xprintf(PSTR("Distance: %f m; sum: %f m\r\n"), dist, System.distance/100.0);
  262. }