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 98 insertions and 55 deletions:
0 comments (0 inline, 0 general)
master/master/lib/logger.c
Show inline comments
 
@@ -177,12 +177,13 @@ void logger_setup()
 
	// Write header information
 
	logger_log(LOGGER_HEADERTEXT);
 
	logger_log("\n-- BEGIN DATA --\n");
 
	
 
	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) 
 
{
 
	uint8_t len = strlen(buffer);
 
	if(fat_write_file(fd_datalog, (uint8_t*) buffer, len) != len)
 
@@ -199,21 +200,43 @@ void error_log(uint8_t errNo, bool flash
 
	
 
	if(errNo <= MAX_ERRNO) 
 
	{
 
		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);
 
	if(fat_write_file(fd_errorlog, (uint8_t*) buffer, len) != len)
 
	{
 
		// Error writing to file
master/master/lib/logger.h
Show inline comments
 
@@ -18,11 +18,12 @@
 
 
void logger_setup();
 
uint8_t logger_writeLine(char* dateLine, uint8_t length);
 
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();
 
 
#endif /* LOGGER_H_ */
 
\ No newline at end of file
master/master/lib/slavesensors.c
Show inline comments
 
@@ -22,12 +22,13 @@
 
#include "serial.h"
 
#include "serparser.h"
 
#include "slavesensors.h"
 
#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! 
 
const char label_0[] PROGMEM = "BoardTemp";
 
const char label_1[] PROGMEM = "HeaterStatus";
 
const char label_2[] PROGMEM = "BatteryLevel";
 
@@ -67,31 +68,34 @@ char* slavesensors_getLabel(uint8_t sens
 
}
 

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

	
 
//#define DEBUG_NETWORKSCAN
 
//#define DEBUG_GETSLAVEDATA
 
 
char* bufPtr = 0x00;
 

	
 
static char slaveAddressLow[MAX_NUM_SLAVES][9];
 
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];
 
}
 

	
 
void slavesensors_network_scan() 
 
@@ -150,15 +154,25 @@ void slavesensors_network_scan()
 
				strncpy(slaveNames[nodeCount], bufPtr, 15);
 
			}
 
			
 
			// 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;
 
				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++;
 
			}
 

	
 
