Abstraction for the glider class

Dependents:   DropTest

Committer:
chasefarmer2808
Date:
Sat Apr 15 00:18:17 2017 +0000
Revision:
8:7fc2caa10bbb
Parent:
7:dc93fe573846
Child:
9:8e34e713e6ef
bmp data

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chasefarmer2808 0:4e6b53a7bd38 1 #include "Glider.h"
chasefarmer2808 0:4e6b53a7bd38 2
chasefarmer2808 2:cc07e65227ff 3 Glider::Glider(Serial* device, PinName sda, PinName scl, PinName tx, PinName rx) : xbeeRxInt(rx) {
chasefarmer2808 0:4e6b53a7bd38 4 dev = device;
chasefarmer2808 3:d8932a3036a8 5 dev->printf("constructing glider...");
chasefarmer2808 5:460412fc4a17 6 initSaveData();
chasefarmer2808 1:7039ae86fe70 7 xbee = new Serial(tx, rx);
chasefarmer2808 1:7039ae86fe70 8 heading = 0.0;
chasefarmer2808 1:7039ae86fe70 9 pressure = 0.0;
chasefarmer2808 1:7039ae86fe70 10 temp = 0.0;
chasefarmer2808 1:7039ae86fe70 11 alt = 0.0;
chasefarmer2808 6:bb82dd1618c2 12 cmdFlag = false;
chasefarmer2808 4:ff59603d9fb6 13 transmitFlag = false;
chasefarmer2808 0:4e6b53a7bd38 14 hmc = new HMC5883L(sda, scl);
chasefarmer2808 0:4e6b53a7bd38 15 bmp = new BMP180(sda, scl);
chasefarmer2808 0:4e6b53a7bd38 16 bmp->Initialize(1013.25, BMP180_OSS_ULTRA_LOW_POWER);
chasefarmer2808 3:d8932a3036a8 17 xbeeRxInt.rise(this, &Glider::setCommandFlag);
chasefarmer2808 0:4e6b53a7bd38 18 }
chasefarmer2808 0:4e6b53a7bd38 19
chasefarmer2808 0:4e6b53a7bd38 20 void Glider::setHeading() {
chasefarmer2808 7:dc93fe573846 21 this->heading = this->hmc->getHeading();
chasefarmer2808 0:4e6b53a7bd38 22 }
chasefarmer2808 0:4e6b53a7bd38 23
chasefarmer2808 8:7fc2caa10bbb 24 void Glider::setTempPressAlt() {
chasefarmer2808 0:4e6b53a7bd38 25 bmp->ReadData(&this->temp, &this->pressure, &this->alt);
chasefarmer2808 0:4e6b53a7bd38 26 }
chasefarmer2808 0:4e6b53a7bd38 27
chasefarmer2808 1:7039ae86fe70 28 void Glider::setMissionTime() {
chasefarmer2808 6:bb82dd1618c2 29 //this->dev->printf("current time: %d\r\n", this->missionTime);
chasefarmer2808 1:7039ae86fe70 30 this->missionTime = time(NULL) - this->startTime;
chasefarmer2808 1:7039ae86fe70 31 }
chasefarmer2808 1:7039ae86fe70 32
chasefarmer2808 4:ff59603d9fb6 33 void Glider::setTransmitFlag() {
chasefarmer2808 4:ff59603d9fb6 34 this->transmitFlag = true;
chasefarmer2808 4:ff59603d9fb6 35 }
chasefarmer2808 4:ff59603d9fb6 36
chasefarmer2808 0:4e6b53a7bd38 37 void Glider::saveTelem() {
chasefarmer2808 0:4e6b53a7bd38 38 FILE *fp = fopen("/telem/telem.txt", "w");
chasefarmer2808 0:4e6b53a7bd38 39 fprintf(fp, "hello,");
chasefarmer2808 1:7039ae86fe70 40 fclose(fp);
chasefarmer2808 1:7039ae86fe70 41 }
chasefarmer2808 1:7039ae86fe70 42
chasefarmer2808 5:460412fc4a17 43 void Glider::initSaveData() {
chasefarmer2808 2:cc07e65227ff 44 this->dev->printf("getting the time...\r\n");
chasefarmer2808 3:d8932a3036a8 45 FILE *fp = fopen("/telem/data.txt", "r"); //attempt to read the saved data
chasefarmer2808 2:cc07e65227ff 46
chasefarmer2808 3:d8932a3036a8 47 if (fp == NULL) { //file does not exist
chasefarmer2808 3:d8932a3036a8 48 this->dev->printf("starting the time...\r\n");
chasefarmer2808 3:d8932a3036a8 49 this->startTime = time(NULL); //initialize the start time to now
chasefarmer2808 6:bb82dd1618c2 50 this->packetCount = 0; //start the packet count at 0
chasefarmer2808 6:bb82dd1618c2 51 this->state = CRUZE;
chasefarmer2808 3:d8932a3036a8 52 FILE *fp1 = fopen("/telem/data.txt", "w"); //create the data file
chasefarmer2808 6:bb82dd1618c2 53 fprintf(fp1, SAVE_DATA_FORMAT, this->startTime, this->packetCount, this->state); //save the start time
chasefarmer2808 2:cc07e65227ff 54 fclose(fp1);
chasefarmer2808 2:cc07e65227ff 55 return;
chasefarmer2808 2:cc07e65227ff 56 }
chasefarmer2808 3:d8932a3036a8 57
chasefarmer2808 6:bb82dd1618c2 58 //else, the file exists. Read the data
chasefarmer2808 6:bb82dd1618c2 59 fscanf(fp, SAVE_DATA_FORMAT, &this->startTime, &this->packetCount, &this->state); //set the start time, packet count, and state
chasefarmer2808 6:bb82dd1618c2 60 //this->dev->printf("start time: %d\r\n", this->startTime);
chasefarmer2808 6:bb82dd1618c2 61 //this->dev->printf("start packetCount: %d\r\n", this->packetCount);
chasefarmer2808 6:bb82dd1618c2 62 rewind(fp); //move cursor back to beginning of file
chasefarmer2808 2:cc07e65227ff 63 fclose(fp);
chasefarmer2808 2:cc07e65227ff 64 }
chasefarmer2808 2:cc07e65227ff 65
chasefarmer2808 6:bb82dd1618c2 66 void Glider::saveData() {
chasefarmer2808 2:cc07e65227ff 67 FILE *fp = fopen("/telem/data.txt", "w");
chasefarmer2808 6:bb82dd1618c2 68 //this->dev->printf("saving packet count: %d\r\n", this->packetCount);
chasefarmer2808 6:bb82dd1618c2 69 fprintf(fp, SAVE_DATA_FORMAT, this->startTime, this->packetCount, this->state);
chasefarmer2808 2:cc07e65227ff 70 fclose(fp);
chasefarmer2808 2:cc07e65227ff 71 }
chasefarmer2808 2:cc07e65227ff 72
chasefarmer2808 1:7039ae86fe70 73 void Glider::transmitPacket() {
chasefarmer2808 6:bb82dd1618c2 74 //TODO: transmit alt, pressure, speed
chasefarmer2808 6:bb82dd1618c2 75 //temp, voltage
chasefarmer2808 4:ff59603d9fb6 76 this->packetCount++;
chasefarmer2808 4:ff59603d9fb6 77
chasefarmer2808 8:7fc2caa10bbb 78 this->xbee->printf("3387, GLIDER, %d, %d, %f, %f, %f, %f, %d\r\n",
chasefarmer2808 8:7fc2caa10bbb 79 this->missionTime,
chasefarmer2808 8:7fc2caa10bbb 80 this->packetCount,
chasefarmer2808 8:7fc2caa10bbb 81 this->alt,
chasefarmer2808 8:7fc2caa10bbb 82 this->pressure,
chasefarmer2808 8:7fc2caa10bbb 83 this->temp,
chasefarmer2808 8:7fc2caa10bbb 84 this->heading,
chasefarmer2808 8:7fc2caa10bbb 85 this->state);
chasefarmer2808 1:7039ae86fe70 86 }
chasefarmer2808 2:cc07e65227ff 87
chasefarmer2808 3:d8932a3036a8 88 void Glider::setCommandFlag() {
chasefarmer2808 6:bb82dd1618c2 89 this->cmdFlag = true;
chasefarmer2808 2:cc07e65227ff 90 }
chasefarmer2808 2:cc07e65227ff 91
chasefarmer2808 2:cc07e65227ff 92 void Glider::processCommand() {
chasefarmer2808 6:bb82dd1618c2 93 if (this->cmdFlag) { //command recieved
chasefarmer2808 6:bb82dd1618c2 94 char command = this->xbee->getc(); //get the command char
chasefarmer2808 3:d8932a3036a8 95
chasefarmer2808 3:d8932a3036a8 96 switch (command) {
chasefarmer2808 6:bb82dd1618c2 97 case CMD_BUZZER: //'b' was recieved. sound the buzzer
chasefarmer2808 3:d8932a3036a8 98 this->dev->printf("buzzing...\r\n");
chasefarmer2808 7:dc93fe573846 99 this->state = LAND;
chasefarmer2808 3:d8932a3036a8 100 while(1);
chasefarmer2808 3:d8932a3036a8 101 break;
chasefarmer2808 6:bb82dd1618c2 102 case CMD_RESET:
chasefarmer2808 6:bb82dd1618c2 103 this->dev->printf("resetting save file...\r\n");
chasefarmer2808 6:bb82dd1618c2 104 remove("/telem/data.txt");
chasefarmer2808 6:bb82dd1618c2 105 this->initSaveData();
chasefarmer2808 3:d8932a3036a8 106 }
chasefarmer2808 3:d8932a3036a8 107
chasefarmer2808 6:bb82dd1618c2 108 this->cmdFlag = false; //reset the flag so a new command can be recieved
chasefarmer2808 2:cc07e65227ff 109 }
chasefarmer2808 2:cc07e65227ff 110 }