Abstraction for the glider class
Glider.cpp@10:4c4f0ea887a4, 2017-04-24 (annotated)
- Committer:
- chasefarmer2808
- Date:
- Mon Apr 24 21:34:11 2017 +0000
- Revision:
- 10:4c4f0ea887a4
- Parent:
- 9:8e34e713e6ef
- Child:
- 11:5e83ff964f5f
accutate
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 | 10:4c4f0ea887a4 | 16 | bmp->Initialize(64, 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 | 10:4c4f0ea887a4 | 37 | this->missionTime = time(NULL) - this->startTime; //duration = current tome - start time |
chasefarmer2808 | 1:7039ae86fe70 | 38 | } |
chasefarmer2808 | 1:7039ae86fe70 | 39 | |
chasefarmer2808 | 4:ff59603d9fb6 | 40 | void Glider::setTransmitFlag() { |
chasefarmer2808 | 4:ff59603d9fb6 | 41 | this->transmitFlag = true; |
chasefarmer2808 | 4:ff59603d9fb6 | 42 | } |
chasefarmer2808 | 4:ff59603d9fb6 | 43 | |
chasefarmer2808 | 0:4e6b53a7bd38 | 44 | void Glider::saveTelem() { |
chasefarmer2808 | 0:4e6b53a7bd38 | 45 | FILE *fp = fopen("/telem/telem.txt", "w"); |
chasefarmer2808 | 0:4e6b53a7bd38 | 46 | fprintf(fp, "hello,"); |
chasefarmer2808 | 1:7039ae86fe70 | 47 | fclose(fp); |
chasefarmer2808 | 1:7039ae86fe70 | 48 | } |
chasefarmer2808 | 1:7039ae86fe70 | 49 | |
chasefarmer2808 | 5:460412fc4a17 | 50 | void Glider::initSaveData() { |
chasefarmer2808 | 2:cc07e65227ff | 51 | this->dev->printf("getting the time...\r\n"); |
chasefarmer2808 | 3:d8932a3036a8 | 52 | FILE *fp = fopen("/telem/data.txt", "r"); //attempt to read the saved data |
chasefarmer2808 | 2:cc07e65227ff | 53 | |
chasefarmer2808 | 3:d8932a3036a8 | 54 | if (fp == NULL) { //file does not exist |
chasefarmer2808 | 3:d8932a3036a8 | 55 | this->dev->printf("starting the time...\r\n"); |
chasefarmer2808 | 3:d8932a3036a8 | 56 | this->startTime = time(NULL); //initialize the start time to now |
chasefarmer2808 | 6:bb82dd1618c2 | 57 | this->packetCount = 0; //start the packet count at 0 |
chasefarmer2808 | 6:bb82dd1618c2 | 58 | this->state = CRUZE; |
chasefarmer2808 | 3:d8932a3036a8 | 59 | FILE *fp1 = fopen("/telem/data.txt", "w"); //create the data file |
chasefarmer2808 | 6:bb82dd1618c2 | 60 | fprintf(fp1, SAVE_DATA_FORMAT, this->startTime, this->packetCount, this->state); //save the start time |
chasefarmer2808 | 2:cc07e65227ff | 61 | fclose(fp1); |
chasefarmer2808 | 2:cc07e65227ff | 62 | return; |
chasefarmer2808 | 2:cc07e65227ff | 63 | } |
chasefarmer2808 | 3:d8932a3036a8 | 64 | |
chasefarmer2808 | 6:bb82dd1618c2 | 65 | //else, the file exists. Read the data |
chasefarmer2808 | 6:bb82dd1618c2 | 66 | fscanf(fp, SAVE_DATA_FORMAT, &this->startTime, &this->packetCount, &this->state); //set the start time, packet count, and state |
chasefarmer2808 | 6:bb82dd1618c2 | 67 | rewind(fp); //move cursor back to beginning of file |
chasefarmer2808 | 2:cc07e65227ff | 68 | fclose(fp); |
chasefarmer2808 | 2:cc07e65227ff | 69 | } |
chasefarmer2808 | 2:cc07e65227ff | 70 | |
chasefarmer2808 | 6:bb82dd1618c2 | 71 | void Glider::saveData() { |
chasefarmer2808 | 2:cc07e65227ff | 72 | FILE *fp = fopen("/telem/data.txt", "w"); |
chasefarmer2808 | 10:4c4f0ea887a4 | 73 | fprintf(fp, SAVE_DATA_FORMAT, this->startTime, this->packetCount, this->state); //save the start time, packet count, and state to flash |
chasefarmer2808 | 2:cc07e65227ff | 74 | fclose(fp); |
chasefarmer2808 | 2:cc07e65227ff | 75 | } |
chasefarmer2808 | 2:cc07e65227ff | 76 | |
chasefarmer2808 | 1:7039ae86fe70 | 77 | void Glider::transmitPacket() { |
chasefarmer2808 | 10:4c4f0ea887a4 | 78 | //TODO: transmit |
chasefarmer2808 | 9:8e34e713e6ef | 79 | // voltage |
chasefarmer2808 | 4:ff59603d9fb6 | 80 | this->packetCount++; |
chasefarmer2808 | 4:ff59603d9fb6 | 81 | |
chasefarmer2808 | 9:8e34e713e6ef | 82 | this->xbee->printf("3387, GLIDER, %d, %d, %f, %f, %f, %f, %f, %d\r\n", |
chasefarmer2808 | 10:4c4f0ea887a4 | 83 | this->missionTime, //duration in seconds |
chasefarmer2808 | 10:4c4f0ea887a4 | 84 | this->packetCount, |
chasefarmer2808 | 10:4c4f0ea887a4 | 85 | this->alt, //meters |
chasefarmer2808 | 10:4c4f0ea887a4 | 86 | this->pressure, // |
chasefarmer2808 | 10:4c4f0ea887a4 | 87 | this->temp, //C |
chasefarmer2808 | 10:4c4f0ea887a4 | 88 | this->speed, //m per s |
chasefarmer2808 | 10:4c4f0ea887a4 | 89 | this->heading, //degrees from North |
chasefarmer2808 | 8:7fc2caa10bbb | 90 | this->state); |
chasefarmer2808 | 1:7039ae86fe70 | 91 | } |
chasefarmer2808 | 2:cc07e65227ff | 92 | |
chasefarmer2808 | 3:d8932a3036a8 | 93 | void Glider::setCommandFlag() { |
chasefarmer2808 | 6:bb82dd1618c2 | 94 | this->cmdFlag = true; |
chasefarmer2808 | 2:cc07e65227ff | 95 | } |
chasefarmer2808 | 2:cc07e65227ff | 96 | |
chasefarmer2808 | 2:cc07e65227ff | 97 | void Glider::processCommand() { |
chasefarmer2808 | 6:bb82dd1618c2 | 98 | if (this->cmdFlag) { //command recieved |
chasefarmer2808 | 6:bb82dd1618c2 | 99 | char command = this->xbee->getc(); //get the command char |
chasefarmer2808 | 3:d8932a3036a8 | 100 | |
chasefarmer2808 | 3:d8932a3036a8 | 101 | switch (command) { |
chasefarmer2808 | 6:bb82dd1618c2 | 102 | case CMD_BUZZER: //'b' was recieved. sound the buzzer |
chasefarmer2808 | 3:d8932a3036a8 | 103 | this->dev->printf("buzzing...\r\n"); |
chasefarmer2808 | 7:dc93fe573846 | 104 | this->state = LAND; |
chasefarmer2808 | 3:d8932a3036a8 | 105 | while(1); |
chasefarmer2808 | 3:d8932a3036a8 | 106 | break; |
chasefarmer2808 | 6:bb82dd1618c2 | 107 | case CMD_RESET: |
chasefarmer2808 | 6:bb82dd1618c2 | 108 | this->dev->printf("resetting save file...\r\n"); |
chasefarmer2808 | 6:bb82dd1618c2 | 109 | remove("/telem/data.txt"); |
chasefarmer2808 | 6:bb82dd1618c2 | 110 | this->initSaveData(); |
chasefarmer2808 | 3:d8932a3036a8 | 111 | } |
chasefarmer2808 | 3:d8932a3036a8 | 112 | |
chasefarmer2808 | 6:bb82dd1618c2 | 113 | this->cmdFlag = false; //reset the flag so a new command can be recieved |
chasefarmer2808 | 2:cc07e65227ff | 114 | } |
chasefarmer2808 | 2:cc07e65227ff | 115 | } |