Changeset - 91a9664e6fd0
[Not reviewed]
default
0 9 0
ethanzonca@CL-ENS241-08.cedarville.edu - 12 years ago 2013-01-24 15:10:36
ethanzonca@CL-ENS241-08.cedarville.edu
Adaptive logging system now fully functional
9 files changed with 143 insertions and 61 deletions:
0 comments (0 inline, 0 general)
master/master/lib/gps.c
Show inline comments
 
/*
 
* gpsMKa.c
 
*
 
* Created: 11/15/2012 12:02:38 PM
 
*  Author: mkanning
 
*/
 
/*
 
 * Master Firmware: NMEA Parser
 
 *
 
 * Wireless Observational Modular Aerial Network
 
 * 
 
 * Ethan Zonca
 
 * Matthew Kanning
 
 * Kyle Ripperger
 
 * Matthew Kroening
 
 *
 
 */
 
 
#include <stdbool.h>
 
#include <string.h>
 
#include <stdio.h>
 
#include <avr/io.h>
 
#include <avr/interrupt.h>
 
#include "gps.h"
 
#include "serial.h"
 
#include "../config.h"
 
#include "led.h"
 
 
// Circular buffer for incoming data
 
uint8_t nmeaBuffer[NMEABUFFER_SIZE];
 
 
// Location of parser in the buffer
 
uint8_t nmeaBufferParsePosition = 0;
 
 
// Location of receive byte interrupt in the buffer
 
volatile uint16_t nmeaBufferDataPosition = 0;
 

	
 
// holds the byte ALREADY PARSED. includes starting character
 
int bytesReceived = 0;
 

	
 
//data (and checksum) of most recent transmission
 
char data[16];
 

	
 
//used to skip over bytes during parse
 
int skipBytes = 0;
 
@@ -107,56 +114,56 @@ enum decodeState {
 
	GGA_WGS84,
 
	GGA_LAST_UPDATE,
 
	GGA_STATION_ID,
 
	//RMC data fields
 
	RMC_TIME,
 
	RMC_VALIDITY,
 
	RMC_LATITUDE,
 
	RMC_LONGITUDE,
 
	RMC_KNOTS,
 
	RMC_COURSE,
 
	RMC_DATE,
 
	RMC_MAG_VARIATION,
 
	
 
}decodeState;
 

	
 

	
 
ISR(USART1_RX_vect)
 
{
 
	nmeaBuffer[nmeaBufferDataPosition % NMEABUFFER_SIZE] = UDR1;
 
	nmeaBufferDataPosition = (nmeaBufferDataPosition + 1) % NMEABUFFER_SIZE;
 
}
 

	
 
void gps_setup() 
 
{
 
	sprintf(timestamp, "0");
 
	sprintf(latitude, "0");
 
	sprintf(longitude, "0");
 
	sprintf(numSatellites, "0");
 
	sprintf(hdop, "0");
 
	sprintf(knots, "0");
 
	sprintf(course, "0");
 
	sprintf(dayofmonth, "0");
 
	snprintf(timestamp,2, "0");
 
	snprintf(latitude,2, "0");
 
	snprintf(longitude,2, "0");
 
	snprintf(numSatellites,2, "0");
 
	snprintf(hdop,2, "0");
 
	snprintf(knots,2, "0");
 
	snprintf(course,2, "0");
 
	snprintf(dayofmonth,2, "0");
 
}
 

	
 

	
 
// Could inline if program space available
 
static void setParserState(uint8_t state)
 
{
 
	decodeState = state;
 
 
	
 
	// If resetting, clear vars
 
	if(state == INITIALIZE)
 
	{
 
		calculatedChecksum = 0;
 
	}
 
	
 
	// Every time we change state, we have parsed a byte
 
	nmeaBufferParsePosition = (nmeaBufferParsePosition + 1) % NMEABUFFER_SIZE;
 
}
 

	
 

	
 

	
 
//// MKa GPS transmission parser START
 
