Chase Farmer / Glider

Dependents:   DropTest

Files at this revision

API Documentation at this revision

Comitter:
chasefarmer2808
Date:
Thu Apr 13 15:38:09 2017 +0000
Parent:
5:460412fc4a17
Child:
7:dc93fe573846
Commit message:
saves state

Changed in this revision

Glider.cpp Show annotated file Show diff for this revision Revisions of this file
Glider.h Show annotated file Show diff for this revision Revisions of this file
--- a/Glider.cpp	Thu Apr 13 15:10:29 2017 +0000
+++ b/Glider.cpp	Thu Apr 13 15:38:09 2017 +0000
@@ -9,8 +9,7 @@
     pressure = 0.0;
     temp = 0.0;
     alt = 0.0;
-    //packetCount = 0;
-    comm = false;
+    cmdFlag = false;
     transmitFlag = false;
     hmc = new HMC5883L(sda, scl);
     bmp = new BMP180(sda, scl);
@@ -27,7 +26,7 @@
 }
 
 void Glider::setMissionTime() {
-    this->dev->printf("current time: %d\r\n", this->missionTime);
+    //this->dev->printf("current time: %d\r\n", this->missionTime);
     this->missionTime = time(NULL) - this->startTime;
 }
 
@@ -48,50 +47,56 @@
      if (fp == NULL) {  //file does not exist
         this->dev->printf("starting the time...\r\n");
         this->startTime = time(NULL); //initialize the start time to now
-        this->packetCount = 0;
+        this->packetCount = 0;  //start the packet count at 0
+        this->state = CRUZE;
         FILE *fp1 = fopen("/telem/data.txt", "w");  //create the data file
-        fprintf(fp1, "%d %d", this->startTime, this->packetCount);  //save the start time
+        fprintf(fp1, SAVE_DATA_FORMAT, this->startTime, this->packetCount, this->state);  //save the start time
         fclose(fp1);
         return;   
      }
      
-     fscanf(fp, "%d %d", &this->startTime, &this->packetCount);
-     this->dev->printf("start time: %d\r\n", this->startTime);
-     this->dev->printf("start packetCount: %d\r\n", this->packetCount);
-     rewind(fp);
+     //else, the file exists.  Read the data
+     fscanf(fp, SAVE_DATA_FORMAT, &this->startTime, &this->packetCount, &this->state);  //set the start time, packet count, and state
+     //this->dev->printf("start time: %d\r\n", this->startTime);
+     //this->dev->printf("start packetCount: %d\r\n", this->packetCount);
+     rewind(fp);  //move cursor back to beginning of file
      fclose(fp);
 }
 
-void Glider::saveData() { //bad
+void Glider::saveData() { 
      FILE *fp = fopen("/telem/data.txt", "w");
-     this->dev->printf("saving packet count: %d\r\n", this->packetCount);
-     fprintf(fp, "%d %d", this->startTime, this->packetCount);
+     //this->dev->printf("saving packet count: %d\r\n", this->packetCount);
+     fprintf(fp, SAVE_DATA_FORMAT, this->startTime, this->packetCount, this->state);
      fclose(fp);   
 }
 
 void Glider::transmitPacket() {
-    //TODO: transmit mission time, packet count, alt, pressure, speed
-            //temp, voltage, state
+    //TODO: transmit alt, pressure, speed
+            //temp, voltage
     this->packetCount++;
     
-    this->xbee->printf("3387, GLIDER, %f, %d, %d\r\n", this->heading, this->missionTime, this->packetCount);
+    this->xbee->printf("3387, GLIDER, %f, %d, %d, %d\r\n", this->heading, this->missionTime, this->packetCount, this->state);
 }
 
 void Glider::setCommandFlag() {
-    this->comm = true;   
+    this->cmdFlag = true;   
 }
 
 void Glider::processCommand() {
-    if (this->comm) {  //command recieved
-        char command = this->xbee->getc();
+    if (this->cmdFlag) {  //command recieved
+        char command = this->xbee->getc();  //get the command char
         
         switch (command) {
-            case BUZZER:
+            case CMD_BUZZER:  //'b' was recieved.  sound the buzzer
                   this->dev->printf("buzzing...\r\n");
                   while(1);
                   break; 
+            case CMD_RESET:
+                  this->dev->printf("resetting save file...\r\n");
+                  remove("/telem/data.txt");
+                  this->initSaveData();
         }
         
-        this->comm = false;
+        this->cmdFlag = false;  //reset the flag so a new command can be recieved
     }  
 }
--- a/Glider.h	Thu Apr 13 15:10:29 2017 +0000
+++ b/Glider.h	Thu Apr 13 15:38:09 2017 +0000
@@ -7,8 +7,13 @@
 #include "HMC5883L.h"
 #include "BMP180.h"
 
-#define BUZZER 'b'
+#define SAVE_DATA_FORMAT "%d %d %d"
+#define CMD_BUZZER 'b'
+#define CMD_RESET 'r'
+
 #define FREQ 1.0
+#define CRUZE 0
+#define LAND 1
 
 class Glider {
 public:
@@ -18,11 +23,12 @@
     float pressure;
     float temp;
     float alt;
-    bool comm;
+    bool cmdFlag;
     bool transmitFlag;
     time_t startTime;
-    time_t missionTime;
-    uint8_t packetCount;
+    time_t missionTime;  //saved data
+    uint8_t packetCount;  //daved data
+    uint8_t state;  //saved data
     HMC5883L* hmc;
     BMP180* bmp;
     Glider(Serial* device, PinName sda, PinName scl, PinName tx, PinName rx);