简体   繁体   English

电机的PID转矩控制

[英]PID torque control of motor

I'm trying to control the motor torque. 我正在尝试控制电动机扭矩。 This I'm trying to accomplish by implementing a PID control on motor current and hence on the PWM. 我正在尝试通过对电动机电流(因此对PWM)实施PID控制来实现此目的。 Higher PWM means higher torque. PWM越高,转矩越高。 I'm new to arduino-uno, hence would need help with the coding. 我是arduino-uno的新手,因此需要编码方面的帮助。 I have written a code for the same but a bit unsure on its correctness. 我已经为相同的代码编写了代码,但是对其正确性不确定。 I'm still unsure about the value of the 'Integral' variable to be used though. 我仍然不确定要使用的'Integral'变量的值。 Any help is much appreciated Thanks in advance 任何帮助都非常感谢

#include "RunningAverage.h"
 int m1 = 13;
int m2 = 12;
int me = 9;
int t = millis()+5000;
RunningAverage myRA(80);
int stat=0;
int pwmn=100;
int counter=0;
int kP;
int kI=0;
int kD=0;
int SetPt;
int Last;
int Actual;
int Error;
int Integral;
float P;
float I;
float D;
int Drive;
int ScaleFactor;


void motorRight(){
digitalWrite(m1,HIGH);
digitalWrite(m2, LOW);
}


void motorLeft(){
digitalWrite(m2,HIGH);
digitalWrite(m1, LOW);
}

void motorOff(){
digitalWrite(m2, LOW);
digitalWrite(m1, LOW);
}


void motorBrake(){
digitalWrite(m2, HIGH);
digitalWrite(m1, HIGH);
delay(10);
motorOff();
}

void setup() {
// put your setup code here, to run once:
pinMode(m1,OUTPUT);
pinMode(m2,OUTPUT);
pinMode(me,OUTPUT);
analogWrite(me,pwmn);
Serial.begin(115200);
motorRight();
}

void loop() {
// put your main code here, to run repeatedly:
myRA.addValue(analogRead(A1));
Serial.print(myRA.getAverage());
Serial.println(",500,600");
Actual = myRA.getAverage();
Error = SetPt - Actual;
P = Error*kP; // calc proportional term
I = Integral*kI; // integral term
D = (Last-Actual)*kD; // derivative term
Drive = P + I + D; // Total drive = P+I+D
Drive = Drive*ScaleFactor; // scale Drive to be in the range 0-255
//Serial.println(pwmn);
if(counter>10){
if(pwmn<250){
pwmn++;
}
counter=0;
}
counter++;
if (abs(Drive)>255) {
Drive=255;
}
analogWrite (me,Drive); // send PWM command to motor board
Last = Actual; // save current value for next time
// analogWrite(me,pwmn);
delay(50);
}' 

You are correct, the integral calculation is wrong. 您是正确的,积分计算是错误的。 Integral (from integrate) means to add up. 积分(来自integral)意味着相加。 What you want is 你想要的是

I = I + kI*Error;

You should also limit the Integral term (I) to some maximum and minimum value to avoid "wind-up" error. 您还应该将积分项(I)限制为某个最大值和最小值,以避免“缠绕”错误。 This happens when the servo cannot reach is goal and since the I term is the sum of the errors it can get out of control. 当伺服器无法达到目标时会发生这种情况,因为I项是它可能失控的误差之和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM