Abstraction for the glider class
Glider.cpp@15:5fea9af1cdb7, 2017-05-11 (annotated)
- Committer:
- chasefarmer2808
- Date:
- Thu May 11 14:49:26 2017 +0000
- Revision:
- 15:5fea9af1cdb7
- Parent:
- 14:a226bedae730
- Child:
- 16:7e1a1cd9f9fb
solar panel voltage input
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 | 13:d95070aa9140 | 60 | fprintf(fp, "3387, GLIDER, %d, %u, %f, %f, %f, %f, %f, %u\r\n", |
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 | 13:d95070aa9140 | 67 | this->heading, //degrees from North |
chasefarmer2808 | 15:5fea9af1cdb7 | 68 | this->state); |
chasefarmer2808 | 1:7039ae86fe70 | 69 | fclose(fp); |
chasefarmer2808 | 1:7039ae86fe70 | 70 | } |
chasefarmer2808 | 1:7039ae86fe70 | 71 | |
chasefarmer2808 | 5:460412fc4a17 | 72 | void Glider::initSaveData() { |
chasefarmer2808 | 2:cc07e65227ff | 73 | this->dev->printf("getting the time...\r\n"); |
chasefarmer2808 | 3:d8932a3036a8 | 74 | FILE *fp = fopen("/telem/data.txt", "r"); //attempt to read the saved data |
chasefarmer2808 | 2:cc07e65227ff | 75 | |
chasefarmer2808 | 3:d8932a3036a8 | 76 | if (fp == NULL) { //file does not exist |
chasefarmer2808 | 3:d8932a3036a8 | 77 | this->dev->printf("starting the time...\r\n"); |
chasefarmer2808 | 3:d8932a3036a8 | 78 | this->startTime = time(NULL); //initialize the start time to now |
chasefarmer2808 | 6:bb82dd1618c2 | 79 | this->packetCount = 0; //start the packet count at 0 |
chasefarmer2808 | 6:bb82dd1618c2 | 80 | this->state = CRUZE; |
chasefarmer2808 | 3:d8932a3036a8 | 81 | FILE *fp1 = fopen("/telem/data.txt", "w"); //create the data file |
chasefarmer2808 | 6:bb82dd1618c2 | 82 | fprintf(fp1, SAVE_DATA_FORMAT, this->startTime, this->packetCount, this->state); //save the start time |
chasefarmer2808 | 2:cc07e65227ff | 83 | fclose(fp1); |
chasefarmer2808 | 2:cc07e65227ff | 84 | return; |
chasefarmer2808 | 2:cc07e65227ff | 85 | } |
chasefarmer2808 | 3:d8932a3036a8 | 86 | |
chasefarmer2808 | 6:bb82dd1618c2 | 87 | //else, the file exists. Read the data |
chasefarmer2808 | 6:bb82dd1618c2 | 88 | fscanf(fp, SAVE_DATA_FORMAT, &this->startTime, &this->packetCount, &this->state); //set the start time, packet count, and state |
chasefarmer2808 | 6:bb82dd1618c2 | 89 | rewind(fp); //move cursor back to beginning of file |
chasefarmer2808 | 2:cc07e65227ff | 90 | fclose(fp); |
chasefarmer2808 | 2:cc07e65227ff | 91 | } |
chasefarmer2808 | 2:cc07e65227ff | 92 | |
chasefarmer2808 | 6:bb82dd1618c2 | 93 | void Glider::saveData() { |
chasefarmer2808 | 2:cc07e65227ff | 94 | FILE *fp = fopen("/telem/data.txt", "w"); |
chasefarmer2808 | 10:4c4f0ea887a4 | 95 | fprintf(fp, SAVE_DATA_FORMAT, this->startTime, this->packetCount, this->state); //save the start time, packet count, and state to flash |
chasefarmer2808 | 2:cc07e65227ff | 96 | fclose(fp); |
chasefarmer2808 | 2:cc07e65227ff | 97 | } |
chasefarmer2808 | 2:cc07e65227ff | 98 | |
chasefarmer2808 | 1:7039ae86fe70 | 99 | void Glider::transmitPacket() { |
chasefarmer2808 | 10:4c4f0ea887a4 | 100 | //TODO: transmit |
chasefarmer2808 | 9:8e34e713e6ef | 101 | // voltage |
chasefarmer2808 | 4:ff59603d9fb6 | 102 | this->packetCount++; |
chasefarmer2808 | 4:ff59603d9fb6 | 103 | |
chasefarmer2808 | 15:5fea9af1cdb7 | 104 | this->xbee->printf("3387, GLIDER, %d, %d, %f, %f, %f, %f, %f, %f, %d\r\n", |
chasefarmer2808 | 10:4c4f0ea887a4 | 105 | this->missionTime, //duration in seconds |
chasefarmer2808 | 10:4c4f0ea887a4 | 106 | this->packetCount, |
chasefarmer2808 | 10:4c4f0ea887a4 | 107 | this->alt, //meters |
chasefarmer2808 | 10:4c4f0ea887a4 | 108 | this->pressure, // |
chasefarmer2808 | 10:4c4f0ea887a4 | 109 | this->temp, //C |
chasefarmer2808 | 10:4c4f0ea887a4 | 110 | this->speed, //m per s |
chasefarmer2808 | 15:5fea9af1cdb7 | 111 | this->solarVoltage, |
chasefarmer2808 | 10:4c4f0ea887a4 | 112 | this->heading, //degrees from North |
chasefarmer2808 | 8:7fc2caa10bbb | 113 | this->state); |
chasefarmer2808 | 1:7039ae86fe70 | 114 | } |
chasefarmer2808 | 2:cc07e65227ff | 115 | |
chasefarmer2808 | 3:d8932a3036a8 | 116 | void Glider::setCommandFlag() { |
chasefarmer2808 | 6:bb82dd1618c2 | 117 | this->cmdFlag = true; |
chasefarmer2808 | 2:cc07e65227ff | 118 | } |
chasefarmer2808 | 2:cc07e65227ff | 119 | |
chasefarmer2808 | 2:cc07e65227ff | 120 | void Glider::processCommand() { |
chasefarmer2808 | 6:bb82dd1618c2 | 121 | if (this->cmdFlag) { //command recieved |
chasefarmer2808 | 6:bb82dd1618c2 | 122 | char command = this->xbee->getc(); //get the command char |
chasefarmer2808 | 3:d8932a3036a8 | 123 | |
chasefarmer2808 | 3:d8932a3036a8 | 124 | switch (command) { |
chasefarmer2808 | 6:bb82dd1618c2 | 125 | case CMD_BUZZER: //'b' was recieved. sound the buzzer |
chasefarmer2808 | 11:5e83ff964f5f | 126 | this->state = LAND; //set state to land |
chasefarmer2808 | 13:d95070aa9140 | 127 | //this->transmitPacket(); //transmit one last packet |
chasefarmer2808 | 13:d95070aa9140 | 128 | //this->soundBuzzer(BUZZER_FREQ, 1, true); //sound buzzer forever |
chasefarmer2808 | 13:d95070aa9140 | 129 | //while (1); |
chasefarmer2808 | 3:d8932a3036a8 | 130 | break; |
chasefarmer2808 | 11:5e83ff964f5f | 131 | case CMD_RESET: //'z' was recieved |
chasefarmer2808 | 13:d95070aa9140 | 132 | xbee->printf("resetting save file...\r\n"); |
chasefarmer2808 | 13:d95070aa9140 | 133 | |
chasefarmer2808 | 13:d95070aa9140 | 134 | state = 0; |
chasefarmer2808 | 13:d95070aa9140 | 135 | packetCount = 0; |
chasefarmer2808 | 13:d95070aa9140 | 136 | startTime = 0; |
chasefarmer2808 | 13:d95070aa9140 | 137 | saveData(); |
chasefarmer2808 | 13:d95070aa9140 | 138 | //remove("/telem/data.txt"); |
chasefarmer2808 | 13:d95070aa9140 | 139 | //this->initSaveData(); |
chasefarmer2808 | 13:d95070aa9140 | 140 | break; |
chasefarmer2808 | 3:d8932a3036a8 | 141 | } |
chasefarmer2808 | 3:d8932a3036a8 | 142 | |
chasefarmer2808 | 6:bb82dd1618c2 | 143 | this->cmdFlag = false; //reset the flag so a new command can be recieved |
chasefarmer2808 | 2:cc07e65227ff | 144 | } |
chasefarmer2808 | 2:cc07e65227ff | 145 | } |
chasefarmer2808 | 11:5e83ff964f5f | 146 | |
chasefarmer2808 | 11:5e83ff964f5f | 147 | void Glider::soundBuzzer(float freq, float dur, bool infinate) { |
chasefarmer2808 | 11:5e83ff964f5f | 148 | buzzer.beep(freq, dur, infinate); |
chasefarmer2808 | 11:5e83ff964f5f | 149 | } |
chasefarmer2808 | 13:d95070aa9140 | 150 | |
chasefarmer2808 | 13:d95070aa9140 | 151 | void Glider::checkForLand() { |
chasefarmer2808 | 13:d95070aa9140 | 152 | xbee->printf("%f\r\n", alt); |
chasefarmer2808 | 13:d95070aa9140 | 153 | if (alt <= ALT_THRESH) { |
chasefarmer2808 | 13:d95070aa9140 | 154 | state = LAND; |
chasefarmer2808 | 13:d95070aa9140 | 155 | } |
chasefarmer2808 | 13:d95070aa9140 | 156 | } |
chasefarmer2808 | 13:d95070aa9140 | 157 | |
chasefarmer2808 | 13:d95070aa9140 | 158 | void Glider::endMission() { |
chasefarmer2808 | 13:d95070aa9140 | 159 | this->transmitPacket(); //transmit one last packet |
chasefarmer2808 | 13:d95070aa9140 | 160 | this->soundBuzzer(BUZZER_FREQ, 1, true); //sound buzzer forever |
chasefarmer2808 | 13:d95070aa9140 | 161 | while (1) { |
chasefarmer2808 | 13:d95070aa9140 | 162 | processCommand(); |
chasefarmer2808 | 13:d95070aa9140 | 163 | } |
chasefarmer2808 | 13:d95070aa9140 | 164 | } |