Changeset - 261d812b2664
[Not reviewed]
default
0 7 0
ethanzonca@CL-SEC241-08.cedarville.edu - 12 years ago 2012-12-03 22:19:53
ethanzonca@CL-SEC241-08.cedarville.edu
Sensor data is now stored properly in SRAM for logging as-needed; indexed by sensor ID.
7 files changed with 97 insertions and 53 deletions:
0 comments (0 inline, 0 general)
master/master/config.h
Show inline comments
 
@@ -41,12 +41,15 @@
 
#define SLAVE3_SENSORS BOARDTEMP | CAMERA
 
#define SLAVE4_SENSORS NONE
 
#define SLAVE5_SENSORS NONE
 
#define SLAVE6_SENSORS NONE
 
#define SLAVE7_SENSORS NONE
 
 
// MAX_SLAVES should be one more than the number of slaves we actually have (loop ends when next slave first sensor is NONE)
 
#define MAX_SLAVES 8
 
#define MAX_SLAVE_SENSORS 8
 
 
// --------------------------------------------------------------------------
 
// Command Parser config (serparser.c)
 
// --------------------------------------------------------------------------
 
 
// Maximum payload size of command
master/master/lib/serparser.c
Show inline comments
 
@@ -29,33 +29,47 @@ volatile uint16_t bufferDataPosition = 0
 
 
// Parser state
 
uint8_t parserState = STATE_RESET;
 
uint8_t lastParserState = STATE_RESET;
 
 
// Length of current payload data (and checksum)
 
uint8_t dataLength = 0;
 
uint8_t payloadLength = 0;
 
 
// Data and checksum of most recent transmission
 
char receivedPayload[MAX_PAYLOAD_LEN];
 
 
// Payload type ID of the sensor of most recent transmission
 
char receivedDataType = 0;
 
char receivedPayloadType = 0;
 
 
// Checksum to be calculated and then compared to the received checksum
 
char checksumCalc = 0;
 
 
// Accessors
 
uint8_t getPayloadLength() 
 
{
 
	return payloadLength;
 
}
 
uint8_t* getPayload() 
 
{
 
	return receivedPayload;
 
}
 
uint8_t getPayloadType() {
 
	return receivedPayloadType;
 
}
 
 
 
// Could inline if program space available
 
static void setParserState(uint8_t state)
 
{
 
	lastParserState = parserState;
 
	parserState = state;
 
	
 
	// If resetting, clear vars
 
	if(state == STATE_RESET) 
 
	{
 
		dataLength = 0;
 
		payloadLength = 0;
 
		checksumCalc = 0;
 
	}
 
	
 
	// Every time we change state, we have parsed a byte
 
	bufferParsePosition = (bufferParsePosition + 1) % BUFFER_SIZE;
 
}
 
