Society of Robots - Robot Forum

Software => Software => Topic started by: madsci1016 on December 05, 2009, 08:49:03 PM

Title: better motor control feedback model
Post by: madsci1016 on December 05, 2009, 08:49:03 PM
I am building my own motor controller that has feedback from the motors, to maintain a constant speed.

The current equation I have for the control system is as follows

if (tempclicks > (speed_req + (DEAD_BAND / 2)))
        {
           power-= ((tempclicks - speed_req) / SPEED_CONTROL_GAIN);
           
            if (power < 0)
           power = 0;
        }
 
  if (tempclicks < (speed_req - (DEAD_BAND / 2)))
        {
           power+= ((speed_req - tempclicks) / SPEED_CONTROL_GAIN);
           
           if (power > 255)
           power = 255;
        }

Where speed-req is the goal, and temp clicks is the actual measure from the encoder. The control loop operates at 35Hz. The dead band is 6 and the speed controller gain is 7 right now. power is a duty cycle of pwm to the h bridge, 255 is 100% duty cycle.

I am no good with control systems, and this model is creating damped oscillations when recovering from a step response (holding the motor to make it work harder and then releasing to let it free spin). It will oscillate twice before settling on the requested speed.

Can someone point me in the right direction of a better control system?
Title: Re: better motor control feedback model
Post by: waltr on December 05, 2009, 10:04:32 PM
What you have is a Proportional control feedback. The response you are getting is what would be expected.

Look up PID (Proportional, Integral, Derivative) feed back control.
Title: Re: better motor control feedback model
Post by: madsci1016 on December 06, 2009, 09:55:27 PM
I have been reading up on PID controllers for a while and while i understand the theory, I am still fuzzy on how to code it.

Speaking of which, do you think i should use a full PID controller or just a PI controller? I am running this on a 8Mhz Arduino that i hijacked a timer to give me a 35Hz control loop. I'm not sure how much math i can put in that interrupt before i overload the uController.

Thoughts?

Thanks.
Title: Re: better motor control feedback model
Post by: waltr on December 07, 2009, 12:24:10 PM
How much code your cpu can run in an ISR depends on other factors. Another way is to do the PID calculations out side the ISR, as a task in your main loop. The ISR code only reads to feedback sensor and writes the motor control ports.

In practice a full PID control is not needed. You may only need PI or PD, only experimentation will tell.
There are many web sources of PID theory and some code. I did see a code example for a BASIC Stamp once but can't find it in my bookmarks.
Here are some starting places:
http://www.piclist.com/techref/io/pid.htm (http://www.piclist.com/techref/io/pid.htm)
http://www.societyofrobots.com/programming_PID.shtml (http://www.societyofrobots.com/programming_PID.shtml)

http://www.seattlerobotics.org/Encoder/200108/using_a_pid.html (http://www.seattlerobotics.org/Encoder/200108/using_a_pid.html)
http://letsmakerobots.com/node/865 (http://letsmakerobots.com/node/865)

And for line following:
http://www.google.com/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=kD3&q=pid+line+follower&revid=1954647101&ei=R0UdS7rDIIXZlAf2h5TyCQ&sa=X&oi=revisions_inline&resnum=0&ct=broad-revision&cd=5&ved=0CEEQ1QIoBA (http://www.google.com/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=kD3&q=pid+line+follower&revid=1954647101&ei=R0UdS7rDIIXZlAf2h5TyCQ&sa=X&oi=revisions_inline&resnum=0&ct=broad-revision&cd=5&ved=0CEEQ1QIoBA)

There is pseudo-code in some links,. Hope this helps.
Title: Re: better motor control feedback model
Post by: madsci1016 on December 07, 2009, 05:04:13 PM
Another way is to do the PID calculations out side the ISR, as a task in your main loop. The ISR code only reads to feedback sensor and writes the motor control ports.

Oops, yeah, I mispoke. I already do this. The ISR just copies the encoder info to a temp variable and sets a flag. My main code will compute the control when it reads the flag as set. I just have to make sure the ISR isn't called again before the control is done computing, but i guess i can check for that pretty easily.

Thanks for the links, i will give them a read over.
Title: Re: better motor control feedback model
Post by: madsci1016 on December 09, 2009, 02:39:26 PM
Ok, I seem to have better knowledge of the subject, but have more questions.

It seems a PD controller is the way to go. The derivative is the rate of change of the error, so it seems you would write a PD equation that if the rate of change of error is high but error is decreasing, the controller should not keep changing the control so fast. Does that make sense? How does that fix oscillations?

edit: nevermind I think I got it.