Abstraction for the glider class
Glider.cpp@18:a4fbe3ce0f16, 2017-05-16 (annotated)
- Committer:
- chasefarmer2808
- Date:
- Tue May 16 16:51:23 2017 +0000
- Revision:
- 18:a4fbe3ce0f16
- Parent:
- 16:7e1a1cd9f9fb
- Child:
- 19:9bdc2b081b16
remove save file
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 | 14:a226bedae730 | 3 | Glider::Glider(Serial* device, PinName sda, PinName scl, PinName tx, PinName rx) : xbeeRxInt(rx), pitot(p15), solVoltIn(p16), 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 | 14:a226bedae730 | 37 | void Glider::setSolarVoltage() { |
chasefarmer2808 | 14:a226bedae730 | 38 | solarVoltage = solVoltIn.read_u16() * PITO_ADC_RATIO; |
chasefarmer2808 | 14:a226bedae730 | 39 | solarVoltage = solarVoltage / SOLAR_V_DIVIDER; |
chasefarmer2808 | 14:a226bedae730 | 40 | } |
chasefarmer2808 | 14:a226bedae730 | 41 | |
chasefarmer2808 | 1:7039ae86fe70 | 42 | void Glider::setMissionTime() { |
chasefarmer2808 | 10:4c4f0ea887a4 | 43 | this->missionTime = time(NULL) - this->startTime; //duration = current tome - start time |
chasefarmer2808 | 1:7039ae86fe70 | 44 | } |
chasefarmer2808 | 1:7039ae86fe70 | 45 | |
chasefarmer2808 | 12:ca3e1c6203f7 | 46 | void Glider::updateTelem() { |
chasefarmer2808 | 12:ca3e1c6203f7 | 47 | setHeading(); |
chasefarmer2808 | 12:ca3e1c6203f7 | 48 | setTempPressAlt(); |
chasefarmer2808 | 12:ca3e1c6203f7 | 49 | setAirSpeed(); |
chasefarmer2808 | 14:a226bedae730 | 50 | setSolarVoltage(); |
chasefarmer2808 | 12:ca3e1c6203f7 | 51 | setMissionTime(); |
chasefarmer2808 | 12:ca3e1c6203f7 | 52 | } |
chasefarmer2808 | 12:ca3e1c6203f7 | 53 | |
chasefarmer2808 | 4:ff59603d9fb6 | 54 | void Glider::setTransmitFlag() { |
chasefarmer2808 | 4:ff59603d9fb6 | 55 | this->transmitFlag = true; |
chasefarmer2808 | 4:ff59603d9fb6 | 56 | } |
chasefarmer2808 | 4:ff59603d9fb6 | 57 | |
chasefarmer2808 | 0:4e6b53a7bd38 | 58 | void Glider::saveTelem() { |
chasefarmer2808 | 13:d95070aa9140 | 59 | FILE *fp = fopen("/telem/telem.csv", "a"); |
chasefarmer2808 | 16:7e1a1cd9f9fb | 60 | fprintf(fp, PACKET_FORMAT, |
chasefarmer2808 | 13:d95070aa9140 | 61 | this->missionTime, //duration in seconds |
chasefarmer2808 | 13:d95070aa9140 | 62 | this->packetCount, |
chasefarmer2808 | 13:d95070aa9140 | 63 | this->alt, //meters |
chasefarmer2808 | 13:d95070aa9140 | 64 | this->pressure, // |
chasefarmer2808 | 13:d95070aa9140 | 65 | this->temp, //C |
chasefarmer2808 | 13:d95070aa9140 | 66 | this->speed, //m per s |
chasefarmer2808 | 16:7e1a1cd9f9fb | 67 | this->solarVoltage, |
chasefarmer2808 | 13:d95070aa9140 | 68 | this->heading, //degrees from North |
chasefarmer2808 | 15:5fea9af1cdb7 | 69 | this->state); |
chasefarmer2808 | 1:7039ae86fe70 | 70 | fclose(fp); |
chasefarmer2808 | 1:7039ae86fe70 | 71 | } |
chasefarmer2808 | 1:7039ae86fe70 | 72 | |
chasefarmer2808 | 5:460412fc4a17 | 73 | void Glider::initSaveData() { |
chasefarmer2808 | 2:cc07e65227ff | 74 | this->dev->printf("getting the time...\r\n"); |
chasefarmer2808 | 3:d8932a3036a8 | 75 | FILE *fp = fopen("/telem/data.txt", "r"); //attempt to read the saved data |
chasefarmer2808 | 2:cc07e65227ff | 76 | |
chasefarmer2808 | 3:d8932a3036a8 | 77 | if (fp == NULL) { //file does not exist |
chasefarmer2808 | 3:d8932a3036a8 | 78 | this->dev->printf("starting the time...\r\n"); |
chasefarmer2808 | 3:d8932a3036a8 | 79 | this->startTime = time(NULL); //initialize the start time to now |
chasefarmer2808 | 6:bb82dd1618c2 | 80 | this->packetCount = 0; //start the packet count at 0 |
chasefarmer2808 | 6:bb82dd1618c2 | 81 | this->state = CRUZE; |
chasefarmer2808 | 3:d8932a3036a8 | 82 | FILE *fp1 = fopen("/telem/data.txt", "w"); //create the data file |
chasefarmer2808 | 6:bb82dd1618c2 | 83 | fprintf(fp1, SAVE_DATA_FORMAT, this->startTime, this->packetCount, this->state); //save the start time |
chasefarmer2808 | 2:cc07e65227ff | 84 | fclose(fp1); |
chasefarmer2808 | 2:cc07e65227ff | 85 | return; |
chasefarmer2808 | 2:cc07e65227ff | 86 | } |
chasefarmer2808 | 3:d8932a3036a8 | 87 | |
chasefarmer2808 | 6:bb82dd1618c2 | 88 | //else, the file exists. Read the data |
chasefarmer2808 | 6:bb82dd1618c2 | 89 | fscanf(fp, SAVE_DATA_FORMAT, &this->startTime, &this->packetCount, &this->state); //set the start time, packet count, and state |
chasefarmer2808 | 6:bb82dd1618c2 | 90 | rewind(fp); //move cursor back to beginning of file |
chasefarmer2808 | 2:cc07e65227ff | 91 | fclose(fp); |
chasefarmer2808 | 2:cc07e65227ff | 92 | } |
chasefarmer2808 | 2:cc07e65227ff | 93 | |
chasefarmer2808 | 6:bb82dd1618c2 | 94 | void Glider::saveData() { |
chasefarmer2808 | 2:cc07e65227ff | 95 | FILE *fp = fopen("/telem/data.txt", "w"); |
chasefarmer2808 | 10:4c4f0ea887a4 | 96 | fprintf(fp, SAVE_DATA_FORMAT, this->startTime, this->packetCount, this->state); //save the start time, packet count, and state to flash |
chasefarmer2808 | 2:cc07e65227ff | 97 | fclose(fp); |
chasefarmer2808 | 2:cc07e65227ff | 98 | } |
chasefarmer2808 | 2:cc07e65227ff | 99 | |
chasefarmer2808 | 1:7039ae86fe70 | 100 | void Glider::transmitPacket() { |
chasefarmer2808 | 4:ff59603d9fb6 | 101 | this->packetCount++; |
chasefarmer2808 | 4:ff59603d9fb6 | 102 | |
chasefarmer2808 | 16:7e1a1cd9f9fb | 103 | this->xbee->printf(PACKET_FORMAT, |
chasefarmer2808 | 10:4c4f0ea887a4 | 104 | this->missionTime, //duration in seconds |
chasefarmer2808 | 10:4c4f0ea887a4 | 105 | this->packetCount, |
chasefarmer2808 | 10:4c4f0ea887a4 | 106 | this->alt, //meters |
chasefarmer2808 | 10:4c4f0ea887a4 | 107 | this->pressure, // |
chasefarmer2808 | 10:4c4f0ea887a4 | 108 | this->temp, //C |
chasefarmer2808 | 10:4c4f0ea887a4 | 109 | this->speed, //m per s |
chasefarmer2808 | 15:5fea9af1cdb7 | 110 | this->solarVoltage, |
chasefarmer2808 | 10:4c4f0ea887a4 | 111 | this->heading, //degrees from North |
chasefarmer2808 | 8:7fc2caa10bbb | 112 | this->state); |
chasefarmer2808 | 1:7039ae86fe70 | 113 | } |
chasefarmer2808 | 2:cc07e65227ff | 114 | |
chasefarmer2808 | 3:d8932a3036a8 | 115 | void Glider::setCommandFlag() { |
chasefarmer2808 | 6:bb82dd1618c2 | 116 | this->cmdFlag = true; |
chasefarmer2808 | 2:cc07e65227ff | 117 | } |
chasefarmer2808 | 2:cc07e65227ff | 118 | |
chasefarmer2808 | 2:cc07e65227ff | 119 | void Glider::processCommand() { |
chasefarmer2808 | 6:bb82dd1618c2 | 120 | if (this->cmdFlag) { //command recieved |
chasefarmer2808 | 6:bb82dd1618c2 | 121 | char command = this->xbee->getc(); //get the command char |
chasefarmer2808 | 3:d8932a3036a8 | 122 | |
chasefarmer2808 | 3:d8932a3036a8 | 123 | switch (command) { |
chasefarmer2808 | 6:bb82dd1618c2 | 124 | case CMD_BUZZER: //'b' was recieved. sound the buzzer |
chasefarmer2808 | 11:5e83ff964f5f | 125 | this->state = LAND; //set state to land |
chasefarmer2808 | 3:d8932a3036a8 | 126 | break; |
chasefarmer2808 | 11:5e83ff964f5f | 127 | case CMD_RESET: //'z' was recieved |
chasefarmer2808 | 13:d95070aa9140 | 128 | xbee->printf("resetting save file...\r\n"); |
chasefarmer2808 | 18:a4fbe3ce0f16 | 129 | /* |
chasefarmer2808 | 13:d95070aa9140 | 130 | state = 0; |
chasefarmer2808 | 13:d95070aa9140 | 131 | packetCount = 0; |
chasefarmer2808 | 13:d95070aa9140 | 132 | startTime = 0; |
chasefarmer2808 | 13:d95070aa9140 | 133 | saveData(); |
chasefarmer2808 | 18:a4fbe3ce0f16 | 134 | */ |
chasefarmer2808 | 18:a4fbe3ce0f16 | 135 | |
chasefarmer2808 | 18:a4fbe3ce0f16 | 136 | remove("/telem/data.txt"); |
chasefarmer2808 | 18:a4fbe3ce0f16 | 137 | |
chasefarmer2808 | 13:d95070aa9140 | 138 | break; |
chasefarmer2808 | 3:d8932a3036a8 | 139 | } |
chasefarmer2808 | 3:d8932a3036a8 | 140 | |
chasefarmer2808 | 6:bb82dd1618c2 | 141 | this->cmdFlag = false; //reset the flag so a new command can be recieved |
chasefarmer2808 | 2:cc07e65227ff | 142 | } |
chasefarmer2808 | 2:cc07e65227ff | 143 | } |
chasefarmer2808 | 11:5e83ff964f5f | 144 | |
chasefarmer2808 | 11:5e83ff964f5f | 145 | void Glider::soundBuzzer(float freq, float dur, bool infinate) { |
chasefarmer2808 | 11:5e83ff964f5f | 146 | buzzer.beep(freq, dur, infinate); |
chasefarmer2808 | 11:5e83ff964f5f | 147 | } |
chasefarmer2808 | 13:d95070aa9140 | 148 | |
chasefarmer2808 | 13:d95070aa9140 | 149 | void Glider::checkForLand() { |
chasefarmer2808 | 13:d95070aa9140 | 150 | xbee->printf("%f\r\n", alt); |
chasefarmer2808 | 13:d95070aa9140 | 151 | if (alt <= ALT_THRESH) { |
chasefarmer2808 | 13:d95070aa9140 | 152 | state = LAND; |
chasefarmer2808 | 13:d95070aa9140 | 153 | } |
chasefarmer2808 | 13:d95070aa9140 | 154 | } |
chasefarmer2808 | 13:d95070aa9140 | 155 | |
chasefarmer2808 | 13:d95070aa9140 | 156 | void Glider::endMission() { |
chasefarmer2808 | 13:d95070aa9140 | 157 | this->transmitPacket(); //transmit one last packet |
chasefarmer2808 | 13:d95070aa9140 | 158 | this->soundBuzzer(BUZZER_FREQ, 1, true); //sound buzzer forever |
chasefarmer2808 | 13:d95070aa9140 | 159 | while (1) { |
chasefarmer2808 | 13:d95070aa9140 | 160 | processCommand(); |
chasefarmer2808 | 13:d95070aa9140 | 161 | } |
chasefarmer2808 | 13:d95070aa9140 | 162 | } |