### Author Topic: better motor control feedback model  (Read 1240 times)

0 Members and 1 Guest are viewing this topic.

• Contest Winner
• Supreme Robot
• Posts: 1,450
##### better motor control feedback model
« 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?

#### waltr

• Supreme Robot
• Posts: 1,941
##### Re: better motor control feedback model
« Reply #1 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.

• Contest Winner
• Supreme Robot
• Posts: 1,450
##### Re: better motor control feedback model
« Reply #2 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.

#### waltr

• Supreme Robot
• Posts: 1,941
##### Re: better motor control feedback model
« Reply #3 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.societyofrobots.com/programming_PID.shtml

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

And for line following:

There is pseudo-code in some links,. Hope this helps.
« Last Edit: December 07, 2009, 12:25:54 PM by waltr »

• Contest Winner
• Supreme Robot
• Posts: 1,450
##### Re: better motor control feedback model
« Reply #4 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.

• Contest Winner
• Supreme Robot
• Posts: 1,450