Files
@ 1ecaddb549ab
Branch filter:
Location: FeatherHAB/wsprhab/src/main.c
1ecaddb549ab
6.7 KiB
text/plain
Disable timer clocks, fix transmission issues
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | //
// WSPRHAB: Minimal high-altitude balloon tracker with WSPR telemetry
//
#include "stm32f0xx_hal.h"
#include "adc.h"
#include "system.h"
#include "i2c.h"
#include "uart.h"
#include "gpio.h"
#include "wspr.h"
#include "rtc.h"
#include "gps.h"
// We have access to the 1PPS pin of the gps... could have trim routine for internal oscillator based on this when we have a fix
// Probabl wake up 1 minute early -- 0.45min possible +/- on wakeup time with 15min sync intervals
enum _state
{
SYSTEM_IDLE = 0, // awaiting RTC interrupt for wakeup TODO wake up before scheduled time to get fix?
SYSTEM_GPSACQ, // RTC interrupted
SYSTEM_WSPRTX, // Wait for timeslot and actually transmit the message
};
static void __calc_gridloc(char *dst, double lat, double lon);
static void ledpulse(void);
uint32_t statled_ontime = 0;
int main(void)
{
HAL_Init();
sysclk_init();
rtc_init();
gpio_init();
adc_init();
wspr_init();
uint32_t led_timer = HAL_GetTick();
led_blink(4);
uint16_t blink_rate = BLINK_FAST;
uint8_t state = SYSTEM_GPSACQ;
uint32_t gps_polltimer = 0;
uint32_t fix_acq_starttime = 0;
uint32_t nextwspr_time = 0;
uint32_t last_wspr_tx_time = 0;
uint8_t fix_ok = 0;
uint8_t numsats = 0;
while (1)
{
// Every 10 minutes, wake up and try to wspr
if(state == SYSTEM_IDLE && (HAL_GetTick() - last_wspr_tx_time > 60000 * 10))
{
state = SYSTEM_GPSACQ;
}
// Update fix status every 2 seconds
if(HAL_GetTick() - gps_polltimer > 2000)
{
if(gps_ison())
{
gps_update_data();
// If odd minute
if(gps_getdata()->minute % 2)
{
// Wait until even minute, coming soon
nextwspr_time = HAL_GetTick() + (60000 - (gps_getdata()->second * 1000));
}
// If even minute
else
{
// Wait until odd minute, one minute and some change away
nextwspr_time = HAL_GetTick() + 60000 + (60000 - (gps_getdata()->second * 1000));
}
}
gps_polltimer = HAL_GetTick();
}
switch(state)
{
// Idling: sleep and wait for RTC timeslot trigger
case SYSTEM_IDLE:
{
blink_rate = BLINK_SLOW;
HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);
// Wait for RTC wakeup interrupt
//wfi();
//enter_sleep();
// Somehow go to another state when we get an interrupt
// state = SYSTEM_GPSACQ;
} break;
// Attempt to acquire GPS fix
case SYSTEM_GPSACQ:
{
blink_rate = BLINK_FAST;
if(!gps_ison())
{
fix_acq_starttime = HAL_GetTick();
gps_poweron(); // power on and initialize GPS module
}
// If 3d fix with a decent enough precision
if( ((gps_getdata()->fixtype == 2) || (gps_getdata()->fixtype == 3)) && gps_getdata()->pdop < 10)
{
// Disable GPS module
gps_poweroff();
// TODO: Set RTC from GPS time
// TODO: Set RTC for countdown to next transmission timeslot!
// TODO: Set wspr countdown timer for this transmission!
fix_acq_starttime = 0;
state = SYSTEM_WSPRTX;
}
// If no decent fix in 3 minutes
else if(HAL_GetTick() - fix_acq_starttime > 60000 * 3)
{
// Flash error code and go to idle, try again next time
led_blink(4);
gps_poweroff();
fix_acq_starttime = 0;
last_wspr_tx_time = HAL_GetTick(); // repeat acq/tx cycle after big time delay
state = SYSTEM_IDLE;
}
} break;
// Wait for wspr timeslot and start transmitting
case SYSTEM_WSPRTX:
{
blink_rate = BLINK_MED;
// Wait for wspr countdown timer to expire and go to tx
// if(timeout_expired)
// {
// If we're after the minute but not more than 2s after the minute, start tx
if(HAL_GetTick() >= nextwspr_time)
{
if(HAL_GetTick() < nextwspr_time + 2000)
{
volatile double latitude_flt = (double)gps_getdata()->latitude / 10000000.0;
volatile double longitude_flt = (double)gps_getdata()->longitude / 10000000.0;
volatile uint8_t grid_locator[7];
__calc_gridloc(grid_locator, latitude_flt, longitude_flt);
last_wspr_tx_time = HAL_GetTick();
wspr_transmit(grid_locator);
state = SYSTEM_IDLE;
}
else
{
// Window was missed, go back to idle, and try again after time delay
last_wspr_tx_time = HAL_GetTick();
state = SYSTEM_IDLE;
}
}
// Schedule next wakeup (maybe 2mins prior ot timeslot if no osc trim)
// Next wakeup should enter SYSTEM_GPSACQ state...
} break;
}
if(HAL_GetTick() - led_timer > blink_rate)
{
ledpulse();
led_timer = HAL_GetTick();
}
if(statled_ontime && HAL_GetTick() - statled_ontime > 10)
{
HAL_GPIO_WritePin(LED_BLUE, 0);
statled_ontime = 0;
}
}
}
static void ledpulse(void)
{
HAL_GPIO_WritePin(LED_BLUE, 1);
statled_ontime = HAL_GetTick();
}
static void __calc_gridloc(char *dst, double lat, double lon)
{
int o1, o2, o3;
int a1, a2, a3;
double remainder;
// longitude
remainder = lon + 180.0;
o1 = (int)(remainder / 20.0);
remainder = remainder - (double)o1 * 20.0;
o2 = (int)(remainder / 2.0);
remainder = remainder - 2.0 * (double)o2;
o3 = (int)(12.0 * remainder);
// latitude
remainder = lat + 90.0;
a1 = (int)(remainder / 10.0);
remainder = remainder - (double)a1 * 10.0;
a2 = (int)(remainder);
remainder = remainder - (double)a2;
a3 = (int)(24.0 * remainder);
dst[0] = (char)o1 + 'A';
dst[1] = (char)a1 + 'A';
dst[2] = (char)o2 + '0';
dst[3] = (char)a2 + '0';
dst[4] = (char)o3 + 'A';
dst[5] = (char)a3 + 'A';
dst[6] = (char)0;
}
|