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 support@ccsinfo.com

Pid controller design for controlling dc motor speed

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



Joined: 20 Feb 2013
Posts: 11

View user's profile Send private message Yahoo Messenger

Pid controller design for controlling dc motor speed
PostPosted: Wed Feb 20, 2013 1:23 pm     Reply with quote

I need to design a project that can control the speed of DC motor with PID controller. At the start, the motor rotates at a set speed but when I put the weight that can cause the motor speed is decreased so I need to increase the motor speed back at the original speed. Is there anyone who can help me because I am still new in programmable c language.
temtronic



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

View user's profile Send private message

PostPosted: Wed Feb 20, 2013 1:48 pm     Reply with quote

There are hundreds of PIC - PID - motor speed controllers on the Web.
That being said, you should tell us basic information like what is your PIC, motor, type of feedback used, etc.
A 'typical' PIC16F877 PIC with 1024 encoder for feedback driving a motor only needs a few lines of code.
Showing us your code will help but we need a LOT more information.
hth
jay
wanraimi



Joined: 20 Feb 2013
Posts: 11

View user's profile Send private message Yahoo Messenger

PostPosted: Wed Feb 20, 2013 11:17 pm     Reply with quote

I used pic18f458 because my project also have can-bus network. The feedback i used is phototransistor for counting the total of one cycle blade encoder. At this time I'm not complete the code because i need to find the formula to set up the speed when the speed of dc motor change from desired speed.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Feb 21, 2013 12:45 am     Reply with quote

Quote:
i need to find the formula to set up the speed when the speed of dc motor change from desired speed
why don't you start with a simple P controller?
Code:
Vmot = Vmot,idle + Kp*(speed,set - speed,actual)

Increase Kp from zero up to the stability margin, then use e.g. half of this value.

After succesfully implementing a P controller, learn about PI and try.

Be aware of the fact that counting encoder pulses involves a considerable controller dead time (frequency counter gate time)
and slows down the achievable controller reaction. If this matters, use a pulse period measurement instead.
Ttelmah



Joined: 11 Mar 2010
Posts: 19342

View user's profile Send private message

PostPosted: Thu Feb 21, 2013 1:36 am     Reply with quote

The critical 'not understanding line', is the phrase:
"the formula to set up the speed when the speed of dc motor change from desired speed".

There isn't 'a formula'. It depends on everything else in _your_ system.

Think about it for a moment. Imagine driving a truck, and then driving a motor bike, and trying to hold speed. The basic parameters are the same. You have 'speed' from the speedometer, and a 'control output', in the throttle position. If your 'speed' is below the target, then you have to increase the control position, if it is above the target, then you have to decrease the control position. However the parameters involved, will vary wildly (this is the P term in PID). You hit a hill with the truck, and the throttle has to go to full. The same hill with the motor bike, and you hardly have to change position at all. Then when actually accelerating to get 'to' the speed on the flat, with the bike, you are having to ease off on the throttle when you are 10mph below the target speed, or you will overshoot (this is the 'D' term in PID). With the truck, this tendency is almost non existent. Then you have the thing of headwinds etc., making you have to open the throttle just a little further than you'd normally expect to maintain a speed. This is the 'I' term in PID.
Now as with the truck, when (for instance) overshoot is unlikely, you can probably dispense with the D term, so with controllers, it may well be possible to not use all three terms. If the motor can't accelerate very quickly, a 'PI' controller may be all you need. FvM has given very good advice here. Start simple. Work out the best way of calculating speed, and how quickly this can be done (imagine driving the bike, and having a speedo that only updated every 10 seconds - would you be able to hold an accurate speed?). Speed is the reciprocal of the interval between successive pulses (think about being on a train, and hearing the thump of the track sections - you 'know' instantly when you get faster or slower as the frequency of this changes - not so much on modern joined tracks though....). Much quicker update and makes things simpler. Then if the interval is longer than required, just raise the 'throttle' a little, if it is shorter, decrease the throttle. You now have a working P controller. Get this working first, then start tweaking the other terms.
You need to get the values coming into, and out of the PID operation working _first_. The 'formula', will depend on the mechanics of the hardware, and the details of your measurement/control system.

Best Wishes
wanraimi



Joined: 20 Feb 2013
Posts: 11

View user's profile Send private message Yahoo Messenger

PostPosted: Thu Feb 21, 2013 4:49 am     Reply with quote

Thanks to the assistance given by you all and I really appreciate it. Sorry for my english language because i'm not quite fluent, but still understand what you all asking. I will trying the information given.

This is my ccs coding only for run the motor without PID, now i'm still learn about PID controller but hard to understand its and how to apply in my programming.
Code:

#include <18f458.h>
#fuses XT,NOLVP,NOWDT,NOPROTECT
#use delay(clock=20000000)

#define Wire1 PIN_B6
#define Wire2 PIN_B7
#define Up    PIN_A0
#define Down  PIN_A1
#define Dir   PIN_A2

int16 duty_cycle=512;

void main()
{
   setup_adc(ADC_OFF);              //Turn ADC Off
   setup_adc_ports(NO_ANALOGS);     //All Ports are digital I/O
   output_high(Wire1);
   output_low(Wire2);               //Set the motor to CW direction
   
   /*PWM Configurations*/
   setup_timer_2(T2_DIV_BY_1,255,1);   //Set PWM frequency
   setup_ccp1(CCP_PWM);                 //Configuration CCP1 to PWM mode
   set_pwm1_duty(512L);                //default duty cycle = 50% (Half Speed)
   
   while(true)
   {
      while(input(Up) && input(Down) && input(Dir)); //Hold until a button is pressed
     
      if(!input(Up)) // Button(Up) Selected?
      {
         duty_cycle+=64;      //increase the duty cycle by some percentage (+6.25%)
         set_pwm1_duty(duty_cycle); //set the duty cycle
      }
      if(!input(Down)) //Button (Down) selected?
      {
         duty_cycle-=64;   // decrease the duty cycle
         set_pwm1_duty(duty_cycle); //set the duty cycle
      }
      if(!input(Dir))   // Button (Direction) selected?
      {
         output_toggle(Wire1);   //Toggel the polarity of IN1 IN2
         output_toggle(Wire2);   //0 becomes 1 , 1 becomes 0
      }
     
      delay_ms(500);  //0.5 second delay to release the selected button
   }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19342

View user's profile Send private message

PostPosted: Thu Feb 21, 2013 5:08 am     Reply with quote

First big problem. Limits.
For example:

Code:

if(!input(Down)) //Button (Down) selected?
   {
   duty_cycle-=64; // decrease the duty cycle
   set_pwm1_duty(duty_cycle); //set the duty cycle
}


What is going to happen when you go 'down' when the PWM is already at zero?.
Also (going the other way), what is going to happen when the PWM is at 960, and you hit the 'up' button'?.

Then second big problem reverse.
If your motor is going full speed forward, and you hit the 'Dir' button, what is going to happen. Will the drivers survive this?. Will the motor survive this?. Where is the huge surge of power going to come from?. Where is all the surplus energy going to go?.

You need to think more carefully. Have a duty cycle number, and when you increase it, do a limit check, and if it goes over the limit select full power. Similarly when you reduce it, check if this causes a maths overflow, and if so set it to zero.
Then when 'reverse' is selected, ramp the speed down, wait for the motor to stop, reverse the drive, and ramp it back up.

Then your control will be a matter of measuring the speed at some point, and if this is below the value required for a given setting, increase the duty cycle. If above, reduce the setting.

Best Wishes
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