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
 
@@ -351,42 +351,28 @@ int slavesensors_enterAT()
 
	serial0_ioff(); // interrupts MUST be off
 
	
 
	// Enter AT mode
 
	serial0_sendChar('+'); // Enter AT mode
 
	serial0_sendChar('+');
 
	serial0_sendChar('+');
 
 
	return xbeeIsOk();
 
}
 
 
int xbeeIsOk() 
 
{
 
	uint32_t scanStart = time_millis();
 
	uint32_t lastBlink = 0;
 
	while(!serial0_hasChar())
 
	{
 
		if(time_millis() - scanStart > 7000)
 
		{
 
			led_errorcode(ERROR_XBEETIMEOUT);
 
			return;
 
		}
 
		if(time_millis() - lastBlink > 50)
 
		{
 
			led_spin();
 
						
 
			lastBlink = time_millis();
 
		}
 
		wdt_reset();
 
	if(waitTimeout(2000)) {
 
		led_errorcode(ERROR_XBEETIMEOUT);
 
		return 1;
 
	}
 
	
 
	char* tmppntr = serial0_readLine();
 
	if(strcmp(tmppntr, "OK") == 0)
 
	{
 
		return 0;
 
	}
 
	else
 
	{
 
		led_errorcode(ERROR_NOXBEE);
 
		return 1;
 
	}
 
}
 
 
@@ -398,57 +384,128 @@ bool slavesensors_dataReady()
 
bool slavesensors_isrequesting() 
 
{
 
	return requesting;	
 
}
 
 
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)
 
uint8_t numReadingsToExpect = 0; // number of values that the slave is about to send
 
 
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
 
		
 
		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++; // 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
 
	{
 
		#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
 
	}
 
}
 
 
 
// 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
 
		
 
@@ -474,81 +531,25 @@ void slavesensors_process(uint8_t parseR
 
			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-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;
 
				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
 
				
 
				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++; // 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
 
			{
 
				#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
 
			}
 
			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) 
 
	{
 
		if(requesting) 
 
		{
 
			slavesensors_request();	// re-request
 
		}			
 
	}
master/master/lib/slavesensors.h
Show inline comments
 
@@ -32,19 +32,20 @@ enum sensorTypes // CMD ID#
 
 
bool waitTimeout(uint32_t timeout);
 
char* slavesensors_getLabel(uint8_t sensorID);
 
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);
 
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();
 
 
#endif /* SLAVESENSORS_H_ */
 
\ No newline at end of file
0 comments (0 inline, 0 general)