Files
@ 7214de94743d
Branch filter:
Location: seniordesign-firmware/master/master/lib/sensordata.c
7214de94743d
5.0 KiB
text/plain
Added additional sensor readings to APRS message
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | /*
* Master Firmware: Sensor Data
*
* Wireless Observational Modular Aerial Network
*
* Ethan Zonca
* Matthew Kanning
* Kyle Ripperger
* Matthew Kroening
*
*/
#include "../config.h"
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "sensordata.h"
#include "slavesensors.h"
#include "boardtemp.h"
#include "looptime.h"
#include "gps.h"
#include "logger.h"
// Slave sensor reading storage
int32_t slaves[MAX_NUM_SLAVES][MAX_NUM_SENSORS];
void sensordata_setup()
{
for(int i=0; i<MAX_NUM_SLAVES; i++)
{
for(int j=0; j<MAX_NUM_SENSORS; j++)
{
slaves[i][j] = -2111111111; // minimum value of 16 bit integer
}
}
}
// Store a sensor value in memory
void sensordata_set(uint8_t nodeID, uint8_t type, int32_t value)
{
if(nodeID < MAX_NUM_SLAVES)
{
slaves[nodeID][type] = value;
}
}
// Retrieve a sensor value from memory
int32_t sensordata_get(uint8_t nodeID, uint8_t type)
{
// Avoid reading out of bad places!
if(nodeID < MAX_NUM_SLAVES)
{
return slaves[nodeID][type];
}
else
{
return 0;
}
}
// Generate APRS comment
// TODO: Can we move this buffer to a local scope of this function?
#define COMMENTBUFFER_SIZE 128
char commentBuffer[COMMENTBUFFER_SIZE];
char* slavesensors_getAPRScomment()
{
snprintf(commentBuffer,COMMENTBUFFER_SIZE, "t9%d s%s v%s h%s _%s |%s ", sensors_getBoardTemp(), get_sv(), get_speedKnots(), get_hdop(), get_latitudeLSBs(), get_longitudeLSBs());
// Find slave sensors to include in this log
for(int i=0; i<MAX_NUM_SLAVES; i++)
{
// Board temperature sensors (all slaves)
uint32_t val = sensordata_get(i, SENSOR_BOARDTEMP);
if(val != -2111111111) {
uint16_t len = strlen(commentBuffer);
snprintf(commentBuffer + len, COMMENTBUFFER_SIZE-len, " t%u%li",i,val);
}
// Battery voltages (all slaves)
val = sensordata_get(i, SENSOR_BATTERYLEVEL);
if(val != -2111111111) {
uint16_t len = strlen(commentBuffer);
snprintf(commentBuffer + len, COMMENTBUFFER_SIZE-len, " l%u%li",i,val);
}
// Pressure
val = sensordata_get(i, SENSOR_PRESSURE);
if(val != -2111111111) {
uint16_t len = strlen(commentBuffer);
snprintf(commentBuffer + len, COMMENTBUFFER_SIZE-len, " P%li",val);
}
// Air Temperature
val = sensordata_get(i, SENSOR_AIRTEMP);
if(val != -2111111111) {
uint16_t len = strlen(commentBuffer);
snprintf(commentBuffer + len, COMMENTBUFFER_SIZE-len, " C%li",val);
}
// Altitude
val = sensordata_get(i, SENSOR_ALTITUDE);
if(val != -2111111111) {
uint16_t len = strlen(commentBuffer);
snprintf(commentBuffer + len, COMMENTBUFFER_SIZE-len, " A%li",val);
}
// Radiation
val = sensordata_get(i, SENSOR_CPM_RADIATION);
if(val != -2111111111) {
uint16_t len = strlen(commentBuffer);
snprintf(commentBuffer + len, COMMENTBUFFER_SIZE-len, " R%li",val);
}
}
if(logger_aprsInfoTextAvailable())
{
uint16_t len = strlen(commentBuffer);
snprintf(commentBuffer + len, COMMENTBUFFER_SIZE-len, " %s",logger_getAprsInfoText());
logger_aprsInfoTextConsumed();
}
return commentBuffer;
}
// Generates CSV headers on first run and logs values to the SD card (if data available)
bool dataWasReady = false;
void sensordata_logvalues()
{
// Generate CSV header after we have queried all slaves once
if(slavesensors_dataReady())
{
// Only generate/write header the first time data is ready
if(!dataWasReady)
{
#define CSV_BUFFER_SIZE 64
char csvHeader[CSV_BUFFER_SIZE];
csvHeader[0] = 0x00;
// Add master data headers
logger_log("Time,BoardTemp,GPSTime,GPSLat,GPSLon,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++)
{
int32_t tmp = sensordata_get(i, j);
// If a sensor value exists, write a header for it
if(tmp != -2111111111)
{
snprintf(csvHeader, CSV_BUFFER_SIZE,"%s-%s,", slavesensors_slavename(i), slavesensors_getLabel(j));
logger_log(csvHeader);
}
}
}
// End line and write to SD card
snprintf(csvHeader, CSV_BUFFER_SIZE,"\r\n");
logger_log(csvHeader);
dataWasReady = true;
}
// 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,%s,%s,%s,%s,%s,%s,%s,", time_millis(), sensors_getBoardTemp(),get_timestamp(),get_latitudeTrimmed(),get_longitudeTrimmed(),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++)
{
int32_t tmp = sensordata_get(i, j);
// If a sensor value exists, log the data
if(tmp != -2111111111)
{
snprintf(logbuf + strlen(logbuf),CSV_LOGLINE_SIZE-strlen(logbuf)," %ld,", tmp);
}
}
}
// End line and write to log
snprintf(logbuf + strlen(logbuf),CSV_LOGLINE_SIZE-strlen(logbuf),"\r\n");
logger_log(logbuf);
}
}
|