]> git.gag.com Git - fw/altos/blob - src/kalman/load_csv.5c
altos: Add makefiles for telemega v6
[fw/altos] / src / kalman / load_csv.5c
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 namespace load_csv {
20         string[*] parse_data(file f) {
21                 while (!File::end(f)) {
22                         string l = File::fgets(f);
23                         if (l[0] == '#')
24                                 continue;
25                         return String::parse_csv(l);
26                 }
27                 return (string[0]) {};
28         }
29
30         public typedef struct {
31                 bool    done;
32                 real    time;
33                 real    height;
34                 real    acceleration;
35                 real    pressure;
36         } record_t;
37
38         public record_t parse_record(file f, real accel_scale) {
39                 string[*] data = parse_data(f);
40                 if (dim(data) == 0)
41                         return (record_t) { .done = true };
42                 int time_off = 4;
43                 int height_off = 11;
44                 int accel_off = 8;
45                 int pres_off = 9;
46                 switch (string_to_integer(data[0])) {
47                 case 2:
48                         time_off = 4;
49                         accel_off = 9;
50                         pres_off = 10;
51                         height_off = 12;
52                         break;
53                 case 5:
54                         time_off = 4;
55                         accel_off = 10;
56                         pres_off = 11;
57                         height_off = 13;
58                         break;
59                 }
60                 return (record_t) {
61                         .done = false,
62                         .time = string_to_real(data[time_off]),
63                         .height = imprecise(string_to_real(data[height_off])),
64                         .acceleration = imprecise(string_to_real(data[accel_off]) * accel_scale),
65                         .pressure = imprecise(string_to_real(data[pres_off]))
66                 };
67         }
68
69         public void dump(file f) {
70                 for (;;) {
71                         record_t        r = parse_record(f, 1);
72                         if (r.done)
73                                 break;
74                         printf ("%f %f %f\n", r.time, r.height, r.acceleration);
75                 }
76         }
77 }