Changeset - a11c21f8113e
[Not reviewed]
default
0 5 0
ethanzonca@CL-ENS241-08.cedarville.edu - 12 years ago 2013-02-19 16:22:45
ethanzonca@CL-ENS241-08.cedarville.edu
Reworked network scanning to avoid treating the logger as a normal node. System now properly operates with no logger node attached.
5 files changed with 94 insertions and 51 deletions:
0 comments (0 inline, 0 general)
master/master/lib/logger.c
Show inline comments
 
@@ -180,6 +180,7 @@ void logger_setup()
 
	
 
	error_log_rawwrite(LOGGER_HEADERTEXT);
 
	error_log_rawwrite("\n-- BEGIN ERROR --\n");
 
	error_log_rawwrite("\nErrorNo,ErrorMsg,ErrorInfo,\r\n");
 
}	
 
 
void logger_log(char *buffer) 
 
@@ -202,15 +203,37 @@ void error_log(uint8_t errNo, bool flash
 
		strncpy_P(labelBuffer,(char*)pgm_read_word(&(errorMessageLookup[errNo])),32);
 
	}
 
	char errorLine[128];
 
	snprintf(errorLine, 128, "%lu, %u, %s,\r\n", time_millis(), errNo, labelBuffer);
 
	snprintf(errorLine, 128, "%lu, %u, %s,,\r\n", time_millis(), errNo, labelBuffer);
 
	error_log_rawwrite(errorLine);
 
	
 
	led_on(LED_ERROR);
 
	if(flashLED) 
 
	{
 
		led_errorcode(errNo);
 
	}	
 
}
 
 
void error_log_msg(uint8_t errNo, bool flashLED, char* infoText)
 
{
 
	char labelBuffer[32];
 
	labelBuffer[0] = 0x00;
 
	
 
	if(errNo <= MAX_ERRNO)
 
	{
 
		strncpy_P(labelBuffer,(char*)pgm_read_word(&(errorMessageLookup[errNo])),32);
 
	}
 
	char errorLine[256];
 
	snprintf(errorLine, 256, "%lu,%u,%s,%s,\r\n", time_millis(), errNo, labelBuffer, infoText);
 
	error_log_rawwrite(errorLine);
 
	
 
	led_on(LED_ERROR);
 
	if(flashLED)
 
	{
 
		led_errorcode(errNo);
 
	}
 
}
 
 
 
void error_log_rawwrite(char *buffer) 
 
{
 
	uint8_t len = strlen(buffer);
master/master/lib/logger.h
Show inline comments
 
@@ -21,6 +21,7 @@ uint8_t logger_writeLine(char* dateLine,
 
struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name);
 
uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name, struct fat_dir_entry_struct* dir_entry);
 
void error_log(uint8_t errNo, bool flashLED);
 
void error_log_msg(uint8_t errNo, bool flashLED, char* infoText);
 
void error_log_rawwrite(char *buffer);
 
void logger_log(char *buffer);
 
void logger_closeLog();
master/master/lib/slavesensors.c
Show inline comments
 
@@ -25,6 +25,7 @@
 
#include "sensordata.h"
 
#include "led.h"
 
#include "looptime.h"
 
#include "logger.h"
 

	
 
// Label lookup table
 
// Make sure there are never more labels than there are MAX_NUM_SENSORS! 
 
@@ -71,11 +72,6 @@ uint8_t currentSlaveSensor = 0;
 
 
bool requesting = false;
 
 
void slavesensors_setup() 
 
{
 
	
 
}
 

	
 
//#define DEBUG_NETWORKSCAN
 
