CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PID motor control

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Zek_De



Joined: 13 Aug 2016
Posts: 100

View user's profile Send private message

PID motor control
PostPosted: Wed Nov 06, 2019 8:29 am     Reply with quote

Hi guys
Codes below control the motor speed with PID but I have a problem. If I change the speed like rpm=1000 and then rpm= 100. PID calculation produce very big negative value and this is why I can't set motor speed. How can I do it?

Code:

rpm = bldcObj->func.Get_RPM(bldcObj);
      //rpm = kalman1Dobj->func.Calculate(kalman1Dobj,rpm);
      err = setRpm - rpm;
      pwm = (int16_t)pidObj->func.Calculate(pidObj,err);

      if(pwm>3000) pwm=3000;
      else if(pwm<0) pwm=0;
      
      
      bldcObj->func.Set_Duty_Cycle(bldcObj,pwm);
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Nov 07, 2019 6:06 am     Reply with quote

OK, no one's responded for awhile so...

1st, your code isn't complete. Code for any functions need to be posted.
2nd, only use unsigned integers for the 'math' as the PWM module only uses unsigned (0 -1023, if 10 bit).
3rd, always post PIC and compiler version.
4th, show us examples of the 'bad numbers' vs. what you think they should be.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Thu Nov 07, 2019 7:03 am     Reply with quote

and if you look at any normal PID implementation there will be limit
tests to prevent 'wind up' when the code receives extreme values....
This actually sounds like extreme 'overshoot' which is normally caused
by having the D value too large.
Zek_De



Joined: 13 Aug 2016
Posts: 100

View user's profile Send private message

PostPosted: Fri Nov 08, 2019 3:36 am     Reply with quote

Code:
rpm = bldcObj->func.Get_RPM(bldcObj);
      //rpm = kalman1Dobj->func.Calculate(kalman1Dobj,rpm);
      err = setRpm - rpm;
      
      saturation=err;   
      
      if(err>3000) saturation=3000;
      else if(err<0) saturation=0;
      
      saturation=saturation-err;
      
      pwm = (int16_t)pidObj->func.Calculate(pidObj,err,saturation);
      
      bldcObj->func.Set_Duty_Cycle(bldcObj,pwm);



and inside PID

Code:
static float Calculate(pid_t *obj, float err,float backCalc)
{
float out;
/* y[n] = y[n-1] + A0 * x[n] + A1 * x[n-1] + A2 * x[n-2]  */
out = (obj->var.A0 * err) + (obj->var.A1 * obj->var.state[0]) + (obj->var.A2 * obj->var.state[1]) + (obj->var.state[2]) + (backCalc*err);

/* Update state */
obj->var.state[1] = obj->var.state[0];
obj->var.state[0] = err;
obj->var.state[2] = out;

return out;

}


I add a new variable for algorithm because of back calculation
Kd=0,Kp=0,Ki=0.2
I changed the uint16 to int16_t, I didn't realised it but now still I got big negative value. I am still working on it.
Zek_De



Joined: 13 Aug 2016
Posts: 100

View user's profile Send private message

PostPosted: Fri Nov 08, 2019 3:40 am     Reply with quote

y[n] = y[n-1] + A0 * x[n] + A1 * x[n-1] + A2 * x[n-2] * x[n]*e[n]

x[n]*e[n] comes back calc using z transform. x[n] equals current limited value for pid. I couldn't write for a while sorry for that.
Zek_De



Joined: 13 Aug 2016
Posts: 100

View user's profile Send private message

PostPosted: Fri Nov 08, 2019 6:27 am     Reply with quote

Actually I can't set it any new rpm that is lower than previous rpm. Just I can set it for higher rpm. Why it behaves like that ?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group