1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| #include <MPU6050_tockn.h> #include <Wire.h> #include <WiFi.h> #include <PID_v1.h>
const char *ssid = "BJZQ"; const char *password = "12345678"; MPU6050 mpu6050(Wire); WiFiServer server(80);
double OutPutA, OutPutB; //角度环 double DegGoal = 1.5, DegIn, DegOut, Degkp = 50.0,Degki = 1.0,Degkd = 0.5; PID DegPID(&DegIn, &DegOut, &DegGoal, Degkp, Degki, Degkd, DIRECT); //转向控制 double DirIn, DirOut, DirGoal = 0.0, Dirkp = 3.0, Dirki = 0.0, Dirkd = 0.3; PID DirPID(&DirIn, &DirOut, &DirGoal, Dirkp, Dirki, Dirkd, DIRECT); //速度环 double SpdIn, SpdOut, SpdGoal = 0.0, Spdkp = 0.0001, Spdki = 0.0001, Spdkd = 0.00005; PID SpdPID(&SpdIn, &SpdOut, &SpdGoal, Spdkp, Spdki, Spdkd, DIRECT);
void setup() { pinMode(27, OUTPUT); pinMode(14, OUTPUT); pinMode(32, OUTPUT); pinMode(33, OUTPUT); pinMode(25, OUTPUT); pinMode(26, OUTPUT); digitalWrite(32, HIGH); digitalWrite(33, HIGH); digitalWrite(25, HIGH); digitalWrite(26, HIGH); ledcSetup(8, 100, 8); ledcAttachPin(27, 8); ledcWrite(8, 120); ledcSetup(6, 100, 8); ledcAttachPin(14, 6); ledcWrite(6, 120); Serial.begin(115200); Wire.begin(); mpu6050.begin(); mpu6050.calcGyroOffsets(true); xTaskCreatePinnedToCore(Task1code, "pid_output", 10000, NULL, 1, NULL, 0); }
void Task1code( void * parameter) {
DirPID.SetOutputLimits(-63, 63); DirPID.SetSampleTime(20); DirPID.SetMode(AUTOMATIC);
DegPID.SetOutputLimits(-127, 127); DegPID.SetSampleTime(20); DegPID.SetMode(AUTOMATIC);
SpdPID.SetOutputLimits(-1, 1); SpdPID.SetSampleTime(15); SpdPID.SetMode(AUTOMATIC); while (true){ mpu6050.update(); DegIn = mpu6050.getAngleY(); DirIn = mpu6050.getAngleZ(); DegPID.Compute(); DirPID.Compute(); if (DegOut<100&&DegOut>-100){ DegGoal = DegGoal - SpdOut; SpdIn = DegOut; SpdPID.Compute(); } OutPutA = DegOut - DirOut; OutPutB = DegOut + DirOut; if (OutPutA>=0){ digitalWrite(32, LOW); digitalWrite(33, HIGH); ledcWrite(6, OutPutA); }else{ digitalWrite(32, HIGH); digitalWrite(33, LOW); ledcWrite(6, -OutPutA); } if (OutPutB>=0){ digitalWrite(25, LOW); digitalWrite(26, HIGH); ledcWrite(8, OutPutB); }else{ digitalWrite(25, HIGH); digitalWrite(26, LOW); ledcWrite(8, -OutPutB); } } }
void loop() { mpu6050.update(); Serial.print(mpu6050.getAngleX()); Serial.print(','); Serial.print(mpu6050.getAngleY()); Serial.print(','); Serial.print(mpu6050.getAngleZ()); Serial.print(','); Serial.print(DegOut); Serial.print(','); Serial.print(DegGoal); Serial.print(','); Serial.print(Degkp); Serial.print(','); Serial.print(Degki); Serial.print(','); Serial.print(Degkd); Serial.print('\n'); if (Serial.available()){ char c = Serial.read(); if (c=='p'){ String readBuff; unsigned long start_time = millis(); while(true){ char c = Serial.read(); if (c=='\n'||millis()-start_time>100)break; readBuff += c; } Degkp = readBuff.toDouble(); readBuff = ""; } if (c=='i'){ String readBuff; unsigned long start_time = millis(); while(true){ char c = Serial.read(); if (c=='\n'||millis()-start_time>100)break; readBuff += c; } Degki = readBuff.toDouble(); readBuff = ""; } if (c=='d'){ String readBuff; unsigned long start_time = millis(); while(true){ char c = Serial.read(); if (c=='\n'||millis()-start_time>100)break; readBuff += c; } Degkd = readBuff.toDouble(); readBuff = ""; } DegPID.SetTunings(Degkp,Degki,Degkd); if (c=='D'){ String readBuff; unsigned long start_time = millis(); while(true){ char c = Serial.read(); if (c=='\n'||millis()-start_time>100)break; readBuff += c; } DirGoal = readBuff.toDouble(); readBuff = ""; } } }
|