Changeset - 88d82c0c283c
[Not reviewed]
default
0 11 0
ethanzonca@CL-ENS241-08.cedarville.edu - 12 years ago 2013-04-11 20:57:33
ethanzonca@CL-ENS241-08.cedarville.edu
Added LSBs of GPS to csv log, added GPS altitude to csv log, added major feature: INI file parsing for dynamic system configuration.
11 files changed with 151 insertions and 42 deletions:
0 comments (0 inline, 0 general)
master/master/config.h
Show inline comments
 
@@ -12,12 +12,14 @@
 
 
#ifndef CONFIG_H_
 
#define CONFIG_H_
 
 
#include <avr/pgmspace.h>
 
 
// NOTE: Some settings may be overridden by SD card config.ini parameters
 
 
// --------------------------------------------------------------------------
 
// Hardware settings
 
// --------------------------------------------------------------------------
 
 
#define F_CPU 11059200
 
#define BOARDTEMP_ADDR 0x90
 
@@ -35,13 +37,13 @@
 
#define BLACKOUT_TIMEOUT 1200000 // Blackout after 20 minutes (hopefully after fix and launch)
 
 
// Board heater setpoint
 
#define HEATER_THRESHOLD 5 // Regulate to 5 degrees Celsius
 
 
// Touchdown buzzer
 
#define BUZZER_RATE 5000
 
#define BUZZER_RATE 7000
 
#define BUZZER_DURATION 1000
 
#define BUZZER_FAILSAFE_DURATION  9600000 // 160min
 
#define BUZZER_TRIGGER_MINDURATION 2700000 // 45min
 
#define BUZZER_TRIGGER_MAXALTITUDE 2000 // feet
 
 
// --------------------------------------------------------------------------
 
@@ -50,18 +52,19 @@
 
 
// SD Card
 
#define ERROR_SLAVETIMEOUT 0
 
#define ERROR_SD_INIT 1
 
#define ERROR_SD_PARTITION 2
 
#define ERROR_SD_FILE 3
 
#define ERROR_XBEETIMEOUT 4
 
#define ERROR_FATAL 5
 
#define ERROR_ATFAIL 6
 
#define ERROR_EXITAT 7
 
#define ERROR_INFOTEXT 8
 
#define ERROR_WATCHDOG 9
 
#define ERROR_CONFIGPARSE 4
 
#define ERROR_XBEETIMEOUT 5
 
#define ERROR_FATAL 6
 
#define ERROR_ATFAIL 7
 
#define ERROR_EXITAT 8
 
#define ERROR_INFOTEXT 9
 
#define ERROR_WATCHDOG 10
 
// !!! Please specify/update detailed messages for these error codes in logger.c
 
 
// --------------------------------------------------------------------------
 
// Slave Sensors config (slavesensors.c)
 
// --------------------------------------------------------------------------
 
 
@@ -161,16 +164,37 @@
 
#define LOGGER_ID_EEPROM_ADDR 0x10
 
 
// Watchdog save/restore of program timer
 
#define WATCHDOG_PROGTIMER_EEPROM_ADDR 0x20
 
 
// Written to the beginning of every log file
 
#define LOGGER_HEADERTEXT "HAB Control Master - 1.1\n"
 
#define LOGGER_HEADERTEXT "HAB Master v1.2\n"
 
 
// Log to SD card every X milliseconds
 
#define LOGGER_RATE 1000 
 
 
// LED cycle indicator speed
 
#define LEDCYCLE_RATE 100 
 
 
 
 
// --------------------------------------------------------------------------
 
// Dynamic Configuration (iniparse.c) - You probably don't want to change this
 
// --------------------------------------------------------------------------
 
#include <stdbool.h>
 
typedef struct
 
{
 
	bool blackout_enable;
 
	uint32_t blackout_timeout;
 
	int8_t heater_threshold;
 
	uint32_t buzzer_failsafe_duration;
 
	uint32_t buzzer_trigger_minduration;
 
	uint32_t buzzer_trigger_maxaltitude;
 
	uint32_t datarequest_rate;
 
	char s_callsign[10];
 
	uint8_t s_callsign_id;
 
	uint32_t aprs_transmit_period;
 
} configuration;
 
 
configuration* sysconfig;
 
 
#endif /* CONFIG_H_ */
 
\ No newline at end of file
master/master/lib/aprs.c
Show inline comments
 
@@ -24,13 +24,13 @@ float meters_to_feet(float m)
 
}
 

	
 
void aprs_send()
 
