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"
 
@@ -125,20 +132,20 @@ 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)
 
{
 
@@ -671,10 +678,11 @@ void parse_gps_transmission(void){
 
	}	
 
	
 

	
 
}
 
/// 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
 
@@ -18,8 +23,10 @@ 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
 
@@ -9,12 +9,13 @@
 
 * 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)
 
	 {
master/master/lib/logger.c
Show inline comments
 
@@ -20,12 +20,13 @@
 
#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;
master/master/lib/sensordata.c
Show inline comments
 
@@ -8,12 +8,13 @@
 
 * 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];
 

	
master/master/lib/serial.h
Show inline comments
 
@@ -32,10 +32,14 @@ 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
 
@@ -7,41 +7,53 @@
 
 * 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;
 
	
 
@@ -51,28 +63,24 @@ void slavesensors_network_scan() {
 
	
 
	_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 ("")	
 
@@ -84,20 +92,19 @@ void slavesensors_network_scan() {
 
			// 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++;
 
@@ -134,13 +141,13 @@ void slavesensors_network_scan() {
 

	
 
	#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");
 
	}
 
@@ -151,13 +158,13 @@ void slavesensors_network_scan() {
 
	{
 
		if(strcmp(slaveNames[i], XBEE_LOGDEST_NAME) == 0) 
 
		{
 
			loggerIndex = i;
 
		}
 
	}
 
	>
 
	_delay_ms(100);
 
	slavesensors_selectlogger();
 
	
 
	serial0_ion();
 
}
 
 
void slavesensors_selectnode(uint8_t nodeIndex)
 
@@ -239,12 +246,15 @@ int xbeeIsOk()
 
	{
 
		led_errorcode(ERROR_NOXBEE);
 
		return 1;
 
	}
 
}
 
 
bool slavesensors_dataReady() {
 
	return dataReady;
 
}
 
 
bool slavesensors_isrequesting() 
 
{
 
	return requesting;	
 
}
 
 
@@ -259,13 +269,13 @@ 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) {
 
@@ -278,54 +288,80 @@ void slavesensors_process(uint8_t parseR
 
		// 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
 
			}
 
		}
master/master/lib/slavesensors.h
Show inline comments
 
@@ -27,14 +27,20 @@ enum sensorTypes // CMD ID#
 
	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
 
@@ -16,25 +16,26 @@
 
#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)
 
@@ -64,26 +65,22 @@ int main(void)
 
	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
 
@@ -114,28 +111,49 @@ int main(void)
 
			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
0 comments (0 inline, 0 general)