void parse_gps_transmission(void){
 
	
 
@@ -653,28 +660,29 @@ void parse_gps_transmission(void){
 
			}
 
			else //store data
 
			{
 
				setParserState(GPS_CHECKSUM);
 
				checksum[numBytes] = byte; //adjust number of bytes to fit array
 
				numBytes++;
 
			}
 
		}
 
		else {
 
			setParserState(INITIALIZE);
 
		}
 
	
 
		if (decodeState!=GPS_CHECKSUM && decodeState!=INITIALIZE)	//want bytes between '$' and '*'
 
		{
 
			//input byte into running checksum
 
			XORbyteWithChecksum(byte);
 
		}
 
		led_off(LED_ACTIVITY);
 
	}	
 
	
 

	
 
}
 
/// MKa GPS transmission parser END
 

	
 
void XORbyteWithChecksum(uint8_t byte){
 
void XORbyteWithChecksum(uint8_t byte)
 
{
 
	calculatedChecksum ^= (int)byte; //this may need to be re-coded
 
}
 

	
master/master/lib/gps.h
Show inline comments
 
/*
 
 * gpsMKa.h
 
 *
 
 * Created: 11/15/2012 12:02:53 PM
 
 *  Author: mkanning
 
 */ 
 
/*
 
 * Master Firmware: NMEA Parser
 
 *
 
 * Wireless Observational Modular Aerial Network
 
 * 
 
 * Ethan Zonca
 
 * Matthew Kanning
 
 * Kyle Ripperger
 
 * Matthew Kroening
 
 *
 
 */
 
 
 
#ifndef GPSMKA_H_
 
#define GPSMKA_H_
 
#define GGA_MESSAGE
 
#define RMC_MESSAGE
 
#define UKN_MESSAGE
 
 
void gps_setup();
 
char* get_longitude();
 
char* get_latitude();
 
char* get_timestamp();
 
char* get_speedKnots();
 
char* get_course();
 
char* get_hdop();
 
char* get_sv();
 
char* get_dayofmonth();
 
void parse_gps_transmission(void);
 
void XORbyteWithChecksum(uint8_t byte);
 
 
#endif /* GPSMKA_H_ */
 
\ No newline at end of file
master/master/lib/heater.c
Show inline comments
 
/*
 
 * Master Firmware: Board Heater
 
 *
 
 * Wireless Observational Modular Aerial Network
 
 * 
 
 * Ethan Zonca
 
 * Matthew Kanning
 
 * Kyle Ripperger
 
 * Matthew Kroening
 
 *
 
 */
 
 
#include "../config.h"
 
#include "led.h"
 
#include "boardtemp.h"
 
 
 
 void heater_regulateTemp()
 
 {
 
	 // Gets board temperature and enables heater if below threshold
 
	 if (sensors_getBoardTemp() <= HEATER_THRESHOLD)
 
	 {
 
		 led_on(LED_HEAT);
 
		 led_on(LED_STATUS);
 
	 }
 
	 else if (sensors_getBoardTemp() > (HEATER_THRESHOLD + 5))
 
	 {
 
		 led_off(LED_HEAT);
 
		 led_off(LED_STATUS);
 
	 }
 
 }
 
\ No newline at end of file
master/master/lib/logger.c
Show inline comments
 
@@ -2,48 +2,49 @@
 
 * Master Firmware: SD Card Data Logger
 
 *
 
 * Wireless Observational Modular Aerial Network
 
 * 
 
 * Ethan Zonca
 
 * Matthew Kanning
 
 * Kyle Ripperger
 
 * Matthew Kroening
 
 *
 
 */
 
 
#include "../config.h"
 
#include <util/delay.h>
 
#include <string.h>
 
#include <stdio.h>
 
#include <avr/pgmspace.h>
 
#include <avr/sleep.h>
 
#include <avr/eeprom.h>
 
#include <string.h>
 
#include "sdcard/fat.h"
 
#include "sdcard/fat_config.h"
 
#include "sdcard/partition.h"
 
#include "sdcard/sd_raw.h"
 
#include "sdcard/sd_raw_config.h"
 
#include "serial.h"
 
#include "logger.h"
 
#include "led.h"
 
 
struct partition_struct* partition;
 
struct fat_fs_struct* fs;
 
struct fat_dir_entry_struct directory;
 
struct fat_dir_struct* dd;
 
struct fat_file_struct* fd;
 
 
void logger_setup()
 
{
 
 
	if(!sd_raw_init())
 
	{
 
		// Initializing SD card failed!
 
		#ifdef DEBUG_OUTPUT
 
		serial0_sendString("SD> Error initializing.\r\n");
 
		#endif
 
		
 
		led_errorcode(ERROR_SD_INIT);
 
		return;
 
	}
 
 
	// TODO: Check SD card switch to see if inserted.
master/master/lib/sensordata.c
Show inline comments
 
/*
 
 * Master Firmware: Sensor Data
 
 *
 
 * Wireless Observational Modular Aerial Network
 
 * 
 
 * Ethan Zonca
 
 * Matthew Kanning
 
 * Kyle Ripperger
 
 * Matthew Kroening
 
 *
 
 */
 

	
 
#include "../config.h"
 
#include <stdio.h>
 
#include "sensordata.h"
 
#include "boardtemp.h"
 
#include "gps.h"
 

	
 
int16_t slaves[MAX_NUM_SLAVES][MAX_NUM_SENSORS];
 

	
 
void sensordata_setup() 
 
{
 
	for(int i=0; i<MAX_NUM_SLAVES; i++) {
 
		for(int j=0; j<MAX_NUM_SENSORS; j++) {
 
			slaves[i][j] = -32768; // minimum value of 16 bit integer
 
		}
 
	}
 
}
 
 
void sensordata_set(uint8_t nodeID, uint8_t type, uint16_t value)
 
{
 
	if(nodeID < MAX_NUM_SLAVES) {
 
		slaves[nodeID][type] = value;
 
	}	
 
}
 
 
int16_t sensordata_get(uint8_t nodeID, uint8_t type) 
 
{
master/master/lib/serial.h
Show inline comments
 
@@ -14,28 +14,32 @@
 
#ifndef SERIAL_H_
 
#define SERIAL_H_
 
 
#include <inttypes.h>
 
 
#define USART0_BAUD_PRESCALE (((F_CPU / (USART0_BAUDRATE * 16UL))) -1 )
 
#define USART1_BAUD_PRESCALE (((F_CPU / (USART1_BAUDRATE * 16UL))) -1 )
 
 
void serial0_sendChar( unsigned char byte );
 
void serial1_sendChar( unsigned char byte );
 
 
void serial0_setup();
 
void serial1_setup();
 
 
void serial0_sendString(const char* stringPtr);
 
void serial1_sendString(const char* stringPtr);
 
 
unsigned char serial0_readChar();
 
uint8_t serial0_hasChar();
 
char* serial0_readLine();
 
 
void serial_sendCommand( char* data );
 
void serial_sendResponseData();
 
 
 
void serial0_ion();
 
void serial0_ioff();
 
 
void serial1_ion();
 
void serial1_ioff();
 
 
#endif /* SERIAL_H_ */
 
\ No newline at end of file
master/master/lib/slavesensors.c
Show inline comments
 
/*
 
 * Master Firmware: Slave Sensor Data Aquisition
 
 *
 
 * Wireless Observational Modular Aerial Network
 
 * 
 
 * Ethan Zonca
 
 * Matthew Kanning
 
 * Kyle Ripperger
 
 * Matthew Kroening
 
 *
 
 */
 
 
#include "../config.h"
 
#include <avr/io.h>
 
#include <stdbool.h>
 
#include "../config.h"
 
#include <stdlib.h>
 
#include <stdio.h>
 
#include <string.h>
 
#include <util/delay.h>
 
#include <avr/wdt.h>
 
#include "serial.h"
 
#include "serparser.h"
 
#include "slavesensors.h"
 
#include "sensordata.h"
 
#include "led.h"
 
#include "looptime.h"
 
#include <util/delay.h>
 
#include <avr/wdt.h>
 

	
 
uint8_t currentSlave = 0;
 
uint8_t currentSlaveSensor = 0;
 
 
bool requesting = false;
 
 
void slavesensors_setup() 
 
{
 
	
 
}
 

	
 
#define DEBUG_NETWORKSCAN
 
//#define DEBUG_NETWORKSCAN
 
//#define DEBUG_GETSLAVEDATA
 
 
char* bufPtr = 0x00;
 
char slaveAddressLow[MAX_NUM_SLAVES][9];
 
char slaveAddressHigh[MAX_NUM_SLAVES][9];
 

	
 
static char slaveAddressLow[MAX_NUM_SLAVES][9];
 
static char slaveAddressHigh[MAX_NUM_SLAVES][9];
 
static char slaveNames[MAX_NUM_SLAVES][20];
 

	
 
uint8_t loggerIndex = 255;
 
uint8_t nodeCount = 0;
 
bool dataReady = false;
 

	
 
char* slavesensors_slavename(uint8_t id) {
 
	return slaveNames[id];
 
}
 

	
 
void slavesensors_network_scan() {
 
	serial0_ioff();
 
	
 
	int atOK;
 
	
 
	#ifdef DEBUG_OUTPUT
 
	serial0_sendString("Beginning network scan...\r\n\r\n");
 
	#endif
 
	
 
	_delay_ms(500); // xbee warmup
 
	wdt_reset();
 
	
 
	led_on(LED_ACTIVITY);
 
	atOK = slavesensors_enterAT();
 

	
 
	char nameString[20] = "NONE";
 
	
 
	char slaveNames[MAX_NUM_SLAVES][16]; // Hold 16-char addresses of max MAX_NUM_SLAVES nodes, local so the memory can be reused
 
	
 
	// wait for OK
 
	if(atOK == 0)
 
	{
 
		led_on(LED_CYCLE);
 
		serial0_sendString("ATND");
 
		serial0_sendChar(0x0D);
 
		
 
		// wait for scan to complete
 
		uint16_t scanStart = time_millis(); 		
 
		while(!serial0_hasChar()) {
 
			if(time_millis() - scanStart > 5000) {
 
			if(time_millis() - scanStart > 7000) {
 
				led_errorcode(ERROR_XBEETIMEOUT);
 
				return;
 
			}
 
			wdt_reset();
 
		}
 
		// Scan data end when newline by itself ("")	
 
		int lineCount = 0;	
 
	
 
		while(1) {
 
			bufPtr = serial0_readLine();
 

	
 
			// If we're starting a new block but got a newline instead, we're done!
 
			if(lineCount == 0 && strcmp(bufPtr, "") == 0) {
 
				break;			
 
			}
 
			
 
			if(lineCount == 1) {
 
				strcpy(slaveAddressHigh[nodeCount], bufPtr);
 
				strncpy(slaveAddressHigh[nodeCount],bufPtr, 9);
 
			}
 
			else if(lineCount == 2) {
 
				strcpy(slaveAddressLow[nodeCount], bufPtr);
 
				strncpy(slaveAddressLow[nodeCount],bufPtr, 9);
 
			}
 
			else if(lineCount == 3) {
 
				strcpy(nameString, bufPtr);
 
				strcpy(slaveNames[nodeCount], bufPtr);
 
				strncpy(slaveNames[nodeCount], bufPtr, 20);
 
			}
 
			
 
			// If we've finished one chunk (including the newline after it). Can't be else if because it controls increment.
 
			if(lineCount == 9) {
 
				// bufPtr should be null at this point, because we read in a newline after one chunk
 
				nodeCount++;
 
				lineCount = 0;
 
			}
 
			else {
 
				lineCount++;
 
			}
 

	
 
		}		
 

	
 
		slavesensors_exitAT();
 

	
 
	}
 
	
 
	// Display number of found nodes on spinning indicator
 
	switch(nodeCount) {
 
		case 0:
 
			break;
 
		case 3:
 
			led_on(LED_ACT2);
 
			_delay_ms(100);
 
		case 2:
 
			led_on(LED_ACT1);
 
			_delay_ms(100);	
 
		case 1:
 
			led_on(LED_ACT0);
 
			_delay_ms(100);
 
	}
 
	
 
	led_on(LED_SIDEBOARD);
 
	_delay_ms(200);
 
	led_off(LED_SIDEBOARD);
 

	
 
	#ifdef DEBUG_OUTPUT
 
	
 
	char debugBuf[64];
 
	serial0_sendString("Discovered: \r\n");
 
	for(int i=0; i<nodeCount; i++) {
 
		snprintf(debugBuf, 64, "  %s - %s%s\r\n", slaveNames[i],slaveAddressHigh,slaveAddressLow[i]);
 
		snprintf(debugBuf, 64, "  %s - %s%s (%u)\r\n", slaveNames[i],slaveAddressHigh,slaveAddressLow[i], nodeCount);
 
		serial0_sendString(debugBuf);
 
	}
 
	serial0_sendString("\r\n");
 
	if(atOK != 0) {
 
		serial0_sendString("AT mode failed \r\n");
 
	}
 
	
 
	#endif
 
	
 
	for(int i=0; i<nodeCount; i++) 
 
	{
 
		if(strcmp(slaveNames[i], XBEE_LOGDEST_NAME) == 0) 
 
		{
 
			loggerIndex = i;
 
		}
 
	}
 
	>
 
	_delay_ms(100);
 
	slavesensors_selectlogger();
 
	
 
	serial0_ion();
 
}
 
 
void slavesensors_selectnode(uint8_t nodeIndex)
 
{
 
	char tmpBuf[23];
 
	
 
	// If we can get into AT mode
 
	if(slavesensors_enterAT() == 0) {
 
		
 
		snprintf(tmpBuf, 23, "ATDH %s%c",slaveAddressHigh[nodeIndex], 0x0D);
 
		serial0_sendString(tmpBuf);
 
		
 
		if(xbeeIsOk() != 0) {
 
			led_errorcode(ERROR_NOXBEE);
 
			return;
 
		}
 
		
 
		snprintf(tmpBuf, 23, "ATDL %s%c",slaveAddressLow[nodeIndex], 0x0D);
 
		serial0_sendString(tmpBuf);
 
		
 
		if(xbeeIsOk() != 0) {
 
@@ -221,129 +228,158 @@ int slavesensors_enterAT()
 
			led_errorcode(ERROR_XBEETIMEOUT);
 
			wdt_reset();
 
			return 1;
 
		}
 
	};	
 
	
 
	return xbeeIsOk();
 
 
}
 
 
int xbeeIsOk() 
 
{
 
	char* tmppntr = serial0_readLine();
 
	if(strcmp(tmppntr, "OK") == 0)
 
	{
 
		return 0;
 
	}
 
	else
 
	{
 
		led_errorcode(ERROR_NOXBEE);
 
		return 1;
 
	}
 
}
 
 
bool slavesensors_dataReady() {
 
	return dataReady;
 
}
 
 
bool slavesensors_isrequesting() 
 
{
 
	return requesting;	
 
}
 
 
void slavesensors_startprocess() 
 
{
 
	requesting = true;
 
	slavesensors_request();		
 
}
 
 
// TODO: inline. static.
 
void slavesensors_request() 
 
{
 
	//slavesensors_selectnode(currentSlave);
 
	serial_sendCommand("@"); // Request data!
 
}
 
 
 
int numReadingsToExpect = 0; // number of values that the slave is about to send (testing)
 
uint8_t numReadingsToExpect = 0; // number of values that the slave is about to send (testing)
 
 
 
// TODO: needs to skip logger!
 
void slavesensors_process(uint8_t parseResult) 
 
{
 
	if(!requesting) {
 
		// we got a command when we didn't request anything. probably skip it.
 
		return;
 
	}
 
	
 
	// TODO: timeout. If we're at NODATA for a long time and we are requesting, that's an issue.
 
	else if(parseResult == PARSERESULT_NODATA) {
 
		// Wait for data
 
	}
 
	
 
	// Finished reception of a message (one sensor data value). If not finished, send out command to get the next one
 
	else if(parseResult == PARSERESULT_PARSEOK)
 
	{
 
		
 
		#ifdef DEBUG_GETSLAVEDATA
 
		char debug[50];
 
		snprintf(debug, 50, "Slave %u sensor %u of total nodes %u\r\n", currentSlave, currentSlaveSensor,nodeCount);
 
		serial0_sendString(debug);
 
		#endif
 
		
 
		// We got some data, let's handle it
 
		// ASCII payload
 
		uint8_t len = getPayloadLength();
 
		uint8_t* load = getPayload();
 
		uint8_t type = getPayloadType();
 
		uint16_t parsedVal = atoi(load);
 

	
 
		// Special case for slave telling us how many things we're about to get		
 
		if(type + 0x30 == '@') {
 
			
 
			#ifdef DEBUG_GETSLAVEDATA
 
			serial0_sendString("Got an awesome count!\r\n");
 
			serial0_sendChar(parsedVal + 0x30);
 
			serial0_sendString("\r\n");
 
			#endif
 
			
 
			numReadingsToExpect = parsedVal;
 
			currentSlave = 0;
 
			currentSlaveSensor = 0;
 
			requesting = true;
 
		}
 
		else {
 
		
 
			// Store data in structure
 
			sensordata_set(currentSlave,type,parsedVal);
 
			
 
			#ifdef DEBUG_GETSLAVEDATA
 
			serial0_sendString("Stored some sexy data!\r\n");
 
		
 
			#endif 
 
			
 
			// If we finished all sensors for all slaves
 
			if(currentSlave >= nodeCount && currentSlaveSensor >= numReadingsToExpect)
 
			
 
			if(currentSlave >= (nodeCount-1) && currentSlaveSensor >= (numReadingsToExpect-1))
 
			{
 
				#ifdef DEBUG_GETSLAVEDATA
 
				serial0_sendString("We got all data for all slaves!\r\n");
 
				#endif
 
				
 
				dataReady = true;
 
				currentSlave = 0;
 
				currentSlaveSensor = 0;
 
				requesting = false;
 
			}
 
			// If we finished up one slave, go to the next
 
			else if(currentSlaveSensor >= (numReadingsToExpect-1)) 
 
			{
 
				#ifdef DEBUG_GETSLAVEDATA
 
				serial0_sendString("Finished up one slave, go to another.\r\n");
 
				#endif
 
				
 
				currentSlave++;
 
				currentSlaveSensor = 0;
 
				requesting = true;
 
				slavesensors_request();
 
			}
 
			// If we haven't finished a slave (or all of them), just get the next sensor of the current slave
 
			else
 
			{
 
				#ifdef DEBUG_GETSLAVEDATA
 
				serial0_sendString("Give me another sensor value...");
 
				#endif
 
				
 
				// request data for the current sensor of the current slave
 
				currentSlaveSensor++;
 
				requesting = true;
 
				//slavesensors_request();	 slaves now send all values at once, we don't need to keep requesting
 
			}
 
		}
 
	}
 
	
 
	// If fail, try retransmit. Or we could skip and hit it next time.
 
	// TODO: Maximum number of retransmissions
 
	else if(parseResult == PARSERESULT_FAIL) {
 
		if(requesting) {
 
			slavesensors_request();	// re-request
 
		}			
 
	}
 
	
 
	
 
	else if(parseResult == PARSERESULT_STILLPARSING)
 
	{
 
		return; // do nothing
 
	}
 
	else {
 
		// something is terribly wrong!
 
		return;
master/master/lib/slavesensors.h
Show inline comments
 
@@ -9,32 +9,38 @@
 
 * Matthew Kroening
 
 *
 
 */
 
 
 
#ifndef SLAVESENSORS_H_
 
#define SLAVESENSORS_H_
 
 
#include <stdbool.h>
 
#include <inttypes.h>
 
 
// Serial Commands
 
enum sensorTypes // CMD ID#
 
{
 
	NONE = 0,
 
	BOARDTEMP,
 
	PRESSURE,
 
	GEIGER,
 
	TEMPERATURE,
 
	HUMIDITY,
 
	AMBIENTLIGHT,
 
	CAMERA,
 
};
 
 
char* slavesensors_slavename(uint8_t id);
 
bool slavesensors_dataReady();
 
bool slavesensors_isrequesting();
 
void slavesensors_setup();
 
void slavesensors_network_scan();
 
void slavesensors_startprocess();
 
void slavesensors_request();
 
void slavesensors_process(uint8_t parseResult);
 
int xbeeIsOk();
 
void slavesensors_selectlogger();
 
void slavesensors_exitAT();
 
int slavesensors_enterAT();
 
 
#endif /* SLAVESENSORS_H_ */
 
\ No newline at end of file
master/master/master.c
Show inline comments
 
/*
 
 * Master Firmware
 
 *
 
 * Wireless Observational Modular Aerial Network
 
 * 
 
 * 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/boardtemp.h"
 

	
 
#include "lib/heater.h"
 
#include "lib/looptime.h"
 
#include "lib/slavesensors.h"
 
#include "lib/serparser.h"
 
#include "lib/sensordata.h"
 

	
 
int main(void)
 
{
 
	led_on(LED_POWER);
 
	
 
	// Initialize libraries
 
	time_setup();
 
	watchdog_setup(); // enables interrupts
 
	led_setup();	
 
	gps_setup();
 
	serial0_setup();
 
	serial1_setup();
 
	i2c_init();
 
	sensordata_setup(); // must happen before sensors/logger/afsk
 
	slavesensors_setup();
 
	logger_setup();
 
	afsk_setup();
 
	//\f
 
	
 
	#ifdef DEBUG_OUTPUT
 
	serial0_sendString("\r\n---------------------------------\r\n");
 
	serial0_sendString("HAB Controller 1.0 - Initialized!\r\n");
 
	serial0_sendString("---------------------------------\r\n");
 
	#endif
 
	
 
	serial0_sendString("\r\nHello.\r\n\r\n");
 
	
 
	slavesensors_network_scan(); // This can take a little while
 
	
 
	// Buffer for string operations
 
	char logbuf[128];
 
	char debugBuf[128];
 
	
 
	// Software timers	
 
	uint32_t lastAprsBroadcast = 0;
 
	uint32_t lastLog = 0;
 
	uint32_t lastLedCycle = 0;
 
	bool dataWasReady = false;
 
	
 
	// Result of last parser run
 
	int parseResult = PARSERESULT_NODATA;
 
	
 
	// Write CSV header to SD card
 
	logger_log("ProgramTime,LastAprsBroadcast,LastLog\n");
 
	
 
	
 
	serial1_ioff();
 
	
 
	while(1)
 
    {
 
		
 
		// Periodic: LED execution indicator
 
		if(time_millis() - lastLedCycle > LEDCYCLE_RATE) {
 
			led_spin();
 
			
 
			if(!afsk_busy())
 
				serial1_ion();
 
			lastLedCycle = time_millis();	
 
		}
 
		
 
		// Periodic: Logging
 
		if(time_millis() - lastLog > LOGGER_RATE) 
 
		{
 
			led_on(LED_CYCLE);
 
			
 
			heater_regulateTemp();
 
			
 
			// Print out GPS debug
 
			//snprintf(debugBuf, 128, "GPS> time: %s lat: %s lon: %s speed: %s hdop: %s course: %s\r\n",
 
			//get_timestamp(),get_latitude(),get_longitude(),get_speedKnots(),get_hdop(), get_course());
 
			//serial0_sendString(debugBuf);
 
			
 
			// Turn on sideboard LED if we have a fix
 
			if(strcmp("99.99", get_hdop()) == 0) {
 
				led_off(LED_SIDEBOARD);
 
			}
 
			else {
 
				led_on(LED_SIDEBOARD);
 
			}
 
			
 
			sensors_readBoardTemp(); // i2c read, 400k
 
		
 
			logbuf[0] = 0x00;
 
			//snprintf(logbuf, 128, "%lu,%d,%u,%s,%s,%s,%s,%s\r\n", time_millis(), sensors_getBoardTemp(),get_timestamp(),get_latitude(),get_longitude(),get_speedKnots(),get_hdop(), get_course());
 
			for(int i=0; i<MAX_NUM_SLAVES; i++) {
 
				for(int j=0; j<MAX_NUM_SENSORS; j++) {
 
					int16_t tmp = sensordata_get(i, j);
 
					if(tmp != -32768) {
 
						//serial0_sendString("OH GOSH DATUM\r\n");
 
						snprintf(logbuf + strlen(logbuf),128," %d,", tmp);
 
						serial0_sendString(logbuf);
 
			// If we've gotten data from all slaves once, we're ready to make a CSV header and start logging
 
			if(slavesensors_dataReady()) {
 
				if(!dataWasReady) {
 
					char csvHeader[128];
 
					csvHeader[0] = 0x00;
 
					
 
					for(uint8_t i=0; i<MAX_NUM_SLAVES; i++) {
 
						for(uint8_t j=0; j<MAX_NUM_SENSORS; j++) {
 
							int16_t tmp = sensordata_get(i, j);
 
							if(tmp != -32768) {
 
								// FIXME: will the 128 here really provide safety? might want to subtract the strlen
 
								snprintf(csvHeader + strlen(csvHeader), 128,"%s-Sensor%u,", slavesensors_slavename(i), j);	
 
							}
 
						}
 
					}
 
					
 
					snprintf(csvHeader + strlen(csvHeader), 128,"\r\n");	
 
					serial0_sendString(csvHeader);
 
					logger_log(csvHeader);
 
					dataWasReady = true;
 
				}
 
			}
 
			snprintf(logbuf + strlen(logbuf),128,"\r\n");
 
		
 
				logbuf[0] = 0x00;
 
				//snprintf(logbuf, 128, "%lu,%d,%u,%s,%s,%s,%s,%s\r\n", time_millis(), sensors_getBoardTemp(),get_timestamp(),get_latitude(),get_longitude(),get_speedKnots(),get_hdop(), get_course());
 
				for(int i=0; i<MAX_NUM_SLAVES; i++) {
 
					for(int j=0; j<MAX_NUM_SENSORS; j++) {
 
						int16_t tmp = sensordata_get(i, j);
 
						if(tmp != -32768) {
 
							snprintf(logbuf + strlen(logbuf),128," %d,", tmp);
 
						}
 
					
 
					}
 
				}
 
				snprintf(logbuf + strlen(logbuf),128,"\r\n");
 
				serial0_sendString(logbuf);			
 
				logger_log(logbuf);
 
			
 
			logger_log(logbuf);
 
			}			
 
			
 
			// Print out logger debug
 
			#ifdef DEBUG_OUTPUT
 
			//serial0_sendString("LOG> ");
 
			//serial0_sendString(logbuf);
 
			#endif
 
			
 
			led_off(LED_CYCLE);
 
			lastLog = time_millis();
 
		}		
 
		
 
		
 
		// Periodic: APRS transmission
 
		if(time_millis() - lastAprsBroadcast > APRS_TRANSMIT_PERIOD) 
 
		{
 
			while(afsk_busy());
 
			serial1_ioff();
 
			aprs_send(); // non-blocking
 
			
 
			//serial0_sendString("Initiating APRS transmission...\r\n");
 
			
 
			// Start getting values for next transmission
 
			if(slavesensors_isrequesting())
 
			{
0 comments (0 inline, 0 general)