Changeset - c21de31ee856
[Not reviewed]
default
0 6 0
ethanzonca@CL-ENS241-08.cedarville.edu - 12 years ago 2013-01-30 21:20:33
ethanzonca@CL-ENS241-08.cedarville.edu
Added labeling feature to logged data for sensor data types,
slave data request system now fully functional. Note: logger node
is no longer selected as previously planned.
6 files changed with 89 insertions and 16 deletions:
0 comments (0 inline, 0 general)
master/master/config.h
Show inline comments
 
@@ -43,24 +43,26 @@
 
#define ERROR_EXITAT 8
 
 
// --------------------------------------------------------------------------
 
// Slave Sensors config (slavesensors.c)
 
// --------------------------------------------------------------------------
 
 
#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"
 
 
#define DATAREQUEST_RATE 3000
 
 
// --------------------------------------------------------------------------
 
// Command Parser config (serparser.c)
 
// --------------------------------------------------------------------------
 
 
// Maximum payload size of command
 
#define MAX_PAYLOAD_LEN 16
 
 
// Circular serial buffer size. Must be at least MAX_CMD_LEN + 5
 
#define BUFFER_SIZE 32 
 
 
// Public broadcast address
 
#define BROADCAST_ADDR 0 
master/master/lib/led.c
Show inline comments
 
@@ -49,24 +49,31 @@ void led_errorcode(uint8_t code)
 
	_delay_ms(400);
 
	for(int i=0; i<code; i++) 
 
	{
 
		led_on(LED_ERROR);
 
		_delay_ms(150);
 
		led_off(LED_ERROR);
 
		_delay_ms(150);
 
	}
 
	_delay_ms(400);
 
	led_on(LED_ERROR);
 
}
 
 
void led_alert() {
 
	led_on(LED_ACT0);
 
	led_on(LED_ACT1);
 
	led_on(LED_ACT2);
 
	led_on(LED_ACT3);
 
	_delay_ms(10);
 
}
 

	
 
uint8_t ctr = 0;
 
