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@40:0aa1cefe80ab, 2018-05-15 (annotated)
- Committer:
- edy05
- Date:
- Tue May 15 10:34:35 2018 +0000
- Branch:
- DistanceRegulation
- Revision:
- 40:0aa1cefe80ab
- Parent:
- 39:93d8aa47f4ce
- Child:
- 41:5fe200d20022
some updates; sonics added
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 | 2:d172c9963f87 | 39 | InterruptIn* _interruptPort = 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 | 35:b09e19c855f6 | 56 | float _groundSetPoint = 0; |
edy05 | 35:b09e19c855f6 | 57 | float _bias = 0; |
edy05 | 35:b09e19c855f6 | 58 | float _groundPidMinOutput = 0; |
edy05 | 35:b09e19c855f6 | 59 | float _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 | 39:93d8aa47f4ce | 65 | float _newGroundSetPoint; |
edy05 | 31:5f1737e480f3 | 66 | bool _tempGroundRegulation = false; |
edy05 | 38:801fee0330e2 | 67 | bool _goAhead = false; |
edy05 | 40:0aa1cefe80ab | 68 | bool _frontWall = false; |
edy05 | 31:5f1737e480f3 | 69 | |
edy05 | 8:b5128641d4cf | 70 | |
edy05 | 11:002927b2675d | 71 | |
edy05 | 11:002927b2675d | 72 | |
edy05 | 11:002927b2675d | 73 | |
edy05 | 10:bb9c778f8e3e | 74 | |
edy05 | 9:86a5af9935b1 | 75 | void loadConfigFile(void){ |
edy05 | 9:86a5af9935b1 | 76 | //reading configFile |
edy05 | 9:86a5af9935b1 | 77 | _configFile.read("/local/config.cfg"); |
edy05 | 9:86a5af9935b1 | 78 | char value[BUFSIZ]; |
edy05 | 25:69190c222dbf | 79 | if (_configFile.getValue("bias", &value[0], sizeof(value))) |
edy05 | 25:69190c222dbf | 80 | _bias = atof(value); |
edy05 | 25:69190c222dbf | 81 | if (_configFile.getValue("groundPidMinOutput", &value[0], sizeof(value))) |
edy05 | 25:69190c222dbf | 82 | _groundPidMinOutput = atof(value); |
edy05 | 25:69190c222dbf | 83 | if (_configFile.getValue("groundPidMaxOutput", &value[0], sizeof(value))) |
edy05 | 25:69190c222dbf | 84 | _groundPidMaxOutput = atof(value); |
edy05 | 9:86a5af9935b1 | 85 | if (_configFile.getValue("P", &value[0], sizeof(value))) |
edy05 | 9:86a5af9935b1 | 86 | _P = atof(value); |
edy05 | 9:86a5af9935b1 | 87 | if (_configFile.getValue("I", &value[0], sizeof(value))) |
edy05 | 9:86a5af9935b1 | 88 | _I = atof(value); |
edy05 | 9:86a5af9935b1 | 89 | if (_configFile.getValue("D", &value[0], sizeof(value))) |
edy05 | 9:86a5af9935b1 | 90 | _D = atof(value); |
edy05 | 14:076ef843e1ba | 91 | if (_configFile.getValue("groundSetPoint", &value[0], sizeof(value))) |
edy05 | 14:076ef843e1ba | 92 | _groundSetPoint = atof(value); |
edy05 | 9:86a5af9935b1 | 93 | |
edy05 | 9:86a5af9935b1 | 94 | } |
edy05 | 9:86a5af9935b1 | 95 | |
edy05 | 11:002927b2675d | 96 | void writeSettingsToConfig(void){ |
edy05 | 25:69190c222dbf | 97 | ConvertToCharArray(_bias); |
edy05 | 25:69190c222dbf | 98 | if(!_configFile.setValue("bias", _str)) |
edy05 | 25:69190c222dbf | 99 | strcat(_serverMessage, "bias value could not be saved to configFile\n\r"); |
edy05 | 25:69190c222dbf | 100 | ConvertToCharArray(_groundPidMinOutput); |
edy05 | 25:69190c222dbf | 101 | if(!_configFile.setValue("groundPidMinOutput", _str)) |
edy05 | 25:69190c222dbf | 102 | strcat(_serverMessage, "groundPidMinOutput value could not be saved to configFile\n\r"); |
edy05 | 25:69190c222dbf | 103 | ConvertToCharArray(_groundPidMaxOutput); |
edy05 | 25:69190c222dbf | 104 | if(!_configFile.setValue("groundPidMaxOutput", _str)) |
edy05 | 25:69190c222dbf | 105 | strcat(_serverMessage, "groundPidMaxOutput value could not be saved to configFile\n\r"); |
edy05 | 10:bb9c778f8e3e | 106 | ConvertToCharArray(_P); |
edy05 | 15:9cdf757269fb | 107 | if(!_configFile.setValue("P", _str)) |
edy05 | 15:9cdf757269fb | 108 | strcat(_serverMessage, "P value could not be saved to configFile\n\r"); |
edy05 | 10:bb9c778f8e3e | 109 | ConvertToCharArray(_I); |
edy05 | 15:9cdf757269fb | 110 | if(!_configFile.setValue("I", _str)) |
edy05 | 15:9cdf757269fb | 111 | strcat(_serverMessage, "I value could not be saved to configFile\n\r"); |
edy05 | 10:bb9c778f8e3e | 112 | ConvertToCharArray(_D); |
edy05 | 15:9cdf757269fb | 113 | if(!_configFile.setValue("D", _str)) |
edy05 | 15:9cdf757269fb | 114 | strcat(_serverMessage, "D value could not be saved to configFile\n\r"); |
edy05 | 14:076ef843e1ba | 115 | ConvertToCharArray(_groundSetPoint); |
edy05 | 15:9cdf757269fb | 116 | if(!_configFile.setValue("groundSetPoint", _str)){ |
edy05 | 15:9cdf757269fb | 117 | strcat(_serverMessage, "groundSetPoint value could not be saved to configFile\n\r"); |
edy05 | 15:9cdf757269fb | 118 | pc.printf("groundSetPoint value could not be saved to configFile\n\r"); |
edy05 | 15:9cdf757269fb | 119 | } |
edy05 | 10:bb9c778f8e3e | 120 | _configFile.write("/local/config.cfg"); |
edy05 | 10:bb9c778f8e3e | 121 | |
edy05 | 10:bb9c778f8e3e | 122 | } |
edy05 | 10:bb9c778f8e3e | 123 | |
edy05 | 11:002927b2675d | 124 | |
edy05 | 11:002927b2675d | 125 | void writeErrorLog(char *message){ |
edy05 | 11:002927b2675d | 126 | FILE *fp = fopen("/local/errorlog.txt", "w"); |
edy05 | 11:002927b2675d | 127 | fprintf(fp, message); |
edy05 | 11:002927b2675d | 128 | fclose(fp); |
edy05 | 11:002927b2675d | 129 | |
edy05 | 11:002927b2675d | 130 | } |
edy05 | 11:002927b2675d | 131 | |
edy05 | 11:002927b2675d | 132 | |
edy05 | 9:86a5af9935b1 | 133 | //Converts float to char array |
edy05 | 9:86a5af9935b1 | 134 | void ConvertToCharArray(float number) |
edy05 | 9:86a5af9935b1 | 135 | { |
edy05 | 25:69190c222dbf | 136 | sprintf(_str, "%3.6f", number ); |
edy05 | 9:86a5af9935b1 | 137 | } |
edy05 | 8:b5128641d4cf | 138 | |
edy05 | 14:076ef843e1ba | 139 | //Converts integer to char array |
edy05 | 14:076ef843e1ba | 140 | void ConvertToCharArray(int number) |
edy05 | 14:076ef843e1ba | 141 | { |
edy05 | 14:076ef843e1ba | 142 | sprintf(_str, "%d", number ); |
edy05 | 14:076ef843e1ba | 143 | } |
edy05 | 14:076ef843e1ba | 144 | |
edy05 | 8:b5128641d4cf | 145 | |
edy05 | 2:d172c9963f87 | 146 | #endif |