@@ -63,13 +77,12 @@ static void setParserState(uint8_t state
 
// Receive data on USART
 
 
char debugBuff[16];
 
 
ISR(USART0__RX_vect)
 
{
 
	led_on(POWER);
 
	buffer[bufferDataPosition % BUFFER_SIZE] = UDR0;
 
	bufferDataPosition = (bufferDataPosition + 1) % BUFFER_SIZE;
 
	/*sprintf(debugBuff, "bdp: %d, bpp: %d \r\n", bufferDataPosition, bufferParsePosition);
 
	serial0_sendString((debugBuff)); */
 
}
 
 
@@ -131,44 +144,46 @@ int serparser_parse(void)
 
		// Get payload type ID
 
		else if(parserState == STATE_GETDATATYPE)
 
		{
 
			#ifdef DEBUG
 
			serial0_sendString("type\r\n");
 
			#endif
 
			receivedDataType = byte; // Store the type of data receiving
 
			receivedPayloadType = byte; // Store the type of data receiving
 
			checksumCalc += byte;
 
			setParserState(STATE_GETDATA);
 
		}
 
		
 
		// Get payload data
 
		else if(parserState == STATE_GETDATA)
 
		{		
 
			if (byte == ']') // End of frame
 
			{
 
				#ifdef DEBUG
 
				//#ifdef DEBUG
 
				serial0_sendString("eof\r\n");
 
				sprintf(debugBuff, "%d B, sum=%d\r\n", dataLength, checksumCalc);
 
				sprintf(debugBuff, "%d B, sum=%d\r\n", payloadLength, checksumCalc);
 
				serial0_sendString((debugBuff));
 
				#endif
 
				//#endif
 
				
 
				receivedPayload[payloadLength] = 0; // null-terminate string for atoi
 
				
 
				setParserState(STATE_GETCHECKSUM);
 
				// Checksum is now after the close bracket to solve bug FS#29		
 
				
 
 
			}
 
			else // Still receiving data
 
			{
 
				#ifdef DEBUG
 
				serial0_sendString("data\r\n");
 
				#endif
 
				receivedPayload[dataLength] = byte;
 
				dataLength++;
 
				receivedPayload[payloadLength] = byte;
 
				payloadLength++;
 
				checksumCalc += byte;
 
				
 
				// Data buffer overrun protection
 
				if(dataLength > MAX_PAYLOAD_LEN) {
 
				if(payloadLength > MAX_PAYLOAD_LEN) {
 
					#ifdef DEBUG
 
					serial0_sendString("ovf\r\n");
 
					#endif
 
					setParserState(STATE_RESET);
 
					return PARSERESULT_FAIL;
 
				}
 
@@ -189,12 +204,13 @@ int serparser_parse(void)
 
				setParserState(STATE_RESET);
 
				return PARSERESULT_PARSEOK;
 
			}
 
			else {
 
				serial0_sendString("bcheck\r\n");
 
				setParserState(STATE_RESET);
 
				
 
				return PARSERESULT_FAIL;
 
			}
 
			
 
			/*
 
			if(bufferParsePosition == bufferDataPosition)
 
			{
master/master/lib/serparser.h
Show inline comments
 
@@ -29,10 +29,16 @@ enum parseStates
 
	STATE_GETID,
 
	STATE_GETDATATYPE,
 
	STATE_GETDATA,
 
	STATE_GETCHECKSUM,
 
};
 
 
 
// Accessors
 
uint8_t getPayloadLength();
 
uint8_t* getPayload();
 
uint8_t getPayloadType();
 
 
// Prototypes
 
int serparser_parse(void);
 
 
#endif /* SERPARSER_H_ */
 
\ No newline at end of file
master/master/lib/slavesensors.c
Show inline comments
 
@@ -14,40 +14,25 @@
 
#include <stdbool.h>
 
#include "../config.h"
 
#include "serial.h"
 
#include "serparser.h"
 
#include "slavesensors.h"
 

	
 
// Serial Commands
 
enum sensorTypes // CMD ID#
 
{
 
	NONE = 0,
 
	BOARDTEMP = (1<<0),
 
	PRESSURE = (1<<1),
 
	GEIGER = (1<<2),
 
	TEMPERATURE = (1<<3),
 
	HUMIDITY = (1<<4),
 
	AMBIENTLIGHT = (1<<5),
 
	CAMERA = (1<<5),
 
};
 

	
 
uint8_t currentSlave = 0;
 
uint8_t currentSlaveSensor = 0;
 
 
uint8_t maxSlave = 8;
 
uint8_t maxSensorsPerSlave = 8;
 
uint16_t slaves[8][8];
 
uint16_t slaves[MAX_SLAVES][MAX_SLAVE_SENSORS];
 
 
bool requesting = false;
 
 
void slavesensors_setup() 
 
{
 
	// Empty array
 
	for(int i=0; i<maxSlave; i++) 
 
	for(int i=0; i<MAX_SLAVES; i++) 
 
	{
 
		for(int j=0; j<maxSensorsPerSlave; j++) 
 
		for(int j=0; j<MAX_SLAVE_SENSORS; j++) 
 
		{
 
			slaves[i][j] = NONE;
 
		}			
 
	}	
 
	
 
	// Slave Configuration
 
@@ -80,15 +65,16 @@ bool slavesensors_isrequesting()
 
void slavesensors_startprocess() 
 
{
 
	requesting = true;
 
	slavesensors_request();		
 
}
 
 
static void slavesensors_request() 
 
// TODO: inline. static.
 
void slavesensors_request() 
 
{
 
	serial_sendCommand(currentSlave + 0x30, currentSlaveSensor + 0x30, "");
 
	serial_sendCommand(currentSlave + 0x30, slaves[currentSlave][currentSlaveSensor] + 0x30, "");
 
}
 
 
void slavesensors_process(uint8_t parseResult) 
 
{
 
	
 
	if(!requesting) {
 
@@ -101,12 +87,29 @@ void slavesensors_process(uint8_t parseR
 
		// 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)
 
	{
 
		// We got some data, let's handle it
 
		// ASCII payload
 
		uint8_t len = getPayloadLength();
 
		uint8_t* load = getPayload();
 
		
 
		uint8_t type = getPayloadType();
 
		// TODO: Check if type matches. if it doesn't, we have a problem...
 
		
 
		uint16_t parsedVal = atoi(load);
 
		
 
		if(slaves[currentSlave][currentSlaveSensor] == BOARDTEMP) {
 
			sensordata_setBoardTemp(currentSlave, parsedVal);
 
		}
 
		else {
 
			sensordata_set(slaves[currentSlave][currentSlaveSensor], parsedVal);
 
		}
 
		
 
		// If we finished all sensors for all slaves
 
		if(slaves[currentSlave+1][0] == NONE && slaves[currentSlave][currentSlaveSensor+1] == NONE) // If next sensor is none and finished all slaves, reset
 
		{
 
			currentSlave = 0;
 
			currentSlaveSensor = 0;
 
			requesting = false;
master/master/lib/slavesensors.h
Show inline comments
 
@@ -14,13 +14,26 @@
 
#ifndef SLAVESENSORS_H_
 
#define SLAVESENSORS_H_
 
 
#include <stdbool.h>
 
#include <inttypes.h>
 
 
// Serial Commands
 
enum sensorTypes // CMD ID#
 
{
 
	NONE = 0,
 
	BOARDTEMP,
 
	PRESSURE,
 
	GEIGER,
 
	TEMPERATURE,
 
	HUMIDITY,
 
	AMBIENTLIGHT,
 
	CAMERA,
 
};
 
 
bool slavesensors_isrequesting();
 
void slavesensors_setup();
 
void slavesensors_startprocess();
 
static void slavesensors_request();
 
void slavesensors_request();
 
void slavesensors_process(uint8_t parseResult);
 
 
#endif /* SLAVESENSORS_H_ */
 
\ No newline at end of file
master/master/master.c
Show inline comments
 
@@ -27,41 +27,47 @@
 
#include "lib/logger.h"
 
#include "lib/watchdog.h"
 
#include "lib/sd/sd_raw_config.h"
 
#include "lib/looptime.h"
 
#include "lib/slavesensors.h"
 
#include "lib/serparser.h"
 
#include "lib/sensordata.h"
 

	
 
void micro_setup() 
 
{
 

	
 
}
 

	
 
int main(void)
 
{
 
    
 
	// Initialize libraries
 
	time_setup();
 
	
 
	micro_setup();
 
	watchdog_setup();
 
	
 
	led_setup();
 
	
 
	serial0_setup();
 
	serial1_setup();
 
	
 
	sensordata_setup(); // must happen before sensors/logger/afsk
 
	slavesensors_setup();
 
	logger_setup();
 
	afsk_setup();
 

	
 
	serial0_sendString("\r\n\r\n---------------------------------\r\n");
 
	serial0_sendString("HAB Controller 1.0 - Initialized!\r\n");
 
	serial0_sendString("---------------------------------\r\n\r\n");
 
	//serial0_sendString("\r\n\r\n---------------------------------\r\n");
 
	//serial0_sendString("HAB Controller 1.0 - Initialized!\r\n");
 
	//serial0_sendString("---------------------------------\r\n\r\n");
 
	serial0_sendString("\r\n\r\nHELLO.\r\n\r\n");
 
	
 
	//led_on(POWER);
 
	led_on(POWER);
 
	
 
	// Buffer for string operations
 
	char logbuf[32];
 
	const char* logBufPtr = logbuf;
 
	
 
	// Software timers	
 
	uint32_t lastAprsBroadcast = 0;
 
	uint32_t lastLog = 0;
 
	
 
	// Result of last parser run
 
@@ -69,44 +75,35 @@ int main(void)
 
	
 
	// Write CSV header to SD card
 
	logger_log("ProgramTime,LastAprsBroadcast,LastLog\n");
 
	
 
	while(1)
 
    {
 
		
 
		// Periodic: Logging
 
		if(time_millis() - lastLog > LOGGER_RATE) 
 
		{
 
			
 
			// TODO: Acquire data from daughterboards
 
			//       This will be complicated because we need timeouts / unreliable transmission, etc
 
			//
 
			// For each daughterboard...
 
			//   1. Send request to daughterboard for sensor data
 
			//   2. Wait for response from daughterboard (timeout!)
 
			//   3. Put data into local variables for transmission / logging
 
			
 
			led_on(STAT);
 
			snprintf(logbuf, 32, "%lu,%lu,%lu,\r\n", time_millis(), lastAprsBroadcast,lastLog);
 
			logger_log(logbuf);
 
			//serial0_sendString("SD Logger: ");
 
			//serial0_sendString(logBufPtr);
 
			//snprintf(logbuf, 32, "%lu,%lu,%lu,\r\n", time_millis(), lastAprsBroadcast,lastLog);
 
			//logger_log(logbuf);
 
			led_off(STAT);
 
			lastLog = time_millis();
 
		}		
 
		
 
		// Periodic: APRS transmission
 
		if(time_millis() - lastAprsBroadcast > APRS_TRANSMIT_PERIOD) 
 
		{
 
			while(afsk_busy());
 
			aprs_send(); // non-blocking
 
			//serial0_sendString("Initiating APRS transmission...\r\n");
 
			
 
			// Start getting values for next transmission
 
			// TODO: Check ifRequesting first. If we are still requesting, something is terribly wrong.
 
			if(!slavesensors_isrequesting())
 
			if(slavesensors_isrequesting())
 
			{
 
				// TODO: something is terribly wrong
 
			}
 
			else 
 
			{
 
				slavesensors_startprocess();
 
			}
 
			
 
			lastAprsBroadcast = time_millis();
 
		}			
master/master/master.cproj
Show inline comments
 
@@ -183,12 +183,18 @@
 
    <Compile Include="lib\sd\sd_raw.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\sd\sd_raw_config.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\sensordata.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\sensordata.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\serial.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\serial.h">
 
      <SubType>compile</SubType>
 
    </Compile>
0 comments (0 inline, 0 general)