Changeset - 48acfcb35ad3
[Not reviewed]
default
0 11 2
ethanzonca@CL-SEC241-08.cedarville.edu - 12 years ago 2012-11-27 21:36:13
ethanzonca@CL-SEC241-08.cedarville.edu
Added slave parser library, added infrastructure for requesting data from slave nodes
13 files changed with 211 insertions and 83 deletions:
0 comments (0 inline, 0 general)
master/master/config.h
Show inline comments
 
/*
 
 * config.h
 
 *
 
 * Created: 10/25/2012 3:28:22 PM
 
 *  Author: ethanzonca
 
 */ 
 
 
 
#ifndef CONFIG_H_
 
#define CONFIG_H_
 
 
#define F_CPU 11059200
 
#define MODULE_ID '1'
 
 
// --------------------------------------------------------------------------
 
// Slave Sensors config (slavesensors.c)
 
// --------------------------------------------------------------------------
 
 
#define SLAVE0_SENSORS BOARDTEMP
 
#define SLAVE1_SENSORS BOARDTEMP | HUMIDITY | TEMPERATURE | PRESSURE | AMBIENTLIGHT
 
#define SLAVE2_SENSORS BOARDTEMP | GEIGER
 
#define SLAVE3_SENSORS BOARDTEMP | CAMERA
 
#define SLAVE4_SENSORS NONE
 
#define SLAVE5_SENSORS NONE
 
#define SLAVE6_SENSORS NONE
 
#define SLAVE7_SENSORS NONE
 
 
// --------------------------------------------------------------------------
 
// USART config (serial.c)
 
// --------------------------------------------------------------------------
 
 
#define USART0_BAUDRATE 115200
 
#define USART1_BAUDRATE 115200
 
 
// --------------------------------------------------------------------------
 
// AX.25 config (ax25.c)
 
// --------------------------------------------------------------------------
 

	
 
// TX delay in milliseconds
 
#define TX_DELAY      300
 

	
 
// Maximum packet delay
 
#define MAX_PACKET_LEN 512  // bytes
 
 

	
 
// --------------------------------------------------------------------------
 
// APRS config (aprs.c)
 
// --------------------------------------------------------------------------
 

	
 
// Set your callsign and SSID here. Common values for the SSID are
 
@@ -41,36 +52,40 @@
 
//
 
// - Balloons:  11
 
// - Cars:       9
 
// - Home:       0
 
// - IGate:      5
 
#define S_CALLSIGN      "MYCALL"
 
#define S_CALLSIGN_ID   11
 

	
 
// Destination callsign: APRS (with SSID=0) is usually okay.
 
#define D_CALLSIGN      "APRS"
 
#define D_CALLSIGN_ID   0
 

	
 
// Digipeating paths:
 
// (read more about digipeating paths here: http://wa8lmf.net/DigiPaths/ )
 
// The recommended digi path for a balloon is WIDE2-1 or pathless. The default
 
// is pathless. Uncomment the following two lines for WIDE2-1 path:
 
#define DIGI_PATH1      "WIDE2"
 
#define DIGI_PATH1_TTL  1
 

	
 
// APRS comment: this goes in the comment portion of the APRS message. You
 
// might want to keep this short. The longer the packet, the more vulnerable
 
// it is to noise.
 
#define APRS_COMMENT    "Trackuino reminder: replace callsign with your own"
 
 
 
// Transmit the APRS sentence every X milliseconds
 
#define APRS_TRANSMIT_PERIOD 5000
 

	
 
// --------------------------------------------------------------------------
 
// Logger config (logger.c)
 
// --------------------------------------------------------------------------
 
 
#define LOGGER_ID_EEPROM_ADDR 0x10
 
 
// Written to the beginning of every log file
 
#define LOGGER_HEADERTEXT "HAB Control Master - 1.0\n"
 
 
// Log to SD card every X milliseconds
 
#define LOGGER_RATE 1000 
 
 
#endif /* CONFIG_H_ */
 
\ No newline at end of file
master/master/lib/aprs.c
Show inline comments
 
/*
 
 * aprs.c
 
 *
 
 * Created: 11/8/2012 3:40:57 PM
 
 *  Author: ethanzonca
 
 */ 
 
 
