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
hardware.h@35:b09e19c855f6, 2018-03-03 (annotated)
- Committer:
- edy05
- Date:
- Sat Mar 03 15:38:10 2018 +0000
- Branch:
- DistanceRegulation
- Revision:
- 35:b09e19c855f6
- Parent:
- 34:5dca557e982f
- Child:
- 38:801fee0330e2
not working - pid result problem
Who changed what in which revision?
User | Revision | Line number | New 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 | 31:5f1737e480f3 | 59 | |
edy05 | 8:b5128641d4cf | 60 | |
edy05 | 11:002927b2675d | 61 | |
edy05 | 11:002927b2675d | 62 | |
edy05 | 11:002927b2675d | 63 | |
edy05 | 10:bb9c778f8e3e | 64 | |
edy05 | 9:86a5af9935b1 | 65 | void loadConfigFile(void){ |
edy05 | 9:86a5af9935b1 | 66 | //reading configFile |
edy05 | 9:86a5af9935b1 | 67 | _configFile.read("/local/config.cfg"); |
edy05 | 9:86a5af9935b1 | 68 | char value[BUFSIZ]; |
edy05 | 25:69190c222dbf | 69 | if (_configFile.getValue("bias", &value[0], sizeof(value))) |
edy05 | 25:69190c222dbf | 70 | _bias = atof(value); |
edy05 | 25:69190c222dbf | 71 | if (_configFile.getValue("groundPidMinOutput", &value[0], sizeof(value))) |
edy05 | 25:69190c222dbf | 72 | _groundPidMinOutput = atof(value); |
edy05 | 25:69190c222dbf | 73 | if (_configFile.getValue("groundPidMaxOutput", &value[0], sizeof(value))) |
edy05 | 25:69190c222dbf | 74 | _groundPidMaxOutput = atof(value); |
edy05 | 9:86a5af9935b1 | 75 | if (_configFile.getValue("P", &value[0], sizeof(value))) |
edy05 | 9:86a5af9935b1 | 76 | _P = atof(value); |
edy05 | 9:86a5af9935b1 | 77 | if (_configFile.getValue("I", &value[0], sizeof(value))) |
edy05 | 9:86a5af9935b1 | 78 | _I = atof(value); |
edy05 | 9:86a5af9935b1 | 79 | if (_configFile.getValue("D", &value[0], sizeof(value))) |
edy05 | 9:86a5af9935b1 | 80 | _D = atof(value); |
edy05 | 14:076ef843e1ba | 81 | if (_configFile.getValue("groundSetPoint", &value[0], sizeof(value))) |
edy05 | 14:076ef843e1ba | 82 | _groundSetPoint = atof(value); |
edy05 | 9:86a5af9935b1 | 83 | |
edy05 | 9:86a5af9935b1 | 84 | } |
edy05 | 9:86a5af9935b1 | 85 | |
edy05 | 11:002927b2675d | 86 | void writeSettingsToConfig(void){ |
edy05 | 25:69190c222dbf | 87 | ConvertToCharArray(_bias); |
edy05 | 25:69190c222dbf | 88 | if(!_configFile.setValue("bias", _str)) |
edy05 | 25:69190c222dbf | 89 | strcat(_serverMessage, "bias value could not be saved to configFile\n\r"); |
edy05 | 25:69190c222dbf | 90 | ConvertToCharArray(_groundPidMinOutput); |
edy05 | 25:69190c222dbf | 91 | if(!_configFile.setValue("groundPidMinOutput", _str)) |
edy05 | 25:69190c222dbf | 92 | strcat(_serverMessage, "groundPidMinOutput value could not be saved to configFile\n\r"); |
edy05 | 25:69190c222dbf | 93 | ConvertToCharArray(_groundPidMaxOutput); |
edy05 | 25:69190c222dbf | 94 | if(!_configFile.setValue("groundPidMaxOutput", _str)) |
edy05 | 25:69190c222dbf | 95 | strcat(_serverMessage, "groundPidMaxOutput value could not be saved to configFile\n\r"); |
edy05 | 10:bb9c778f8e3e | 96 | ConvertToCharArray(_P); |
edy05 | 15:9cdf757269fb | 97 | if(!_configFile.setValue("P", _str)) |
edy05 | 15:9cdf757269fb | 98 | strcat(_serverMessage, "P value could not be saved to configFile\n\r"); |
edy05 | 10:bb9c778f8e3e | 99 | ConvertToCharArray(_I); |
edy05 | 15:9cdf757269fb | 100 | if(!_configFile.setValue("I", _str)) |
edy05 | 15:9cdf757269fb | 101 | strcat(_serverMessage, "I value could not be saved to configFile\n\r"); |
edy05 | 10:bb9c778f8e3e | 102 | ConvertToCharArray(_D); |
edy05 | 15:9cdf757269fb | 103 | if(!_configFile.setValue("D", _str)) |
edy05 | 15:9cdf757269fb | 104 | strcat(_serverMessage, "D value could not be saved to configFile\n\r"); |
edy05 | 14:076ef843e1ba | 105 | ConvertToCharArray(_groundSetPoint); |
edy05 | 15:9cdf757269fb | 106 | if(!_configFile.setValue("groundSetPoint", _str)){ |
edy05 | 15:9cdf757269fb | 107 | strcat(_serverMessage, "groundSetPoint value could not be saved to configFile\n\r"); |
edy05 | 15:9cdf757269fb | 108 | pc.printf("groundSetPoint value could not be saved to configFile\n\r"); |
edy05 | 15:9cdf757269fb | 109 | } |
edy05 | 10:bb9c778f8e3e | 110 | _configFile.write("/local/config.cfg"); |
edy05 | 10:bb9c778f8e3e | 111 | |
edy05 | 10:bb9c778f8e3e | 112 | } |
edy05 | 10:bb9c778f8e3e | 113 | |
edy05 | 11:002927b2675d | 114 | |
edy05 | 11:002927b2675d | 115 | void writeErrorLog(char *message){ |
edy05 | 11:002927b2675d | 116 | FILE *fp = fopen("/local/errorlog.txt", "w"); |
edy05 | 11:002927b2675d | 117 | fprintf(fp, message); |
edy05 | 11:002927b2675d | 118 | fclose(fp); |
edy05 | 11:002927b2675d | 119 | |
edy05 | 11:002927b2675d | 120 | } |
edy05 | 11:002927b2675d | 121 | |
edy05 | 11:002927b2675d | 122 | |
edy05 | 9:86a5af9935b1 | 123 | //Converts float to char array |
edy05 | 9:86a5af9935b1 | 124 | void ConvertToCharArray(float number) |
edy05 | 9:86a5af9935b1 | 125 | { |
edy05 | 25:69190c222dbf | 126 | sprintf(_str, "%3.6f", number ); |
edy05 | 9:86a5af9935b1 | 127 | } |
edy05 | 8:b5128641d4cf | 128 | |
edy05 | 14:076ef843e1ba | 129 | //Converts integer to char array |
edy05 | 14:076ef843e1ba | 130 | void ConvertToCharArray(int number) |
edy05 | 14:076ef843e1ba | 131 | { |
edy05 | 14:076ef843e1ba | 132 | sprintf(_str, "%d", number ); |
edy05 | 14:076ef843e1ba | 133 | } |
edy05 | 14:076ef843e1ba | 134 | |
edy05 | 8:b5128641d4cf | 135 | |
edy05 | 2:d172c9963f87 | 136 | #endif |