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@41:5fe200d20022, 2018-05-22 (annotated)
- Committer:
- edy05
- Date:
- Tue May 22 19:43:09 2018 +0000
- Branch:
- DistanceRegulation
- Revision:
- 41:5fe200d20022
- Parent:
- 40:0aa1cefe80ab
final updates
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 | 40:0aa1cefe80ab | 21 | uint8_t _frontDistance; |
edy05 | 40:0aa1cefe80ab | 22 | uint8_t _leftDistance; |
edy05 | 40:0aa1cefe80ab | 23 | uint8_t _rightDistance; |
edy05 | 40:0aa1cefe80ab | 24 | uint8_t _backDistance; |
edy05 | 26:11539036f0fb | 25 | PpmRegen* _ppmRegen; |
edy05 | 11:002927b2675d | 26 | |
edy05 | 11:002927b2675d | 27 | ConfigFile _configFile; |
edy05 | 11:002927b2675d | 28 | LocalFileSystem local("local"); |
edy05 | 2:d172c9963f87 | 29 | |
edy05 | 40:0aa1cefe80ab | 30 | Semaphore _semaphore(1); |
edy05 | 40:0aa1cefe80ab | 31 | Thread _serverThread; |
edy05 | 40:0aa1cefe80ab | 32 | Thread _frontSensorThread; |
edy05 | 40:0aa1cefe80ab | 33 | Thread _leftSensorThread; |
edy05 | 40:0aa1cefe80ab | 34 | Thread _rightSensorThread; |
edy05 | 40:0aa1cefe80ab | 35 | Thread _backSensorThread; |
edy05 | 40:0aa1cefe80ab | 36 | Thread *_distanceThread; |
edy05 | 27:5956d5e3ff63 | 37 | |
edy05 | 2:d172c9963f87 | 38 | |
edy05 | 41:5fe200d20022 | 39 | InterruptIn* _interruptPin = new InterruptIn(p28); |
edy05 | 2:d172c9963f87 | 40 | PwmOut* _roll = new PwmOut(p21); |
edy05 | 2:d172c9963f87 | 41 | PwmOut* _pitch = new PwmOut(p22); |
edy05 | 2:d172c9963f87 | 42 | PwmOut* _throttle = new PwmOut(p23); |
edy05 | 2:d172c9963f87 | 43 | PwmOut* _yaw = new PwmOut(p24); |
edy05 | 2:d172c9963f87 | 44 | PwmOut* _aux1 = new PwmOut(p25); |
edy05 | 2:d172c9963f87 | 45 | PwmOut* _aux2 = new PwmOut(p26); |
edy05 | 2:d172c9963f87 | 46 | |
edy05 | 2:d172c9963f87 | 47 | |
edy05 | 9:86a5af9935b1 | 48 | char* _str = new char[1024]; |
edy05 | 15:9cdf757269fb | 49 | char* _serverMessage = new char[1024]; |
edy05 | 17:1896d242945b | 50 | bool _configChanges = false; |
edy05 | 31:5f1737e480f3 | 51 | bool _onlyDistanChanged = false; |
edy05 | 15:9cdf757269fb | 52 | // zero is default value |
edy05 | 9:86a5af9935b1 | 53 | float _P = 0; |
edy05 | 9:86a5af9935b1 | 54 | float _I = 0; |
edy05 | 9:86a5af9935b1 | 55 | float _D = 0; |
edy05 | 41:5fe200d20022 | 56 | int _groundSetPoint = 0; |
edy05 | 41:5fe200d20022 | 57 | int _bias = 0; |
edy05 | 41:5fe200d20022 | 58 | int _groundPidMinOutput = 0; |
edy05 | 41:5fe200d20022 | 59 | int _groundPidMaxOutput = 0; |
edy05 | 21:0afb91824792 | 60 | bool _groundRegulation = false; |
edy05 | 31:5f1737e480f3 | 61 | // Temporary values for Server to filter noise |
edy05 | 32:c729e6da7f9a | 62 | float _newP; |
edy05 | 32:c729e6da7f9a | 63 | float _newI; |
edy05 | 32:c729e6da7f9a | 64 | float _newD; |
edy05 | 41:5fe200d20022 | 65 | int _newGroundSetPoint; |
edy05 | 31:5f1737e480f3 | 66 | bool _tempGroundRegulation = false; |
edy05 | 38:801fee0330e2 | 67 | bool _goAhead = false; |
edy05 | 41:5fe200d20022 | 68 | bool _backWall = true; |
edy05 | 40:0aa1cefe80ab | 69 | bool _frontWall = false; |
edy05 | 31:5f1737e480f3 | 70 | |
edy05 | 8:b5128641d4cf | 71 | |
edy05 | 11:002927b2675d | 72 | |
edy05 | 11:002927b2675d | 73 | |
edy05 | 11:002927b2675d | 74 | |
edy05 | 10:bb9c778f8e3e | 75 | |
edy05 | 41:5fe200d20022 | 76 | |
edy05 | 9:86a5af9935b1 | 77 | void loadConfigFile(void){ |
edy05 | 9:86a5af9935b1 | 78 | //reading configFile |
edy05 | 9:86a5af9935b1 | 79 | _configFile.read("/local/config.cfg"); |
edy05 | 9:86a5af9935b1 | 80 | char value[BUFSIZ]; |
edy05 | 25:69190c222dbf | 81 | if (_configFile.getValue("bias", &value[0], sizeof(value))) |
edy05 | 25:69190c222dbf | 82 | _bias = atof(value); |
edy05 | 25:69190c222dbf | 83 | if (_configFile.getValue("groundPidMinOutput", &value[0], sizeof(value))) |
edy05 | 25:69190c222dbf | 84 | _groundPidMinOutput = atof(value); |
edy05 | 25:69190c222dbf | 85 | if (_configFile.getValue("groundPidMaxOutput", &value[0], sizeof(value))) |
edy05 | 25:69190c222dbf | 86 | _groundPidMaxOutput = atof(value); |
edy05 | 9:86a5af9935b1 | 87 | if (_configFile.getValue("P", &value[0], sizeof(value))) |
edy05 | 9:86a5af9935b1 | 88 | _P = atof(value); |
edy05 | 9:86a5af9935b1 | 89 | if (_configFile.getValue("I", &value[0], sizeof(value))) |
edy05 | 9:86a5af9935b1 | 90 | _I = atof(value); |
edy05 | 9:86a5af9935b1 | 91 | if (_configFile.getValue("D", &value[0], sizeof(value))) |
edy05 | 9:86a5af9935b1 | 92 | _D = atof(value); |
edy05 | 14:076ef843e1ba | 93 | if (_configFile.getValue("groundSetPoint", &value[0], sizeof(value))) |
edy05 | 14:076ef843e1ba | 94 | _groundSetPoint = atof(value); |
edy05 | 9:86a5af9935b1 | 95 | |
edy05 | 9:86a5af9935b1 | 96 | } |
edy05 | 9:86a5af9935b1 | 97 | |
edy05 | 11:002927b2675d | 98 | void writeSettingsToConfig(void){ |
edy05 | 25:69190c222dbf | 99 | ConvertToCharArray(_bias); |
edy05 | 25:69190c222dbf | 100 | if(!_configFile.setValue("bias", _str)) |
edy05 | 25:69190c222dbf | 101 | strcat(_serverMessage, "bias value could not be saved to configFile\n\r"); |
edy05 | 25:69190c222dbf | 102 | ConvertToCharArray(_groundPidMinOutput); |
edy05 | 25:69190c222dbf | 103 | if(!_configFile.setValue("groundPidMinOutput", _str)) |
edy05 | 25:69190c222dbf | 104 | strcat(_serverMessage, "groundPidMinOutput value could not be saved to configFile\n\r"); |
edy05 | 25:69190c222dbf | 105 | ConvertToCharArray(_groundPidMaxOutput); |
edy05 | 25:69190c222dbf | 106 | if(!_configFile.setValue("groundPidMaxOutput", _str)) |
edy05 | 25:69190c222dbf | 107 | strcat(_serverMessage, "groundPidMaxOutput value could not be saved to configFile\n\r"); |
edy05 | 10:bb9c778f8e3e | 108 | ConvertToCharArray(_P); |
edy05 | 15:9cdf757269fb | 109 | if(!_configFile.setValue("P", _str)) |
edy05 | 15:9cdf757269fb | 110 | strcat(_serverMessage, "P value could not be saved to configFile\n\r"); |
edy05 | 10:bb9c778f8e3e | 111 | ConvertToCharArray(_I); |
edy05 | 15:9cdf757269fb | 112 | if(!_configFile.setValue("I", _str)) |
edy05 | 15:9cdf757269fb | 113 | strcat(_serverMessage, "I value could not be saved to configFile\n\r"); |
edy05 | 10:bb9c778f8e3e | 114 | ConvertToCharArray(_D); |
edy05 | 15:9cdf757269fb | 115 | if(!_configFile.setValue("D", _str)) |
edy05 | 15:9cdf757269fb | 116 | strcat(_serverMessage, "D value could not be saved to configFile\n\r"); |
edy05 | 14:076ef843e1ba | 117 | ConvertToCharArray(_groundSetPoint); |
edy05 | 15:9cdf757269fb | 118 | if(!_configFile.setValue("groundSetPoint", _str)){ |
edy05 | 15:9cdf757269fb | 119 | strcat(_serverMessage, "groundSetPoint value could not be saved to configFile\n\r"); |
edy05 | 15:9cdf757269fb | 120 | pc.printf("groundSetPoint value could not be saved to configFile\n\r"); |
edy05 | 15:9cdf757269fb | 121 | } |
edy05 | 10:bb9c778f8e3e | 122 | _configFile.write("/local/config.cfg"); |
edy05 | 10:bb9c778f8e3e | 123 | |
edy05 | 10:bb9c778f8e3e | 124 | } |
edy05 | 10:bb9c778f8e3e | 125 | |
edy05 | 11:002927b2675d | 126 | |
edy05 | 11:002927b2675d | 127 | void writeErrorLog(char *message){ |
edy05 | 11:002927b2675d | 128 | FILE *fp = fopen("/local/errorlog.txt", "w"); |
edy05 | 11:002927b2675d | 129 | fprintf(fp, message); |
edy05 | 11:002927b2675d | 130 | fclose(fp); |
edy05 | 11:002927b2675d | 131 | |
edy05 | 11:002927b2675d | 132 | } |
edy05 | 11:002927b2675d | 133 | |
edy05 | 11:002927b2675d | 134 | |
edy05 | 9:86a5af9935b1 | 135 | //Converts float to char array |
edy05 | 9:86a5af9935b1 | 136 | void ConvertToCharArray(float number) |
edy05 | 9:86a5af9935b1 | 137 | { |
edy05 | 41:5fe200d20022 | 138 | sprintf(_str, "%3.2f", number ); |
edy05 | 9:86a5af9935b1 | 139 | } |
edy05 | 8:b5128641d4cf | 140 | |
edy05 | 14:076ef843e1ba | 141 | //Converts integer to char array |
edy05 | 14:076ef843e1ba | 142 | void ConvertToCharArray(int number) |
edy05 | 14:076ef843e1ba | 143 | { |
edy05 | 14:076ef843e1ba | 144 | sprintf(_str, "%d", number ); |
edy05 | 14:076ef843e1ba | 145 | } |
edy05 | 14:076ef843e1ba | 146 | |
edy05 | 8:b5128641d4cf | 147 | |
edy05 | 2:d172c9963f87 | 148 | #endif |