#include "../config.h"
 
#include "aprs.h"
 
#include "ax25.h"
 
#include <stdio.h>
 

	
 
const char *gps_aprs_lat = "somelatitudesomelatitudesomelatitudesomelatitudesomelatitudesomelatitude";
 
const char *gps_aprs_lon = "somelongitudesomelongitudesomelongitudesomelongitudesomelongitudesomelongitude";
 
const char *gps_time = "sometimedatasometimedatasometimedatasometimedatasometimedatasometimedata";
 
const char *gps_aprs_lat = "omgmylatitude";
 
const char *gps_aprs_lon = "omgmylongitude";
 
const char *gps_time = "omgmygpstime";
 
float gps_altitude = 123.5;
 
int gps_course = 5;
 
int gps_speed = 13;
 

	
 
float meters_to_feet(float m)
 
{
 
  // 10000 ft = 3048 m
 
  return m / 0.3048;
 
}
 

	
 
void aprs_send()
 
{
 
  char temp[12];                   // Temperature (int/ext)
 
  const struct s_address addresses[] = { 
 
    {D_CALLSIGN, D_CALLSIGN_ID},  // Destination callsign
 
    {S_CALLSIGN, 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/logger.c
Show inline comments
 
/*
 
 * CFile1.c
 
 *
 
 * Created: 11/7/2012 8:05:44 PM
 
 *  Author: mkanning
 
 */ 
 
 
#include "../config.h"
 
#include <util/delay.h>
 
#include <string.h>
 
#include <stdio.h>
 
#include <avr/pgmspace.h>
 
#include <avr/sleep.h>
 
#include <avr/eeprom.h>
 
#include <string.h>
 
#include "sd/fat.h"
 
#include "sd/fat_config.h"
 
#include "sd/partition.h"
 
#include "sd/sd_raw.h"
 
#include "sd/sd_raw_config.h"
 
#include "logger.h"
 
 
 
/* 
 
	//config edits
 
  * By changing the MCU* variables in the Makefile, you can use other Atmel
 
  * microcontrollers or different clock speeds. You might also want to change
 
  * the configuration defines in the files fat_config.h, partition_config.h,
 
  * sd_raw_config.h and sd-reader_config.h. For example, you could disable
 
  * write support completely if you only need read support.
 
 */
 
 
 
struct partition_struct* partition;
 
struct fat_fs_struct* fs;
 
@@ -98,50 +99,49 @@ void logger_setup()
 
	eeprom_update_byte(LOGGER_ID_EEPROM_ADDR, logid);
 

	
 
	// Search for file in current directory and open it
 
	fd = open_file_in_dir(fs, dd, filename);
 
	if(!fd)
 
	{
 
		_delay_ms(10);
 
		return;
 
	}
 
	
 
	// Seek to beginning of file
 
	// TODO: Is this needed?
 
	int32_t offset = 0; 
 
	if(!fat_seek_file(fd, &offset, FAT_SEEK_SET))
 
	{
 
		// Error seeking to file
 
		_delay_ms(10);
 
		fat_close_file(fd);
 
		return;
 
	}
 
		
 
	// Write header information
 
	logger_log(LOGGER_HEADERTEXT);
 
	logger_log("\n-- BEGIN DATA --\n");
 
	logger_log("C1, C2, C3, C4, C5, C6\n");
 
	
 
 
}	
 
 
void logger_log(char *buffer) {
 
	uint8_t len = strlen(buffer);
 
	if(fat_write_file(fd, (uint8_t*) buffer, len) != len)
 
	{
 
		// Error writing to file
 
		return;
 
	}
 
}
 
 
void logger_closeLog() {
 
	fat_close_file(fd);
 
	fat_close_dir(dd);
 
	fat_close(fs);
 
	partition_close(partition);
 
}
 
 
 
 
 
 
 
// INTERNAL FUNCTIONS
master/master/lib/looptime.c
Show inline comments
 
/*
 
 * looptime.c
 
 *
 
 * Created: 11/19/2012 8:56:42 PM
 
 *  Author: ethanzonca
 
 */ 
 
 
#include "../config.h"
 
#include <avr/io.h>
 
#include <avr/interrupt.h>
 
#include <avr/delay.h>
 
extern volatile uint32_t millis = 0; // Milliseconds since initialization
 
#include <util/delay.h>
 
volatile uint32_t millis = 0; // Milliseconds since initialization
 

	
 

	
 

	
 
void time_setup() {
 
	DDRA = 0xff;
 
	
 
	// Generic microcontroller config options
 
	OCR0A = 173; // Approx 172.7969 ticks per ms with 64 prescaler
 
	
 
	TCCR0A |= (1 << WGM01) | (1 << WGM00); // Count until OCR0A, then overflow (wgm02 in the next line specifies this as well)
 
	TCCR0B |= (1 << CS01) | (1 << CS00) | (1 << WGM02); // clock div by 64
 
	TIFR0 |= ( 1 << TOV0 ); // clear pending interrupts
 
	TIMSK0 |= (1 << TOIE0); // enable overflow interrupt
 
}
 

	
 

	
 
ISR(TIMER0_OVF_vect) {
 
	millis = millis + 1;
 
}
 

	
 
uint32_t time_millis() {
 
	return millis; // meh accuracy, but that's OK
 
}
master/master/lib/sd/sd_raw.c
Show inline comments
 
@@ -170,60 +170,51 @@ static uint8_t sd_raw_send_command(uint8
 
 *
 
 * \returns 0 on failure, 1 on success.
 
 */
 
uint8_t sd_raw_init()
 
{
 
    /* enable inputs for reading card status */
 
    configure_pin_available();
 
    configure_pin_locked();
 

	
 
    /* enable outputs for MOSI, SCK, SS, input for MISO */
 
    configure_pin_mosi();
 
    configure_pin_sck();
 
    configure_pin_ss();
 
    configure_pin_miso();
 

	
 
    unselect_card();
 
	
 
    // initialize SPI with lowest frequency; max. 400kHz during identification mode of card
 
    SPCR0 = (0 << SPIE0) | // SPI Interrupt Enable 
 
            (1 << SPE0)  | // SPI Enable 
 
            (0 << DORD0) | // Data Order: MSB first 
 
            (1 << MSTR0) | // Master mode 
 
            (0 << CPOL0) | // Clock Polarity: SCK low when idle 
 
            (0 << CPHA0) | // Clock Phase: sample on rising SCK edge 
 
            (1 << SPR10);   // Clock Frequency: f_OSC / 128 
 
            //(1 << SPR00); // commentnig this out means /64, which gives over 100khz as required
 
            (1 << SPR10);   // Clock Frequency: f_OSC / 64 which gives over 100khz required minimum clock
 
    SPSR0 &= ~(1 << SPI2X0); // No doubled clock frequency 
 

	
 
/*
 
	while(1) {
 
		SPDR0 = 'a';					//Load byte to Data register
 
		while(!(SPSR0 & (1<<SPIF0))); 	// Wait for transmission complete
 
	}
 
*/
 

	
 

	
 
    /* initialization procedure */
 
    sd_raw_card_type = 0;
 
    
 
    if(!sd_raw_available())
 
        return 0;
 

	
 
    /* card needs 74 cycles minimum to start up */
 
    for(uint8_t i = 0; i < 10; ++i)
 
    {
 
        /* wait 8 clock cycles */
 
        sd_raw_rec_byte();
 
    }
 

	
 
    /* address card */
 
    select_card();
 

	
 
    /* reset card */
 
    uint8_t response;
 
    for(uint16_t i = 0; ; ++i)
 
    {
 
        response = sd_raw_send_command(CMD_GO_IDLE_STATE, 0);
 
        if(response == (1 << R1_IDLE_STATE))
 
            break;
 

	
master/master/lib/serial.c
Show inline comments
 
/*
 
 * serial.c
 
 *
 
 * Created: 10/25/2012 3:19:49 PM
 
 *  Author: ethanzonca
 
 */ 
 
 
#include "serial.h"
 
#include "../config.h"
 
#include <avr/io.h>
 
 
 
void serial0_setup() {
 
	/* Set baud rate */
 
	UBRR0H = (unsigned char)(USART0_BAUD_PRESCALE>>8);
 
	UBRR0L = (unsigned char)USART0_BAUD_PRESCALE;
 
	/* Enable receiver and transmitter */
 
	UCSR0B = (1<<RXEN0)|(1<<TXEN0);
 
	/* Set frame format: 8data, 1stop bit */
 
	UCSR0C = (3<<UCSZ00); // - 1 stop bit
 
}
 
 
void serial1_setup() {
 
	/* Set baud rate */
 
	UBRR1H = (unsigned char)(USART1_BAUD_PRESCALE>>8);
 
	UBRR1L = (unsigned char)USART1_BAUD_PRESCALE;
 
	/* Enable receiver and transmitter */
 
	UCSR1B = (1<<RXEN1)|(1<<TXEN1);
 
	/* Set frame format: 8data, 1stop bit */
 
	UCSR1C = (3<<UCSZ10); // - 1 stop bit
 
}
 
 
void serial0_sendChar( unsigned char byte )
 
{
 
	while (!(UCSR0A & (1<<UDRE0)));
 
	UDR0 = byte;
 
}
 
 
void serial1_sendChar( unsigned char byte )
 
{
 
	while (!(UCSR1A & (1<<UDRE1)));
 
	UDR1 = byte;
 
}
 
 
void serial0_sendString(char* stringPtr){
 
unsigned char serial0_readChar()
 
{
 
	while(!(UCSR0A &(1<<RXC0)));
 
	return UDR0;
 
}
 
 
unsigned char serial1_readChar()
 
{
 
	while(!(UCSR1A &(1<<RXC1)));
 
	return UDR1;
 
}
 
 
void serial0_sendString(const char* stringPtr){
 
	while(*stringPtr != 0x00){
 
		serial0_sendChar(*stringPtr);
 
		stringPtr++;
 
	}
 
}
 
 
void serial1_sendString(char* stringPtr){
 
void serial1_sendString(const char* stringPtr){
 
	while(*stringPtr != 0x00){
 
		serial1_sendChar(*stringPtr);
 
		stringPtr++;
 
	}
 
}
 
 
 
void serial_sendCommand( char moduleID, char sensorID, uint8_t dataLength, char* data )
 
void serial_sendCommand( char moduleID, char sensorID, char* data )
 
{
 
	char checkSum = 0x00; //initialize checksum
 
	
 
	serial0_sendChar('['); //bracket indicates start of command
 
	serial0_sendChar(moduleID);
 
	checkSum+=moduleID;
 
	
 
	serial0_sendChar(sensorID);
 
	checkSum+=sensorID;
 
	
 
	//send each character of data individually
 
	for (int i=0; i<dataLength; i++)
 
	{
 
		serial0_sendChar(data[i]);
 
		checkSum+=data[i];
 
	// send data, null-terminated
 
	while(*data != 0x00){
 
		serial0_sendChar(*data);
 
		checkSum += *data;
 
		data++;
 
	}
 
	
 
	serial0_sendChar(checkSum);
 
	serial0_sendChar(']'); //bracket indicates end of command
 
}
 
 
void serial_sendResponseData(){
 
	
 
}
 
master/master/lib/serial.h
Show inline comments
 
/*
 
 * serial.h
 
 *
 
 * Created: 10/25/2012 3:19:42 PM
 
 *  Author: ethanzonca
 
 */ 
 
 
 
#ifndef SERIAL_H_
 
#define SERIAL_H_
 
 
#include <inttypes.h>
 
 
#define USART0_BAUD_PRESCALE (((F_CPU / (USART0_BAUDRATE * 16UL))) -1 )
 
#define USART1_BAUD_PRESCALE (((F_CPU / (USART1_BAUDRATE * 16UL))) -1 )
 
 
void serial0_sendChar( unsigned char byte );
 
void serial1_sendChar( unsigned char byte );
 
 
unsigned char serial0_readChar();
 
unsigned char serial1_readChar();
 
 
void serial0_setup();
 
void serial1_setup();
 
 
void serial0_sendString(char* stringPtr);
 
void serial1_sendString(char* stringPtr);
 
void serial0_sendString(const char* stringPtr);
 
void serial1_sendString(const char* stringPtr);
 
 
void serial_sendCommand( char moduleID, char sensorID, uint8_t dataLength, char* data );
 
void serial_sendCommand( char moduleID, char sensorID, char* data );
 
void serial_sendResponseData();
 
 
 
#endif /* SERIAL_H_ */
 
\ No newline at end of file
master/master/lib/serparser.c
Show inline comments
 
/*
 
* serparser.c
 
*
 
* Created: 10/25/2012 8:11:43 PM
 
*  Author: ethanzonca
 
*/
 
 
 
// ************* Macros ***************
 
#define SERIAL_RX_HASBYTES UCSR0A & _BV(RXC0)
 
#define MAX_CMD_LEN 16
 
#define BROADCAST_ADDR 0 //public address
 
#include <avr/io.h>
 
#include "../config.h"
 
 
#include "serial.h"
 
//#define DEBUG
 
 
// ************* Command Definitions ***************
 
 
// Serial Commands
 
enum cmd // CMD ID#
 
{
 
	BOARDTEMP = 0, // 0
 
	PRESSURE,      // 1
 
};
 
 
// Incoming command buffer
 
uint8_t buffer[MAX_CMD_LEN+2];
 
 
// Current buffer location
 
uint8_t bufferPosition = 0;
 
 
//current length of the data transmission (and checksum)
 
int dataLength;
 
 
//data (and checksum) of most recent transmission
 
char data[16];
 
 
//ID of the sensor of most recent transmission
 
char sensorID;
 
 
//checksum to be calculated and then compared to the received checksum
 
char checksumCalc;
 
 
/* return char from UART (h/w buffer) */
 
uint8_t uart_getchar(void)
 
{
 
	// Wait for chars
 
	/*	while (!(UCSRA & (1<<RXC)))
 
	{
 
		idle();
 
	}
 
	return UDR;
 
	*/
 
	return 0;
 
}
 
 
/* Process incoming serial data */
 
int uart_Parser(void)
 
// TODO: Maybe make this suck up the whole buffer if there is more to be had. That would make this much more efficient.
 
// we could parse while(SERIAL_RX_HASBYTES && notOverTimeout), don't have it parse for more than 100ms straight or something...
 
int serparser_parse(void)
 
{
 
 
	char byte;
 
 
	if(SERIAL_RX_HASBYTES) // Only process data if buffer has a byte
 
	{
 
		
 
		// Pull byte off of the buffer
 
		byte = uart_getchar();
 
		byte = serial0_readChar();
 
		buffer[bufferPosition] = byte;
 
		
 
		// byte checking/validation
 
		if(bufferPosition == 0)
 
		{
 
			if(byte == '[') // If this is a start byte, we're OK. Keep reading.
 
			{
 
				bufferPosition++;
 
			}
 
			else // Not a start byte that we're expecting. Reset to initial state.
 
			{
 
				bufferPosition = 0;
 
			}
 
		}
 
		else if(bufferPosition == 1)
 
		{
 
			if(byte == MODULE_ID) //this transmission is intended for the master; continue parsing
 
			{
 
				bufferPosition++;
 
				checksumCalc += byte;
 
				dataLength = 0; //reset dataLength here to protect from bad inputs
 
			}
 
			else //this transmission is intended for another module; stop parsing and reset
 
			{
 
				bufferPosition = 0;
 
			}
 
		}
 
		else if(bufferPosition == 2)
 
		{
 
			sensorID = byte; //store the type of data receiving
 
			bufferPosition++;
 
			checksumCalc += byte;
 
		}
 
		else if (bufferPosition == MAX_CMD_LEN) //command is too long and bad data has been recieved; reset
 
		{
 
			bufferPosition = 0;
 
		}
 
		else
 
		{
 
			if (byte == ']') //this is the end of the transmission
 
			{
 
				bufferPosition = 0;
 
				return 2; // got whole msg
 
			}
 
			else //still receiving data
 
			{
 
				data[dataLength] = byte;
 
				dataLength++;
 
				bufferPosition++;
 
				checksumCalc += byte;
 
			}
 
 
		}
 
	}
 
	else
 
	{
 
		return 0; // serial data not available
 
	}
 
}
 
\ No newline at end of file
master/master/lib/serparser.h
Show inline comments
 
/*
 
 * serparser.h
 
 *
 
 * Created: 10/25/2012 8:11:49 PM
 
 *  Author: ethanzonca
 
 */ 
 
 
 
#ifndef SERPARSER_H_
 
#define SERPARSER_H_
 
 
 
// Prototypes
 
uint8_t convertchar(uint8_t);
 
uint8_t uart_getchar(void);
 
void got_char(uint8_t);
 
int uart_machine(void);
 
void uart_putchar(uint8_t);
 
void exec_cmd(void);
 
void idle(void);
 
void uart_init(void);
 
 
 
int serparser_parse(void);
 
 
#endif /* SERPARSER_H_ */
 
\ No newline at end of file
master/master/lib/slavesensors.c
Show inline comments
 
new file 100644
 
/*
 
 * slavesensors.c
 
 *
 
 * Created: 11/27/2012 9:02:12 PM
 
 *  Author: ethanzonca
 
 */ 
 
 
#include <avr/io.h>
 
#include <stdbool.h>
 
#include "../config.h"
 
#include "serial.h"
 
#include "serparser.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 receptionFinished = 0;
 
uint8_t currentSlave = 0;
 
uint8_t currentSlaveSensor = 0;
 
 
uint8_t maxSlave = 8;
 
uint16_t slaves[8];
 
 
void slavesensors_setup() 
 
{
 
	// Empty array
 
	for(int i=0; i<8; i++) 
 
	{
 
		slaves[i] = 0;
 
	}	
 
	
 
	// Configure sensors
 
	slaves[0] = SLAVE0_SENSORS;
 
	slaves[1] = SLAVE1_SENSORS;
 
	slaves[2] = SLAVE2_SENSORS;
 
	slaves[3] = SLAVE3_SENSORS;
 
	slaves[4] = SLAVE4_SENSORS;
 
	slaves[5] = SLAVE5_SENSORS;
 
	slaves[6] = SLAVE6_SENSORS;
 
	slaves[7] = SLAVE7_SENSORS;	
 
}
 
 
bool slavesensors_process() 
 
{
 
	// Periodic: Every Iteration
 
		
 
	// maybe we should do this in an ISR on byte received. only problem is that this could interrupt things,
 
	// but we only care about interruption during logging and such. don't log when parsing a message?
 
	receptionFinished = serparser_parse();
 
		
 
	// Finished reception of a message (one sensor data value). If not finished, send out command to get the next one
 
	if(receptionFinished == 2)
 
	{
 
		bool weFinishedTHeLastSlaveSensor = false; // ex.
 
		// if we finished the final reception for this slave, set gettingValues = false;
 
		if(currentSlave >= maxSlave && weFinishedTHeLastSlaveSensor)
 
		{
 
			currentSlave = 0;
 
			return false; // We're done for now!
 
		}
 
		else
 
		{
 
			// Request data from the next slave
 
			currentSlave++;
 
			
 
			// Need to check the next bit over to see if it has that...
 
			
 
			serial_sendCommand(currentSlave, 'a', ""); // send req for data
 
			return true; // Keep going...
 
		}
 
	}
 
	else 
 
	{
 
		return true; // Keep going...
 
	}
 
}		
 
\ No newline at end of file
master/master/lib/slavesensors.h
Show inline comments
 
new file 100644
 
/*
 
 * slavesensors.h
 
 *
 
 * Created: 11/27/2012 9:05:47 PM
 
 *  Author: ethanzonca
 
 */ 
 
 
 
#ifndef SLAVESENSORS_H_
 
#define SLAVESENSORS_H_
 
 
#include <stdbool.h>
 
 
void slavesensors_setup();
 
bool slavesensors_process();
 
 
 
#endif /* SLAVESENSORS_H_ */
 
\ No newline at end of file
master/master/master.c
Show inline comments
 
/*
 
 * Master Firmware
 
 *
 
 * Wireless Observational Modular Aerial Network
 
 * 
 
 * Ethan Zonca
 
 * Matthew Kanning
 
 * Kyle Ripperger
 
 * Matthew Kroening
 
 *
 
 */
 

	
 

	
 
#include "config.h"
 

	
 
#include <avr/io.h>
 
#include <util/delay.h>
 
#include <avr/wdt.h>
 
#include <avr/interrupt.h>
 
#include <stdio.h>
 
#include <stdbool.h>
 

	
 
#include "lib/serial.h"
 
#include "lib/aprs.h"
 
#include "lib/afsk.h"
 
#include "lib/led.h"
 
#include "lib/logger.h"
 
#include "lib/watchdog.h"
 
#include "lib/sd/sd_raw_config.h"
 
#include "lib/looptime.h"
 
#include "lib/slavesensors.h"
 

	
 
void micro_setup() {
 

	
 
}
 

	
 
int main(void)
 
{
 
    
 
	// Initialize. If this takes more than 4 seconds, be sure to reset the WDT
 
	// Initialize libraries
 
	time_setup();
 
	micro_setup();
 
	watchdog_setup();
 
	led_setup();
 
	serial0_setup(); // Config serial ports
 
	serial1_setup(); // Config serial ports
 
	logger_setup(); // this takes a few ms
 
	afsk_setup(); // can take a few ms
 
	serial0_setup();
 
	serial1_setup();
 
	slavesensors_setup();
 
	logger_setup();
 
	afsk_setup();
 

	
 
	serial0_sendString("\r\n\r\n---------------------------------\r\nHAB Controller 1.0 - Initialized!\r\n---------------------------------\r\n\r\n");
 
	
 
	led_on(POWER);
 
		
 
	// Buffer for string operations
 
	char logbuf[32];
 
	const char* logBufPtr = logbuf;
 
	
 
	uint16_t ctr1 = 0;
 
	uint16_t ctr2 = 255;
 
		
 
	char logbuf[32];
 
	
 
	// Software timers	
 
	uint32_t lastAprsBroadcast = 0;
 
	uint32_t lastLog = 0;
 
	
 
	// If we are currently processing sensor data
 
	bool isProcessing = false;
 
	
 
	// Write CSV header to SD card
 
	logger_log("ProgramTime,LastAprsBroadcast,LastLog\n");
 
	
 
	while(1)
 
    {
 
		
 
		if(time_millis() - lastLog > 1000) {
 
		// 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);
 
			sprintf(logbuf, "%lu,%u,%u,%u,%u,%u\r\n", time_millis(),5*ctr1,ctr2, 12*ctr2,43*ctr1,5*ctr2);
 
			snprintf(logbuf, 32, "%lu,%lu,%lu,\r\n", time_millis(), lastAprsBroadcast,lastLog);
 
			logger_log(logbuf);
 
			serial0_sendString("SD Logger: ");
 
			serial0_sendString(logbuf);
 
			serial0_sendString(logBufPtr);
 
			led_off(STAT);
 
			lastLog = time_millis();
 
		}		
 
		
 
		// 8 second periodic for APRS transmission
 
		// for some reason this seems to lock up and the WDT resets everything when the period exceeds 8 seconds. Which is the WDTimeout. Weird.
 
		// If we want ~8+ seconds, then we'll need to do something fun here. Maybe reset the wdt...
 
		if(time_millis() - lastAprsBroadcast > 10000) {
 
		// 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
 
			isProcessing = true;
 
			
 
			lastAprsBroadcast = time_millis();
 
		}			
 
		
 
		ctr1++;
 
		ctr2-=6;
 
		// keep calling processSensors until it is finished
 
		if(isProcessing) 
 
		{
 
			isProcessing = slavesensors_process();
 
		}
 
		wdt_reset();
 
    }
 
}
 
\ No newline at end of file
master/master/master.cproj
Show inline comments
 
@@ -177,48 +177,54 @@
 
    <Compile Include="lib\sd\sd-reader_config.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\sd\sd_raw.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <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\serial.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\serial.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\serparser.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\serparser.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\slavesensors.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\slavesensors.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\trackuinoGPS\config.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\trackuinoGPS\gpsMKa.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\trackuinoGPS\gpsMKa.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\watchdog.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\watchdog.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="master.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
  </ItemGroup>
 
  <ItemGroup>
 
    <Folder Include="lib" />
 
    <Folder Include="lib\trackuinoGPS" />
 
    <Folder Include="lib\sd" />
 
  </ItemGroup>
0 comments (0 inline, 0 general)