Regenerating PPM signal based on distances from ultrasonic sensors, ESP8266 for connectin via wifi. Autonomous quadcopter behaviour, autonomou height holding. Flying direction based on front and back ultrasonic sensors.

Dependencies:   ConfigFile HCSR04 PID PPM2 mbed-rtos mbed

Committer:
edy05
Date:
Fri Mar 23 14:45:52 2018 +0000
Branch:
DistanceRegulation
Revision:
38:801fee0330e2
Parent:
35:b09e19c855f6
Child:
39:93d8aa47f4ce
goAhead added, not tested

Who changed what in which revision?

UserRevisionLine numberNew contents of line
edy05 11:002927b2675d 1 #ifndef HARDWARE_H
edy05 11:002927b2675d 2 #define HARDWARE_H
edy05 2:d172c9963f87 3
edy05 2:d172c9963f87 4 #include "mbed.h"
edy05 2:d172c9963f87 5 #include "PpmRegen.h"
edy05 2:d172c9963f87 6 #include "PID.h"
edy05 2:d172c9963f87 7 #include "hcsr04.h"
edy05 8:b5128641d4cf 8 #include "ConfigFile.h"
edy05 2:d172c9963f87 9
edy05 11:002927b2675d 10
edy05 11:002927b2675d 11 void loadConfigFile(void);
edy05 11:002927b2675d 12 void writeSettingsToConfig(void);
edy05 11:002927b2675d 13 void writeErrorLog(char *message);
edy05 11:002927b2675d 14 void ConvertToCharArray(float number);
edy05 14:076ef843e1ba 15 void ConvertToCharArray(int number);
edy05 11:002927b2675d 16
edy05 26:11539036f0fb 17 Serial pc(USBTX, USBRX); // tx, rx
edy05 26:11539036f0fb 18 PID* _groundDistance;
edy05 27:5956d5e3ff63 19 //RtosTimer *_groudRegulationUpdateTimer;
edy05 26:11539036f0fb 20 HCSR04* _sonic;
edy05 34:5dca557e982f 21 uint32_t _frontDistance;
edy05 26:11539036f0fb 22 PpmRegen* _ppmRegen;
edy05 11:002927b2675d 23
edy05 11:002927b2675d 24 ConfigFile _configFile;
edy05 11:002927b2675d 25 LocalFileSystem local("local");
edy05 2:d172c9963f87 26
edy05 34:5dca557e982f 27 Thread _serverThread;
edy05 34:5dca557e982f 28 Thread _frontSensorThread;
edy05 27:5956d5e3ff63 29 Thread *_distanceThread;
edy05 27:5956d5e3ff63 30
edy05 2:d172c9963f87 31
edy05 2:d172c9963f87 32 InterruptIn* _interruptPort = new InterruptIn(p28);
edy05 2:d172c9963f87 33 PwmOut* _roll = new PwmOut(p21);
edy05 2:d172c9963f87 34 PwmOut* _pitch = new PwmOut(p22);
edy05 2:d172c9963f87 35 PwmOut* _throttle = new PwmOut(p23);
edy05 2:d172c9963f87 36 PwmOut* _yaw = new PwmOut(p24);
edy05 2:d172c9963f87 37 PwmOut* _aux1 = new PwmOut(p25);
edy05 2:d172c9963f87 38 PwmOut* _aux2 = new PwmOut(p26);
edy05 2:d172c9963f87 39
edy05 2:d172c9963f87 40
edy05 9:86a5af9935b1 41 char* _str = new char[1024];
edy05 15:9cdf757269fb 42 char* _serverMessage = new char[1024];
edy05 17:1896d242945b 43 bool _configChanges = false;
edy05 31:5f1737e480f3 44 bool _onlyDistanChanged = false;
edy05 15:9cdf757269fb 45 // zero is default value
edy05 9:86a5af9935b1 46 float _P = 0;
edy05 9:86a5af9935b1 47 float _I = 0;
edy05 9:86a5af9935b1 48 float _D = 0;
edy05 35:b09e19c855f6 49 float _groundSetPoint = 0;
edy05 35:b09e19c855f6 50 float _bias = 0;
edy05 35:b09e19c855f6 51 float _groundPidMinOutput = 0;
edy05 35:b09e19c855f6 52 float _groundPidMaxOutput = 0;
edy05 21:0afb91824792 53 bool _groundRegulation = false;
edy05 31:5f1737e480f3 54 // Temporary values for Server to filter noise
edy05 32:c729e6da7f9a 55 float _newP;
edy05 32:c729e6da7f9a 56 float _newI;
edy05 32:c729e6da7f9a 57 float _newD;
edy05 31:5f1737e480f3 58 bool _tempGroundRegulation = false;
edy05 38:801fee0330e2 59 bool _goAhead = false;
edy05 31:5f1737e480f3 60
edy05 8:b5128641d4cf 61
edy05 11:002927b2675d 62
edy05 11:002927b2675d 63
edy05 11:002927b2675d 64
edy05 10:bb9c778f8e3e 65
edy05 9:86a5af9935b1 66 void loadConfigFile(void){
edy05 9:86a5af9935b1 67 //reading configFile
edy05 9:86a5af9935b1 68 _configFile.read("/local/config.cfg");
edy05 9:86a5af9935b1 69 char value[BUFSIZ];
edy05 25:69190c222dbf 70 if (_configFile.getValue("bias", &value[0], sizeof(value)))
edy05 25:69190c222dbf 71 _bias = atof(value);
edy05 25:69190c222dbf 72 if (_configFile.getValue("groundPidMinOutput", &value[0], sizeof(value)))
edy05 25:69190c222dbf 73 _groundPidMinOutput = atof(value);
edy05 25:69190c222dbf 74 if (_configFile.getValue("groundPidMaxOutput", &value[0], sizeof(value)))
edy05 25:69190c222dbf 75 _groundPidMaxOutput = atof(value);
edy05 9:86a5af9935b1 76 if (_configFile.getValue("P", &value[0], sizeof(value)))
edy05 9:86a5af9935b1 77 _P = atof(value);
edy05 9:86a5af9935b1 78 if (_configFile.getValue("I", &value[0], sizeof(value)))
edy05 9:86a5af9935b1 79 _I = atof(value);
edy05 9:86a5af9935b1 80 if (_configFile.getValue("D", &value[0], sizeof(value)))
edy05 9:86a5af9935b1 81 _D = atof(value);
edy05 14:076ef843e1ba 82 if (_configFile.getValue("groundSetPoint", &value[0], sizeof(value)))
edy05 14:076ef843e1ba 83 _groundSetPoint = atof(value);
edy05 9:86a5af9935b1 84
edy05 9:86a5af9935b1 85 }
edy05 9:86a5af9935b1 86
edy05 11:002927b2675d 87 void writeSettingsToConfig(void){
edy05 25:69190c222dbf 88 ConvertToCharArray(_bias);
edy05 25:69190c222dbf 89 if(!_configFile.setValue("bias", _str))
edy05 25:69190c222dbf 90 strcat(_serverMessage, "bias value could not be saved to configFile\n\r");
edy05 25:69190c222dbf 91 ConvertToCharArray(_groundPidMinOutput);
edy05 25:69190c222dbf 92 if(!_configFile.setValue("groundPidMinOutput", _str))
edy05 25:69190c222dbf 93 strcat(_serverMessage, "groundPidMinOutput value could not be saved to configFile\n\r");
edy05 25:69190c222dbf 94 ConvertToCharArray(_groundPidMaxOutput);
edy05 25:69190c222dbf 95 if(!_configFile.setValue("groundPidMaxOutput", _str))
edy05 25:69190c222dbf 96 strcat(_serverMessage, "groundPidMaxOutput value could not be saved to configFile\n\r");
edy05 10:bb9c778f8e3e 97 ConvertToCharArray(_P);
edy05 15:9cdf757269fb 98 if(!_configFile.setValue("P", _str))
edy05 15:9cdf757269fb 99 strcat(_serverMessage, "P value could not be saved to configFile\n\r");
edy05 10:bb9c778f8e3e 100 ConvertToCharArray(_I);
edy05 15:9cdf757269fb 101 if(!_configFile.setValue("I", _str))
edy05 15:9cdf757269fb 102 strcat(_serverMessage, "I value could not be saved to configFile\n\r");
edy05 10:bb9c778f8e3e 103 ConvertToCharArray(_D);
edy05 15:9cdf757269fb 104 if(!_configFile.setValue("D", _str))
edy05 15:9cdf757269fb 105 strcat(_serverMessage, "D value could not be saved to configFile\n\r");
edy05 14:076ef843e1ba 106 ConvertToCharArray(_groundSetPoint);
edy05 15:9cdf757269fb 107 if(!_configFile.setValue("groundSetPoint", _str)){
edy05 15:9cdf757269fb 108 strcat(_serverMessage, "groundSetPoint value could not be saved to configFile\n\r");
edy05 15:9cdf757269fb 109 pc.printf("groundSetPoint value could not be saved to configFile\n\r");
edy05 15:9cdf757269fb 110 }
edy05 10:bb9c778f8e3e 111 _configFile.write("/local/config.cfg");
edy05 10:bb9c778f8e3e 112
edy05 10:bb9c778f8e3e 113 }
edy05 10:bb9c778f8e3e 114
edy05 11:002927b2675d 115
edy05 11:002927b2675d 116 void writeErrorLog(char *message){
edy05 11:002927b2675d 117 FILE *fp = fopen("/local/errorlog.txt", "w");
edy05 11:002927b2675d 118 fprintf(fp, message);
edy05 11:002927b2675d 119 fclose(fp);
edy05 11:002927b2675d 120
edy05 11:002927b2675d 121 }
edy05 11:002927b2675d 122
edy05 11:002927b2675d 123
edy05 9:86a5af9935b1 124 //Converts float to char array
edy05 9:86a5af9935b1 125 void ConvertToCharArray(float number)
edy05 9:86a5af9935b1 126 {
edy05 25:69190c222dbf 127 sprintf(_str, "%3.6f", number );
edy05 9:86a5af9935b1 128 }
edy05 8:b5128641d4cf 129
edy05 14:076ef843e1ba 130 //Converts integer to char array
edy05 14:076ef843e1ba 131 void ConvertToCharArray(int number)
edy05 14:076ef843e1ba 132 {
edy05 14:076ef843e1ba 133 sprintf(_str, "%d", number );
edy05 14:076ef843e1ba 134 }
edy05 14:076ef843e1ba 135
edy05 8:b5128641d4cf 136
edy05 2:d172c9963f87 137 #endif