gpx.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. struct avg_store_s avg_store;
  46. struct location_s last_saved;
  47. struct kalman_s kalman[2];
  48. } gpx;
  49. float kalman_predict(struct kalman_s *k, float data);
  50. void kalman_init(struct kalman_s *k);
  51. float distance(struct location_s *pos1, struct location_s *pos2);
  52. void prev_points_append(struct location_s *new){
  53. gpx.prev_points.data[(gpx.prev_points.start + gpx.prev_points.count)%PREV_POINTS_LENGTH] = *new;
  54. if(++gpx.prev_points.count > PREV_POINTS_LENGTH){
  55. gpx.prev_points.count--;
  56. gpx.prev_points.start++;
  57. gpx.prev_points.start %= PREV_POINTS_LENGTH;
  58. }
  59. }
  60. struct location_s *prev_points_get(unsigned char index){
  61. unsigned char i, addr = gpx.prev_points.start;
  62. for(i=0; i<index; i++){
  63. addr++;
  64. addr %= PREV_POINTS_LENGTH;
  65. }
  66. return &gpx.prev_points.data[addr];
  67. }
  68. unsigned char gpx_init(FIL *file) {
  69. unsigned int bw;
  70. kalman_init(&gpx.kalman[0]);
  71. kalman_init(&gpx.kalman[1]);
  72. gpx.prev_points.count = 0;
  73. gpx.avg_count = 0;
  74. gpx.last_saved.lon = 0;
  75. gpx.last_saved.lat = 0;
  76. gpx.last_saved.time = 0;
  77. gpx.paused = 1; /* make it add a <trkseg> tag */
  78. strcpy_P(buf, xml_header);
  79. strcat_P(buf, xml_trk_start);
  80. return f_write(file, buf, strlen(buf), &bw);
  81. }
  82. void gpx_save_single_point(struct location_s *loc) {
  83. FIL gpx;
  84. UINT bw;
  85. unsigned char err = 0;
  86. char *time = get_iso_time(loc->time, 1);
  87. iso_time_to_filename(time);
  88. xsprintf(buf, PSTR("%s-POINT.GPX"), time);
  89. xprintf(PSTR("Writing single point in %s\r\n"), buf);
  90. if ((err = f_open(&gpx, buf, FA_WRITE | FA_OPEN_ALWAYS))) {
  91. f_close(&gpx);
  92. // System.status = STATUS_FILE_OPEN_ERROR;
  93. xputs_P(PSTR("File open error\r\n"));
  94. return; /* Failed to open file */
  95. }
  96. strcpy_P(buf, xml_header);
  97. err |= f_write(&gpx, buf, strlen(buf), &bw);
  98. xsprintf(buf, PSTR("\t<wpt lat=\"%.8f\" lon=\"%.8f\"></wpt>\n</gpx>\n"), loc->lat, loc->lon);
  99. err |= f_write(&gpx, buf, strlen(buf), &bw);
  100. err |= f_close(&gpx);
  101. if (err) {
  102. /* TODO */
  103. }
  104. }
  105. unsigned char gpx_write(struct location_s *loc, FIL *file) {
  106. unsigned int bw;
  107. const char *time;
  108. if (System.tracking_paused) {
  109. if (!gpx.paused) {
  110. strcpy_P(buf, xml_trkseg_end);
  111. gpx.paused = 1;
  112. } else {
  113. return 0; /* nothing to store */
  114. }
  115. } else {
  116. if (gpx.paused) {
  117. strcpy_P(buf, xml_trkseg_start);
  118. f_write(file, buf, strlen(buf), &bw);
  119. gpx.paused = 0;
  120. }
  121. time = get_iso_time(loc->time, 0);
  122. 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);
  123. /* alt */
  124. strcat_P(buf, PSTR("\t\t\t</trkpt>\n"));
  125. }
  126. return f_write(file, buf, strlen(buf), &bw);
  127. }
  128. unsigned char gpx_close(FIL *file) {
  129. unsigned int bw;
  130. buf[0] = '\0';
  131. if (!gpx.paused)
  132. strcpy_P(buf, xml_trkseg_end);
  133. strcat_P(buf, PSTR("\t</trk>\n</gpx>\n"));
  134. f_write(file, buf, strlen(buf), &bw);
  135. return f_close(file);
  136. }
  137. void gpx_process_point(struct location_s *loc, FIL *file){
  138. float lon_est, lon_err, lat_est, lat_err, dist = NAN;
  139. struct location_s *ptr;
  140. struct location_s nloc;
  141. if (get_flag(CONFFLAG_DISABLE_FILTERS)) {
  142. xputs_P(PSTR("Write with filters disabled\r\n"));
  143. prev_points_append(loc);
  144. if(gpx.prev_points.count == PREV_POINTS_LENGTH){
  145. dist = distance(prev_points_get(2), prev_points_get(3));
  146. }
  147. gpx_write(loc, file);
  148. } else {
  149. lat_est = kalman_predict(&gpx.kalman[0], loc->lat);
  150. lon_est = kalman_predict(&gpx.kalman[1], loc->lon);
  151. lat_err = fabs(loc->lat - lat_est);
  152. lon_err = fabs(loc->lon - lon_est);
  153. // xprintf(PSTR("lat_err: %e, lon_err: %e, limit: %e\r\n"), lat_err, lon_err, (float)KALMAN_ERR_MAX);
  154. if(lat_err > KALMAN_ERR_MAX || lon_err > KALMAN_ERR_MAX){
  155. xputs_P(PSTR("KALMAN REJECT\r\n"));
  156. return;
  157. }
  158. loc->lat = lat_est;
  159. loc->lon = lon_est;
  160. prev_points_append(loc);
  161. if(gpx.prev_points.count == PREV_POINTS_LENGTH){
  162. float dist12 = distance(prev_points_get(0), prev_points_get(1));
  163. float dist34 = distance(prev_points_get(2), prev_points_get(3));
  164. float dist32 = distance(prev_points_get(2), prev_points_get(1));
  165. xprintf(PSTR("New distance: %fm\r\n"), dist32);
  166. if(dist34 > dist12 && dist32 > dist12){
  167. xputs_P(PSTR("DISTANCE DIFF REJECT\r\n"));
  168. return;
  169. }
  170. ptr = prev_points_get(PREV_POINTS_LENGTH - 2);
  171. dist = dist34;
  172. } else {
  173. if(gpx.prev_points.count >= PREV_POINTS_LENGTH-2){
  174. ptr = prev_points_get(gpx.prev_points.count - 2);
  175. xputs_P(PSTR("NEW\r\n"));
  176. } else {
  177. return;
  178. }
  179. }
  180. if(distance(&gpx.last_saved, ptr) < MIN_DIST_DELTA){
  181. xputs_P(PSTR("Too small position change REJECT\r\n"));
  182. return;
  183. }
  184. xputs_P(PSTR("ACCEPT\r\n"));
  185. gpx.avg_store.lat += ptr->lat;
  186. gpx.avg_store.lon += ptr->lon;
  187. if(gpx.avg_count == AVG_COUNT/2)
  188. gpx.avg_store.time = ptr->time;
  189. if(++gpx.avg_count == AVG_COUNT){
  190. nloc.lat = gpx.avg_store.lat / AVG_COUNT;
  191. nloc.lon = gpx.avg_store.lon / AVG_COUNT;
  192. nloc.time = gpx.avg_store.time;
  193. gpx.avg_count = 0;
  194. gpx.avg_store.lat = 0;
  195. gpx.avg_store.lon = 0;
  196. gpx.avg_store.time = 0;
  197. gpx.last_saved = nloc;
  198. gpx_write(&nloc, file);
  199. }
  200. }
  201. if (isnan(dist))
  202. return;
  203. add_distance(dist);
  204. }
  205. void kalman_init(struct kalman_s *k){
  206. k->initialized = 0;
  207. k->P_last = 0;
  208. //the noise in the system
  209. k->Q = KALMAN_Q; // process variance
  210. k->R = KALMAN_R; // measurement variance
  211. k->K = 0;
  212. }
  213. float kalman_predict(struct kalman_s *k, float data){
  214. if(!k->initialized){
  215. //initial values for the kalman filter
  216. k->x_est_last = data;
  217. k->initialized = 1;
  218. return data;
  219. }
  220. //do a prediction
  221. float x_temp_est = k->x_est_last;
  222. float P_temp = k->P_last + k->Q;
  223. //calculate the Kalman gain
  224. k->K = P_temp * (1.0/(P_temp + k->R));
  225. //correct
  226. float x_est = x_temp_est + k->K * (data - x_temp_est);
  227. k->P_last = (1 - k->K) * P_temp;
  228. k->x_est_last = x_est;
  229. return x_est;
  230. }
  231. #define R_EARTH 6371e3 // m
  232. float distance(struct location_s *pos1, struct location_s *pos2){
  233. float lat1 = pos1->lat * M_PI / 180.0;
  234. float lat2 = pos2->lat * M_PI / 180.0;
  235. float dlat = (pos2->lat - pos1->lat) * M_PI / 180.0;
  236. float dlon = (pos2->lon - pos1->lon) * M_PI / 180.0;
  237. float a = sinf(dlat/2.0) * sinf(dlat/2.0) + cosf(lat1) * cosf(lat2) * sinf(dlon/2.0) * sinf(dlon/2.0);
  238. float c = 2 * atan2f(sqrtf(a), sqrtf(1-a));
  239. float ret = R_EARTH * c;
  240. System.distance += (ret+0.005)*100.0;
  241. xprintf(PSTR("Distance: %f m; sum: %f m\r\n"), ret, System.distance/100.0);
  242. return ret;
  243. }
  244. void add_distance(float dist) {
  245. System.distance += (dist+0.005)*100.0;
  246. xprintf(PSTR("Distance: %f m; sum: %f m\r\n"), dist, System.distance/100.0);
  247. }