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@30:4d1b3926a3cc, 2018-01-21 (annotated)
- Committer:
- edy05
- Date:
- Sun Jan 21 17:58:41 2018 +0000
- Branch:
- DistanceRegulation
- Revision:
- 30:4d1b3926a3cc
- Parent:
- 27:5956d5e3ff63
Added pressure sensor ms5611 - not working - too slow
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 | 30:4d1b3926a3cc | 9 | #include "ms5611.h" |
edy05 | 2:d172c9963f87 | 10 | |
edy05 | 11:002927b2675d | 11 | |
edy05 | 11:002927b2675d | 12 | void loadConfigFile(void); |
edy05 | 11:002927b2675d | 13 | void writeSettingsToConfig(void); |
edy05 | 11:002927b2675d | 14 | void writeErrorLog(char *message); |
edy05 | 11:002927b2675d | 15 | void ConvertToCharArray(float number); |
edy05 | 14:076ef843e1ba | 16 | void ConvertToCharArray(int number); |
edy05 | 11:002927b2675d | 17 | |
edy05 | 26:11539036f0fb | 18 | Serial pc(USBTX, USBRX); // tx, rx |
edy05 | 26:11539036f0fb | 19 | PID* _groundDistance; |
edy05 | 27:5956d5e3ff63 | 20 | //RtosTimer *_groudRegulationUpdateTimer; |
edy05 | 26:11539036f0fb | 21 | HCSR04* _sonic; |
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 | 2:d172c9963f87 | 27 | Thread serverThread; |
edy05 | 27:5956d5e3ff63 | 28 | Thread *_distanceThread; |
edy05 | 27:5956d5e3ff63 | 29 | |
edy05 | 2:d172c9963f87 | 30 | |
edy05 | 2:d172c9963f87 | 31 | InterruptIn* _interruptPort = new InterruptIn(p28); |
edy05 | 2:d172c9963f87 | 32 | PwmOut* _roll = new PwmOut(p21); |
edy05 | 2:d172c9963f87 | 33 | PwmOut* _pitch = new PwmOut(p22); |
edy05 | 2:d172c9963f87 | 34 | PwmOut* _throttle = new PwmOut(p23); |
edy05 | 2:d172c9963f87 | 35 | PwmOut* _yaw = new PwmOut(p24); |
edy05 | 2:d172c9963f87 | 36 | PwmOut* _aux1 = new PwmOut(p25); |
edy05 | 2:d172c9963f87 | 37 | PwmOut* _aux2 = new PwmOut(p26); |
edy05 | 2:d172c9963f87 | 38 | |
edy05 | 2:d172c9963f87 | 39 | |
edy05 | 9:86a5af9935b1 | 40 | char* _str = new char[1024]; |
edy05 | 15:9cdf757269fb | 41 | char* _serverMessage = new char[1024]; |
edy05 | 17:1896d242945b | 42 | bool _configChanges = false; |
edy05 | 15:9cdf757269fb | 43 | // zero is default value |
edy05 | 9:86a5af9935b1 | 44 | float _P = 0; |
edy05 | 9:86a5af9935b1 | 45 | float _I = 0; |
edy05 | 9:86a5af9935b1 | 46 | float _D = 0; |
edy05 | 30:4d1b3926a3cc | 47 | float _groundSetPoint = 0; |
edy05 | 25:69190c222dbf | 48 | int _bias = 0; |
edy05 | 25:69190c222dbf | 49 | int _groundPidMinOutput = 0; |
edy05 | 25:69190c222dbf | 50 | int _groundPidMaxOutput = 0; |
edy05 | 21:0afb91824792 | 51 | bool _groundRegulation = false; |
edy05 | 8:b5128641d4cf | 52 | |
edy05 | 30:4d1b3926a3cc | 53 | // ms5611 - pressure senzor |
edy05 | 30:4d1b3926a3cc | 54 | Thread *_altitudeThread; |
edy05 | 30:4d1b3926a3cc | 55 | ms5611 ms(p9, p10, ms5611::CSBpin_0); |
edy05 | 30:4d1b3926a3cc | 56 | Thread *_ms5611Thread; |
edy05 | 30:4d1b3926a3cc | 57 | float _startAltitude = 0.0; |
edy05 | 30:4d1b3926a3cc | 58 | float _setPointAltitude = 0.0; |
edy05 | 30:4d1b3926a3cc | 59 | float _currentAltitude = 0.0; |
edy05 | 11:002927b2675d | 60 | |
edy05 | 11:002927b2675d | 61 | |
edy05 | 10:bb9c778f8e3e | 62 | |
edy05 | 9:86a5af9935b1 | 63 | void loadConfigFile(void){ |
edy05 | 9:86a5af9935b1 | 64 | //reading configFile |
edy05 | 9:86a5af9935b1 | 65 | _configFile.read("/local/config.cfg"); |
edy05 | 9:86a5af9935b1 | 66 | char value[BUFSIZ]; |
edy05 | 25:69190c222dbf | 67 | if (_configFile.getValue("bias", &value[0], sizeof(value))) |
edy05 | 25:69190c222dbf | 68 | _bias = atof(value); |
edy05 | 25:69190c222dbf | 69 | if (_configFile.getValue("groundPidMinOutput", &value[0], sizeof(value))) |
edy05 | 25:69190c222dbf | 70 | _groundPidMinOutput = atof(value); |
edy05 | 25:69190c222dbf | 71 | if (_configFile.getValue("groundPidMaxOutput", &value[0], sizeof(value))) |
edy05 | 25:69190c222dbf | 72 | _groundPidMaxOutput = atof(value); |
edy05 | 9:86a5af9935b1 | 73 | if (_configFile.getValue("P", &value[0], sizeof(value))) |
edy05 | 9:86a5af9935b1 | 74 | _P = atof(value); |
edy05 | 9:86a5af9935b1 | 75 | if (_configFile.getValue("I", &value[0], sizeof(value))) |
edy05 | 9:86a5af9935b1 | 76 | _I = atof(value); |
edy05 | 9:86a5af9935b1 | 77 | if (_configFile.getValue("D", &value[0], sizeof(value))) |
edy05 | 9:86a5af9935b1 | 78 | _D = atof(value); |
edy05 | 14:076ef843e1ba | 79 | if (_configFile.getValue("groundSetPoint", &value[0], sizeof(value))) |
edy05 | 14:076ef843e1ba | 80 | _groundSetPoint = atof(value); |
edy05 | 9:86a5af9935b1 | 81 | |
edy05 | 9:86a5af9935b1 | 82 | } |
edy05 | 9:86a5af9935b1 | 83 | |
edy05 | 11:002927b2675d | 84 | void writeSettingsToConfig(void){ |
edy05 | 25:69190c222dbf | 85 | ConvertToCharArray(_bias); |
edy05 | 25:69190c222dbf | 86 | if(!_configFile.setValue("bias", _str)) |
edy05 | 25:69190c222dbf | 87 | strcat(_serverMessage, "bias value could not be saved to configFile\n\r"); |
edy05 | 25:69190c222dbf | 88 | ConvertToCharArray(_groundPidMinOutput); |
edy05 | 25:69190c222dbf | 89 | if(!_configFile.setValue("groundPidMinOutput", _str)) |
edy05 | 25:69190c222dbf | 90 | strcat(_serverMessage, "groundPidMinOutput value could not be saved to configFile\n\r"); |
edy05 | 25:69190c222dbf | 91 | ConvertToCharArray(_groundPidMaxOutput); |
edy05 | 25:69190c222dbf | 92 | if(!_configFile.setValue("groundPidMaxOutput", _str)) |
edy05 | 25:69190c222dbf | 93 | strcat(_serverMessage, "groundPidMaxOutput value could not be saved to configFile\n\r"); |
edy05 | 10:bb9c778f8e3e | 94 | ConvertToCharArray(_P); |
edy05 | 15:9cdf757269fb | 95 | if(!_configFile.setValue("P", _str)) |
edy05 | 15:9cdf757269fb | 96 | strcat(_serverMessage, "P value could not be saved to configFile\n\r"); |
edy05 | 10:bb9c778f8e3e | 97 | ConvertToCharArray(_I); |
edy05 | 15:9cdf757269fb | 98 | if(!_configFile.setValue("I", _str)) |
edy05 | 15:9cdf757269fb | 99 | strcat(_serverMessage, "I value could not be saved to configFile\n\r"); |
edy05 | 10:bb9c778f8e3e | 100 | ConvertToCharArray(_D); |
edy05 | 15:9cdf757269fb | 101 | if(!_configFile.setValue("D", _str)) |
edy05 | 15:9cdf757269fb | 102 | strcat(_serverMessage, "D value could not be saved to configFile\n\r"); |
edy05 | 14:076ef843e1ba | 103 | ConvertToCharArray(_groundSetPoint); |
edy05 | 15:9cdf757269fb | 104 | if(!_configFile.setValue("groundSetPoint", _str)){ |
edy05 | 15:9cdf757269fb | 105 | strcat(_serverMessage, "groundSetPoint value could not be saved to configFile\n\r"); |
edy05 | 15:9cdf757269fb | 106 | pc.printf("groundSetPoint value could not be saved to configFile\n\r"); |
edy05 | 15:9cdf757269fb | 107 | } |
edy05 | 10:bb9c778f8e3e | 108 | |
edy05 | 10:bb9c778f8e3e | 109 | _configFile.write("/local/config.cfg"); |
edy05 | 10:bb9c778f8e3e | 110 | |
edy05 | 17:1896d242945b | 111 | _configChanges = true; |
edy05 | 13:33024b5880b3 | 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 |