Abstraction for the glider class

Dependents:   DropTest

Committer:
chasefarmer2808
Date:
Thu Apr 13 05:44:14 2017 +0000
Revision:
4:ff59603d9fb6
Parent:
3:d8932a3036a8
Child:
5:460412fc4a17
transmits 1 Hz

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 3:d8932a3036a8 6 readTime();
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 4:ff59603d9fb6 12 packetCount = 0;
chasefarmer2808 2:cc07e65227ff 13 comm = false;
chasefarmer2808 4:ff59603d9fb6 14 transmitFlag = false;
chasefarmer2808 0:4e6b53a7bd38 15 hmc = new HMC5883L(sda, scl);
chasefarmer2808 0:4e6b53a7bd38 16 bmp = new BMP180(sda, scl);
chasefarmer2808 0:4e6b53a7bd38 17 bmp->Initialize(1013.25, BMP180_OSS_ULTRA_LOW_POWER);
chasefarmer2808 3:d8932a3036a8 18 xbeeRxInt.rise(this, &Glider::setCommandFlag);
chasefarmer2808 0:4e6b53a7bd38 19 }
chasefarmer2808 0:4e6b53a7bd38 20
chasefarmer2808 0:4e6b53a7bd38 21 void Glider::setHeading() {
chasefarmer2808 0:4e6b53a7bd38 22 this->heading = this->hmc->getHeadingXYDeg();
chasefarmer2808 0:4e6b53a7bd38 23 }
chasefarmer2808 0:4e6b53a7bd38 24
chasefarmer2808 0:4e6b53a7bd38 25 void Glider::setTempPress() {
chasefarmer2808 0:4e6b53a7bd38 26 bmp->ReadData(&this->temp, &this->pressure, &this->alt);
chasefarmer2808 0:4e6b53a7bd38 27 }
chasefarmer2808 0:4e6b53a7bd38 28
chasefarmer2808 1:7039ae86fe70 29 void Glider::setMissionTime() {
chasefarmer2808 3:d8932a3036a8 30 this->dev->printf("current time: %d\r\n", this->missionTime);
chasefarmer2808 1:7039ae86fe70 31 this->missionTime = time(NULL) - this->startTime;
chasefarmer2808 1:7039ae86fe70 32 }
chasefarmer2808 1:7039ae86fe70 33
chasefarmer2808 4:ff59603d9fb6 34 void Glider::setTransmitFlag() {
chasefarmer2808 4:ff59603d9fb6 35 this->transmitFlag = true;
chasefarmer2808 4:ff59603d9fb6 36 }
chasefarmer2808 4:ff59603d9fb6 37
chasefarmer2808 0:4e6b53a7bd38 38 void Glider::saveTelem() {
chasefarmer2808 0:4e6b53a7bd38 39 FILE *fp = fopen("/telem/telem.txt", "w");
chasefarmer2808 0:4e6b53a7bd38 40 fprintf(fp, "hello,");
chasefarmer2808 1:7039ae86fe70 41 fclose(fp);
chasefarmer2808 1:7039ae86fe70 42 }
chasefarmer2808 1:7039ae86fe70 43
chasefarmer2808 2:cc07e65227ff 44 void Glider::readTime() {
chasefarmer2808 2:cc07e65227ff 45 this->dev->printf("getting the time...\r\n");
chasefarmer2808 3:d8932a3036a8 46 FILE *fp = fopen("/telem/data.txt", "r"); //attempt to read the saved data
chasefarmer2808 2:cc07e65227ff 47
chasefarmer2808 3:d8932a3036a8 48 if (fp == NULL) { //file does not exist
chasefarmer2808 3:d8932a3036a8 49 this->dev->printf("starting the time...\r\n");
chasefarmer2808 3:d8932a3036a8 50 this->startTime = time(NULL); //initialize the start time to now
chasefarmer2808 3:d8932a3036a8 51 FILE *fp1 = fopen("/telem/data.txt", "w"); //create the data file
chasefarmer2808 3:d8932a3036a8 52 fprintf(fp1, "%d", this->startTime); //save the start time
chasefarmer2808 2:cc07e65227ff 53 fclose(fp1);
chasefarmer2808 2:cc07e65227ff 54 return;
chasefarmer2808 2:cc07e65227ff 55 }
chasefarmer2808 3:d8932a3036a8 56
chasefarmer2808 3:d8932a3036a8 57 fscanf(fp, "%d", &this->startTime);
chasefarmer2808 2:cc07e65227ff 58 this->dev->printf("start time: %d\r\n", this->startTime);
chasefarmer2808 2:cc07e65227ff 59 rewind(fp);
chasefarmer2808 2:cc07e65227ff 60 fclose(fp);
chasefarmer2808 2:cc07e65227ff 61 }
chasefarmer2808 2:cc07e65227ff 62
chasefarmer2808 3:d8932a3036a8 63 void Glider::saveData() { //bad
chasefarmer2808 2:cc07e65227ff 64 FILE *fp = fopen("/telem/data.txt", "w");
chasefarmer2808 3:d8932a3036a8 65 this->dev->printf("saving time: %d\r\n", this->missionTime);
chasefarmer2808 2:cc07e65227ff 66 fprintf(fp, "%d", this->missionTime);
chasefarmer2808 2:cc07e65227ff 67 fclose(fp);
chasefarmer2808 2:cc07e65227ff 68 }
chasefarmer2808 2:cc07e65227ff 69
chasefarmer2808 1:7039ae86fe70 70 void Glider::transmitPacket() {
chasefarmer2808 1:7039ae86fe70 71 //TODO: transmit mission time, packet count, alt, pressure, speed
chasefarmer2808 1:7039ae86fe70 72 //temp, voltage, state
chasefarmer2808 4:ff59603d9fb6 73 this->packetCount++;
chasefarmer2808 4:ff59603d9fb6 74
chasefarmer2808 4:ff59603d9fb6 75 this->xbee->printf("3387, GLIDER, %f, %d, %d\r\n", this->heading, this->missionTime, this->packetCount);
chasefarmer2808 1:7039ae86fe70 76 }
chasefarmer2808 2:cc07e65227ff 77
chasefarmer2808 3:d8932a3036a8 78 void Glider::setCommandFlag() {
chasefarmer2808 2:cc07e65227ff 79 this->comm = true;
chasefarmer2808 2:cc07e65227ff 80 }
chasefarmer2808 2:cc07e65227ff 81
chasefarmer2808 2:cc07e65227ff 82 void Glider::processCommand() {
chasefarmer2808 2:cc07e65227ff 83 if (this->comm) { //command recieved
chasefarmer2808 2:cc07e65227ff 84 char command = this->xbee->getc();
chasefarmer2808 3:d8932a3036a8 85
chasefarmer2808 3:d8932a3036a8 86 switch (command) {
chasefarmer2808 3:d8932a3036a8 87 case BUZZER:
chasefarmer2808 3:d8932a3036a8 88 this->dev->printf("buzzing...\r\n");
chasefarmer2808 3:d8932a3036a8 89 while(1);
chasefarmer2808 3:d8932a3036a8 90 break;
chasefarmer2808 3:d8932a3036a8 91 }
chasefarmer2808 3:d8932a3036a8 92
chasefarmer2808 2:cc07e65227ff 93 this->comm = false;
chasefarmer2808 2:cc07e65227ff 94 }
chasefarmer2808 2:cc07e65227ff 95 }