update arduino/Betas/RGB_V1.1.1/main/main.ino.

master
慕炎 2022-01-03 14:22:05 +00:00 committed by Gitee
parent fc52ac42b8
commit de48439754
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 74 additions and 26 deletions

View File

@ -514,8 +514,9 @@ void loop() {
double pitch = acc2rotation(accX, accY); double pitch = acc2rotation(accX, accY);
//double pitch2 = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG; //double pitch2 = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;
double gyroZrate = gyroZ / 131.0; // Convert to deg/s double gyroZrate = gyroZ / 131.0; // Convert to deg/s
if (abs(pitch - last_pitch) > 100) if (abs(pitch - last_pitch) > 100){
kalmanZ.setAngle(pitch); //kalmanZ.setAngle(pitch);
}
kalAngleZ = kalmanZ.getAngle(pitch, gyroZrate + gyroZ_OFF, dt); kalAngleZ = kalmanZ.getAngle(pitch, gyroZrate + gyroZ_OFF, dt);
last_pitch = pitch; last_pitch = pitch;
@ -629,23 +630,34 @@ void loop() {
TenthSecondsSinceStartTask(); TenthSecondsSinceStartTask();
} }
/* mpu6050加速度转换为角度 /* mpu6050加速度转换为角度
acc2rotation(ax, ay) acc2rotation(ax, ay)
acc2rotation(az, ay) */ acc2rotation(az, ay) */
double acc2rotation(double x, double y) double acc2rotation(double x, double y)
{ {
double tmp_kalAngleZ = (atan(x / y) / 1.570796 * 90);
if (y < 0) if (y < 0)
{ {
return atan(x / y) / 1.570796 * 90 + 180; return (tmp_kalAngleZ + 180);
} }
else if (x < 0) else if (x < 0)
{ {
return (atan(x / y) / 1.570796 * 90 + 360); //将当前值与前值比较当前差值大于100则认为异常
if (!isnan(kalAngleZ) && (tmp_kalAngleZ + 360 - kalAngleZ) > 100) {
//Serial.print("X<0"); Serial.print("\t");
//Serial.print(tmp_kalAngleZ); Serial.print("\t");
//Serial.print(kalAngleZ); Serial.print("\t");
//Serial.print("\r\n");
if (tmp_kalAngleZ < 0 && kalAngleZ < 0) //按键右边角
return tmp_kalAngleZ;
else //按键边异常处理
return tmp_kalAngleZ;
} else
return (tmp_kalAngleZ + 360);
} }
else else
{ {
return (atan(x / y) / 1.570796 * 90); return tmp_kalAngleZ;
} }
} }
@ -884,26 +896,9 @@ String ProcessUpdate() //页面更新
ReturnString += ","; ReturnString += ",";
ReturnString += debug_log_control; ReturnString += debug_log_control;
ReturnString += ","; ReturnString += ",";
ReturnString += test_flag;
ReturnString += ",";
ReturnString += bat_voltage; ReturnString += bat_voltage;
if (log_control) {
ReturnString += ",";
ReturnString += motor.shaftVelocity();
ReturnString += ",";
ReturnString += motor.voltage.q;
ReturnString += ",";
ReturnString += target_velocity;
ReturnString += ",";
ReturnString += pendulum_angle;
ReturnString += ",";
ReturnString += target_angle;
ReturnString += ",";
ReturnString += kalAngleZ;
ReturnString += ",";
ReturnString += gyroZangle;
} else {
ReturnString += ",,,,,,,";
}
ReturnString += ","; ReturnString += ",";
ReturnString += EEPROM.readFloat(0); ReturnString += EEPROM.readFloat(0);
ReturnString += ","; ReturnString += ",";
@ -918,6 +913,28 @@ String ProcessUpdate() //页面更新
ReturnString += v_i_2; ReturnString += v_i_2;
ReturnString += ","; ReturnString += ",";
ReturnString += v_p_2; ReturnString += v_p_2;
if (log_control) {
ReturnString += ",";
ReturnString += motor.shaftVelocity();
ReturnString += ",";
ReturnString += motor.voltage.q;
ReturnString += ",";
ReturnString += target_velocity;
ReturnString += ",";
ReturnString += pendulum_angle;
ReturnString += ",";
ReturnString += target_angle;
ReturnString += ",";
ReturnString += last_pitch;
ReturnString += ",";
ReturnString += kalAngleZ;
ReturnString += ",";
ReturnString += gyroZangle;
} else {
ReturnString += ",,,,,,,";
}
ReturnString += ","; ReturnString += ",";
if (debug_log_control) { if (debug_log_control) {
Debug_Log_func("debug print begin",1); Debug_Log_func("debug print begin",1);
@ -1035,6 +1052,26 @@ void PocessControl(int DeviceType, int DeviceIndex, int Operation, float Operati
} else if (DeviceIndex == 6) //速度环I2 } else if (DeviceIndex == 6) //速度环I2
{ {
do_vi2(do_commd); do_vi2(do_commd);
} else if (DeviceIndex == 7) //do_VQ
{
do_VQ(do_commd);
} else if (DeviceIndex == 8) //do_VV
{
do_VV(do_commd);
} else if (DeviceIndex == 77) //TVQ
{
do_TVQ(do_commd);
if (test_flag == 1)
ReturnString += "打开电机速度测试";
else
ReturnString += "关闭电机速度测试";
} else if (DeviceIndex == 88) //TVV
{
do_TVV(do_commd);
if (test_flag == 2)
ReturnString += "打开电机电压测试";
else
ReturnString += "关闭电机电压测试";
} else if (DeviceIndex == 99) //电机启停 } else if (DeviceIndex == 99) //电机启停
{ {
do_MOTOR(do_commd); do_MOTOR(do_commd);
@ -1044,7 +1081,6 @@ void PocessControl(int DeviceType, int DeviceIndex, int Operation, float Operati
ReturnString += "电机停机..."; ReturnString += "电机停机...";
} }
EEPROM.commit(); EEPROM.commit();
} }
} }
ESP32Server.send(200, "text/plain", ReturnString); ESP32Server.send(200, "text/plain", ReturnString);
@ -1092,6 +1128,18 @@ void handleNotFound()
// 通过handleFileRead函数处处理用户访问 // 通过handleFileRead函数处处理用户访问
handleFileRead(webAddress); handleFileRead(webAddress);
} }
else if (webAddress.endsWith("jquery.js")) {
webAddress = "/jquery.js";
// 通过handleFileRead函数处处理用户访问
handleFileRead(webAddress);
}
else if (webAddress.endsWith("highcharts.js")) {
webAddress = "/highcharts.js";
// 通过handleFileRead函数处处理用户访问
handleFileRead(webAddress);
}
else if (webAddress.endsWith("update")) else if (webAddress.endsWith("update"))
{ {
ESP32Server.send(200, "text/plain", ProcessUpdate()); ESP32Server.send(200, "text/plain", ProcessUpdate());