altos: Implement remote launch protocol
[fw/altos] / src / 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
20 __xdata uint16_t ao_launch_ignite;
21
22 static void
23 ao_launch_run(void)
24 {
25         for (;;) {
26                 while (!ao_launch_ignite)
27                         ao_sleep(&ao_launch_ignite);
28                 while (ao_launch_ignite) {
29                         ao_launch_ignite = 0;
30
31                         ao_ignition[ao_igniter_drogue].firing = 1;
32                         ao_ignition[ao_igniter_main].firing = 1;
33                         AO_IGNITER_DROGUE = 1;
34                         ao_delay(AO_MS_TO_TICKS(500));
35                         AO_IGNITER_DROGUE = 0;
36                         ao_ignition[ao_igniter_drogue].firing = 0;
37                         ao_ignition[ao_igniter_main].firing = 0;
38                 }
39         }
40 }
41
42 static void
43 ao_launch_status(void)
44 {
45         uint8_t i;
46         for (;;) {
47                 ao_delay(AO_SEC_TO_TICKS(1));
48                 if (ao_igniter_status(ao_igniter_drogue) == ao_igniter_ready) {
49                         if (ao_igniter_status(ao_igniter_main) == ao_igniter_ready) {
50                                 for (i = 0; i < 5; i++) {
51                                         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(50));
52                                         ao_delay(AO_MS_TO_TICKS(100));
53                                 }
54                         } else {
55                                 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
56                         }
57                 }
58         }
59 }
60
61 static __pdata uint8_t  ao_launch_armed;
62 static __pdata uint16_t ao_launch_arm_time;
63
64 static void
65 ao_launch(void)
66 {
67         static __xdata struct ao_launch_command command;
68         static __xdata struct ao_launch_query   query;
69         int16_t time_difference;
70
71         ao_led_off(AO_LED_RED);
72         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
73         for (;;) {
74                 if (ao_radio_cmac_recv(&command, sizeof (command), 0) != AO_RADIO_CMAC_OK)
75                         continue;
76                 
77                 printf ("tick %d serial %d cmd %d channel %d\n",
78                         command.tick, command.serial, command.cmd, command.channel);
79
80                 if (command.serial != ao_serial_number) {
81                         printf ("serial number mismatch\n");
82                         continue;
83                 }
84
85                 switch (command.cmd) {
86                 case AO_LAUNCH_QUERY:
87                         if (command.channel == 0) {
88                                 query.valid = 1;
89                                 query.arm_status = ao_igniter_status(ao_igniter_drogue);
90                                 query.igniter_status = ao_igniter_status(ao_igniter_main);
91                         } else {
92                                 query.valid = 0;
93                         }
94                         query.tick = ao_time();
95                         query.serial = ao_serial_number;
96                         query.channel = command.channel;
97                         printf ("query tick %d serial %d channel %d valid %d arm %d igniter %d\n",
98                                 query.tick, query.serial, query.channel, query.valid, query.arm_status,
99                                 query.igniter_status);
100                         ao_radio_cmac_send(&query, sizeof (query));
101                         break;
102                 case AO_LAUNCH_ARM:
103                         if (command.channel != 0)
104                                 break;
105                         time_difference = command.tick - ao_time();
106                         printf ("arm tick %d local tick %d\n", command.tick, ao_time());
107                         if (time_difference < 0)
108                                 time_difference = -time_difference;
109                         if (time_difference > 10) {
110                                 printf ("time difference too large %d\n", time_difference);
111                                 break;
112                         }
113                         printf ("armed\n");
114                         ao_launch_armed = 1;
115                         ao_launch_arm_time = ao_time();
116                         break;
117                 case AO_LAUNCH_FIRE:
118                         if (command.channel != 0)
119                                 break;
120                         if (!ao_launch_armed) {
121                                 printf ("not armed\n");
122                                 break;
123                         }
124                         if ((uint16_t) (ao_launch_arm_time - ao_time()) > AO_SEC_TO_TICKS(20)) {
125                                 printf ("late launch arm_time %d time %d\n",
126                                         ao_launch_arm_time, ao_time());
127                                 break;
128                         }
129                         time_difference = command.tick - ao_time();
130                         if (time_difference < 0)
131                                 time_difference = -time_difference;
132                         if (time_difference > 10) {
133                                 printf ("time different too large %d\n", time_difference);
134                                 break;
135                         }
136                         printf ("ignite\n");
137                         ao_launch_ignite = 1;
138                         ao_wakeup(&ao_launch_ignite);
139                         break;
140                 }
141         }
142 }
143
144 void
145 ao_launch_test(void)
146 {
147         switch (ao_igniter_status(ao_igniter_drogue)) {
148         case ao_igniter_ready:
149         case ao_igniter_active:
150                 printf ("Armed: ");
151                 switch (ao_igniter_status(ao_igniter_main)) {
152                 default:
153                         printf("unknown status\n");
154                         break;
155                 case ao_igniter_ready:
156                         printf("igniter good\n");
157                         break;
158                 case ao_igniter_open:
159                         printf("igniter bad\n");
160                         break;
161                 }
162                 break;
163         default:
164                 printf("Disarmed\n");
165         }
166 }
167
168 void
169 ao_launch_manual(void)
170 {
171         ao_cmd_white();
172         if (!ao_match_word("DoIt"))
173                 return;
174         ao_cmd_white();
175         ao_launch_ignite = 1;
176         ao_wakeup(&ao_launch_ignite);
177 }
178
179 static __xdata struct ao_task ao_launch_task;
180 static __xdata struct ao_task ao_launch_ignite_task;
181 static __xdata struct ao_task ao_launch_status_task;
182
183 __code struct ao_cmds ao_launch_cmds[] = {
184         { ao_launch_test,       "t\0Test launch continuity" },
185         { ao_launch_manual,     "i <key>\0Fire igniter. <key> is doit with D&I" },
186         { 0, NULL }
187 };
188
189 void
190 ao_launch_init(void)
191 {
192         AO_IGNITER_DROGUE = 0;
193         AO_IGNITER_MAIN = 0;
194         AO_IGNITER_DIR |= AO_IGNITER_DROGUE_BIT | AO_IGNITER_MAIN_BIT;
195         ao_cmd_register(&ao_launch_cmds[0]);
196         ao_add_task(&ao_launch_task, ao_launch, "launch listener");
197         ao_add_task(&ao_launch_ignite_task, ao_launch_run, "launch igniter");
198         ao_add_task(&ao_launch_status_task, ao_launch_status, "launch status");
199 }