gpx.c 7.8 KB

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