diff --git a/master/master/config.h b/master/master/config.h --- a/master/master/config.h +++ b/master/master/config.h @@ -12,7 +12,18 @@ #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) @@ -62,7 +73,8 @@ // 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) @@ -73,4 +85,7 @@ // 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 diff --git a/master/master/lib/aprs.c b/master/master/lib/aprs.c --- a/master/master/lib/aprs.c +++ b/master/master/lib/aprs.c @@ -10,9 +10,9 @@ #include "ax25.h" #include -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; diff --git a/master/master/lib/logger.c b/master/master/lib/logger.c --- a/master/master/lib/logger.c +++ b/master/master/lib/logger.c @@ -8,6 +8,7 @@ #include "../config.h" #include #include +#include #include #include #include @@ -119,8 +120,7 @@ void logger_setup() // 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) { diff --git a/master/master/lib/looptime.c b/master/master/lib/looptime.c --- a/master/master/lib/looptime.c +++ b/master/master/lib/looptime.c @@ -8,8 +8,8 @@ #include "../config.h" #include #include -#include -extern volatile uint32_t millis = 0; // Milliseconds since initialization +#include +volatile uint32_t millis = 0; // Milliseconds since initialization diff --git a/master/master/lib/sd/sd_raw.c b/master/master/lib/sd/sd_raw.c --- a/master/master/lib/sd/sd_raw.c +++ b/master/master/lib/sd/sd_raw.c @@ -191,18 +191,9 @@ uint8_t sd_raw_init() (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< - void serial0_setup() { /* Set baud rate */ UBRR0H = (unsigned char)(USART0_BAUD_PRESCALE>>8); @@ -42,14 +41,26 @@ void serial1_sendChar( unsigned char byt UDR1 = byte; } -void serial0_sendString(char* stringPtr){ +unsigned char serial0_readChar() +{ + while(!(UCSR0A &(1< #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]; @@ -42,21 +35,10 @@ 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< +#include +#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 diff --git a/master/master/lib/slavesensors.h b/master/master/lib/slavesensors.h new file mode 100644 --- /dev/null +++ b/master/master/lib/slavesensors.h @@ -0,0 +1,18 @@ +/* + * slavesensors.h + * + * Created: 11/27/2012 9:05:47 PM + * Author: ethanzonca + */ + + +#ifndef SLAVESENSORS_H_ +#define SLAVESENSORS_H_ + +#include + +void slavesensors_setup(); +bool slavesensors_process(); + + +#endif /* SLAVESENSORS_H_ */ \ No newline at end of file diff --git a/master/master/master.c b/master/master/master.c --- a/master/master/master.c +++ b/master/master/master.c @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include "lib/serial.h" #include "lib/aprs.h" @@ -26,6 +28,7 @@ #include "lib/watchdog.h" #include "lib/sd/sd_raw_config.h" #include "lib/looptime.h" +#include "lib/slavesensors.h" void micro_setup() { @@ -34,53 +37,75 @@ 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 diff --git a/master/master/master.cproj b/master/master/master.cproj --- a/master/master/master.cproj +++ b/master/master/master.cproj @@ -198,6 +198,12 @@ compile + + compile + + + compile + compile