@@ -191,13 +205,13 @@ void slavesensors_network_scan()
 
	}
 
	_delay_ms(500);
 
	led_on(LED_SIDEBOARD);
 
	_delay_ms(500);
 
	led_off(LED_SIDEBOARD);
 

	
 
	#ifdef DEBUG_OUTPUT
 
	#ifdef DEBUG_NETWORKSCAN
 
	
 
	char debugBuf[64];
 
	serial0_sendString("Discovered: \r\n");
 
	for(int i=0; i<nodeCount; i++) 
 
	{
 
		snprintf(debugBuf, 64, "  %s - %s%s (%u)\r\n", slaveNames[i],slaveAddressHigh,slaveAddressLow[i], i);
 
@@ -208,41 +222,44 @@ void slavesensors_network_scan()
 
	{
 
		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();
 
}
 
 
//#define DEBUG_CONTEXTSWITCH
 
//#define DEBUG_SELECTNODE
 
 
uint8_t selectedNode = 0;
 
uint8_t selectedNode = 255;
 
uint8_t slavesensors_getselectednode() 
 
{
 
	return selectedNode;
 
}
 
 
void slavesensors_selectnode(uint8_t nodeIndex)
 
{
 
	if(selectedNode == nodeIndex) 
 
	if(selectedNode == nodeIndex)
 
	{
 
		return;
 
	}
 
	
 
	if(slavesensors_selectaddress(slaveAddressHigh[nodeIndex],slaveAddressLow[nodeIndex]) == true) 
 
	{
 
		selectedNode = nodeIndex;
 
	}	
 
}
 
	
 
bool slavesensors_selectaddress(char* addrHigh, char* addrLow) 
 
{
 
	serial0_ioff();
 
	
 
	#ifdef DEBUG_CONTEXTSWITCH
 
	uint32_t startTime = time_millis();
 
	#endif
 
	
 
@@ -256,32 +273,31 @@ void slavesensors_selectnode(uint8_t nod
 
	char tmpBuf[23];
 
	
 
	// If we can get into AT mode
 
	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);
 
	
 
	#ifdef DEBUG_SELECTNODE
 
	serial0_sendString("Selected ");
 
	serial0_sendChar(nodeIndex + 0x30);
 
@@ -293,20 +309,20 @@ void slavesensors_selectnode(uint8_t nod
 
	char tmpB[32];
 
	snprintf(tmpB, 32, "CTXSW: %lu ms\r\n", switchTime);
 
	serial0_sendString(tmpB);
 
	#endif
 
	
 
	serial0_ion();
 
	return;
 
	return true;
 
}
 
 
void slavesensors_selectlogger() 
 
{
 
	if(loggerIndex != 255) 
 
	if(loggerAddressLow[0] != 0x00) 
 
	{
 
		slavesensors_selectnode(loggerIndex);
 
		slavesensors_selectaddress(loggerAddressHigh,loggerAddressLow);
 
	}	
 
}
 
 
void slavesensors_exitAT() 
 
{
 
	// Exit AT
 
@@ -325,13 +341,13 @@ bool waitTimeout(uint32_t timeout) {
 
	uint32_t scanStart = time_millis();
 
	uint32_t lastBlink = 0;
 
	while(!serial0_hasChar())
 
	{
 
		if(time_millis() - scanStart > timeout)
 
		{
 
			error_log(ERROR_XBEETIMEOUT);
 
			error_log(ERROR_XBEETIMEOUT, true);
 
			return true;
 
		}
 
		if(time_millis() - lastBlink > 50)
 
		{
 
			led_spin();
 
			
 
@@ -358,23 +374,23 @@ int slavesensors_enterAT()
 
	return xbeeIsOk();
 
}
 
 
int xbeeIsOk() 
 
{
 
	if(waitTimeout(2000)) {
 
		error_log(ERROR_XBEETIMEOUT);
 
		error_log(ERROR_XBEETIMEOUT, true);
 
		return 1;
 
	}
 
	char* tmppntr = serial0_readLine();
 
	if(strcmp(tmppntr, "OK") == 0)
 
	{
 
		return 0;
 
	}
 
	else
 
	{
 
		error_log(ERROR_SLAVETIMEOUT);
 
		error_log(ERROR_SLAVETIMEOUT, true);
 
		return 1;
 
	}
 
}
 
 
bool slavesensors_dataReady() 
 
{
 
@@ -393,16 +409,12 @@ void slavesensors_startprocess()
 
}
 
 
// TODO: inline. static.
 
uint32_t beginRequest = 0;
 
void slavesensors_request() 
 
{
 
	if(currentSlave == loggerIndex) {
 
		gotoNextSlaveOrSensor(true);
 
		return;
 
	}
 
	slavesensors_selectnode(currentSlave);
 
	beginRequest = time_millis();
 
	serial_sendCommand("@"); // Request data!
 
}
 
 
 
@@ -435,29 +447,30 @@ void gotoNextSlaveOrSensor(bool fail) {
 
		#endif
 
		
 
		currentSlave++;
 
		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();
 
	}
 
	// If we haven't finished a slave (or all of them), just get the next sensor of the current slave
 
	else
 
	{
 
@@ -487,13 +500,15 @@ void slavesensors_process(uint8_t parseR
 
	{
 
		// Wait for data
 
		if(requesting && time_millis() - beginRequest > 1000) {
 
			// 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
 
		}
 
	}
 
	
 
	// 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)
 
	{
master/master/lib/slavesensors.h
Show inline comments
 
@@ -36,12 +36,13 @@ char* slavesensors_slavename(uint8_t id)
 
bool slavesensors_dataReady();
 
bool slavesensors_isrequesting();
 
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);
 
void slavesensors_process(uint8_t parseResult);
 
int xbeeIsOk();
 
void slavesensors_selectlogger();
master/master/master.c
Show inline comments
 
@@ -35,12 +35,15 @@
 
#include "lib/slavesensors.h"
 
#include "lib/serparser.h"
 
#include "lib/sensordata.h"
 

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