gpx.c 5.5 KB

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