Files @ 7033f0b40c6f
Branch filter:

Location: seniordesign-firmware/master/master/master.c

ethanzonca@CL-ENS241-08.cedarville.edu
Merge
/*
 * 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();
    }
}