Files
@ ecf8bc28f289
Branch filter:
Location: seniordesign-firmware/master/master/lib/slavesensors.c
ecf8bc28f289
9.2 KiB
text/plain
Fixed issue from previous commit where logger skipping was improprerly handled, freezing the microcontroller if the logger was the last discovered node.
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | /*
* Master Firmware: Slave Sensor Data Aquisition
*
* Wireless Observational Modular Aerial Network
*
* Ethan Zonca
* Matthew Kanning
* Kyle Ripperger
* Matthew Kroening
*
*/
#include "../config.h"
#include <avr/io.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <util/delay.h>
#include <avr/wdt.h>
#include "serial.h"
#include "serparser.h"
#include "slavesensors.h"
#include "sensordata.h"
#include "led.h"
#include "looptime.h"
uint8_t currentSlave = 0;
uint8_t currentSlaveSensor = 0;
bool requesting = false;
void slavesensors_setup()
{
}
//#define DEBUG_NETWORKSCAN
//#define DEBUG_GETSLAVEDATA
char* bufPtr = 0x00;
static char slaveAddressLow[MAX_NUM_SLAVES][9];
static char slaveAddressHigh[MAX_NUM_SLAVES][9];
static char slaveNames[MAX_NUM_SLAVES][20];
uint8_t loggerIndex = 255;
uint8_t nodeCount = 0;
bool dataReady = false;
char* slavesensors_slavename(uint8_t id) {
return slaveNames[id];
}
void slavesensors_network_scan() {
serial0_ioff();
int atOK;
#ifdef DEBUG_OUTPUT
serial0_sendString("Beginning network scan...\r\n\r\n");
#endif
_delay_ms(500); // xbee warmup
wdt_reset();
led_on(LED_ACTIVITY);
atOK = slavesensors_enterAT();
// wait for OK
if(atOK == 0)
{
led_on(LED_CYCLE);
serial0_sendString("ATND");
serial0_sendChar(0x0D);
// wait for scan to complete
uint16_t scanStart = time_millis();
while(!serial0_hasChar()) {
if(time_millis() - scanStart > 7000) {
led_errorcode(ERROR_XBEETIMEOUT);
return;
}
wdt_reset();
}
// Scan data end when newline by itself ("")
int lineCount = 0;
while(1) {
bufPtr = serial0_readLine();
// If we're starting a new block but got a newline instead, we're done!
if(lineCount == 0 && strcmp(bufPtr, "") == 0) {
break;
}
if(lineCount == 1) {
strncpy(slaveAddressHigh[nodeCount],bufPtr, 9);
}
else if(lineCount == 2) {
strncpy(slaveAddressLow[nodeCount],bufPtr, 9);
}
else if(lineCount == 3) {
strncpy(slaveNames[nodeCount], bufPtr, 20);
}
// If we've finished one chunk (including the newline after it). Can't be else if because it controls increment.
if(lineCount == 9) {
// bufPtr should be null at this point, because we read in a newline after one chunk
nodeCount++;
lineCount = 0;
}
else {
lineCount++;
}
}
slavesensors_exitAT();
}
// Display number of found nodes on spinning indicator
switch(nodeCount) {
case 0:
break;
case 3:
led_on(LED_ACT2);
_delay_ms(100);
case 2:
led_on(LED_ACT1);
_delay_ms(100);
case 1:
led_on(LED_ACT0);
_delay_ms(100);
}
led_on(LED_SIDEBOARD);
_delay_ms(200);
led_off(LED_SIDEBOARD);
#ifdef DEBUG_OUTPUT
char debugBuf[64];
serial0_sendString("Discovered: \r\n");
for(int i=0; i<nodeCount; i++) {
snprintf(debugBuf, 64, " %s - %s%s (%u)\r\n", slaveNames[i],slaveAddressHigh,slaveAddressLow[i], i);
serial0_sendString(debugBuf);
}
serial0_sendString("\r\n");
if(atOK != 0) {
serial0_sendString("AT mode failed \r\n");
}
#endif
for(int i=0; i<nodeCount; i++)
{
if(strcmp(slaveNames[i], XBEE_LOGDEST_NAME) == 0)
{
loggerIndex = i;
}
}
_delay_ms(100);
slavesensors_selectlogger();
serial0_ion();
}
// #define DEBUG_SELECTNODE
void slavesensors_selectnode(uint8_t nodeIndex)
{
serial0_ioff();
#ifdef DEBUG_SELECTNODE
serial0_sendString("Switch to node ");
serial0_sendChar(nodeIndex + 0x30);
serial0_sendString("\r\n");
#endif
_delay_ms(20);
char tmpBuf[23];
// If we can get into AT mode
if(slavesensors_enterAT() == 0) {
snprintf(tmpBuf, 23, "ATDH %s%c",slaveAddressHigh[nodeIndex], 0x0D);
serial0_sendString(tmpBuf);
if(xbeeIsOk() != 0) {
led_errorcode(ERROR_NOXBEE);
return;
}
snprintf(tmpBuf, 23, "ATDL %s%c",slaveAddressLow[nodeIndex], 0x0D);
serial0_sendString(tmpBuf);
if(xbeeIsOk() != 0) {
led_errorcode(ERROR_NOXBEE);
return;
}
slavesensors_exitAT();
}
_delay_ms(2);
#ifdef DEBUG_SELECTNODE
serial0_sendString("Selected ");
serial0_sendChar(nodeIndex + 0x30);
serial0_sendString("\r\n");
#endif
serial0_ion();
return;
}
void slavesensors_selectlogger()
{
if(loggerIndex != 255) {
slavesensors_selectnode(loggerIndex);
}
}
void slavesensors_exitAT()
{
// Exit AT
serial0_sendString("ATCN");
serial0_sendChar(0x0D);
_delay_ms(2);
//// Wait until we get "OK"
//int atStart = time_millis();
//while(!serial0_hasChar()) {
//if(time_millis() - atStart > 500) {
//led_errorcode(ERROR_EXITAT);
//wdt_reset();
//return 1;
//}
//};
xbeeIsOk();
}
// Enter AT mode. Leaves "OK" on the buffer.
int slavesensors_enterAT()
{
// Delay guard time
_delay_ms(2);
serial0_ioff(); // interrupts MUST be off
// Enter AT mode
serial0_sendChar('+'); // Enter AT mode
serial0_sendChar('+');
serial0_sendChar('+');
_delay_ms(2);
// Wait 1ms until we get "OK"
//int atStart = time_millis();
//while(!serial0_hasChar()) {
//if(time_millis() - atStart > 500) {
//led_errorcode(ERROR_ATFAIL);
//wdt_reset();
//return 1;
//}
//};
return xbeeIsOk();
}
int xbeeIsOk()
{
char* tmppntr = serial0_readLine();
if(strcmp(tmppntr, "OK") == 0)
{
return 0;
}
else
{
led_errorcode(ERROR_NOXBEE);
return 1;
}
}
bool slavesensors_dataReady() {
return dataReady;
}
bool slavesensors_isrequesting()
{
return requesting;
}
void slavesensors_startprocess()
{
requesting = true;
slavesensors_request();
}
// TODO: inline. static.
void slavesensors_request()
{
if(currentSlave == loggerIndex) {
currentSlave++;
}
slavesensors_selectnode(currentSlave);
serial_sendCommand("@"); // Request data!
slavesensors_selectlogger();
}
uint8_t numReadingsToExpect = 0; // number of values that the slave is about to send (testing)
// TODO: needs to skip logger!
void slavesensors_process(uint8_t parseResult)
{
if(!requesting) {
// we got a command when we didn't request anything. probably skip it.
return;
}
// TODO: timeout. If we're at NODATA for a long time and we are requesting, that's an issue.
// TODO: If we time out, WE NEED TO RESET THE PARSER. It could be in a bad state.
else if(parseResult == PARSERESULT_NODATA) {
// 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)
{
#ifdef DEBUG_GETSLAVEDATA
char debug[50];
snprintf(debug, 50, "Slave %u sensor %u of total nodes %u\r\n", currentSlave, currentSlaveSensor,nodeCount);
serial0_sendString(debug);
#endif
// We got some data, let's handle it
// ASCII payload
uint8_t len = getPayloadLength();
uint8_t* load = getPayload();
uint8_t type = getPayloadType();
uint16_t parsedVal = atoi(load);
// Special case for slave telling us how many things we're about to get
if(type + 0x30 == '@') {
#ifdef DEBUG_GETSLAVEDATA
serial0_sendString("Got an awesome count!\r\n");
serial0_sendChar(parsedVal + 0x30);
serial0_sendString("\r\n");
#endif
numReadingsToExpect = parsedVal;
currentSlaveSensor = 0;
requesting = true;
}
else {
// Store data in structure
sensordata_set(currentSlave,type,parsedVal);
#ifdef DEBUG_GETSLAVEDATA
serial0_sendString("Stored some sexy data!\r\n");
#endif
// If we finished all sensors for all slaves
if(currentSlave >= (nodeCount-1) && currentSlaveSensor >= (numReadingsToExpect-1))
{
#ifdef DEBUG_GETSLAVEDATA
serial0_sendString("We got all data for all slaves!\r\n");
#endif
dataReady = true;
currentSlave = 0;
currentSlaveSensor = 0;
requesting = false;
}
// If we finished up one slave, go to the next
else if(currentSlaveSensor >= (numReadingsToExpect-1))
{
#ifdef DEBUG_GETSLAVEDATA
serial0_sendString("Finished up one slave, go to another.\r\n");
#endif
currentSlave++;
currentSlaveSensor = 0;
requesting = true;
if(currentSlave == loggerIndex) {
if(currentSlave >= (nodeCount-1)) {
// We hit the last one, we're done.
dataReady = true;
currentSlave = 0;
currentSlaveSensor = 0;
requesting = false;
return;
}
else {
currentSlave++; // increment to the next slave after the logger
}
}
slavesensors_request();
}
// If we haven't finished a slave (or all of them), just get the next sensor of the current slave
else
{
#ifdef DEBUG_GETSLAVEDATA
serial0_sendString("Give me another sensor value...");
#endif
// request data for the current sensor of the current slave
currentSlaveSensor++;
requesting = true;
//slavesensors_request(); slaves now send all values at once, we don't need to keep requesting
}
}
}
// If fail, try retransmit. Or we could skip and hit it next time.
// TODO: Maximum number of retransmissions
else if(parseResult == PARSERESULT_FAIL) {
if(requesting) {
slavesensors_request(); // re-request
}
}
else if(parseResult == PARSERESULT_STILLPARSING)
{
return; // do nothing
}
else {
// something is terribly wrong!
return;
}
}
|