两轮平衡小车心得(十四)在线修改PID

  1. 11.1V的电池供电和DC9V供电不能用同一个PID,否则动力不足;
  2. 分别调整合适的11.1V和DC9V的PID,记录成两组参数,中间的电压用线性插值法,
  3. 最好是在网页中写入本次供电电压,实时计算出PID
  4. 全部代码:
#include <PID_v1.h>
#include <MPU6050_tockn.h>
#include <Wire.h>
#include <Arduino_FreeRTOS.h>
#include <WiFiS3.h>
#include "arduino_secrets.h" 

char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;        // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;                 // your network key index number (needed only for WEP)

int STBY = 8;        //使能端
int PWMA = 9;        //右电机PWM输出控制脚   
int AIN1 = 7;        //右电机正极  
int AIN2 = 6;        //右电机负极  
int PWMB = 10;       //左电机PWM输出控制脚
int BIN1 = 13;       //左电机正极 
int BIN2 = 12;       //左电机负极  
#define PinA_left 4  //中断 
#define PinA_right 2 //中断1
int count_right  = 0, count_left = 0;
double leftSpeed = 0, rightSpeed = 0, speed = 0, angleX = 0,leftOutput= 0, rightOutput= 0;;
double zeroAngle = -5;                                //小车初始平衡角度,需要微调
unsigned long now = 0, past = 0, interval = 40;       //interval为小车调整姿态的时长,1000为一秒,40毫秒最佳
double VIN = 9;                                    //输入电压:电池或DC电源电压

// PID控制器对象
double angleKp, angleKi, angleKd;                     //角度环参数Kp,Ki,Kd
double speedKp, speedKi, speedKd;                     //速度环参数Kp,Ki,Kd
double turnKp, turnKi, turnKd;                        //转向环参数Kp,Ki,Kd
double angleInput = 0, angleOutput = 0,speedInput = 0, speedOutput = 0,turnInput = 0, turnOutput = 0;
double angleSetpoint = 0;                                                           // 角度设定值
double speedSetpoint = 0;                                                           // 速度设定值,正为向前,负为向后,绝对值不要超过3
double turnSetpoint = 0;                                                            // 转向设定值,正为左,负为右,调整幅度±1
PID anglePID(&angleInput, &angleOutput, &angleSetpoint, angleKp, angleKi, angleKd, DIRECT);
PID speedPID(&speedInput, &speedOutput, &speedSetpoint, speedKp, speedKi, speedKd, DIRECT);
PID turnPID(&turnInput, &turnOutput, &turnSetpoint, turnKp, turnKi, turnKd, DIRECT);

// MPU6050实例化
MPU6050 mpu6050(Wire);

// 任务句柄
TaskHandle_t TaskGetAngleXHandle = NULL;
TaskHandle_t TaskGetSpeedHandle = NULL;
TaskHandle_t TaskUpdateAndAdjustMotorsHandle = NULL;
TaskHandle_t TaskWebServerHandle = NULL;

// Web服务器实例
WiFiServer server(80);
bool webServerEnabled = true;  // 默认情况下不启用网页服务器任务

void setup() {
  pinMode(STBY, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
  pinMode(PinA_left, INPUT);  //测速码盘输入
  pinMode(PinA_right, INPUT);
  attachInterrupt(digitalPinToInterrupt(PinA_left), Code_left, CHANGE);
  attachInterrupt(digitalPinToInterrupt(PinA_right), Code_right, CHANGE);
  Serial.begin(9600);
  Wire.begin();
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);

  // 初始化PID控制器
  setPIDParameters(VIN);
  anglePID.SetMode(AUTOMATIC);
  anglePID.SetSampleTime(interval);
  anglePID.SetOutputLimits(-254, 254);

  speedPID.SetMode(AUTOMATIC);
  speedPID.SetSampleTime(interval);
  speedPID.SetOutputLimits(-254, 254);

  turnPID.SetMode(AUTOMATIC);
  turnPID.SetSampleTime(interval);
  turnPID.SetOutputLimits(-254, 254);

  // 连接Wi-Fi
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  // 启动Web服务器
  server.begin();

  // 创建任务
  xTaskCreate(
    TaskGetAngleX,                // 任务函数
    "GetAngleX",                  // 任务名称
    128,                          // 任务堆栈大小
    NULL,                         // 传递给任务的参数
    4,                            // 任务优先级
    &TaskGetAngleXHandle          // 任务句柄
  );

  xTaskCreate(
    TaskGetSpeed,                 // 任务函数
    "GetSpeed",                   // 任务名称
    128,                          // 任务堆栈大小
    NULL,                         // 传递给任务的参数
    3,                            // 任务优先级
    &TaskGetSpeedHandle           // 任务句柄
  );

  xTaskCreate(
    TaskUpdateAndAdjustMotors,    // 任务函数
    "UpdateAndAdjustMotors",      // 任务名称
    128,                          // 任务堆栈大小
    NULL,                         // 传递给任务的参数
    2,                            // 任务优先级
    &TaskUpdateAndAdjustMotorsHandle // 任务句柄
  );

  xTaskCreate(
    TaskWebServer,                // 任务函数
    "WebServer",                  // 任务名称
    1024,                          // 任务堆栈大小
    NULL,                         // 传递给任务的参数
    1,                            // 任务优先级
    &TaskWebServerHandle          // 任务句柄
  );

  // 启动调度器
  vTaskStartScheduler();
}

