Changeset - e3153d5a767c
[Not reviewed]
default
0 6 0
ethanzonca@CL-ENS241-08.cedarville.edu - 12 years ago 2013-01-21 20:59:15
ethanzonca@CL-ENS241-08.cedarville.edu
Implemented new serial protocol and slave sensor data storage technique, appears to be functional.
6 files changed with 52 insertions and 65 deletions:
0 comments (0 inline, 0 general)
master/master/config.h
Show inline comments
 
@@ -40,26 +40,14 @@
 
#define ERROR_CRAP 15
 
 
// --------------------------------------------------------------------------
 
// Slave Sensors config (slavesensors.c)
 
// --------------------------------------------------------------------------
 
 
// NOT USED. Could integrate into slavesensors.c setup function for configurability eventually.
 
// Currently manual configuration of sensors is done in slavesensors.c
 
#define SLAVE0_SENSORS BOARDTEMP
 
#define SLAVE1_SENSORS BOARDTEMP | HUMIDITY | TEMPERATURE | PRESSURE | AMBIENTLIGHT
 
#define SLAVE2_SENSORS BOARDTEMP | GEIGER
 
#define SLAVE3_SENSORS BOARDTEMP | CAMERA
 
#define SLAVE4_SENSORS NONE
 
#define SLAVE5_SENSORS NONE
 
#define SLAVE6_SENSORS NONE
 
#define SLAVE7_SENSORS NONE
 
 
// MAX_SLAVES should be one more than the number of slaves we actually have (loop ends when next slave first sensor is NONE)
 
#define MAX_SLAVES 8
 
#define MAX_SLAVE_SENSORS 8
 
#define MAX_NUM_SLAVES 5  // Maximum number of nodes in the system
 
#define MAX_NUM_SENSORS 20 // Maximum number of unique types of sensors in the system
 
 
// Node identifier of log destination xbee
 
#define XBEE_LOGDEST_NAME "HAB-LOGGER"
 
 
// --------------------------------------------------------------------------
 
// Command Parser config (serparser.c)
master/master/lib/gps.c
Show inline comments
 
@@ -117,21 +117,16 @@ enum decodeState {
 
	RMC_DATE,
 
	RMC_MAG_VARIATION,
 
	
 
}decodeState;
 

	
 

	
 
char debugBuff[128];
 

	
 
ISR(USART1_RX_vect)
 
{
 
	nmeaBuffer[nmeaBufferDataPosition % NMEABUFFER_SIZE] = UDR1;
 
	nmeaBufferDataPosition = (nmeaBufferDataPosition + 1) % NMEABUFFER_SIZE;
 
	//serial0_sendChar(UDR1);
 
	//snprintf(debugBuff, 32, "GPS: bdp: %d, bpp: %d decodestate: %u \r\n", nmeaBufferDataPosition, nmeaBufferParsePosition, decodeState);
 
	//serial0_sendString((debugBuff));
 
}
 

	
 

	
 
// Could inline if program space available
 
static void setParserState(uint8_t state)
 
