gpx.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #define KALMAN_Q 8.5e-6
  10. #define KALMAN_R 4e-5
  11. #define KALMAN_ERR_MAX 6e-4
  12. __flash const char xml_header[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  13. "<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"
  14. "\t<trk>\n"
  15. "\t\t<trkseg>\n";
  16. FIL gpx_file;
  17. static char buf[sizeof(xml_header)+1];
  18. struct kalman_s {
  19. unsigned char initialized;
  20. float x_est_last;
  21. float P_last;
  22. float Q;
  23. float R;
  24. float K;
  25. };
  26. #define PREV_POINTS_LENGTH 4
  27. #define AVG_COUNT 3
  28. #define MIN_DIST_DELTA 2.0
  29. struct prev_points_s {
  30. struct location_s data[PREV_POINTS_LENGTH];
  31. unsigned char start;
  32. unsigned char count;
  33. };
  34. struct avg_store_s {
  35. float lat;
  36. float lon;
  37. time_t time;
  38. };
  39. static struct {
  40. struct prev_points_s prev_points;
  41. unsigned char avg_count;
  42. unsigned char paused;
  43. struct avg_store_s avg_store;
  44. struct location_s last_saved;
  45. struct kalman_s kalman[2];
  46. } gpx;
  47. float kalman_predict(struct kalman_s *k, float data);
  48. void kalman_init(struct kalman_s *k);
  49. float distance(struct location_s *pos1, struct location_s *pos2);
  50. void prev_points_append(struct location_s *new){
  51. gpx.prev_points.data[(gpx.prev_points.start + gpx.prev_points.count)%PREV_POINTS_LENGTH] = *new;
  52. if(++gpx.prev_points.count > PREV_POINTS_LENGTH){
  53. gpx.prev_points.count--;
  54. gpx.prev_points.start++;
  55. gpx.prev_points.start %= PREV_POINTS_LENGTH;
  56. }
  57. }
  58. struct location_s *prev_points_get(unsigned char index){
  59. unsigned char i, addr = gpx.prev_points.start;
  60. for(i=0; i<index; i++){
  61. addr++;
  62. addr %= PREV_POINTS_LENGTH;
  63. }
  64. return &gpx.prev_points.data[addr];
  65. }
  66. unsigned char gpx_init(FIL *file) {
  67. unsigned int bw;
  68. kalman_init(&gpx.kalman[0]);
  69. kalman_init(&gpx.kalman[1]);
  70. gpx.prev_points.count = 0;
  71. gpx.avg_count = 0;
  72. gpx.last_saved.lon = 0;
  73. gpx.last_saved.lat = 0;
  74. gpx.last_saved.time = 0;
  75. gpx.paused = 0;
  76. strcpy_P(buf, xml_header);
  77. return f_write(file, buf, strlen(buf), &bw);
  78. }
  79. unsigned char gpx_write(struct location_s *loc, FIL *file) {
  80. unsigned int bw;
  81. const char *time;
  82. if (System.tracking_paused) {
  83. if (!gpx.paused) {
  84. strcpy_P(buf, PSTR("\t\t</trkseg>\n"));
  85. gpx.paused = 1;
  86. } else {
  87. return 0; /* nothing to store */
  88. }
  89. } else {
  90. if (gpx.paused) {
  91. strcpy_P(buf, PSTR("\t\t<trkseg>\n"));
  92. f_write(file, buf, strlen(buf), &bw);
  93. gpx.paused = 0;
  94. }
  95. time = get_iso_time(loc->time, 0);
  96. 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);
  97. /* alt */
  98. strcat_P(buf, PSTR("\t\t\t</trkpt>\n"));
  99. }
  100. return f_write(file, buf, strlen(buf), &bw);
  101. }
  102. unsigned char gpx_close(FIL *file) {
  103. unsigned int bw;
  104. buf[0] = '\0';
  105. if (!gpx.paused)
  106. strcpy_P(buf, PSTR("\t\t</trkseg>\n"));
  107. strcat_P(buf, PSTR("\t</trk>\n</gpx>\n"));
  108. f_write(file, buf, strlen(buf), &bw);
  109. return f_close(file);
  110. }
  111. void gpx_process_point(struct location_s *loc, FIL *file){
  112. float lon_est, lon_err, lat_est, lat_err;
  113. struct location_s *ptr;
  114. struct location_s nloc;
  115. if (get_flag(CONFFLAG_DISABLE_FILTERS)) {
  116. xputs_P(PSTR("Write with filters disabled\r\n"));
  117. gpx_write(loc, file);
  118. } else {
  119. lat_est = kalman_predict(&gpx.kalman[0], loc->lat);
  120. lon_est = kalman_predict(&gpx.kalman[1], loc->lon);
  121. lat_err = fabs(loc->lat - lat_est);
  122. lon_err = fabs(loc->lon - lon_est);
  123. // xprintf(PSTR("lat_err: %e, lon_err: %e, limit: %e\r\n"), lat_err, lon_err, (float)KALMAN_ERR_MAX);
  124. if(lat_err > KALMAN_ERR_MAX || lon_err > KALMAN_ERR_MAX){
  125. xputs_P(PSTR("KALMAN REJECT\r\n"));
  126. return;
  127. }
  128. loc->lat = lat_est;
  129. loc->lon = lon_est;
  130. prev_points_append(loc);
  131. if(gpx.prev_points.count == PREV_POINTS_LENGTH){
  132. float dist12 = distance(prev_points_get(0), prev_points_get(1));
  133. float dist34 = distance(prev_points_get(2), prev_points_get(3));
  134. float dist32 = distance(prev_points_get(2), prev_points_get(1));
  135. xprintf(PSTR("New distance: %fm\r\n"), dist32);
  136. if(dist34 > dist12 && dist32 > dist12){
  137. xputs_P(PSTR("DISTANCE DIFF REJECT\r\n"));
  138. return;
  139. }
  140. ptr = prev_points_get(PREV_POINTS_LENGTH - 2);
  141. } else {
  142. if(gpx.prev_points.count >= PREV_POINTS_LENGTH-2){
  143. ptr = prev_points_get(gpx.prev_points.count - 2);
  144. xputs_P(PSTR("NEW\r\n"));
  145. } else {
  146. return;
  147. }
  148. }
  149. if(distance(&gpx.last_saved, ptr) < MIN_DIST_DELTA){
  150. xputs_P(PSTR("Too small position change REJECT\r\n"));
  151. return;
  152. }
  153. xputs_P(PSTR("ACCEPT\r\n"));
  154. gpx.avg_store.lat += ptr->lat;
  155. gpx.avg_store.lon += ptr->lon;
  156. if(gpx.avg_count == AVG_COUNT/2)
  157. gpx.avg_store.time = ptr->time;
  158. if(++gpx.avg_count == AVG_COUNT){
  159. nloc.lat = gpx.avg_store.lat / AVG_COUNT;
  160. nloc.lon = gpx.avg_store.lon / AVG_COUNT;
  161. nloc.time = gpx.avg_store.time;
  162. gpx.avg_count = 0;
  163. gpx.avg_store.lat = 0;
  164. gpx.avg_store.lon = 0;
  165. gpx.avg_store.time = 0;
  166. gpx.last_saved = nloc;
  167. gpx_write(&nloc, file);
  168. return;
  169. }
  170. }
  171. }
  172. void kalman_init(struct kalman_s *k){
  173. k->initialized = 0;
  174. k->P_last = 0;
  175. //the noise in the system
  176. k->Q = KALMAN_Q; // process variance
  177. k->R = KALMAN_R; // measurement variance
  178. k->K = 0;
  179. }
  180. float kalman_predict(struct kalman_s *k, float data){
  181. if(!k->initialized){
  182. //initial values for the kalman filter
  183. k->x_est_last = data;
  184. k->initialized = 1;
  185. return data;
  186. }
  187. //do a prediction
  188. float x_temp_est = k->x_est_last;
  189. float P_temp = k->P_last + k->Q;
  190. //calculate the Kalman gain
  191. k->K = P_temp * (1.0/(P_temp + k->R));
  192. //correct
  193. float x_est = x_temp_est + k->K * (data - x_temp_est);
  194. k->P_last = (1 - k->K) * P_temp;
  195. k->x_est_last = x_est;
  196. return x_est;
  197. }
  198. float distance(struct location_s *pos1, struct location_s *pos2){
  199. float lon_delta = fabs(pos1->lon - pos2->lon) * 111139.0;
  200. float lat_delta = fabs(pos1->lat - pos2->lat) * 111139.0;
  201. // xprintf(PSTR("lat1=%f; lat2=%f; lon1=%f; lon2=%f; lat_delta=%f; lon_delta=%f\r\n"), pos1->lat, pos2->lat, pos1->lon, pos2->lon, lon_delta, lat_delta);
  202. return sqrtf(lon_delta * lon_delta + lat_delta * lat_delta);
  203. }