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:
Sun Feb 04 15:21:12 2018 +0000
Branch:
DistanceRegulation
Revision:
31:5f1737e480f3
Parent:
27:5956d5e3ff63
Child:
32:c729e6da7f9a
Better Server respond to ground regulation

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 26:11539036f0fb 21 PpmRegen* _ppmRegen;
edy05 11:002927b2675d 22
edy05 11:002927b2675d 23 ConfigFile _configFile;
edy05 11:002927b2675d 24 LocalFileSystem local("local");
edy05 2:d172c9963f87 25
edy05 2:d172c9963f87 26 Thread serverThread;
edy05 27:5956d5e3ff63 27 Thread *_distanceThread;
edy05 27:5956d5e3ff63 28
edy05 2:d172c9963f87 29
edy05 2:d172c9963f87 30 InterruptIn* _interruptPort = new InterruptIn(p28);
edy05 2:d172c9963f87 31 PwmOut* _roll = new PwmOut(p21);
edy05 2:d172c9963f87 32 PwmOut* _pitch = new PwmOut(p22);
edy05 2:d172c9963f87 33 PwmOut* _throttle = new PwmOut(p23);
edy05 2:d172c9963f87 34 PwmOut* _yaw = new PwmOut(p24);
edy05 2:d172c9963f87 35 PwmOut* _aux1 = new PwmOut(p25);
edy05 2:d172c9963f87 36 PwmOut* _aux2 = new PwmOut(p26);
edy05 2:d172c9963f87 37
edy05 2:d172c9963f87 38
edy05 9:86a5af9935b1 39 char* _str = new char[1024];
edy05 15:9cdf757269fb 40 char* _serverMessage = new char[1024];
edy05 17:1896d242945b 41 bool _configChanges = false;
edy05 31:5f1737e480f3 42 bool _onlyDistanChanged = 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 14:076ef843e1ba 47 int _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 31:5f1737e480f3 52 // Temporary values for Server to filter noise
edy05 31:5f1737e480f3 53 float _tempP;
edy05 31:5f1737e480f3 54 float _tempI;
edy05 31:5f1737e480f3 55 float _tempD;
edy05 31:5f1737e480f3 56 bool _tempGroundRegulation = false;
edy05 31:5f1737e480f3 57
edy05 8:b5128641d4cf 58
edy05 11:002927b2675d 59
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 31:5f1737e480f3 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