void led_spin() {
 
	
 
	if(ctr == 0) {
 
		led_on(LED_ACT0);
 
		led_off(LED_ACT1);
 
		led_off(LED_ACT2);
 
		led_off(LED_ACT3);
 
	}
 
	else if (ctr == 1) {
 
		led_on(LED_ACT1);
master/master/lib/led.h
Show inline comments
 
@@ -58,14 +58,15 @@ static led_t ledList[] = {
 
	{&DDRA, &PORTA, PA6}, // HEAT
 
	{&DDRA, &PORTA, PA7}, // BUZZER
 
};
 
 
#define NUM_LEDS 12
 
 
void led_setup();
 
void led_on(uint8_t led);
 
void led_off(uint8_t led);
 
void led_toggle(uint8_t led);
 
void led_errorcode(uint8_t code);
 
void led_spin();
 
void led_alert();
 
 
#endif /* LED_H_ */
master/master/lib/slavesensors.c
Show inline comments
 
@@ -9,43 +9,74 @@
 
 * Matthew Kroening
 
 *
 
 */
 
 
#include "../config.h"
 
#include <avr/io.h>
 
#include <stdbool.h>
 
#include <stdlib.h>
 
#include <stdio.h>
 
#include <string.h>
 
#include <util/delay.h>
 
#include <avr/wdt.h>
 
#include <avr/pgmspace.h>
 
#include "serial.h"
 
#include "serparser.h"
 
#include "slavesensors.h"
 
#include "sensordata.h"
 
#include "led.h"
 
#include "looptime.h"
 

	
 
// Label lookup table
 
const char label_0[] PROGMEM = "BoardTemp";
 
const char label_1[] PROGMEM = "HeaterStatus";
 
const char label_2[] PROGMEM = "BatteryLevel";
 
const char label_3[] PROGMEM = "AirTemp";
 
const char label_4[] PROGMEM = "AmbientLight";
 
const char label_5[] PROGMEM = "Humidity";
 
const char label_6[] PROGMEM = "Pressure";
 
const char label_7[] PROGMEM = "Altitude";
 
const char label_8[] PROGMEM = "CPM-Radiation";
 

	
 
const char *const labelLookup[] PROGMEM =
 
{
 
	label_0,
 
	label_1,
 
	label_2,
 
	label_3,
 
	label_4,
 
	label_5,
 
	label_6,
 
	label_7,
 
	label_8,
 
};
 

	
 
char labelBuffer[25]; // Size to length of label
 
char* slavesensors_getLabel(uint8_t sensorID) {
 
	strncpy_P(labelBuffer,(char*)pgm_read_word(&(labelLookup[sensorID])),25);
 
	return labelBuffer;
 
}
 

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

	
 
//#define DEBUG_NETWORKSCAN
 
//#define DEBUG_GETSLAVEDATA
 
#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][20];
 

	
 
uint8_t loggerIndex = 255;
 
uint8_t nodeCount = 0;
 
bool dataReady = false;
 

	
 
char* slavesensors_slavename(uint8_t id) {
 
@@ -159,29 +190,43 @@ void slavesensors_network_scan() {
 
		if(strcmp(slaveNames[i], XBEE_LOGDEST_NAME) == 0) 
 
		{
 
			loggerIndex = i;
 
		}
 
	}
 
	_delay_ms(100);
 
	
 
	slavesensors_selectlogger();
 
	
 
	serial0_ion();
 
}
 
 
// #define DEBUG_SELECTNODE
 
//#define DEBUG_CONTEXTSWITCH
 
//#define DEBUG_SELECTNODE
 
 
uint8_t selectedNode = 0;
 
uint8_t slavesensors_getselectednode() {
 
	return selectedNode;
 
}
 
 
void slavesensors_selectnode(uint8_t nodeIndex)
 
{
 
	if(selectedNode == nodeIndex) {
 
		return;
 
	}
 
	serial0_ioff();
 
	
 
	#ifdef DEBUG_CONTEXTSWITCH
 
	uint32_t startTime = time_millis();
 
	#endif
 
	
 
	#ifdef DEBUG_SELECTNODE
 
	serial0_sendString("Switch to node ");
 
	serial0_sendChar(nodeIndex + 0x30);
 
	serial0_sendString("\r\n");
 
	#endif
 
	
 
	_delay_ms(20);
 
	char tmpBuf[23];
 
	
 
	// If we can get into AT mode
 
	if(slavesensors_enterAT() == 0) {
 
		
 
@@ -193,37 +238,41 @@ void slavesensors_selectnode(uint8_t nod
 
			return;
 
		}
 
		
 
		snprintf(tmpBuf, 23, "ATDL %s%c",slaveAddressLow[nodeIndex], 0x0D);
 
		serial0_sendString(tmpBuf);
 
		
 
		if(xbeeIsOk() != 0) {
 
			led_errorcode(ERROR_NOXBEE);
 
			return;
 
		}
 
		
 
		slavesensors_exitAT();
 
		selectedNode = nodeIndex;
 
	}
 
	_delay_ms(2);
 
	
 
	#ifdef DEBUG_SELECTNODE
 
	serial0_sendString("Selected ");
 
	serial0_sendChar(nodeIndex + 0x30);
 
	serial0_sendString("\r\n");
 
	#endif
 
	
 
	#ifdef DEBUG_CONTEXTSWITCH
 
	uint32_t switchTime = time_millis() - startTime;
 
	char tmpB[32];
 
	snprintf(tmpB, 32, "CTXSW: %lu ms\r\n", switchTime);
 
	serial0_sendString(tmpB);
 
	#endif
 
	
 
	serial0_ion();
 
	return;
 
}
 
 
void slavesensors_selectlogger() 
 
{
 
	if(loggerIndex != 255) {
 
		slavesensors_selectnode(loggerIndex);
 
	}	
 
}
 
 
void slavesensors_exitAT() 
 
@@ -298,28 +347,31 @@ bool slavesensors_isrequesting()
 
 
void slavesensors_startprocess() 
 
{
 
	requesting = true;
 
	slavesensors_request();		
 
}
 
 
// TODO: inline. static.
 
void slavesensors_request() 
 
{
 
	if(currentSlave == loggerIndex) {
 
		currentSlave++;
 
		if(currentSlave >= (nodeCount)) {
 
			slavesensors_selectlogger();
 
			return;
 
		}
 
	}
 
	slavesensors_selectnode(currentSlave);
 
	serial_sendCommand("@"); // Request data!
 
	slavesensors_selectlogger();
 
}
 
 
 
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;
 
@@ -373,43 +425,45 @@ void slavesensors_process(uint8_t parseR
 
			// 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
 
			{
master/master/lib/slavesensors.h
Show inline comments
 
@@ -21,26 +21,29 @@
 
enum sensorTypes // CMD ID#
 
{
 
	NONE = 0,
 
	BOARDTEMP,
 
	PRESSURE,
 
	GEIGER,
 
	TEMPERATURE,
 
	HUMIDITY,
 
	AMBIENTLIGHT,
 
	CAMERA,
 
};
 
 
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 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
master/master/master.c
Show inline comments
 
@@ -68,24 +68,26 @@ int main(void)
 
	
 
	serial0_sendString("\r\nHello.\r\n\r\n");
 
	
 
	slavesensors_network_scan(); // This can take a little while
 
	
 
	// Buffer for string operations
 
	char logbuf[128];
 
	
 
	// Software timers	
 
	uint32_t lastAprsBroadcast = 0;
 
	uint32_t lastLog = 0;
 
	uint32_t lastLedCycle = 0;
 
	uint32_t lastDataReq = 0;
 
	
 
	bool dataWasReady = false;
 
	
 
	// Result of last parser run
 
	int parseResult = PARSERESULT_NODATA;
 
	
 
	serial1_ioff();
 
	
 
	while(1)
 
    {
 
		
 
		// Periodic: LED execution indicator
 
		if(time_millis() - lastLedCycle > LEDCYCLE_RATE) {
 
@@ -123,25 +125,25 @@ int main(void)
 
				if(!dataWasReady) {
 
					char csvHeader[128];
 
					csvHeader[0] = 0x00;
 
					
 
					// Add master data headers
 
					snprintf(csvHeader, 128, "Time,BoardTemp,Lat,Lon,HDOP,Speed,GPS SV,");
 
					
 
					for(uint8_t i=0; i<MAX_NUM_SLAVES; i++) {
 
						for(uint8_t j=0; j<MAX_NUM_SENSORS; j++) {
 
							int16_t tmp = sensordata_get(i, j);
 
							if(tmp != -32768) {
 
								// FIXME: will the 128 here really provide safety? might want to subtract the strlen
 
								snprintf(csvHeader + strlen(csvHeader), 128,"%s-Sensor%u,", slavesensors_slavename(i), j);	
 
								snprintf(csvHeader + strlen(csvHeader), 128,"%s-%s,", slavesensors_slavename(i), slavesensors_getLabel(j));	
 
							}
 
						}
 
					}
 
					snprintf(csvHeader + strlen(csvHeader), 128,"\r\n");	
 
					serial0_sendString(csvHeader);
 
					logger_log(csvHeader);
 
					dataWasReady = true;
 
				}
 
		
 
				logbuf[0] = 0x00;
 
				snprintf(logbuf, 128, "%lu,%d,%u,%s,%s,%s,%s,%s", time_millis(), sensors_getBoardTemp(),get_timestamp(),get_latitude(),get_longitude(),get_speedKnots(),get_hdop(), get_course());
 
				for(int i=0; i<MAX_NUM_SLAVES; i++) {
 
@@ -161,41 +163,45 @@ int main(void)
 
			
 
			// Print out logger debug
 
			#ifdef DEBUG_OUTPUT
 
			//serial0_sendString("LOG> ");
 
			//serial0_sendString(logbuf);
 
			#endif
 
			
 
			led_off(LED_CYCLE);
 
			lastLog = time_millis();
 
		}		
 
		
 
		
 
		// Periodic: Data Request
 
		if(time_millis() - lastDataReq > DATAREQUEST_RATE)  {
 
			// Start getting values for next transmission
 
			if(slavesensors_isrequesting())
 
			{
 
				// TODO: something is terribly wrong
 
			}
 
			else
 
			{
 
				slavesensors_startprocess();
 
			}
 
			
 
			lastDataReq = time_millis();
 
		}
 
		
 
		
 
		// Periodic: APRS transmission
 
		if(time_millis() - lastAprsBroadcast > APRS_TRANSMIT_PERIOD) 
 
		{
 
			while(afsk_busy());
 
			serial1_ioff();
 
			aprs_send(); // non-blocking
 
			
 
			//serial0_sendString("Initiating APRS transmission...\r\n");
 
			
 
			// Start getting values for next transmission
 
			if(slavesensors_isrequesting())
 
			{
 
				// TODO: something is terribly wrong
 
			}
 
			else 
 
			{				
 
				slavesensors_startprocess();
 
			}
 
			
 
			lastAprsBroadcast = time_millis();
 
		}			
 
		
 

	
 
		parseResult = serparser_parse();
 
		slavesensors_process(parseResult);
 
		parse_gps_transmission();
 
		wdt_reset();
 
    }
 
}
 
\ No newline at end of file
0 comments (0 inline, 0 general)