Files
@ 7033f0b40c6f
Branch filter:
Location: seniordesign-firmware/master/master/master.c
7033f0b40c6f
4.4 KiB
text/plain
Merge
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 | /*
* Master Firmware
*
* This file is part of OpenTrack.
*
* OpenTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with OpenTrack. If not, see <http://www.gnu.org/licenses/>.
*
* Ethan Zonca
* Matthew Kanning
* Kyle Ripperger
* Matthew Kroening
*
*/
#include "config.h"
#include <avr/io.h>
#include <util/delay.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "lib/serial.h"
#include "lib/aprs.h"
#include "lib/afsk.h"
#include "lib/led.h"
#include "lib/logger.h"
#include "lib/watchdog.h"
#include "lib/gps.h"
#include "lib/i2c.h"
#include "lib/sensors.h"
#include "lib/heater.h"
#include "lib/looptime.h"
#include "lib/slavesensors.h"
#include "lib/serparser.h"
#include "lib/sensordata.h"
#include "lib/iniparse.h"
int main(void)
{
// Initialize libraries
time_setup();
watchdog_setup(); // enables interrupts
// Power debounce
_delay_ms(1000);
led_setup();
gps_setup();
serial0_setup();
serial1_setup();
i2c_init();
sensordata_setup(); // must happen before slavesensors/logger/AFSK
slavesensors_setup();
sensors_setup();
logger_setup();
watchdog_checkreset();
iniparse_getconfig();
afsk_setup();
serial0_sendString("Hello.\n\n");
// ZigBee network discovery (blocking ~6s)
slavesensors_network_scan();
// Software timers
uint32_t lastAprsBroadcast = 0;
uint32_t lastLog = 0;
uint32_t lastLedCycle = 0;
uint32_t lastDataReq = 0;
uint32_t lastBuzz = 0;
uint32_t lastBuzzOn = 0;
bool buzz = false;
// Result of last parser run
int parseResult = PARSERESULT_NODATA;
while(1)
{
// Periodic: LED execution indicator
if(time_millis() - lastLedCycle > LEDCYCLE_RATE)
{
led_power_toggle();
led_spin();
// Enable GPS serial interrupts if we aren't doing AFSK
if(!afsk_busy())
serial1_ion();
lastLedCycle = time_millis();
}
// Periodic: Logging
if(time_millis() - lastLog > LOGGER_RATE)
{
led_on(LED_CYCLE);
heater_regulateTemp();
// Turn on sideboard LED if we have a fix
if(gps_hasfix())
{
led_on(LED_SIDEBOARD);
}
else
{
led_off(LED_SIDEBOARD);
}
// Read board temperature and battery level
sensors_readBoardTemp();
sensors_readBatt();
// Write CSV header and log data values
sensordata_logvalues();
led_off(LED_CYCLE);
lastLog = time_millis();
}
// Periodic: Buzzer
if(time_millis() - lastBuzz > BUZZER_RATE)
{
if(sensordata_isTouchdown())
{
led_on(LED_BUZZ);
lastBuzzOn = time_millis();
buzz = true;
}
lastBuzz = time_millis();
}
if(buzz && time_millis() - lastBuzzOn > BUZZER_DURATION)
{
led_off(LED_BUZZ);
buzz = false;
}
// Periodic: Data Request
if(time_millis() - lastDataReq > sysconfig->datarequest_rate)
{
// Start getting values for next transmission
if(slavesensors_isrequesting())
{
// This probably is a non-issue
//error_log_msg(ERROR_FATAL, false, "Still requesting on following loop");
}
else
{
slavesensors_startprocess();
}
lastDataReq = time_millis();
}
// Periodic: APRS transmission
if(time_millis() - lastAprsBroadcast > sysconfig->aprs_transmit_period)
{
// Check for touchdown
sensordata_checkTouchdown();
// Ensure we aren't already transmitting
while(afsk_busy());
// Turn off interrupts and transmit APRS sentence
serial1_ioff();
aprs_send(); // non-blocking
lastAprsBroadcast = time_millis();
}
// Periodic: Blackout timer check
bool black = false;
if(sysconfig->blackout_enable && !black && time_millis() > sysconfig->blackout_timeout)
{
// LED blackout
led_blackout();
black = true;
}
// Parse any serial data in the XBee software buffer
parseResult = serparser_parse();
slavesensors_process(parseResult);
// Parse any NMEA messages in the GPS software buffer
parse_gps_transmission();
wdt_reset();
}
}
|