void loop() {
  // 主循环不需要做任何事情,FreeRTOS 会管理任务
}

double mapFloat(double x, double in_min, double in_max, double out_min, double out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

//按输入电压值线性插值法计算动态PID
void setPIDParameters(double VIN) {
  // 定义 12V 和 9V 下的 PID 参数
  double angleKp_12V = 5, angleKi_12V = 0.01, angleKd_12V = 0.2;
  double speedKp_12V = 5, speedKi_12V = 0.4, speedKd_12V = 0.1;
  double turnKp_12V = 0.3, turnKi_12V = 0.01, turnKd_12V = 0.02;

  double angleKp_9V = 7, angleKi_9V = 0.01, angleKd_9V = 0.2;
  double speedKp_9V = 7, speedKi_9V = 0.4, speedKd_9V = 0.1;
  double turnKp_9V = 0.3, turnKi_9V = 0.01, turnKd_9V = 0.02;

  // 使用 mapFloat 函数进行线性插值
  angleKp = mapFloat(VIN, 9, 12, angleKp_9V, angleKp_12V);
  angleKi = mapFloat(VIN, 9, 12, angleKi_9V, angleKi_12V);
  angleKd = mapFloat(VIN, 9, 12, angleKd_9V, angleKd_12V);

  speedKp = mapFloat(VIN, 9, 12, speedKp_9V, speedKp_12V);
  speedKi = mapFloat(VIN, 9, 12, speedKi_9V, speedKi_12V);
  speedKd = mapFloat(VIN, 9, 12, speedKd_9V, speedKd_12V);

  turnKp = mapFloat(VIN, 9, 12, turnKp_9V, turnKp_12V);
  turnKi = mapFloat(VIN, 9, 12, turnKi_9V, turnKi_12V);
  turnKd = mapFloat(VIN, 9, 12, turnKd_9V, turnKd_12V);

  // 更新 PID 控制器的参数
  anglePID.SetTunings(angleKp, angleKi, angleKd);
  speedPID.SetTunings(speedKp, speedKi, speedKd);
  turnPID.SetTunings(turnKp, turnKi, turnKd);
}

void runset(int motor, int speed, int direction) {
  digitalWrite(STBY, 1);
  if (motor == 1 && direction == 1) {
    digitalWrite(AIN1, 1);
    digitalWrite(AIN2, 0);
    analogWrite(PWMA, speed);
  }
  if (motor == 2 && direction == 1) {
    digitalWrite(BIN1, 1);
    digitalWrite(BIN2, 0);
    analogWrite(PWMB, speed);
  }
  if (motor == 1 && direction == 0) {
    digitalWrite(AIN1, 0);
    digitalWrite(AIN2, 1);
    analogWrite(PWMA, speed);
  }
  if (motor == 2 && direction == 0) {
    digitalWrite(BIN1, 0);
    digitalWrite(BIN2, 1);
    analogWrite(PWMB, speed);
  }
}

void stop() {
  digitalWrite(STBY, LOW);
}

void Code_left() {
  count_left++;
} //左测速码盘计数

void Code_right() {
  count_right++;
} //右测速码盘计数

void TaskGetAngleX(void *pvParameters) {
  for (;;) {
    mpu6050.update();
    angleX = mpu6050.getAngleX() - zeroAngle;
    vTaskDelay(pdMS_TO_TICKS(interval));  // 延迟一段时间
  }
}

// 任务函数:获取速度
void TaskGetSpeed(void *pvParameters) {
  for (;;) {
    rightSpeed = (digitalRead(AIN1) == 1 && digitalRead(AIN2) == 0) ? count_right : -(count_right);
    leftSpeed = (digitalRead(BIN1) == 1 && digitalRead(BIN2) == 0) ? count_left : -(count_left);
    speed = (leftSpeed + rightSpeed) / 2;
    count_left = 0;
    count_right = 0;
    vTaskDelay(pdMS_TO_TICKS(interval));  // 延迟一段时间
  }
}

// 任务函数:更新和调整电机
void TaskUpdateAndAdjustMotors(void *pvParameters) {
  for (;;) {
    angleInput = angleX;
    speedInput = speed;
    turnInput = leftSpeed - rightSpeed;

    // 计算PID输出
    anglePID.Compute();
    speedPID.Compute();
    turnPID.Compute();

    // 计算电机输出
    if(abs(angleX)>25){
      leftOutput  = 0;
      rightOutput = 0;
    }
    leftOutput = angleOutput - speedOutput - turnOutput;
    leftOutput = constrain(leftOutput, -254, 254);
    rightOutput = angleOutput - speedOutput + turnOutput;
    rightOutput = constrain(rightOutput, -254, 254);

    // 调整电机动作
    runset(2, abs((int)leftOutput), leftOutput > 0 ? 1 : 0);
    runset(1, abs((int)rightOutput), rightOutput > 0 ? 1 : 0);
    vTaskDelay(pdMS_TO_TICKS(interval));  // 延迟一段时间
  }
}


void TaskWebServer(void *pvParameters) {
  for (;;) {
    WiFiClient client = server.available();
    if (client) {                             // if you get a client,
      Serial.println("new client");           // print a message out the serial port
      String currentLine = "";                // make a String to hold incoming data from the client
      while (client.connected()) {            // loop while the client's connected
        if (client.available()) {             // if there's bytes to read from the client,
          char c = client.read();             // read a byte, then
          Serial.write(c);                    // print it out to the serial monitor
          if (c == '\n') {                    // if the byte is a newline character

            // if the current line is blank, you got two newline characters in a row.
            // that's the end of the client HTTP request, so send a response:
            if (currentLine.length() == 0) {
              // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
              // and a content-type so the client knows what's coming, then a blank line:
              client.println("HTTP/1.1 200 OK");
              client.println("Content-type:text/html; charset=UTF-8");
              client.println();

              // the content of the HTTP response follows the header:

              client.println("<html><head><title>Control Panel</title><style>");
              client.println("body { text-align: center; font-family: Arial, sans-serif; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; flex-direction: column; }");
              client.println(".button { display: inline-block; width: 80px; height: 80px; border-radius: 50%; background-color: blue; color: white; text-align: center; line-height: 50px; text-decoration: none; margin: 5px; }");
              client.println(".top-div { margin-bottom: 20px; }"); // 添加间距
              client.println("</style></head><body>");
              client.println("<div class='top-div'>");
              client.print("<p style=\"font-size:4vw;\">speedSetpoint: "+ String(speedSetpoint) +"<br></p>");
              client.print("<p style=\"font-size:4vw;\">turnSetpoint: "+ String(turnSetpoint) +"<br></p>");
              client.println("</div>");
              client.println("<div>");
              client.print("<a href=\"/forward\" class='button'>向前</a> <br>");
              client.println("<div style='display: flex; justify-content: center;'>");
              client.print("<a href=\"/left\" class='button'>向左</a> <br>");
              client.print("<a href=\"/stop\" class='button'>停</a> <br>");
              client.print("<a href=\"/right\" class='button'>向右</a> <br>");
              client.println("</div>");
              client.print("<a href=\"/backward\" class='button'>向后</a> <br></p>");
              client.println("</div>");
              client.println("<div>");
              client.print("<a href=\"/debug\">debug</a>");
              client.println("</div>");
              client.println("</body></html>");

              // The HTTP response ends with another blank line:
              client.println();
              // break out of the while loop:
              break;
            } else {    // if you got a newline, then clear currentLine:
              currentLine = "";
            }
          } else if (c != '\r') {  // if you got anything else but a carriage return character,
            currentLine += c;      // add it to the end of the currentLine
          }

          // Check to see if the client request was "GET /H" or "GET /L":
          if (currentLine.endsWith("GET /forward")) {
            speedSetpoint += 1;
          }
          if (currentLine.endsWith("GET /backward")) {
            speedSetpoint -= 1;
          }
          if (currentLine.endsWith("GET /left")) {
            turnSetpoint += 1;
          }
          if (currentLine.endsWith("GET /right")) {
            turnSetpoint -= 1;
          }
          if (currentLine.endsWith("GET /stop")) {
            speedSetpoint = 0;
            turnSetpoint = 0;
          }
          if (currentLine.endsWith("GET /debug")) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html; charset=UTF-8");
            client.println();

            // the content of the HTTP response follows the header:
            client.println("<html><head><title>car control</title><style>");
            client.println("body { text-align: center; font-family: Arial, sans-serif; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; flex-direction: column; }");
            client.println("table { width: 50%; border-collapse: collapse; margin: 20px 0; }");
            client.println("th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }");
            client.println("th { background-color: #f2f2f2; }");
            client.println(".button { display: inline-block; width: 30px; height: 30px; border-radius: 50%; background-color: blue; color: white; text-align: center; line-height: 28px; text-decoration: none; margin: 5px; }");
            client.println("</style></head><body>");

            // 실시간 데이터 표
            client.println("<h2>实时参数</h2>");
            client.println("<table>");
            client.println("<tr><th>参数名</th><th>值</th></tr>");
            client.println("<tr><td>leftSpeed</td><td>" + String(leftSpeed) +"</td></tr>");
            client.println("<tr><td>rightSpeed</td><td>" + String(rightSpeed) +"</td></tr>");
            client.println("<tr><td>speed</td><td>" + String(speed)+"</td></tr>");
            client.println("<tr><td>angleX</td><td>" + String(angleX)+"</td> </tr>");
            client.println("<tr><td>leftOutput</td><td>" + String(leftOutput)+"</td> </tr>");
            client.println("<tr><td>rightOutput</td><td>" + String(rightOutput)+"</td></tr>");
            
            client.println("</td></tr>");
            client.println("</table>");

            // PID 参数及原点
            client.println("<h2>PID 参数及原点</h2>");
            client.println("<table>");
            client.println("<tr><th>参数名</th><th>值</th><th>增减</th></tr>");
            client.println("<tr><td>angleKp</td><td>" + String(angleKp) + "</td><td>");
            client.print("<a href=\"/increaseAngleKp\" class='button'>+</a>");
            client.print("<a href=\"/decreaseAngleKp\" class='button'>-</a>");
            client.println("</td></tr>");
            client.println("<tr><td>angleKi</td><td>" + String(angleKi) + "</td><td>");
            client.print("<a href=\"/increaseAngleKi\" class='button'>+</a>");
            client.print("<a href=\"/decreaseAngleKi\" class='button'>-</a>");
            client.println("</td></tr>");
            client.println("<tr><td>angleKd</td><td>" + String(angleKd) + "</td><td>");
            client.print("<a href=\"/increaseAngleKd\" class='button'>+</a>");
            client.print("<a href=\"/decreaseAngleKd\" class='button'>-</a>");
            client.println("</td></tr>");
            client.println("<tr><td>speedKp</td><td>" + String(speedKp) + "</td><td>");
            client.print("<a href=\"/increaseSpeedKp\" class='button'>+</a>");
            client.print("<a href=\"/decreaseSpeedKp\" class='button'>-</a>");
            client.println("</td></tr>");
            client.println("<tr><td>speedKi</td><td>" + String(speedKi) + "</td><td>");
            client.print("<a href=\"/increaseSpeedKi\" class='button'>+</a>");
            client.print("<a href=\"/decreaseSpeedKi\" class='button'>-</a>");
            client.println("</td></tr>");
            client.println("<tr><td>speedKd</td><td>" + String(speedKd) + "</td><td>");
            client.print("<a href=\"/increaseSpeedKd\" class='button'>+</a>");
            client.print("<a href=\"/decreaseSpeedKd\" class='button'>-</a>");
            client.println("</td></tr>");
            client.println("<tr><td>turnKp</td><td>" + String(turnKp) + "</td><td>");
            client.print("<a href=\"/increaseTurnKp\" class='button'>+</a>");
            client.print("<a href=\"/decreaseTurnKp\" class='button'>-</a>");
            client.println("</td></tr>");
            client.println("<tr><td>turnKi</td><td>" + String(turnKi) + "</td><td>");
            client.print("<a href=\"/increaseTurnKi\" class='button'>+</a>");
            client.print("<a href=\"/decreaseTurnKi\" class='button'>-</a>");
            client.println("</td></tr>");
            client.println("<tr><td>turnKd</td><td>" + String(turnKd) + "</td><td>");
            client.print("<a href=\"/increaseTurnKd\" class='button'>+</a>");
            client.print("<a href=\"/decreaseTurnKd\" class='button'>-</a>");
            client.println("<tr><td>zeroAngle</td><td>" + String(zeroAngle) + "</td><td>");
            client.print("<a href=\"/increaseZeroAngle\" class='button'>+</a>");
            client.print("<a href=\"/decreaseZeroAngle\" class='button'>-</a>");
            client.println("</td></tr>");
            client.println("</table>");

            client.println("<div>");
            client.print("<a href=\"/\">car control</a>");
            client.println("</div>");
            client.println("</body></html>");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          if (currentLine.endsWith("GET /increaseZeroAngle")) {
            zeroAngle += 1;
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /decreaseZeroAngle")) {
            zeroAngle -= 1;
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /increaseAngleKp")) {
            angleKp += 0.1;
            anglePID.SetTunings(angleKp, angleKi, angleKd);
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /decreaseAngleKp")) {
            angleKp -= 0.1;
            anglePID.SetTunings(angleKp, angleKi, angleKd);
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /increaseAngleKi")) {
            angleKi += 0.1;
            anglePID.SetTunings(angleKp, angleKi, angleKd);
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /decreaseAngleKi")) {
            angleKi -= 0.1;
            anglePID.SetTunings(angleKp, angleKi, angleKd);
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /increaseAngleKd")) {
            angleKd += 0.1;
            anglePID.SetTunings(angleKp, angleKi, angleKd);
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /decreaseAngleKd")) {
            angleKd -= 0.1;
            anglePID.SetTunings(angleKp, angleKi, angleKd);
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /increaseSpeedKp")) {
            speedKp += 0.1;
            speedPID.SetTunings(speedKp, speedKi, speedKd);
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /decreaseSpeedKp")) {
            speedKp -= 0.1;
            speedPID.SetTunings(speedKp, speedKi, speedKd);
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /increaseSpeedKi")) {
            speedKi += 0.1;
            speedPID.SetTunings(speedKp, speedKi, speedKd);
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /decreaseSpeedKi")) {
            speedKi -= 0.1;
            speedPID.SetTunings(speedKp, speedKi, speedKd);
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /increaseSpeedKd")) {
            speedKd += 0.1;
            speedPID.SetTunings(speedKp, speedKi, speedKd);
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /decreaseSpeedKd")) {
            speedKd -= 0.1;
            speedPID.SetTunings(speedKp, speedKi, speedKd);
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /increaseTurnKp")) {
            turnKp += 0.1;
            turnPID.SetTunings(turnKp, turnKi, turnKd);
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /decreaseTurnKp")) {
            turnKp -= 0.1;
            turnPID.SetTunings(turnKp, turnKi, turnKd);
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /increaseTurnKi")) {
            turnKi += 0.1;
            turnPID.SetTunings(turnKp, turnKi, turnKd);
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /decreaseTurnKi")) {
            turnKi -= 0.1;
            turnPID.SetTunings(turnKp, turnKi, turnKd);
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /increaseTurnKd")) {
            turnKd += 0.1;
            turnPID.SetTunings(turnKp, turnKi, turnKd);
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
          if (currentLine.endsWith("GET /decreaseTurnKd")) {
            turnKd -= 0.1;
            turnPID.SetTunings(turnKp, turnKi, turnKd);
            // Redirect back to the debug page
            client.println("HTTP/1.1 302 Found");
            client.println("Location: /debug");
            client.println("Connection: close");
            client.println();
            break;
          }
        }
      }
      // close the connection:
      client.stop();
      Serial.println("client disconnected");
    }
    vTaskDelay(pdMS_TO_TICKS(100));  // 延迟0.1秒
  }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,490评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,581评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,830评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,957评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,974评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,754评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,464评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,847评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,995评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,137评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,819评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,482评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,023评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,149评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,409评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,086评论 2 355

推荐阅读更多精彩内容