gpx.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #ifdef PC_BUILD
  2. #include "../gps-test-tool/main.h"
  3. #else
  4. #include "main.h"
  5. #endif
  6. /* These are now included via main.h */
  7. #ifndef PC_BUILD
  8. #include <string.h>
  9. #include <avr/pgmspace.h>
  10. #include "xprintf.h"
  11. #include "math.h"
  12. #include "gpx.h"
  13. #include "ff.h"
  14. #include "settings.h"
  15. #include "timec.h"
  16. #endif
  17. #define KALMAN_Q 8.5e-6
  18. #define KALMAN_R 4e-5
  19. #define KALMAN_ERR_MAX 6e-4
  20. __flash const char xml_header[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  21. "<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";
  22. __flash const char xml_trk_start[] = "\t<trk>\n";
  23. __flash const char xml_trkseg_end[] = "\t\t</trkseg>\n";
  24. __flash const char xml_trkseg_start[] = "\t\t<trkseg>\n";
  25. FIL gpx_file;
  26. static char buf[sizeof(xml_header)+sizeof(xml_trk_start)+2];
  27. struct kalman_s {
  28. unsigned char initialized;
  29. float x_est_last;
  30. float P_last;
  31. float Q;
  32. float R;
  33. float K;
  34. };
  35. #define PREV_POINTS_LENGTH 4
  36. #define AVG_COUNT 3
  37. #define MIN_DIST_DELTA 2.0
  38. struct prev_points_s {
  39. struct location_s data[PREV_POINTS_LENGTH];
  40. unsigned char start;
  41. unsigned char count;
  42. };
  43. struct avg_store_s {
  44. float lat;
  45. float lon;
  46. time_t time;
  47. };
  48. static struct gpx_s {
  49. struct prev_points_s prev_points;
  50. unsigned char avg_count;
  51. unsigned char paused;
  52. unsigned char point_count;
  53. struct avg_store_s avg_store;
  54. struct location_s last_saved;
  55. struct location_s last_distance_point; /* Last accepted point for distance calculation */
  56. struct kalman_s kalman[2];
  57. } gpx;
  58. float kalman_predict(struct kalman_s *k, float data);
  59. void kalman_init(struct kalman_s *k);
  60. float distance(struct location_s *pos1, struct location_s *pos2);
  61. void prev_points_append(struct location_s *new){
  62. gpx.prev_points.data[(gpx.prev_points.start + gpx.prev_points.count)%PREV_POINTS_LENGTH] = *new;
  63. if(++gpx.prev_points.count > PREV_POINTS_LENGTH){
  64. gpx.prev_points.count--;
  65. gpx.prev_points.start++;
  66. gpx.prev_points.start %= PREV_POINTS_LENGTH;
  67. }
  68. }
  69. struct location_s *prev_points_get(unsigned char index){
  70. unsigned char i, addr = gpx.prev_points.start;
  71. for(i=0; i<index; i++){
  72. addr++;
  73. }
  74. addr %= PREV_POINTS_LENGTH;
  75. return &gpx.prev_points.data[addr];
  76. }
  77. unsigned char gpx_init(FIL *file) {
  78. unsigned int bw;
  79. kalman_init(&gpx.kalman[0]);
  80. kalman_init(&gpx.kalman[1]);
  81. gpx.prev_points.count = 0;
  82. gpx.avg_count = 0;
  83. gpx.last_saved.lon = 0;
  84. gpx.last_saved.lat = 0;
  85. gpx.last_saved.time = 0;
  86. gpx.last_distance_point.lon = 0;
  87. gpx.last_distance_point.lat = 0;
  88. gpx.last_distance_point.time = 0;
  89. gpx.paused = 1; /* make it add a <trkseg> tag */
  90. strcpy_P(buf, xml_header);
  91. strcat_P(buf, xml_trk_start);
  92. return f_write(file, buf, strlen(buf), &bw);
  93. }
  94. void gpx_save_single_point(struct location_s *loc) {
  95. FIL gpx;
  96. UINT bw;
  97. unsigned char err = 0;
  98. char *time = get_iso_time(loc->time, 1);
  99. iso_time_to_filename(time);
  100. xsprintf(buf, PSTR("%s-POINT.GPX"), time);
  101. xprintf(PSTR("Writing single point in %s\r\n"), buf);
  102. if ((err = f_open(&gpx, buf, FA_WRITE | FA_OPEN_ALWAYS))) {
  103. f_close(&gpx);
  104. // System.status = STATUS_FILE_OPEN_ERROR;
  105. xputs_P(PSTR("File open error\r\n"));
  106. return; /* Failed to open file */
  107. }
  108. strcpy_P(buf, xml_header);
  109. err |= f_write(&gpx, buf, strlen(buf), &bw);
  110. xsprintf(buf, PSTR("\t<wpt lat=\"%.8f\" lon=\"%.8f\"></wpt>\n</gpx>\n"), loc->lat, loc->lon);
  111. err |= f_write(&gpx, buf, strlen(buf), &bw);
  112. err |= f_close(&gpx);
  113. if (err) {
  114. /* TODO */
  115. }
  116. }
  117. unsigned char is_paused(void) {
  118. return gpx.paused;
  119. }
  120. unsigned char gpx_write(struct location_s *loc, FIL *file) {
  121. unsigned int bw;
  122. const char *time;
  123. unsigned char paused = System.tracking_paused || System.tracking_auto_paused;
  124. if (paused) {
  125. if (!gpx.paused) {
  126. strcpy_P(buf, xml_trkseg_end);
  127. gpx.paused = 1;
  128. gpx.point_count = 0;
  129. System.current_pause_start = utc;
  130. } else {
  131. return 0; /* nothing to store */
  132. }
  133. } else {
  134. if (gpx.paused) {
  135. strcpy_P(buf, xml_trkseg_start);
  136. f_write(file, buf, strlen(buf), &bw);
  137. gpx.paused = 0;
  138. if (System.current_pause_start)
  139. System.pause_time += utc - System.current_pause_start;
  140. }
  141. time = get_iso_time(loc->time, 0);
  142. xsprintf(buf, PSTR("\t\t\t<trkpt lat=\"%.8f\" lon=\"%.8f\">\n\t\t\t\t<ele>%.2f</ele>\n\t\t\t\t<time>%s</time>\n"), loc->lat, loc->lon, loc->alt, time);
  143. strcat_P(buf, PSTR("\t\t\t</trkpt>\n"));
  144. }
  145. return f_write(file, buf, strlen(buf), &bw);
  146. }
  147. unsigned char gpx_close(FIL *file) {
  148. unsigned int bw;
  149. buf[0] = '\0';
  150. if (!gpx.paused)
  151. strcpy_P(buf, xml_trkseg_end);
  152. strcat_P(buf, PSTR("\t</trk>\n</gpx>\n"));
  153. f_write(file, buf, strlen(buf), &bw);
  154. return f_close(file);
  155. }
  156. void gpx_process_point(struct location_s *loc, FIL *file){
  157. float lon_est, lon_err, lat_est, lat_err, dist;
  158. struct location_s *ptr;
  159. struct location_s nloc;
  160. struct location_s filtered_loc;
  161. if (gpx.point_count < System.conf.skip_points) { /* Skipping initial points */
  162. gpx.point_count++;
  163. return;
  164. }
  165. /* Always apply Kalman filtering for distance calculation */
  166. lat_est = kalman_predict(&gpx.kalman[0], loc->lat);
  167. lon_est = kalman_predict(&gpx.kalman[1], loc->lon);
  168. filtered_loc.lat = lat_est;
  169. filtered_loc.lon = lon_est;
  170. filtered_loc.time = loc->time;
  171. filtered_loc.alt = loc->alt;
  172. if (get_flag(CONFFLAG_DISABLE_FILTERS)) {
  173. /* Write unfiltered data to GPX, but calculate distance from filtered data */
  174. xputs_P(PSTR("Write with filters disabled\r\n"));
  175. gpx_write(loc, file);
  176. /* Calculate distance and elevation from filtered points */
  177. if (gpx.last_distance_point.lat != 0) {
  178. float ele_change;
  179. dist = distance(&gpx.last_distance_point, &filtered_loc);
  180. add_distance(dist);
  181. ele_change = filtered_loc.alt - gpx.last_distance_point.alt;
  182. add_elevation(ele_change);
  183. }
  184. gpx.last_distance_point = filtered_loc;
  185. } else {
  186. /* Apply Kalman error check */
  187. lat_err = fabs(loc->lat - lat_est);
  188. lon_err = fabs(loc->lon - lon_est);
  189. // xprintf(PSTR("lat_err: %e, lon_err: %e, limit: %e\r\n"), lat_err, lon_err, (float)KALMAN_ERR_MAX);
  190. if(lat_err > KALMAN_ERR_MAX || lon_err > KALMAN_ERR_MAX){
  191. xputs_P(PSTR("KALMAN REJECT\r\n"));
  192. return;
  193. }
  194. prev_points_append(&filtered_loc);
  195. if(gpx.prev_points.count == PREV_POINTS_LENGTH){
  196. float dist12 = distance(prev_points_get(0), prev_points_get(1));
  197. float dist34 = distance(prev_points_get(2), prev_points_get(3));
  198. float dist32 = distance(prev_points_get(2), prev_points_get(1));
  199. xprintf(PSTR("New distance: %fm\r\n"), dist32);
  200. if(dist34 > dist12 && dist32 > dist12){
  201. xputs_P(PSTR("DISTANCE DIFF REJECT\r\n"));
  202. return;
  203. }
  204. ptr = prev_points_get(PREV_POINTS_LENGTH - 2);
  205. } else {
  206. if(gpx.prev_points.count >= PREV_POINTS_LENGTH-2){
  207. ptr = prev_points_get(gpx.prev_points.count - 2);
  208. xputs_P(PSTR("NEW\r\n"));
  209. } else {
  210. return;
  211. }
  212. }
  213. if(distance(&gpx.last_saved, ptr) < MIN_DIST_DELTA){
  214. xputs_P(PSTR("Too small position change REJECT\r\n"));
  215. return;
  216. }
  217. xputs_P(PSTR("ACCEPT\r\n"));
  218. /* Calculate distance and elevation for accepted point */
  219. if (gpx.last_distance_point.lat != 0) {
  220. float ele_change;
  221. dist = distance(&gpx.last_distance_point, ptr);
  222. add_distance(dist);
  223. ele_change = ptr->alt - gpx.last_distance_point.alt;
  224. add_elevation(ele_change);
  225. }
  226. gpx.last_distance_point = *ptr;
  227. gpx.avg_store.lat += ptr->lat;
  228. gpx.avg_store.lon += ptr->lon;
  229. if(gpx.avg_count == AVG_COUNT/2)
  230. gpx.avg_store.time = ptr->time;
  231. if(++gpx.avg_count == AVG_COUNT){
  232. nloc.lat = gpx.avg_store.lat / AVG_COUNT;
  233. nloc.lon = gpx.avg_store.lon / AVG_COUNT;
  234. nloc.time = gpx.avg_store.time;
  235. gpx.avg_count = 0;
  236. gpx.avg_store.lat = 0;
  237. gpx.avg_store.lon = 0;
  238. gpx.avg_store.time = 0;
  239. gpx.last_saved = nloc;
  240. gpx_write(&nloc, file);
  241. }
  242. }
  243. if (System.time_start == 0)
  244. System.time_start = utc;
  245. }
  246. void kalman_init(struct kalman_s *k){
  247. k->initialized = 0;
  248. k->P_last = 0;
  249. //the noise in the system
  250. k->Q = KALMAN_Q; // process variance
  251. k->R = KALMAN_R; // measurement variance
  252. k->K = 0;
  253. }
  254. float kalman_predict(struct kalman_s *k, float data){
  255. if(!k->initialized){
  256. //initial values for the kalman filter
  257. k->x_est_last = data;
  258. k->initialized = 1;
  259. return data;
  260. }
  261. //do a prediction
  262. float x_temp_est = k->x_est_last;
  263. float P_temp = k->P_last + k->Q;
  264. //calculate the Kalman gain
  265. k->K = P_temp * (1.0/(P_temp + k->R));
  266. //correct
  267. float x_est = x_temp_est + k->K * (data - x_temp_est);
  268. k->P_last = (1 - k->K) * P_temp;
  269. k->x_est_last = x_est;
  270. return x_est;
  271. }
  272. #define R_EARTH 6371e3 // m
  273. float distance(struct location_s *pos1, struct location_s *pos2){
  274. float lat1 = pos1->lat * M_PI / 180.0;
  275. float lat2 = pos2->lat * M_PI / 180.0;
  276. float dlat = (pos2->lat - pos1->lat) * M_PI / 180.0;
  277. float dlon = (pos2->lon - pos1->lon) * M_PI / 180.0;
  278. float a = sinf(dlat/2.0) * sinf(dlat/2.0) + cosf(lat1) * cosf(lat2) * sinf(dlon/2.0) * sinf(dlon/2.0);
  279. float c = 2 * atan2f(sqrtf(a), sqrtf(1-a));
  280. float ret = R_EARTH * c;
  281. return ret;
  282. }
  283. void add_distance(float dist) {
  284. unsigned char paused = System.tracking_paused || System.tracking_auto_paused;
  285. if (!paused)
  286. System.distance += (dist+0.005)*100.0;
  287. xprintf(PSTR("Distance: %f m; sum: %f m\r\n"), dist, System.distance/100.0);
  288. }
  289. void add_elevation(float ele_change) {
  290. unsigned char paused = System.tracking_paused || System.tracking_auto_paused;
  291. if (!paused) {
  292. if (ele_change > 0) {
  293. System.elevation_gain += (ele_change+0.05)*10.0;
  294. } else if (ele_change < 0) {
  295. System.elevation_loss += (-ele_change+0.05)*10.0;
  296. }
  297. }
  298. xprintf(PSTR("Elevation change: %f m; gain: %f m, loss: %f m\r\n"),
  299. ele_change, System.elevation_gain/10.0, System.elevation_loss/10.0);
  300. }