altosdroid: initial implementation of telemetry logging.
[fw/altos] / src / cc1111 / ao_launch.c
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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao.h>
19 #include <ao_radio_cmac.h>
20
21 __xdata uint16_t ao_launch_ignite;
22
23 #if 0
24 #define PRINTD(...) printf(__VA_ARGS__)
25 #else
26 #define PRINTD(...) 
27 #endif
28
29 static void
30 ao_launch_run(void)
31 {
32         for (;;) {
33                 while (!ao_launch_ignite)
34                         ao_sleep(&ao_launch_ignite);
35                 ao_ignition[ao_igniter_drogue].firing = 1;
36                 ao_ignition[ao_igniter_main].firing = 1;
37                 AO_IGNITER_DIR |= AO_IGNITER_DROGUE_BIT | AO_IGNITER_MAIN_BIT;
38                 AO_IGNITER_DROGUE = 1;
39                 while (ao_launch_ignite) {
40                         ao_launch_ignite = 0;
41                         ao_delay(AO_MS_TO_TICKS(500));
42                 }
43                 AO_IGNITER_DROGUE = 0;
44                 ao_ignition[ao_igniter_drogue].firing = 0;
45                 ao_ignition[ao_igniter_main].firing = 0;
46         }
47 }
48
49 static void
50 ao_launch_status(void)
51 {
52         uint8_t i;
53         for (;;) {
54                 ao_delay(AO_SEC_TO_TICKS(1));
55                 if (ao_igniter_status(ao_igniter_drogue) == ao_igniter_ready) {
56                         if (ao_igniter_status(ao_igniter_main) == ao_igniter_ready) {
57                                 for (i = 0; i < 5; i++) {
58                                         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(50));
59                                         ao_delay(AO_MS_TO_TICKS(100));
60                                 }
61                         } else {
62                                 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
63                         }
64                 }
65         }
66 }
67
68 static __pdata uint8_t  ao_launch_armed;
69 static __pdata uint16_t ao_launch_arm_time;
70
71 static void
72 ao_launch(void)
73 {
74         static __xdata struct ao_launch_command command;
75         static __xdata struct ao_launch_query   query;
76         int16_t time_difference;
77
78         ao_led_off(AO_LED_RED);
79         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
80         for (;;) {
81                 flush();
82                 if (ao_radio_cmac_recv(&command, sizeof (command), 0) != AO_RADIO_CMAC_OK)
83                         continue;
84                 
85                 PRINTD ("tick %d serial %d cmd %d channel %d\n",
86                         command.tick, command.serial, command.cmd, command.channel);
87
88                 switch (command.cmd) {
89                 case AO_LAUNCH_QUERY:
90                         if (command.serial != ao_serial_number) {
91                                 PRINTD ("serial number mismatch\n");
92                                 break;
93                         }
94
95                         if (command.channel == 0) {
96                                 query.valid = 1;
97                                 query.arm_status = ao_igniter_status(ao_igniter_drogue);
98                                 query.igniter_status = ao_igniter_status(ao_igniter_main);
99                         } else {
100                                 query.valid = 0;
101                         }
102                         query.tick = ao_time();
103                         query.serial = ao_serial_number;
104                         query.channel = command.channel;
105                         PRINTD ("query tick %d serial %d channel %d valid %d arm %d igniter %d\n",
106                                 query.tick, query.serial, query.channel, query.valid, query.arm_status,
107                                 query.igniter_status);
108                         ao_radio_cmac_send(&query, sizeof (query));
109                         break;
110                 case AO_LAUNCH_ARM:
111                         if (command.serial != ao_serial_number) {
112                                 PRINTD ("serial number mismatch\n");
113                                 break;
114                         }
115
116                         if (command.channel != 0)
117                                 break;
118                         time_difference = command.tick - ao_time();
119                         PRINTD ("arm tick %d local tick %d\n", command.tick, ao_time());
120                         if (time_difference < 0)
121                                 time_difference = -time_difference;
122                         if (time_difference > 10) {
123                                 PRINTD ("time difference too large %d\n", time_difference);
124                                 break;
125                         }
126                         PRINTD ("armed\n");
127                         ao_launch_armed = 1;
128                         ao_launch_arm_time = ao_time();
129                         break;
130                 case AO_LAUNCH_FIRE:
131                         if (!ao_launch_armed) {
132                                 PRINTD ("not armed\n");
133                                 break;
134                         }
135                         if ((uint16_t) (ao_time() - ao_launch_arm_time) > AO_SEC_TO_TICKS(20)) {
136                                 PRINTD ("late launch arm_time %d time %d\n",
137                                         ao_launch_arm_time, ao_time());
138                                 break;
139                         }
140                         time_difference = command.tick - ao_time();
141                         if (time_difference < 0)
142                                 time_difference = -time_difference;
143                         if (time_difference > 10) {
144                                 PRINTD ("time different too large %d\n", time_difference);
145                                 break;
146                         }
147                         PRINTD ("ignite\n");
148                         ao_launch_ignite = 1;
149                         ao_wakeup(&ao_launch_ignite);
150                         break;
151                 }
152         }
153 }
154
155 void
156 ao_launch_test(void)
157 {
158         switch (ao_igniter_status(ao_igniter_drogue)) {
159         case ao_igniter_ready:
160         case ao_igniter_active:
161                 printf ("Armed: ");
162                 switch (ao_igniter_status(ao_igniter_main)) {
163                 default:
164                         printf("unknown status\n");
165                         break;
166                 case ao_igniter_ready:
167                         printf("igniter good\n");
168                         break;
169                 case ao_igniter_open:
170                         printf("igniter bad\n");
171                         break;
172                 }
173                 break;
174         default:
175                 printf("Disarmed\n");
176         }
177 }
178
179 void
180 ao_launch_manual(void)
181 {
182         ao_cmd_white();
183         if (!ao_match_word("DoIt"))
184                 return;
185         ao_cmd_white();
186         ao_launch_ignite = 1;
187         ao_wakeup(&ao_launch_ignite);
188 }
189
190 static __xdata struct ao_task ao_launch_task;
191 static __xdata struct ao_task ao_launch_ignite_task;
192 static __xdata struct ao_task ao_launch_status_task;
193
194 __code struct ao_cmds ao_launch_cmds[] = {
195         { ao_launch_test,       "t\0Test launch continuity" },
196         { ao_launch_manual,     "i <key>\0Fire igniter. <key> is doit with D&I" },
197         { 0, NULL }
198 };
199
200 void
201 ao_launch_init(void)
202 {
203         AO_IGNITER_DROGUE = 0;
204         AO_IGNITER_MAIN = 0;
205         AO_IGNITER_DIR |= AO_IGNITER_DROGUE_BIT | AO_IGNITER_MAIN_BIT;
206         ao_cmd_register(&ao_launch_cmds[0]);
207         ao_add_task(&ao_launch_task, ao_launch, "launch listener");
208         ao_add_task(&ao_launch_ignite_task, ao_launch_run, "launch igniter");
209         ao_add_task(&ao_launch_status_task, ao_launch_status, "launch status");
210 }