//#define DEBUG_GETSLAVEDATA
 
 
@@ -85,10 +81,18 @@ static char slaveAddressLow[MAX_NUM_SLAV
 
static char slaveAddressHigh[MAX_NUM_SLAVES][9];
 
static char slaveNames[MAX_NUM_SLAVES][15];
 

	
 
uint8_t loggerIndex = 255;
 
static char loggerAddressLow[9];
 
static char loggerAddressHigh[9];
 

	
 
uint8_t nodeCount = 0;
 
bool dataReady = false;
 

	
 
void slavesensors_setup()
 
{
 
	loggerAddressLow[0] = 0x00;
 
	loggerAddressHigh[0] = 0x00;
 
}
 

	
 
char* slavesensors_slavename(uint8_t id) 
 
{
 
	return slaveNames[id];
 
@@ -153,10 +157,20 @@ void slavesensors_network_scan()
 
			// If we've finished one chunk (including the newline after it). Can't be else if because it controls increment.
 
			if(lineCount == 9) 
 
			{
 
				if(strcmp(slaveNames[nodeCount], XBEE_LOGDEST_NAME) == 0)
 
				{
 
					// Save logger address in the loggerAddressXXXX variables
 
					strncpy(loggerAddressHigh, slaveAddressHigh[nodeCount], 9);
 
					strncpy(loggerAddressLow, slaveAddressLow[nodeCount], 9);
 
					lineCount = 0;
 
					// don't increment, just overwrite this next time
 
				}
 
				else {
 
				// bufPtr should be null at this point, because we read in a newline after one chunk
 
				nodeCount++;
 
				lineCount = 0;
 
			}
 
			}
 
			else 
 
			{
 
				lineCount++;
 
@@ -194,7 +208,7 @@ void slavesensors_network_scan()
 
	_delay_ms(500);
 
	led_off(LED_SIDEBOARD);
 

	
 
	#ifdef DEBUG_OUTPUT
 
	#ifdef DEBUG_NETWORKSCAN
 
	
 
	char debugBuf[64];
 
	serial0_sendString("Discovered: \r\n");
 
@@ -211,13 +225,7 @@ void slavesensors_network_scan()
 
	
 
	#endif
 
	
 
	for(int i=0; i<nodeCount; i++) 
 
	{
 
		if(strcmp(slaveNames[i], XBEE_LOGDEST_NAME) == 0) 
 
		{
 
			loggerIndex = i;
 
		}
 
	}
 

	
 
	_delay_ms(100);
 
	
 
	slavesensors_selectlogger();
 
@@ -228,7 +236,7 @@ void slavesensors_network_scan()
 
//#define DEBUG_CONTEXTSWITCH
 
//#define DEBUG_SELECTNODE
 
 
uint8_t selectedNode = 0;
 
uint8_t selectedNode = 255;
 
uint8_t slavesensors_getselectednode() 
 
{
 
	return selectedNode;
 
@@ -240,6 +248,15 @@ void slavesensors_selectnode(uint8_t nod
 
	{
 
		return;
 
	}
 
	
 
	if(slavesensors_selectaddress(slaveAddressHigh[nodeIndex],slaveAddressLow[nodeIndex]) == true) 
 
	{
 
		selectedNode = nodeIndex;
 
	}	
 
}
 
	
 
bool slavesensors_selectaddress(char* addrHigh, char* addrLow) 
 
{
 
	serial0_ioff();
 
	
 
	#ifdef DEBUG_CONTEXTSWITCH
 
@@ -259,26 +276,25 @@ void slavesensors_selectnode(uint8_t nod
 
	if(slavesensors_enterAT() == 0) 
 
	{
 
		
 
		snprintf(tmpBuf, 23, "ATDH %s%c",slaveAddressHigh[nodeIndex], 0x0D);
 
		snprintf(tmpBuf, 23, "ATDH %s%c",addrHigh, 0x0D);
 
		serial0_sendString(tmpBuf);
 
		
 
		if(xbeeIsOk() != 0) 
 
		{
 
			error_log(ERROR_XBEETIMEOUT);
 
			return;
 
			error_log(ERROR_XBEETIMEOUT, true);
 
			return false;
 
		}
 
		
 
		snprintf(tmpBuf, 23, "ATDL %s%c",slaveAddressLow[nodeIndex], 0x0D);
 
		snprintf(tmpBuf, 23, "ATDL %s%c",addrLow, 0x0D);
 
		serial0_sendString(tmpBuf);
 
		
 
		if(xbeeIsOk() != 0) 
 
		{
 
			error_log(ERROR_XBEETIMEOUT);
 
			return;
 
			error_log(ERROR_XBEETIMEOUT, true);
 
			return false;
 
		}
 
		
 
		slavesensors_exitAT();
 
		selectedNode = nodeIndex;
 
	}
 
	_delay_ms(2);
 
	
 
@@ -296,14 +312,14 @@ void slavesensors_selectnode(uint8_t nod
 
	#endif
 
	
 
	serial0_ion();
 
	return;
 
	return true;
 
}
 
 
void slavesensors_selectlogger() 
 
{
 
	if(loggerIndex != 255) 
 
	if(loggerAddressLow[0] != 0x00) 
 
	{
 
		slavesensors_selectnode(loggerIndex);
 
		slavesensors_selectaddress(loggerAddressHigh,loggerAddressLow);
 
	}	
 
}
 
 
@@ -328,7 +344,7 @@ bool waitTimeout(uint32_t timeout) {
 
	{
 
		if(time_millis() - scanStart > timeout)
 
		{
 
			error_log(ERROR_XBEETIMEOUT);
 
			error_log(ERROR_XBEETIMEOUT, true);
 
			return true;
 
		}
 
		if(time_millis() - lastBlink > 50)
 
@@ -361,7 +377,7 @@ int slavesensors_enterAT()
 
int xbeeIsOk() 
 
{
 
	if(waitTimeout(2000)) {
 
		error_log(ERROR_XBEETIMEOUT);
 
		error_log(ERROR_XBEETIMEOUT, true);
 
		return 1;
 
	}
 
	char* tmppntr = serial0_readLine();
 
@@ -371,7 +387,7 @@ int xbeeIsOk()
 
	}
 
	else
 
	{
 
		error_log(ERROR_SLAVETIMEOUT);
 
		error_log(ERROR_SLAVETIMEOUT, true);
 
		return 1;
 
	}
 
}
 
@@ -396,10 +412,6 @@ void slavesensors_startprocess()
 
uint32_t beginRequest = 0;
 
void slavesensors_request() 
 
{
 
	if(currentSlave == loggerIndex) {
 
		gotoNextSlaveOrSensor(true);
 
		return;
 
	}
 
	slavesensors_selectnode(currentSlave);
 
	beginRequest = time_millis();
 
	serial_sendCommand("@"); // Request data!
 
@@ -438,23 +450,24 @@ void gotoNextSlaveOrSensor(bool fail) {
 
		currentSlaveSensor = 0;
 
		requesting = true;
 
		
 
		if(currentSlave == loggerIndex)
 
		{
 
			if(currentSlave >= (nodeCount-1))
 
			{
 
				// We hit the last one, we're done.
 
				dataReady = true;
 
				currentSlave = 0;
 
				currentSlaveSensor = 0;
 
				requesting = false;
 
				led_alert();
 
				return;
 
			}
 
			else
 
			{
 
				currentSlave++; // Skip the logger: increment to the next slave after the logger
 
			}
 
		}
 
		// EXPECT PROBLEMS HERE FROM NOT INCREMENTING
 
		//if(currentSlave == loggerIndex)
 
		//{
 
			//if(currentSlave >= (nodeCount-1))
 
			//{
 
				//// We hit the last one, we're done.
 
				//dataReady = true;
 
				//currentSlave = 0;
 
				//currentSlaveSensor = 0;
 
				//requesting = false;
 
				//led_alert();
 
				//return;
 
			//}
 
			//else
 
			//{
 
				//currentSlave++; // Skip the logger: increment to the next slave after the logger
 
			//}
 
		//}
 
		
 
		slavesensors_request();
 
	}
 
@@ -490,7 +503,9 @@ void slavesensors_process(uint8_t parseR
 
			// if we're requesting, we have no data, and we're over the timeout, this is bad!
 
			// setParserState(STATE_RESET); - meh, can't do this because it freaking increments the cirbufptr
 
			gotoNextSlaveOrSensor(true);
 
			error_log(ERROR_SLAVETIMEOUT, false); // log error, don't blink LED
 
			char* msg[128];
 
			snprintf(msg, 128, "Slave %u (%s) timeout",currentSlave,slaveNames[currentSlave]);
 
			error_log_msg(ERROR_SLAVETIMEOUT, false, msg); // log error, don't blink LED
 
		}
 
	}
 
	
master/master/lib/slavesensors.h
Show inline comments
 
@@ -39,6 +39,7 @@ void slavesensors_setup();
 
void slavesensors_network_scan();
 
uint8_t slavesensors_getselectednode();
 
void slavesensors_selectnode(uint8_t nodeIndex);
 
bool slavesensors_selectaddress(char* addrHigh, char* addrLow);
 
void slavesensors_startprocess();
 
void slavesensors_request();
 
void gotoNextSlaveOrSensor(bool fail);
master/master/master.c
Show inline comments
 
@@ -38,6 +38,9 @@
 

	
 
int main(void)
 
{
 
	// Power debounce
 
	_delay_ms(100);
 
	
 
	// Initialize libraries
 
	time_setup();
 
	watchdog_setup(); // enables interrupts
0 comments (0 inline, 0 general)