Abstraction for the glider class

Dependents:   DropTest

Committer:
chasefarmer2808
Date:
Sat Apr 29 18:49:55 2017 +0000
Revision:
12:ca3e1c6203f7
Parent:
11:5e83ff964f5f
Child:
13:d95070aa9140
init hmc

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chasefarmer2808 0:4e6b53a7bd38 1 #include "Glider.h"
chasefarmer2808 0:4e6b53a7bd38 2
chasefarmer2808 11:5e83ff964f5f 3 Glider::Glider(Serial* device, PinName sda, PinName scl, PinName tx, PinName rx) : xbeeRxInt(rx), pitot(p15), buzzer(p26) {
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 12:ca3e1c6203f7 15 hmc->init();
chasefarmer2808 0:4e6b53a7bd38 16 bmp = new BMP180(sda, scl);
chasefarmer2808 10:4c4f0ea887a4 17 bmp->Initialize(64, 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 7:dc93fe573846 22 this->heading = this->hmc->getHeading();
chasefarmer2808 0:4e6b53a7bd38 23 }
chasefarmer2808 0:4e6b53a7bd38 24
chasefarmer2808 8:7fc2caa10bbb 25 void Glider::setTempPressAlt() {
chasefarmer2808 0:4e6b53a7bd38 26 bmp->ReadData(&this->temp, &this->pressure, &this->alt);
chasefarmer2808 0:4e6b53a7bd38 27 }
chasefarmer2808 0:4e6b53a7bd38 28
chasefarmer2808 9:8e34e713e6ef 29 void Glider::setAirSpeed() {
chasefarmer2808 9:8e34e713e6ef 30 float vInput = this->pitot.read_u16() * PITO_ADC_RATIO;
chasefarmer2808 9:8e34e713e6ef 31 vInput = vInput / PITO_V_DIVIDER;
chasefarmer2808 9:8e34e713e6ef 32
chasefarmer2808 9:8e34e713e6ef 33 float diffPressure = (vInput - 0.5) / (0.2 * 5.0); //kPa
chasefarmer2808 9:8e34e713e6ef 34 this->speed = sqrt((2*diffPressure) / AIR_DENSITY);
chasefarmer2808 9:8e34e713e6ef 35 }
chasefarmer2808 9:8e34e713e6ef 36
chasefarmer2808 1:7039ae86fe70 37 void Glider::setMissionTime() {
chasefarmer2808 10:4c4f0ea887a4 38 this->missionTime = time(NULL) - this->startTime; //duration = current tome - start time
chasefarmer2808 1:7039ae86fe70 39 }
chasefarmer2808 1:7039ae86fe70 40
chasefarmer2808 12:ca3e1c6203f7 41 void Glider::updateTelem() {
chasefarmer2808 12:ca3e1c6203f7 42 setHeading();
chasefarmer2808 12:ca3e1c6203f7 43 setTempPressAlt();
chasefarmer2808 12:ca3e1c6203f7 44 setAirSpeed();
chasefarmer2808 12:ca3e1c6203f7 45 setMissionTime();
chasefarmer2808 12:ca3e1c6203f7 46 }
chasefarmer2808 12:ca3e1c6203f7 47
chasefarmer2808 4:ff59603d9fb6 48 void Glider::setTransmitFlag() {
chasefarmer2808 4:ff59603d9fb6 49 this->transmitFlag = true;
chasefarmer2808 4:ff59603d9fb6 50 }
chasefarmer2808 4:ff59603d9fb6 51
chasefarmer2808 0:4e6b53a7bd38 52 void Glider::saveTelem() {
chasefarmer2808 0:4e6b53a7bd38 53 FILE *fp = fopen("/telem/telem.txt", "w");
chasefarmer2808 0:4e6b53a7bd38 54 fprintf(fp, "hello,");
chasefarmer2808 1:7039ae86fe70 55 fclose(fp);
chasefarmer2808 1:7039ae86fe70 56 }
chasefarmer2808 1:7039ae86fe70 57
chasefarmer2808 5:460412fc4a17 58 void Glider::initSaveData() {
chasefarmer2808 2:cc07e65227ff 59 this->dev->printf("getting the time...\r\n");
chasefarmer2808 3:d8932a3036a8 60 FILE *fp = fopen("/telem/data.txt", "r"); //attempt to read the saved data
chasefarmer2808 2:cc07e65227ff 61
chasefarmer2808 3:d8932a3036a8 62 if (fp == NULL) { //file does not exist
chasefarmer2808 3:d8932a3036a8 63 this->dev->printf("starting the time...\r\n");
chasefarmer2808 3:d8932a3036a8 64 this->startTime = time(NULL); //initialize the start time to now
chasefarmer2808 6:bb82dd1618c2 65 this->packetCount = 0; //start the packet count at 0
chasefarmer2808 6:bb82dd1618c2 66 this->state = CRUZE;
chasefarmer2808 3:d8932a3036a8 67 FILE *fp1 = fopen("/telem/data.txt", "w"); //create the data file
chasefarmer2808 6:bb82dd1618c2 68 fprintf(fp1, SAVE_DATA_FORMAT, this->startTime, this->packetCount, this->state); //save the start time
chasefarmer2808 2:cc07e65227ff 69 fclose(fp1);
chasefarmer2808 2:cc07e65227ff 70 return;
chasefarmer2808 2:cc07e65227ff 71 }
chasefarmer2808 3:d8932a3036a8 72
chasefarmer2808 6:bb82dd1618c2 73 //else, the file exists. Read the data
chasefarmer2808 6:bb82dd1618c2 74 fscanf(fp, SAVE_DATA_FORMAT, &this->startTime, &this->packetCount, &this->state); //set the start time, packet count, and state
chasefarmer2808 6:bb82dd1618c2 75 rewind(fp); //move cursor back to beginning of file
chasefarmer2808 2:cc07e65227ff 76 fclose(fp);
chasefarmer2808 2:cc07e65227ff 77 }
chasefarmer2808 2:cc07e65227ff 78
chasefarmer2808 6:bb82dd1618c2 79 void Glider::saveData() {
chasefarmer2808 2:cc07e65227ff 80 FILE *fp = fopen("/telem/data.txt", "w");
chasefarmer2808 10:4c4f0ea887a4 81 fprintf(fp, SAVE_DATA_FORMAT, this->startTime, this->packetCount, this->state); //save the start time, packet count, and state to flash
chasefarmer2808 2:cc07e65227ff 82 fclose(fp);
chasefarmer2808 2:cc07e65227ff 83 }
chasefarmer2808 2:cc07e65227ff 84
chasefarmer2808 1:7039ae86fe70 85 void Glider::transmitPacket() {
chasefarmer2808 10:4c4f0ea887a4 86 //TODO: transmit
chasefarmer2808 9:8e34e713e6ef 87 // voltage
chasefarmer2808 4:ff59603d9fb6 88 this->packetCount++;
chasefarmer2808 4:ff59603d9fb6 89
chasefarmer2808 9:8e34e713e6ef 90 this->xbee->printf("3387, GLIDER, %d, %d, %f, %f, %f, %f, %f, %d\r\n",
chasefarmer2808 10:4c4f0ea887a4 91 this->missionTime, //duration in seconds
chasefarmer2808 10:4c4f0ea887a4 92 this->packetCount,
chasefarmer2808 10:4c4f0ea887a4 93 this->alt, //meters
chasefarmer2808 10:4c4f0ea887a4 94 this->pressure, //
chasefarmer2808 10:4c4f0ea887a4 95 this->temp, //C
chasefarmer2808 10:4c4f0ea887a4 96 this->speed, //m per s
chasefarmer2808 10:4c4f0ea887a4 97 this->heading, //degrees from North
chasefarmer2808 8:7fc2caa10bbb 98 this->state);
chasefarmer2808 1:7039ae86fe70 99 }
chasefarmer2808 2:cc07e65227ff 100
chasefarmer2808 3:d8932a3036a8 101 void Glider::setCommandFlag() {
chasefarmer2808 6:bb82dd1618c2 102 this->cmdFlag = true;
chasefarmer2808 2:cc07e65227ff 103 }
chasefarmer2808 2:cc07e65227ff 104
chasefarmer2808 2:cc07e65227ff 105 void Glider::processCommand() {
chasefarmer2808 6:bb82dd1618c2 106 if (this->cmdFlag) { //command recieved
chasefarmer2808 6:bb82dd1618c2 107 char command = this->xbee->getc(); //get the command char
chasefarmer2808 3:d8932a3036a8 108
chasefarmer2808 3:d8932a3036a8 109 switch (command) {
chasefarmer2808 6:bb82dd1618c2 110 case CMD_BUZZER: //'b' was recieved. sound the buzzer
chasefarmer2808 11:5e83ff964f5f 111 this->state = LAND; //set state to land
chasefarmer2808 11:5e83ff964f5f 112 this->transmitPacket(); //transmit one last packet
chasefarmer2808 11:5e83ff964f5f 113 this->soundBuzzer(BUZZER_FREQ, 1, true); //sound buzzer forever
chasefarmer2808 11:5e83ff964f5f 114 while (1);
chasefarmer2808 3:d8932a3036a8 115 break;
chasefarmer2808 11:5e83ff964f5f 116 case CMD_RESET: //'z' was recieved
chasefarmer2808 6:bb82dd1618c2 117 this->dev->printf("resetting save file...\r\n");
chasefarmer2808 6:bb82dd1618c2 118 remove("/telem/data.txt");
chasefarmer2808 6:bb82dd1618c2 119 this->initSaveData();
chasefarmer2808 3:d8932a3036a8 120 }
chasefarmer2808 3:d8932a3036a8 121
chasefarmer2808 6:bb82dd1618c2 122 this->cmdFlag = false; //reset the flag so a new command can be recieved
chasefarmer2808 2:cc07e65227ff 123 }
chasefarmer2808 2:cc07e65227ff 124 }
chasefarmer2808 11:5e83ff964f5f 125
chasefarmer2808 11:5e83ff964f5f 126 void Glider::soundBuzzer(float freq, float dur, bool infinate) {
chasefarmer2808 11:5e83ff964f5f 127 buzzer.beep(freq, dur, infinate);
chasefarmer2808 11:5e83ff964f5f 128 }