- 11.1V的电池供电和DC9V供电不能用同一个PID,否则动力不足;
- 分别调整合适的11.1V和DC9V的PID,记录成两组参数,中间的电压用线性插值法,
- 最好是在网页中写入本次供电电压,实时计算出PID
- 全部代码:
#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秒
}
}