Changeset - f12f7ddbf232
[Not reviewed]
default
0 2 0
ethanzonca@CL-ENS241-08.cedarville.edu - 12 years ago 2013-02-11 17:05:35
ethanzonca@CL-ENS241-08.cedarville.edu
Slave data aquisition fully functional with multiple slaves, fault-tolerant if slaves do not respond within timeout period
2 files changed with 77 insertions and 75 deletions:
0 comments (0 inline, 0 general)
master/master/lib/slavesensors.c
Show inline comments
 
@@ -357,30 +357,16 @@ int slavesensors_enterAT()
 
 
	return xbeeIsOk();
 
}
 
 
int xbeeIsOk() 
 
{
 
	uint32_t scanStart = time_millis();
 
	uint32_t lastBlink = 0;
 
	while(!serial0_hasChar())
 
	{
 
		if(time_millis() - scanStart > 7000)
 
		{
 
	if(waitTimeout(2000)) {
 
			led_errorcode(ERROR_XBEETIMEOUT);
 
			return;
 
		return 1;
 
		}
 
		if(time_millis() - lastBlink > 50)
 
		{
 
			led_spin();
 
						
 
			lastBlink = time_millis();
 
		}
 
		wdt_reset();
 
	}
 
	
 
	char* tmppntr = serial0_readLine();
 
	if(strcmp(tmppntr, "OK") == 0)
 
	{
 
		return 0;
 
	}
 
	else
 
@@ -404,102 +390,52 @@ void slavesensors_startprocess()
 
{
 
	requesting = true;
 
	slavesensors_request();		
 
}
 
 
// TODO: inline. static.
 
uint32_t beginRequest = 0;
 
void slavesensors_request() 
 
{
 
	if(currentSlave == loggerIndex) 
 
	{
 
		currentSlave++;
 
		if(currentSlave >= (nodeCount)) 
 
		{
 
			slavesensors_selectlogger();
 
			return;
 
		}
 
	}
 
	slavesensors_selectnode(currentSlave);
 
	beginRequest = time_millis();
 
	serial_sendCommand("@"); // Request data!
 
}
 
 
 
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.
 
	// TODO: If we time out, WE NEED TO RESET THE PARSER. It could be in a bad state.
 
	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
 
uint8_t numReadingsToExpect = 0; // number of values that the slave is about to send
 
		
 
		// We got some data, let's handle it
 
		// ASCII payload
 
		uint8_t len = getPayloadLength();
 
		char* load = getPayload();
 
		uint8_t type = getPayloadType();
 
		int32_t parsedVal = strtol(load, NULL, 10);//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;
 
			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 
 
			
 
void gotoNextSlaveOrSensor(bool fail) {
 
			// If we finished all sensors for all slaves
 
			
 
			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(!fail) 
 
		{
 
				led_alert();
 
			}
 
		
 
	}
 
			// 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
 
@@ -538,12 +474,77 @@ void slavesensors_process(uint8_t parseR
 
				// 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
 
			}
 
		}
 
 
 
// 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.
 
	// TODO: If we time out, WE NEED TO RESET THE PARSER. It could be in a bad state.
 
	else if(parseResult == PARSERESULT_NODATA) 
 
	{
 
		// 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!
 
			gotoNextSlaveOrSensor(true);
 
		}
 
	}
 
	
 
	// 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();
 
		char* load = getPayload();
 
		uint8_t type = getPayloadType();
 
		int32_t parsedVal = strtol(load, NULL, 10);//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;
 
			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 
 
			
 
			gotoNextSlaveOrSensor(false);
 
		}
 
	}
 
	
 
	// If fail, try retransmit. Or we could skip and hit it next time.
 
	// TODO: Maximum number of retransmissions
 
	else if(parseResult == PARSERESULT_FAIL) 
 
	{
master/master/lib/slavesensors.h
Show inline comments
 
@@ -38,12 +38,13 @@ bool slavesensors_isrequesting();
 
void slavesensors_setup();
 
void slavesensors_network_scan();
 
uint8_t slavesensors_getselectednode();
 
void slavesensors_selectnode(uint8_t nodeIndex);
 
void slavesensors_startprocess();
 
void slavesensors_request();
 
void gotoNextSlaveOrSensor(bool fail);
 
void slavesensors_process(uint8_t parseResult);
 
int xbeeIsOk();
 
void slavesensors_selectlogger();
 
void slavesensors_exitAT();
 
int slavesensors_enterAT();
 
0 comments (0 inline, 0 general)