altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / ao-tools / ao-test-igniter / ao-test-igniter.c
1 /*
2  * Copyright © 2014 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 #include <err.h>
20 #include <fcntl.h>
21 #include <gelf.h>
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <sysexits.h>
26 #include <unistd.h>
27 #include <getopt.h>
28 #include <string.h>
29 #include <stdbool.h>
30 #include <ctype.h>
31 #include "ao-elf.h"
32 #include "ccdbg.h"
33 #include "cc-usb.h"
34 #include "cc.h"
35 #include "ao-verbose.h"
36
37 static const struct option options[] = {
38         { .name = "tty", .has_arg = 1, .val = 'T' },
39         { .name = "device", .has_arg = 1, .val = 'D' },
40         { .name = "raw", .has_arg = 0, .val = 'r' },
41         { .name = "verbose", .has_arg = 1, .val = 'v' },
42         { .name = "rplus", .has_arg = 1, .val = 'a' },
43         { .name = "rminus", .has_arg = 1, .val = 'b' },
44         { .name = "adcmax", .has_arg = 1, .val = 'm' },
45         { 0, 0, 0, 0},
46 };
47
48 static void usage(char *program)
49 {
50         fprintf(stderr, "usage: %s [--verbose=<verbose>] [--device=<device>] [--tty=<tty>] [--rplus=val] [--rminus=val] [--adcmax=val] main|drogue\n", program);
51         exit(1);
52 }
53
54 static void
55 done(struct cc_usb *cc, int code)
56 {
57 /*      cc_usb_printf(cc, "a\n"); */
58         cc_usb_close(cc);
59         exit (code);
60 }
61
62 struct igniter {
63         struct igniter  *next;
64         char            name[512];
65         char            status[512];
66         int             adc;
67 };
68
69 static bool
70 map_igniter_name(char *adc_name, char *igniter_name)
71 {
72         char    *colon = strchr(adc_name, ':');
73         if (!colon)
74                 return false;
75         *colon = '\0';
76         if (strlen(adc_name) == 1 && isupper(adc_name[0])) {
77                 igniter_name[0] = '0' + adc_name[0] - 'A';
78                 igniter_name[1] = '\0';
79         } else {
80                 strcpy(igniter_name, adc_name);
81         }
82         return true;
83 }
84
85 static const char *
86 other_igniter_name(const char *name)
87 {
88         if (!strcmp(name, "drogue"))
89                 return "apogee";
90         if (!strcmp(name, "apogee"))
91                 return "drogue";
92         return name;
93 }
94
95 static struct igniter *
96 igniters(struct cc_usb *usb)
97 {
98         struct igniter  *head = NULL, **tail = &head;
99         cc_usb_printf(usb, "t\na\nv\n");
100         for (;;) {
101                 char    line[512];
102                 char    name[512];
103                 char    status[512];
104                 char    adc_name[512];
105                 char    igniter_name[512];
106
107                 cc_usb_getline(usb, line, sizeof (line));
108                 if (strstr(line, "software-version"))
109                         break;
110                 if (sscanf(line, "Igniter: %s Status: %s", name, status) == 2) {
111                         struct igniter  *i = calloc (1, sizeof (struct igniter));
112                         strcpy(i->name, name);
113                         strcpy(i->status, status);
114                         i->next = NULL;
115                         *tail = i;
116                         tail = &i->next;
117                 }
118                 if (strncmp(line, "tick:", 5) == 0) {
119                         char    *tok;
120                         char    *l = line;
121                         bool    found_igniter = false;
122
123                         while ((tok = strtok(l, " ")) != NULL) {
124                                 l = NULL;
125                                 if (found_igniter) {
126                                         struct igniter *i;
127                                         for (i = head; i; i = i->next)
128                                                 if (!strcmp(i->name, igniter_name) ||
129                                                     !strcmp(i->name, other_igniter_name(igniter_name)))
130                                                 {
131                                                         i->adc = atoi(tok);
132                                                         break;
133                                                 }
134                                         found_igniter = false;
135                                 } else {
136                                         strcpy(adc_name, tok);
137                                         found_igniter = map_igniter_name(adc_name, igniter_name);
138                                 }
139                         }
140                 }
141         }
142         return head;
143 }
144
145 static void
146 free_igniters(struct igniter *i) {
147         struct igniter *n;
148
149         while (i) {
150                 n = i->next;
151                 free(i);
152                 i = n;
153         }
154 }
155
156 static struct igniter *
157 find_igniter(struct igniter *i, char *name)
158 {
159         for (; i; i = i->next)
160                 if (strcmp(i->name, name) == 0) {
161                         printf("igniter %s adc %d\n", i->name, i->adc);
162                         return i;
163                 }
164         return NULL;
165 }
166
167 static const double ref_volts = 3.3;
168
169 static double
170 compute_voltage(int adc, double rplus, double rminus, int adc_max)
171 {
172         return (double) adc / (double) adc_max * ref_volts * (rplus + rminus) / rminus;
173 }
174
175 static int
176 do_igniter(struct cc_usb *usb, char *name, double rplus, double rminus, int adc_max)
177 {
178         struct igniter  *all = igniters(usb);
179         struct igniter  *this = find_igniter(all, name);
180         double volts = -1;
181         if (!this) {
182                 struct igniter  *i;
183                 printf("no igniter %s found in", name);
184                 for (i = all; i; i = i->next)
185                         printf(" %s", i->name);
186                 printf("\n");
187                 free_igniters(all);
188                 return 0;
189         }
190         if (rplus && rminus && adc_max) {
191                 volts = compute_voltage(this->adc, rplus, rminus, adc_max);
192                 if (volts < 1 || volts > 4) {
193                         printf("igniter %s voltage is %f, not in range of 1-4 volts\n", this->name, volts);
194                         free_igniters(all);
195                         return 0;
196                 }
197         }
198         if (strcmp(this->status, "open") == 0) {
199                 printf("igniter %s status is %s\n", this->name, this->status);
200                 free_igniters(all);
201                 return 0;
202         }
203         cc_usb_printf(usb, "i DoIt %s\n", this->name);
204         cc_usb_sync(usb);
205         free_igniters(all);
206         usleep(200000);
207         return 1;
208 }
209
210 int
211 main (int argc, char **argv)
212 {
213         char                    *device = NULL;
214         int                     i;
215         int                     c;
216         struct cc_usb           *cc = NULL;
217         char                    *tty = NULL;
218         int                     verbose = 0;
219         int                     ret = 0;
220         double                  rplus = 0.0;
221         double                  rminus = 0.0;
222         int                     adcmax = 0;
223
224         while ((c = getopt_long(argc, argv, "rT:D:c:s:v:a:b:m:", options, NULL)) != -1) {
225                 switch (c) {
226                 case 'T':
227                         tty = optarg;
228                         break;
229                 case 'D':
230                         device = optarg;
231                         break;
232                 case 'v':
233                         verbose++;
234                         break;
235                 case 'a':
236                         rplus = strtod(optarg, NULL);
237                         break;
238                 case 'b':
239                         rminus = strtod(optarg, NULL);
240                         break;
241                 case 'm':
242                         adcmax = strtol(optarg, NULL, 0);
243                         break;
244                 default:
245                         usage(argv[0]);
246                         break;
247                 }
248         }
249
250         ao_verbose = verbose;
251
252         if (verbose > 1)
253                 ccdbg_add_debug(CC_DEBUG_BITBANG);
254
255         if (!tty)
256                 tty = cc_usbdevs_find_by_arg(device, "TeleMega-v1.0");
257         if (!tty)
258                 tty = cc_usbdevs_find_by_arg(device, "TeleMetrum-v2.0");
259         if (!tty)
260                 tty = cc_usbdevs_find_by_arg(device, "TeleMini-v2.0");
261         if (!tty)
262                 tty = cc_usbdevs_find_by_arg(device, "EasyMega-v1.0");
263         if (!tty)
264                 tty = cc_usbdevs_find_by_arg(device, "EasyMetrum-v1.0");
265         if (!tty)
266                 tty = cc_usbdevs_find_by_arg(device, "EasyMini-v1.0");
267         if (!tty)
268                 tty = getenv("ALTOS_TTY");
269         if (!tty)
270                 tty="/dev/ttyACM0";
271
272         cc = cc_usb_open(tty);
273
274         if (!cc)
275                 exit(1);
276
277         for (i = optind; i < argc; i++) {
278                 char    *name = argv[i];
279
280                 if (!do_igniter(cc, name, rplus, rminus, adcmax))
281                         ret++;
282         }
283         done(cc, ret);
284 }