Abstraction for the glider class
Glider.cpp@9:8e34e713e6ef, 2017-04-20 (annotated)
- Committer:
- chasefarmer2808
- Date:
- Thu Apr 20 16:39:12 2017 +0000
- Revision:
- 9:8e34e713e6ef
- Parent:
- 8:7fc2caa10bbb
- Child:
- 10:4c4f0ea887a4
air speed;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chasefarmer2808 | 0:4e6b53a7bd38 | 1 | #include "Glider.h" |
chasefarmer2808 | 0:4e6b53a7bd38 | 2 | |
chasefarmer2808 | 9:8e34e713e6ef | 3 | Glider::Glider(Serial* device, PinName sda, PinName scl, PinName tx, PinName rx) : xbeeRxInt(rx), pitot(p15) { |
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 | 9:8e34e713e6ef | 28 | void Glider::setAirSpeed() { |
chasefarmer2808 | 9:8e34e713e6ef | 29 | float vInput = this->pitot.read_u16() * PITO_ADC_RATIO; |
chasefarmer2808 | 9:8e34e713e6ef | 30 | vInput = vInput / PITO_V_DIVIDER; |
chasefarmer2808 | 9:8e34e713e6ef | 31 | |
chasefarmer2808 | 9:8e34e713e6ef | 32 | float diffPressure = (vInput - 0.5) / (0.2 * 5.0); //kPa |
chasefarmer2808 | 9:8e34e713e6ef | 33 | this->speed = sqrt((2*diffPressure) / AIR_DENSITY); |
chasefarmer2808 | 9:8e34e713e6ef | 34 | } |
chasefarmer2808 | 9:8e34e713e6ef | 35 | |
chasefarmer2808 | 1:7039ae86fe70 | 36 | void Glider::setMissionTime() { |
chasefarmer2808 | 6:bb82dd1618c2 | 37 | //this->dev->printf("current time: %d\r\n", this->missionTime); |
chasefarmer2808 | 1:7039ae86fe70 | 38 | this->missionTime = time(NULL) - this->startTime; |
chasefarmer2808 | 1:7039ae86fe70 | 39 | } |
chasefarmer2808 | 1:7039ae86fe70 | 40 | |
chasefarmer2808 | 4:ff59603d9fb6 | 41 | void Glider::setTransmitFlag() { |
chasefarmer2808 | 4:ff59603d9fb6 | 42 | this->transmitFlag = true; |
chasefarmer2808 | 4:ff59603d9fb6 | 43 | } |
chasefarmer2808 | 4:ff59603d9fb6 | 44 | |
chasefarmer2808 | 0:4e6b53a7bd38 | 45 | void Glider::saveTelem() { |
chasefarmer2808 | 0:4e6b53a7bd38 | 46 | FILE *fp = fopen("/telem/telem.txt", "w"); |
chasefarmer2808 | 0:4e6b53a7bd38 | 47 | fprintf(fp, "hello,"); |
chasefarmer2808 | 1:7039ae86fe70 | 48 | fclose(fp); |
chasefarmer2808 | 1:7039ae86fe70 | 49 | } |
chasefarmer2808 | 1:7039ae86fe70 | 50 | |
chasefarmer2808 | 5:460412fc4a17 | 51 | void Glider::initSaveData() { |
chasefarmer2808 | 2:cc07e65227ff | 52 | this->dev->printf("getting the time...\r\n"); |
chasefarmer2808 | 3:d8932a3036a8 | 53 | FILE *fp = fopen("/telem/data.txt", "r"); //attempt to read the saved data |
chasefarmer2808 | 2:cc07e65227ff | 54 | |
chasefarmer2808 | 3:d8932a3036a8 | 55 | if (fp == NULL) { //file does not exist |
chasefarmer2808 | 3:d8932a3036a8 | 56 | this->dev->printf("starting the time...\r\n"); |
chasefarmer2808 | 3:d8932a3036a8 | 57 | this->startTime = time(NULL); //initialize the start time to now |
chasefarmer2808 | 6:bb82dd1618c2 | 58 | this->packetCount = 0; //start the packet count at 0 |
chasefarmer2808 | 6:bb82dd1618c2 | 59 | this->state = CRUZE; |
chasefarmer2808 | 3:d8932a3036a8 | 60 | FILE *fp1 = fopen("/telem/data.txt", "w"); //create the data file |
chasefarmer2808 | 6:bb82dd1618c2 | 61 | fprintf(fp1, SAVE_DATA_FORMAT, this->startTime, this->packetCount, this->state); //save the start time |
chasefarmer2808 | 2:cc07e65227ff | 62 | fclose(fp1); |
chasefarmer2808 | 2:cc07e65227ff | 63 | return; |
chasefarmer2808 | 2:cc07e65227ff | 64 | } |
chasefarmer2808 | 3:d8932a3036a8 | 65 | |
chasefarmer2808 | 6:bb82dd1618c2 | 66 | //else, the file exists. Read the data |
chasefarmer2808 | 6:bb82dd1618c2 | 67 | fscanf(fp, SAVE_DATA_FORMAT, &this->startTime, &this->packetCount, &this->state); //set the start time, packet count, and state |
chasefarmer2808 | 6:bb82dd1618c2 | 68 | //this->dev->printf("start time: %d\r\n", this->startTime); |
chasefarmer2808 | 6:bb82dd1618c2 | 69 | //this->dev->printf("start packetCount: %d\r\n", this->packetCount); |
chasefarmer2808 | 6:bb82dd1618c2 | 70 | rewind(fp); //move cursor back to beginning of file |
chasefarmer2808 | 2:cc07e65227ff | 71 | fclose(fp); |
chasefarmer2808 | 2:cc07e65227ff | 72 | } |
chasefarmer2808 | 2:cc07e65227ff | 73 | |
chasefarmer2808 | 6:bb82dd1618c2 | 74 | void Glider::saveData() { |
chasefarmer2808 | 2:cc07e65227ff | 75 | FILE *fp = fopen("/telem/data.txt", "w"); |
chasefarmer2808 | 6:bb82dd1618c2 | 76 | //this->dev->printf("saving packet count: %d\r\n", this->packetCount); |
chasefarmer2808 | 6:bb82dd1618c2 | 77 | fprintf(fp, SAVE_DATA_FORMAT, this->startTime, this->packetCount, this->state); |
chasefarmer2808 | 2:cc07e65227ff | 78 | fclose(fp); |
chasefarmer2808 | 2:cc07e65227ff | 79 | } |
chasefarmer2808 | 2:cc07e65227ff | 80 | |
chasefarmer2808 | 1:7039ae86fe70 | 81 | void Glider::transmitPacket() { |
chasefarmer2808 | 9:8e34e713e6ef | 82 | //TODO: transmit speed |
chasefarmer2808 | 9:8e34e713e6ef | 83 | // voltage |
chasefarmer2808 | 4:ff59603d9fb6 | 84 | this->packetCount++; |
chasefarmer2808 | 4:ff59603d9fb6 | 85 | |
chasefarmer2808 | 9:8e34e713e6ef | 86 | this->xbee->printf("3387, GLIDER, %d, %d, %f, %f, %f, %f, %f, %d\r\n", |
chasefarmer2808 | 8:7fc2caa10bbb | 87 | this->missionTime, |
chasefarmer2808 | 8:7fc2caa10bbb | 88 | this->packetCount, |
chasefarmer2808 | 8:7fc2caa10bbb | 89 | this->alt, |
chasefarmer2808 | 8:7fc2caa10bbb | 90 | this->pressure, |
chasefarmer2808 | 8:7fc2caa10bbb | 91 | this->temp, |
chasefarmer2808 | 9:8e34e713e6ef | 92 | this->speed, |
chasefarmer2808 | 8:7fc2caa10bbb | 93 | this->heading, |
chasefarmer2808 | 8:7fc2caa10bbb | 94 | this->state); |
chasefarmer2808 | 1:7039ae86fe70 | 95 | } |
chasefarmer2808 | 2:cc07e65227ff | 96 | |
chasefarmer2808 | 3:d8932a3036a8 | 97 | void Glider::setCommandFlag() { |
chasefarmer2808 | 6:bb82dd1618c2 | 98 | this->cmdFlag = true; |
chasefarmer2808 | 2:cc07e65227ff | 99 | } |
chasefarmer2808 | 2:cc07e65227ff | 100 | |
chasefarmer2808 | 2:cc07e65227ff | 101 | void Glider::processCommand() { |
chasefarmer2808 | 6:bb82dd1618c2 | 102 | if (this->cmdFlag) { //command recieved |
chasefarmer2808 | 6:bb82dd1618c2 | 103 | char command = this->xbee->getc(); //get the command char |
chasefarmer2808 | 3:d8932a3036a8 | 104 | |
chasefarmer2808 | 3:d8932a3036a8 | 105 | switch (command) { |
chasefarmer2808 | 6:bb82dd1618c2 | 106 | case CMD_BUZZER: //'b' was recieved. sound the buzzer |
chasefarmer2808 | 3:d8932a3036a8 | 107 | this->dev->printf("buzzing...\r\n"); |
chasefarmer2808 | 7:dc93fe573846 | 108 | this->state = LAND; |
chasefarmer2808 | 3:d8932a3036a8 | 109 | while(1); |
chasefarmer2808 | 3:d8932a3036a8 | 110 | break; |
chasefarmer2808 | 6:bb82dd1618c2 | 111 | case CMD_RESET: |
chasefarmer2808 | 6:bb82dd1618c2 | 112 | this->dev->printf("resetting save file...\r\n"); |
chasefarmer2808 | 6:bb82dd1618c2 | 113 | remove("/telem/data.txt"); |
chasefarmer2808 | 6:bb82dd1618c2 | 114 | this->initSaveData(); |
chasefarmer2808 | 3:d8932a3036a8 | 115 | } |
chasefarmer2808 | 3:d8932a3036a8 | 116 | |
chasefarmer2808 | 6:bb82dd1618c2 | 117 | this->cmdFlag = false; //reset the flag so a new command can be recieved |
chasefarmer2808 | 2:cc07e65227ff | 118 | } |
chasefarmer2808 | 2:cc07e65227ff | 119 | } |