gpx.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 {
  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 gpx_write(struct location_s *loc, FIL *file) {
  107. unsigned int bw;
  108. const char *time;
  109. unsigned char paused = System.tracking_paused || System.tracking_auto_paused;
  110. if (paused) {
  111. if (!gpx.paused) {
  112. strcpy_P(buf, xml_trkseg_end);
  113. gpx.paused = 1;
  114. gpx.point_count = 0;
  115. } else {
  116. return 0; /* nothing to store */
  117. }
  118. } else {
  119. if (gpx.paused) {
  120. strcpy_P(buf, xml_trkseg_start);
  121. f_write(file, buf, strlen(buf), &bw);
  122. gpx.paused = 0;
  123. }
  124. time = get_iso_time(loc->time, 0);
  125. 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);
  126. /* alt */
  127. strcat_P(buf, PSTR("\t\t\t</trkpt>\n"));
  128. }
  129. return f_write(file, buf, strlen(buf), &bw);
  130. }
  131. unsigned char gpx_close(FIL *file) {
  132. unsigned int bw;
  133. buf[0] = '\0';
  134. if (!gpx.paused)
  135. strcpy_P(buf, xml_trkseg_end);
  136. strcat_P(buf, PSTR("\t</trk>\n</gpx>\n"));
  137. f_write(file, buf, strlen(buf), &bw);
  138. return f_close(file);
  139. }
  140. void gpx_process_point(struct location_s *loc, FIL *file){
  141. float lon_est, lon_err, lat_est, lat_err, dist = NAN;
  142. struct location_s *ptr;
  143. struct location_s nloc;
  144. if (gpx.point_count < System.conf.skip_points) { /* Skipping initial points */
  145. gpx.point_count++;
  146. return;
  147. }
  148. if (get_flag(CONFFLAG_DISABLE_FILTERS)) {
  149. xputs_P(PSTR("Write with filters disabled\r\n"));
  150. prev_points_append(loc);
  151. if(gpx.prev_points.count == PREV_POINTS_LENGTH){
  152. dist = distance(prev_points_get(2), prev_points_get(3));
  153. }
  154. gpx_write(loc, file);
  155. } else {
  156. lat_est = kalman_predict(&gpx.kalman[0], loc->lat);
  157. lon_est = kalman_predict(&gpx.kalman[1], loc->lon);
  158. lat_err = fabs(loc->lat - lat_est);
  159. lon_err = fabs(loc->lon - lon_est);
  160. // xprintf(PSTR("lat_err: %e, lon_err: %e, limit: %e\r\n"), lat_err, lon_err, (float)KALMAN_ERR_MAX);
  161. if(lat_err > KALMAN_ERR_MAX || lon_err > KALMAN_ERR_MAX){
  162. xputs_P(PSTR("KALMAN REJECT\r\n"));
  163. return;
  164. }
  165. loc->lat = lat_est;
  166. loc->lon = lon_est;
  167. prev_points_append(loc);
  168. if(gpx.prev_points.count == PREV_POINTS_LENGTH){
  169. float dist12 = distance(prev_points_get(0), prev_points_get(1));
  170. float dist34 = distance(prev_points_get(2), prev_points_get(3));
  171. float dist32 = distance(prev_points_get(2), prev_points_get(1));
  172. xprintf(PSTR("New distance: %fm\r\n"), dist32);
  173. if(dist34 > dist12 && dist32 > dist12){
  174. xputs_P(PSTR("DISTANCE DIFF REJECT\r\n"));
  175. return;
  176. }
  177. ptr = prev_points_get(PREV_POINTS_LENGTH - 2);
  178. dist = dist34;
  179. } else {
  180. if(gpx.prev_points.count >= PREV_POINTS_LENGTH-2){
  181. ptr = prev_points_get(gpx.prev_points.count - 2);
  182. xputs_P(PSTR("NEW\r\n"));
  183. } else {
  184. return;
  185. }
  186. }
  187. if(distance(&gpx.last_saved, ptr) < MIN_DIST_DELTA){
  188. xputs_P(PSTR("Too small position change REJECT\r\n"));
  189. return;
  190. }
  191. xputs_P(PSTR("ACCEPT\r\n"));
  192. gpx.avg_store.lat += ptr->lat;
  193. gpx.avg_store.lon += ptr->lon;
  194. if(gpx.avg_count == AVG_COUNT/2)
  195. gpx.avg_store.time = ptr->time;
  196. if(++gpx.avg_count == AVG_COUNT){
  197. nloc.lat = gpx.avg_store.lat / AVG_COUNT;
  198. nloc.lon = gpx.avg_store.lon / AVG_COUNT;
  199. nloc.time = gpx.avg_store.time;
  200. gpx.avg_count = 0;
  201. gpx.avg_store.lat = 0;
  202. gpx.avg_store.lon = 0;
  203. gpx.avg_store.time = 0;
  204. gpx.last_saved = nloc;
  205. gpx_write(&nloc, file);
  206. }
  207. }
  208. if (System.time_start == 0)
  209. System.time_start = utc;
  210. if (isnan(dist))
  211. return;
  212. /* FIXME distance is always calculated from unfiltered data and never paused! */
  213. add_distance(dist);
  214. }
  215. void kalman_init(struct kalman_s *k){
  216. k->initialized = 0;
  217. k->P_last = 0;
  218. //the noise in the system
  219. k->Q = KALMAN_Q; // process variance
  220. k->R = KALMAN_R; // measurement variance
  221. k->K = 0;
  222. }
  223. float kalman_predict(struct kalman_s *k, float data){
  224. if(!k->initialized){
  225. //initial values for the kalman filter
  226. k->x_est_last = data;
  227. k->initialized = 1;
  228. return data;
  229. }
  230. //do a prediction
  231. float x_temp_est = k->x_est_last;
  232. float P_temp = k->P_last + k->Q;
  233. //calculate the Kalman gain
  234. k->K = P_temp * (1.0/(P_temp + k->R));
  235. //correct
  236. float x_est = x_temp_est + k->K * (data - x_temp_est);
  237. k->P_last = (1 - k->K) * P_temp;
  238. k->x_est_last = x_est;
  239. return x_est;
  240. }
  241. #define R_EARTH 6371e3 // m
  242. float distance(struct location_s *pos1, struct location_s *pos2){
  243. float lat1 = pos1->lat * M_PI / 180.0;
  244. float lat2 = pos2->lat * M_PI / 180.0;
  245. float dlat = (pos2->lat - pos1->lat) * M_PI / 180.0;
  246. float dlon = (pos2->lon - pos1->lon) * M_PI / 180.0;
  247. float a = sinf(dlat/2.0) * sinf(dlat/2.0) + cosf(lat1) * cosf(lat2) * sinf(dlon/2.0) * sinf(dlon/2.0);
  248. float c = 2 * atan2f(sqrtf(a), sqrtf(1-a));
  249. float ret = R_EARTH * c;
  250. return ret;
  251. }
  252. void add_distance(float dist) {
  253. System.distance += (dist+0.005)*100.0;
  254. xprintf(PSTR("Distance: %f m; sum: %f m\r\n"), dist, System.distance/100.0);
  255. }