{
 
  const struct s_address addresses[] = { 
 
    {D_CALLSIGN, D_CALLSIGN_ID},  // Destination callsign
 
    {S_CALLSIGN, S_CALLSIGN_ID},  // Source callsign (-11 = balloon, -9 = car)
 
    {sysconfig->s_callsign, sysconfig->s_callsign_id},  // Source callsign (-11 = balloon, -9 = car)
 
#ifdef DIGI_PATH1
 
    {DIGI_PATH1, DIGI_PATH1_TTL}, // Digi1 (first digi in the chain)
 
#endif
 
#ifdef DIGI_PATH2
 
    {DIGI_PATH2, DIGI_PATH2_TTL}, // Digi2 (second digi in the chain)
 
#endif
master/master/lib/gps.c
Show inline comments
 
@@ -94,12 +94,17 @@ char hdop[6];		//xx.x
 
char* get_hdop() 
 
{
 
	return hdop;
 
}
 

	
 
char altitude[10];	//xxxxxx.x
 
char* get_gpsaltitude()
 
{
 
	return altitude;
 
}
 

	
 
char wgs84Height[8];	//sxxx.x
 
char lastUpdated[8];	//blank - included for testing
 
char stationID[8];	//blank - included for testing
 
char checksum[3];	//xx
 

	
 
char knots[8];		//xxx.xx
master/master/lib/gps.h
Show inline comments
 
@@ -23,12 +23,13 @@
 
void gps_setup();
 
char* get_longitudeTrimmed();
 
char* get_longitudeLSBs();
 
char* get_latitudeTrimmed();
 
char* get_latitudeLSBs();
 
char* get_timestamp();
 
char* get_gpsaltitude();
 
char* get_speedKnots();
 
char* get_course();
 
char* get_hdop();
 
char* get_sv();
 
char* get_dayofmonth();
 
bool gps_hasfix();
master/master/lib/heater.c
Show inline comments
 
@@ -14,17 +14,17 @@
 
#include "led.h"
 
#include "sensors.h"
 
 
 
 void heater_regulateTemp()
 
 {
 
	 // Gets board temperature and enables heater if below threshold
 
	 if (sensors_getBoardTemp() <= HEATER_THRESHOLD)
 
	 if (sensors_getBoardTemp() <= sysconfig->heater_threshold)
 
	 {
 
		 led_on(LED_HEAT);
 
		 led_on(LED_STATUS);
 
	 }
 
	 else if (sensors_getBoardTemp() > (HEATER_THRESHOLD + 5))
 
	 else if (sensors_getBoardTemp() > (sysconfig->heater_threshold + 5))
 
	 {
 
		 led_off(LED_HEAT);
 
		 led_off(LED_STATUS);
 
	 }
 
 }
 
\ No newline at end of file
master/master/lib/logger.c
Show inline comments
 
@@ -33,40 +33,44 @@
 
// Label lookup table
 
// Make sure there are never more labels than there are MAX_NUM_SENSORS!
 
const char err_0[] PROGMEM = "slave timeout";
 
const char err_1[] PROGMEM = "initializing SD card failed";
 
const char err_2[] PROGMEM = "opening SD partition failed";
 
const char err_3[] PROGMEM = "opening SD file failed";
 
const char err_4[] PROGMEM = "XBee timeout";
 
const char err_5[] PROGMEM = "FATAL UNHANDLED ERROR";
 
const char err_6[] PROGMEM = "enter AT mode failed";
 
const char err_7[] PROGMEM = "exit AT mode failed";
 
const char err_8[] PROGMEM = "infotext";
 
const char err_9[] PROGMEM = "watchdog";
 
const char err_4[] PROGMEM = "error parsing config file";
 
const char err_5[] PROGMEM = "XBee timeout";
 
const char err_6[] PROGMEM = "FATAL UNHANDLED ERROR";
 
const char err_7[] PROGMEM = "enter AT mode failed";
 
const char err_8[] PROGMEM = "exit AT mode failed";
 
const char err_9[] PROGMEM = "infotext";
 
const char err_10[] PROGMEM = "watchdog";
 

	
 
const char *const errorMessageLookup[] PROGMEM =
 
{
 
	err_0,
 
	err_1,
 
	err_2,
 
	err_3,
 
	err_4,
 
	err_5,
 
	err_6,
 
	err_7,
 
	err_8,
 
	err_9
 
	err_9,
 
	err_10
 
};
 
 
 
 
struct partition_struct* partition;
 
struct fat_fs_struct* fs;
 
struct fat_dir_struct* dd;
 
 
struct fat_file_struct* fd_datalog;
 
struct fat_file_struct* fd_errorlog;
 
struct fat_file_struct* fd_config;
 
 
void logger_setup()
 
{
 
 
	if(!sd_raw_init())
 
	{
 
@@ -119,28 +123,25 @@ void logger_setup()
 
	
 
	// Form filename
 
	snprintf(errorFilename, 17, "run%derror.csv",logid);
 
	struct fat_dir_entry_struct errorDirEntry;
 
	if(fat_create_file(dd, errorFilename, &errorDirEntry) == 0)
 
	{
 
		serial0_sendString("Error create errorlog\r\n");
 
		error_log(ERROR_SD_FILE, true);
 
	}
 
	// Search for file in current directory and open it
 
	fd_errorlog = open_file_in_dir(fs, dd, errorFilename);
 
	if(!fd_errorlog)
 
	{
 
		serial0_sendString("Error open errorlog!\r\n");
 
		error_log(ERROR_SD_FILE, true);
 
		return;
 
	}
 
	errorOffset=0;
 
	if(!fat_seek_file(fd_errorlog, &errorOffset, FAT_SEEK_SET))
 
	{
 
		// Error seeking to file
 
		serial0_sendString("Error seek errorlog!\r\n");
 
		error_log(ERROR_SD_FILE, true);
 
		fat_close_file(fd_errorlog);
 
		return;
 
	}
 
	
 
		
 
@@ -150,43 +151,69 @@ void logger_setup()
 
	// Form filename
 
	snprintf(dataFilename, 17, "run%ddata.csv",logid);
 
	struct fat_dir_entry_struct dataDirEntry;
 
	// Create new data log file
 
	if(fat_create_file(dd, dataFilename, &dataDirEntry) == 0) 
 
	{
 
		serial0_sendString("Error create datalog\r\n");
 
		error_log(ERROR_SD_FILE, true);
 
	}
 
	// Search for file in current directory and open it
 
	fd_datalog = open_file_in_dir(fs, dd, dataFilename);
 
	if(!fd_datalog)
 
	{
 
		serial0_sendString("Error open datalog!\r\n");
 
		error_log(ERROR_SD_FILE, true);
 
		return;
 
	}
 
	dataOffset=0;
 
	if(!fat_seek_file(fd_datalog, &dataOffset, FAT_SEEK_SET))
 
	{
 
		// Error seeking to file
 
		serial0_sendString("Error seek datalog!\r\n");
 
		error_log(ERROR_SD_FILE, true);
 
		fat_close_file(fd_datalog);
 
		return;
 
	}
 
	
 
	
 
	
 
	int32_t configOffset = 0;
 
	
 
	// Search for file in current directory and open it
 
	fd_config = open_file_in_dir(fs, dd, "config.ini");
 
	if(!fd_config)
 
	{
 
		error_log_msg(ERROR_CONFIGPARSE, true, "opening");
 
		return;
 
	}
 
	configOffset=0;
 
	if(!fat_seek_file(fd_config, &configOffset, FAT_SEEK_SET))
 
	{
 
		// Error seeking to file
 
		error_log_msg(ERROR_CONFIGPARSE, true, "seeking");
 
		fat_close_file(fd_config);
 
		//Don't return, just continue
 
	}
 
	
 
	
 
	
 
	// 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");
 
	error_log_rawwrite("\nErrNo,ErrMsg,ErrInfo,\r\n");
 
}	
 
 
struct fat_file_struct* logger_getconfigfd() 
 
{
 
	return fd_config;	
 
}
 
 
void logger_closeconffd()
 
{
 
	fat_close_file(fd_config);
 
}
 
 
void logger_log(char *buffer) 
 
{
 
	uint8_t len = strlen(buffer);
 
	if(fat_write_file(fd_datalog, (uint8_t*) buffer, len) != len)
 
	{
 
		// Error writing to file
 
@@ -282,12 +309,51 @@ void logger_closeLog()
 
	fat_close_file(fd_datalog);
 
	fat_close_dir(dd);
 
	fat_close(fs);
 
	partition_close(partition);
 
}
 
 
char* logger_config_fgets(char* line, uint8_t max) {
 
	int len = 0;
 
    //char line[50];
 
	while(len<max) {
 
		if(fat_read_file(fd_config, (uint8_t*)line+len, 1)) {
 
			// lf
 
			if(len == 0 && line[len] == 0x0a)
 
			{
 
				// skip lf, keep going
 
				len = 0;
 
			}
 
			// cr
 
			else if(line[len] == 0x0d)
 
			{
 
				break;
 
			}
 
			else {
 
				len++;
 
			}			
 
		}
 
		else 
 
		{
 
			return NULL; // End of file
 
		}			
 
	}
 
 
	if(len >= max) // too long line
 
	{
 
		// Null-terminate and hope that the comment was the long part
 
		line[len] = 0x00;
 
	}	
 
	else
 
	{
 
		line[len] = 0x00; // terminate string
 
	}	
 
	
 
	return line;
 
}
 
 
 
// INTERNAL FUNCTIONS
 
 
// Opens a file so it can be read/written
 
struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name)
 
{
master/master/lib/logger.h
Show inline comments
 
@@ -14,12 +14,15 @@
 
#ifndef LOGGER_H_
 
#define LOGGER_H_
 
 
#include <stdbool.h>
 
 
void logger_setup();
 
struct fat_file_struct* logger_getconfigfd();
 
char* logger_config_fgets(char* line, uint8_t max);
 
void logger_closeconffd();
 
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);
 
char* logger_getAprsInfoText();
 
bool logger_aprsInfoTextAvailable();
master/master/lib/sdcard/fat_config.h
Show inline comments
 
@@ -99,13 +99,13 @@ void get_datetime(uint16_t* year, uint8_
 
#define FAT_FS_COUNT 1
 

	
 
/**
 
 * \ingroup fat_config
 
 * Maximum number of file handles.
 
 */
 
#define FAT_FILE_COUNT 2
 
#define FAT_FILE_COUNT 3
 

	
 
/**
 
 * \ingroup fat_config
 
 * Maximum number of directory handles.
 
 */
 
#define FAT_DIR_COUNT 3
master/master/lib/sensordata.c
Show inline comments
 
@@ -161,13 +161,13 @@ void sensordata_logvalues()
 
		{
 
			#define CSV_BUFFER_SIZE 64
 
			char csvHeader[CSV_BUFFER_SIZE];
 
			csvHeader[0] = 0x00;
 
			
 
			// Add master data headers
 
			logger_log("Time,BoardTemp,VBatt,GPSTime,GPSLat,GPSLon,GPSSpeed,GPSHDOP,GPSCourse,GPSSV,");
 
			logger_log("Time,BoardTemp,VBatt,GPSTime,GPSLat,GPSLon,GPSAlt,GPSSpeed,GPSHDOP,GPSCourse,GPSSV,");
 
			
 
			// Add slave data headers
 
			for(uint8_t i=0; i<MAX_NUM_SLAVES; i++) 
 
			{
 
				for(uint8_t j=0; j<MAX_NUM_SENSORS; j++) 
 
				{
 
@@ -192,13 +192,13 @@ void sensordata_logvalues()
 
		// Write CSV sensor values to SD card
 
		#define CSV_LOGLINE_SIZE 512
 
		char logbuf[CSV_LOGLINE_SIZE];
 
		logbuf[0] = 0x00;
 
		
 
		// Write master sensor values
 
		snprintf(logbuf, CSV_LOGLINE_SIZE, "%lu,%d,%u,%s,%s,%s,%s,%s,%s,%s,", time_millis(), sensors_getBoardTemp(), sensors_getBatt(), get_timestamp(),get_latitudeTrimmed(),get_longitudeTrimmed(),get_speedKnots(),get_hdop(), get_course(), get_sv());
 
		snprintf(logbuf, CSV_LOGLINE_SIZE, "%lu,%d,%u,%s,%s%s,%s%s,%s,%s,%s,%s,%s,", time_millis(), sensors_getBoardTemp(), sensors_getBatt(), get_timestamp(), get_latitudeTrimmed(), get_latitudeLSBs(), get_longitudeTrimmed(), get_longitudeLSBs(),get_gpsaltitude(), get_speedKnots(), get_hdop(), get_course(), get_sv());
 
		
 
		// Write slave sensor values
 
		for(int i=0; i<MAX_NUM_SLAVES; i++) 
 
		{
 
			for(int j=0; j<MAX_NUM_SENSORS; j++) 
 
			{
 
@@ -243,19 +243,19 @@ void sensordata_checkTouchdown()
 
	// If we have reached touchdown, never get out of this state
 
	if(isTouchdown)
 
	{
 
		return;
 
	}
 
	
 
	if(time_millis() > BUZZER_FAILSAFE_DURATION)
 
	if(time_millis() > sysconfig->buzzer_failsafe_duration)
 
	{
 
		isTouchdown = true;
 
	}
 
	else
 
	{
 
		int32_t altitude = sensordata_getSensorValue(SENSOR_ALTITUDE);
 
		if(altitude != -2111111111 && altitude < BUZZER_TRIGGER_MAXALTITUDE && time_millis() > BUZZER_TRIGGER_MINDURATION)
 
		if(altitude != -2111111111 && altitude < sysconfig->buzzer_trigger_maxaltitude && time_millis() > sysconfig->buzzer_trigger_minduration)
 
		{
 
			isTouchdown = true;
 
		}			
 
	}
 
}
 
\ No newline at end of file
master/master/master.c
Show inline comments
 
@@ -32,12 +32,13 @@
 
#include "lib/sensors.h"
 
#include "lib/heater.h"
 
#include "lib/looptime.h"
 
#include "lib/slavesensors.h"
 
#include "lib/serparser.h"
 
#include "lib/sensordata.h"
 
#include "lib/iniparse.h"
 

	
 
int main(void)
 
{
 
	// Initialize libraries
 
	time_setup();
 
	watchdog_setup(); // enables interrupts
 
@@ -52,14 +53,15 @@ int main(void)
 
	i2c_init();
 
	sensordata_setup(); // must happen before slavesensors/logger/AFSK
 
	slavesensors_setup();
 
	sensors_setup();
 
	logger_setup();
 
	watchdog_checkreset();
 
	iniparse_getconfig();
 
	afsk_setup();
 
	serial0_sendString("\r\nHello.\r\n\r\n");
 
	serial0_sendString("Hello.\n\n");
 
	
 
	// Blocking ZigBee node discovery
 
	slavesensors_network_scan();
 
	
 
	// Software timers	
 
	uint32_t lastAprsBroadcast = 0;
 
@@ -105,16 +107,12 @@ int main(void)
 
			{
 
				led_off(LED_SIDEBOARD);
 
			}
 
			
 
			sensors_readBoardTemp();
 
			sensors_readBatt();
 
			
 
			//char buftmp[15];
 
			//snprintf(buftmp, 15, "vbatt: %u\r\n", sensors_getBatt());
 
			//serial0_sendString(buftmp);
 
		
 
			// Write CSV header and log data values
 
			sensordata_logvalues();			
 
			
 
			led_off(LED_CYCLE);
 
			lastLog = time_millis();
 
@@ -134,13 +132,13 @@ int main(void)
 
		if(buzz && time_millis() - lastBuzzOn > BUZZER_DURATION) {
 
			led_off(LED_BUZZ);
 
			buzz = false;
 
		}
 
		
 
		// Periodic: Data Request
 
		if(time_millis() - lastDataReq > DATAREQUEST_RATE)  
 
		if(time_millis() - lastDataReq > sysconfig->datarequest_rate)  
 
		{
 
			// Start getting values for next transmission
 
			if(slavesensors_isrequesting())
 
			{
 
				// This probably is a non-issue
 
				//error_log_msg(ERROR_FATAL, false, "Still requesting on following loop");
 
@@ -152,13 +150,13 @@ int main(void)
 
			
 
			lastDataReq = time_millis();
 
		}
 
		
 
		
 
		// Periodic: APRS transmission
 
		if(time_millis() - lastAprsBroadcast > APRS_TRANSMIT_PERIOD) 
 
		if(time_millis() - lastAprsBroadcast > sysconfig->aprs_transmit_period) 
 
		{
 
			// Check for touchdown
 
			sensordata_checkTouchdown();
 
			
 
			// Ensure we aren't already transmitting
 
			while(afsk_busy());
 
@@ -167,21 +165,20 @@ int main(void)
 
			serial1_ioff();
 
			aprs_send(); // non-blocking
 
			
 
			lastAprsBroadcast = time_millis();
 
		}			
 
		
 
		#ifdef BLACKOUT_ENABLE
 
		// Periodic: Blackout timer check
 
		bool black = false;
 
		if(!black && time_millis() > BLACKOUT_TIMEOUT) 
 
		if(sysconfig->blackout_enable && !black && time_millis() > sysconfig->blackout_timeout) 
 
		{
 
			// LED blackout
 
			led_blackout();
 
			black = true;
 
		}
 
		#endif
 
		
 
		// Parse any serial data in the XBee software buffer
 
		parseResult = serparser_parse();
 
		slavesensors_process(parseResult);
 
		
 
		
master/master/master.cproj
Show inline comments
 
@@ -129,12 +129,24 @@
 
    <Compile Include="lib\ax25.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\ax25.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\iniparse.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\iniparse.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\ini\ini.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\ini\ini.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\sensors.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\sensors.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
@@ -243,10 +255,11 @@
 
    <Compile Include="master.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
  </ItemGroup>
 
  <ItemGroup>
 
    <Folder Include="lib" />
 
    <Folder Include="lib\ini" />
 
    <Folder Include="lib\sdcard" />
 
  </ItemGroup>
 
  <Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
 
</Project>
 
\ No newline at end of file
0 comments (0 inline, 0 general)