altos: Fix ability to disable telemetry by setting interval to 0
[fw/altos] / src / kernel / ao_telemetry.c
index a1c19185416fd56cdd3a1dac6bcabf892ef8ec0c..27306a344c27a0cdb27b7e2bf19789e1fef4c97f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2011 Keith Packard <keithp@keithp.com>
+ * Copyright © 2011 Keth Packard <keithp@keithp.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 static __pdata uint16_t ao_telemetry_interval;
 
+#if HAS_RADIO_RATE
+static __xdata uint16_t ao_telemetry_desired_interval;
+#endif
+
 #if HAS_RDF
 static __pdata uint8_t ao_rdf = 0;
 static __pdata uint16_t ao_rdf_time;
@@ -64,7 +68,7 @@ static void
 ao_send_sensor(void)
 {
        __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
-                       
+
        telemetry.generic.tick = packet->tick;
        telemetry.generic.type = AO_TELEMETRY_SENSOR;
 
@@ -106,12 +110,13 @@ ao_send_sensor(void)
 
 
 #ifdef AO_SEND_MEGA
+
 /* Send mega sensor packet */
 static void
 ao_send_mega_sensor(void)
 {
        __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
-                       
+
        telemetry.generic.tick = packet->tick;
        telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR;
 
@@ -240,7 +245,7 @@ static void
 ao_send_mini(void)
 {
        __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
-                       
+
        telemetry.generic.tick = packet->tick;
        telemetry.generic.type = AO_TELEMETRY_MINI;
 
@@ -305,8 +310,14 @@ ao_send_configuration(void)
 #endif
                telemetry.configuration.config_major = AO_CONFIG_MAJOR;
                telemetry.configuration.config_minor = AO_CONFIG_MINOR;
+#if AO_idProduct_NUMBER == 0x25 && HAS_ADC
+               /* TeleGPS gets battery voltage instead of apogee delay */
+               telemetry.configuration.apogee_delay = ao_data_ring[ao_data_ring_prev(ao_data_head)].adc.v_batt;
+#else
                telemetry.configuration.apogee_delay = ao_config.apogee_delay;
                telemetry.configuration.main_deploy = ao_config.main_deploy;
+#endif
+
                telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
                ao_xmemcpy (telemetry.configuration.callsign,
                            ao_config.callsign,
@@ -333,7 +344,7 @@ ao_send_location(void)
                ao_mutex_get(&ao_gps_mutex);
                ao_xmemcpy(&telemetry.location.flags,
                       &ao_gps_data.flags,
-                      26);
+                      27);
                telemetry.location.tick = ao_gps_tick;
                ao_mutex_put(&ao_gps_mutex);
                ao_radio_send(&telemetry, sizeof (telemetry));
@@ -484,12 +495,33 @@ ao_telemetry(void)
        }
 }
 
+#if HAS_RADIO_RATE
+void
+ao_telemetry_reset_interval(void)
+{
+       ao_telemetry_set_interval(ao_telemetry_desired_interval);
+}
+#endif
+
 void
 ao_telemetry_set_interval(uint16_t interval)
 {
        int8_t  cur = 0;
+
+#if HAS_RADIO_RATE
+       /* Limit max telemetry rate based on available radio bandwidth.
+        */
+       static __xdata const uint16_t min_interval[] = {
+               /* [AO_RADIO_RATE_38400] = */ AO_MS_TO_TICKS(100),
+               /* [AO_RADIO_RATE_9600] = */ AO_MS_TO_TICKS(500),
+               /* [AO_RADIO_RATE_2400] = */ AO_MS_TO_TICKS(1000)
+       };
+
+       ao_telemetry_desired_interval = interval;
+       if (interval && interval < min_interval[ao_config.radio_rate])
+               interval = min_interval[ao_config.radio_rate];
+#endif
        ao_telemetry_interval = interval;
-       
 #if AO_SEND_MEGA
        if (interval > 1)
                ao_telemetry_mega_data_max = 1;
@@ -515,7 +547,7 @@ ao_telemetry_set_interval(uint16_t interval)
        ao_telemetry_companion_cur = cur;
 #endif
 
-       ao_telemetry_config_max = AO_SEC_TO_TICKS(1) / interval;
+       ao_telemetry_config_max = AO_SEC_TO_TICKS(5) / interval;
 #if HAS_COMPANION
        if (ao_telemetry_config_max > cur)
                cur++;