{
 
@@ -160,15 +155,12 @@ void parse_gps_transmission(void){
 
	
 
	while(nmeaBufferDataPosition != nmeaBufferParsePosition) {
 
	led_on(LED_ACTIVITY);
 
		
 
		byte = nmeaBuffer[nmeaBufferParsePosition];
 
		
 
		//snprintf(debugBuff, 64, "GPSParse: byte [%c] decodestate: %u bp: %u pp: %u\r\n",  byte, decodeState, nmeaBufferDataPosition, nmeaBufferParsePosition);
 
		//serial0_sendString((debugBuff));
 
		
 
		if(decodeState == INITIALIZE) //start of transmission sentence
 
		{
 
			if(byte == '$') {
 
				#ifdef DEBUG_NMEA
 
				serial0_sendString("found $\r\n");
 
				#endif
 
@@ -641,13 +633,12 @@ void parse_gps_transmission(void){
 
				
 
				}
 
				#ifdef DEBUG_NMEA
 
				serial0_sendString("OMG GOT TO CHECKSUM!\r\n");
 
				#endif
 
				
 
				serial0_sendString((debugBuff));
 
				setParserState(INITIALIZE);
 
				numBytes = 0; //prep for next phase of parse
 
			}
 
			else //store data
 
			{
 
				setParserState(GPS_CHECKSUM);
master/master/lib/sensordata.c
Show inline comments
 
@@ -10,36 +10,34 @@
 
 *
 
 */
 

	
 
#include "../config.h"
 
#include "sensordata.h"
 

	
 
int16_t boardTemps[MAX_SLAVES];
 
uint16_t variousSensors[MAX_SLAVES * (MAX_SLAVE_SENSORS - 1)];
 
int16_t slaves[MAX_NUM_SLAVES][MAX_NUM_SENSORS];
 

	
 
void sensordata_setup() 
 
{
 
	for(int i=0; i<MAX_SLAVES; i++)
 
	{
 
		boardTemps[i] = 0;
 
	for(int i=0; i<MAX_NUM_SLAVES; i++) {
 
		for(int j=0; j<MAX_NUM_SLAVES; j++) {
 
			slaves[i][j] = -32768; // minimum value of 16 bit integer
 
		}
 
	}
 
}
 
 
void sensordata_set(uint8_t type, uint16_t value) 
 
void sensordata_set(uint8_t nodeID, uint8_t type, uint16_t value)
 
{
 
	variousSensors[type] = value;
 
	if(nodeID < MAX_NUM_SLAVES) {
 
		slaves[nodeID][type] = value;
 
}
 
 
uint16_t sensordata_get(uint8_t type) 
 
{
 
	return variousSensors[type];
 
}
 
 
void sensordata_setBoardTemp(uint8_t slaveID, int16_t value) 
 
uint16_t sensordata_get(uint8_t nodeID, uint8_t type) 
 
{
 
	boardTemps[slaveID] = value;
 
	// Avoid reading out of bad places!
 
	if(nodeID < MAX_NUM_SLAVES) {
 
		return slaves[nodeID][type];
 
}
 
 
int16_t sensordata_getBoardTemp(uint8_t slaveID)
 
{
 
	return boardTemps[slaveID];
 
}
 
\ No newline at end of file
 
	else {
 
		return 0;
 
	}
 
}
master/master/lib/sensordata.h
Show inline comments
 
@@ -14,12 +14,12 @@
 
#ifndef SENSORDATA_H_
 
#define SENSORDATA_H_
 
 
#include "slavesensors.h"
 
 
void sensordata_setup();
 
void sensordata_set(uint8_t type, uint16_t value);
 
uint16_t sensordata_get(uint8_t type);
 
void sensordata_set(uint8_t nodeID, uint8_t type, uint16_t value);
 
uint16_t sensordata_get(uint8_t nodeID, uint8_t type);
 
void sensordata_setBoardTemp(uint8_t slaveID, int16_t value);
 
int16_t sensordata_getBoardTemp(uint8_t slaveID);
 
 
#endif /* SENSORDATA_H_ */
 
\ No newline at end of file
master/master/lib/serparser.c
Show inline comments
 
@@ -87,13 +87,13 @@ ISR(USART0_RX_vect)
 
	//serial0_sendString((debugBuff)); 
 
}
 
 
 
 
 
#define DEBUG
 
//#define DEBUG
 
 
// Parse data from circular buffer
 
int serparser_parse(void)
 
{
 
	
 
	char byte;
 
@@ -126,13 +126,13 @@ int serparser_parse(void)
 
		// Get payload type ID
 
		else if(parserState == STATE_GETDATATYPE)
 
		{
 
			#ifdef DEBUG
 
			serial0_sendString("type\r\n");
 
			#endif
 
			receivedPayloadType = byte; // Store the type of data receiving
 
			receivedPayloadType = byte - 0x30; // Store the type of data receiving
 
			checksumCalc += byte;
 
			setParserState(STATE_GETDATA);
 
		}
 
		
 
		// Get payload data
 
		else if(parserState == STATE_GETDATA)
 
@@ -191,13 +191,14 @@ int serparser_parse(void)
 
			else {
 
				#ifdef DEBUG
 
				serial0_sendString("bcheck\r\n");
 
				#endif
 
				setParserState(STATE_RESET);
 
				
 
				return PARSERESULT_FAIL;
 
				return PARSERESULT_PARSEOK;
 
				// !!!!!!!!!!!!!DEBUGGGGG ignore checksum ///// return PARSERESULT_FAIL;
 
			}
 
			
 
			/*
 
			if(bufferParsePosition == bufferDataPosition)
 
			{
 
				// We are at the end of the line. No more data to parse.
master/master/lib/slavesensors.c
Show inline comments
 
@@ -21,28 +21,24 @@
 
#include <util/delay.h>
 
#include <avr/wdt.h>
 

	
 
uint8_t currentSlave = 0;
 
uint8_t currentSlaveSensor = 0;
 
 
uint16_t slaves[MAX_SLAVES][MAX_SLAVE_SENSORS];
 
 
bool requesting = false;
 
 
void slavesensors_setup() 
 
{
 
 
}
 

	
 
 
#define DEBUG_NETWORKSCAN
 
 
char* bufPtr = 0x00;
 
char debugBuf[64];
 
char slaveAddressLow[6][9];
 
char slaveAddressHigh[6][9];
 
char slaveAddressLow[MAX_NUM_SLAVES][9];
 
char slaveAddressHigh[MAX_NUM_SLAVES][9];
 
uint8_t loggerIndex = 255;
 
uint8_t nodeCount = 0;
 

	
 
void slavesensors_network_scan() {
 
	serial0_ioff();
 
	
 
@@ -57,18 +53,15 @@ void slavesensors_network_scan() {
 
	
 
	led_on(LED_ACTIVITY);
 
	atOK = slavesensors_enterAT();
 

	
 
	char nameString[20] = "NONE";
 
	
 
	char slaveNames[6][16]; // Hold 16-char addresses of max 6 nodes, we only need them for debug so they are local 
 
	
 
	
 
	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
 
	//todo
 
	if(atOK == 0)
 
	{
 
		led_on(LED_CYCLE);
 
		serial0_sendString("ATND");
 
		serial0_sendChar(0x0D);
 
		
 
@@ -102,13 +95,13 @@ void slavesensors_network_scan() {
 
				strcpy(nameString, bufPtr);
 
				strcpy(slaveNames[nodeCount], bufPtr);
 
			}
 
			
 
			// 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 newlinem after one chunk
 
				// bufPtr should be null at this point, because we read in a newline after one chunk
 
				nodeCount++;
 
				lineCount = 0;
 
			}
 
			else {
 
				lineCount++;
 
			}
 
@@ -136,21 +129,24 @@ void slavesensors_network_scan() {
 
	
 
	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]);
 
		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) 
 
		{
 
@@ -242,12 +238,14 @@ int xbeeIsOk()
 
	{
 
		led_errorcode(ERROR_NOXBEE);
 
		return 1;
 
	}
 
}
 
 
 
 
bool slavesensors_isrequesting() 
 
{
 
	return requesting;	
 
}
 
 
void slavesensors_startprocess() 
 
@@ -261,13 +259,13 @@ void slavesensors_request()
 
{
 
	//slavesensors_selectnode(currentSlave);
 
	serial_sendCommand("@"); // Request data!
 
}
 
 
 
int numValues = 0; // number of values that the slave is about to send (testing)
 
int 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) {
 
@@ -284,43 +282,54 @@ void slavesensors_process(uint8_t parseR
 
	else if(parseResult == PARSERESULT_PARSEOK)
 
	{
 
		// 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 == '@') {
 
			serial0_sendString("Got an awesome count!\r\n");
 
			serial0_sendChar(parsedVal + 0x30);
 
			serial0_sendString("\r\n");
 
			numReadingsToExpect = parsedVal;
 
			currentSlave = 0;
 
			currentSlaveSensor = 0;
 
			requesting = true;
 
		}
 
		else {
 
		
 
		// Store data in structure
 
		//sensordata_set(slaves[currentSlave][currentSlaveSensor], parsedVal);
 
			sensordata_set(currentSlave,type,parsedVal);
 
			serial0_sendString("Stored some sexy data!\r\n");
 
		
 
		// If we finished all sensors for all slaves
 
		if(currentSlave >= nodeCount && currentSlaveSensor >= numValues)
 
			if(currentSlave >= nodeCount && currentSlaveSensor >= numReadingsToExpect)
 
		{
 
			currentSlave = 0;
 
			currentSlaveSensor = 0;
 
			requesting = false;
 
		}
 
		// If we finished up one slave, go to the next
 
		else if(currentSlaveSensor >= numValues) 
 
			else if(currentSlaveSensor >= (numReadingsToExpect-1)) 
 
		{
 
			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
 
		{
 
			// request data for the current sensor of the current slave
 
			currentSlaveSensor++;
 
			requesting = true;
 
			slavesensors_request();	
 
				//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) {
0 comments (0 inline, 0 general)