davek0974
Posts: 196
Joined: Mon Jul 22, 2019 1:52 pm

PID library ??

Sat May 15, 2021 6:17 am

Hi all, just started learning the Pico and was trying to find out if there was a PID library for it??

I was hoping to make a two-wheel balancing robot thing but they need a PID function to work.

Just thinking I may be learning the wrong device??

User avatar
HermannSW
Posts: 4122
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: PID library ??

Sat May 15, 2021 7:10 pm

Not tested with Pico yet, but Arduino PID library should work with Pico (with C-SDK) as well:
viewtopic.php?f=33&t=257238
The article series mentioned is really helpful.
For MicroPython there are different options to use, make extension funtions for Arduino code, or reimplement in MicroPython directly.
I was hoping to make a two-wheel balancing robot thing but they need a PID function to work.
My working two wheel balancing robot did not need PID ibrary.
Robot measures distance to ground.
Algorithm:
If distance>target, move backward.
If distance<target, move forward.

Value of target determines whether robot balances without moving, moving forward or backward, and determines moving speed.
What you see is balancing algorithm on right wheel only, left wheel does not move:bbb
Image
https://stamm-wilbrandt.de/2wheel_balancing_robot
https://stamm-wilbrandt.de/en#raspcatbot
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/raspiraw
https://stamm-wilbrandt.de/en/Raspberry_camera.html

davek0974
Posts: 196
Joined: Mon Jul 22, 2019 1:52 pm

Re: PID library ??

Sat May 15, 2021 8:01 pm

Great, thanks for that and the balancing idea seems good too :)

Nitro_fpv
Posts: 74
Joined: Tue Mar 30, 2021 11:56 am
Location: Switzerland

Re: PID library ??

Sat May 15, 2021 10:55 pm

What do you need a PID library for?

This is PID:

esum = esum + e
y = Kp * e + Ki * Ta * esum + Kd * (e - ealt) / Ta
ealt = e

That's the whole magic.
Last edited by Nitro_fpv on Sun May 16, 2021 10:19 am, edited 1 time in total.

danjperron
Posts: 3790
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: PID library ??

Sat May 15, 2021 11:28 pm

@Nitro_fpv
esum = esum + e
y = Kp * e + Ki * Ta * esum + Kd * (e - ealt) / Ta
old = e
i know is simple but to your forget to explain

Kp,Ki,Kd are the calibration factor

and e is ( target - actual value)

What is Ta and ealt ? Normally for integration part is only the sum of e and the differential is only the last value - the new one,

What is your reference for this equation?

An equation without describing all the variable worth nothing!

User avatar
HermannSW
Posts: 4122
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: PID library ??

Sun May 16, 2021 12:36 am

Multiple article series on the development of Arduino PID library, discussing design decisions:
http://brettbeauregard.com/blog/2011/04 ... roduction/
https://stamm-wilbrandt.de/2wheel_balancing_robot
https://stamm-wilbrandt.de/en#raspcatbot
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/raspiraw
https://stamm-wilbrandt.de/en/Raspberry_camera.html

Nitro_fpv
Posts: 74
Joined: Tue Mar 30, 2021 11:56 am
Location: Switzerland

Re: PID library ??

Sun May 16, 2021 1:17 am

danjperron wrote:
Sat May 15, 2021 11:28 pm
i know is simple but to your forget to explain
Kp,Ki,Kd are the calibration factor
and e is ( target - actual value)
What is Ta and ealt ? Normally for integration part is only the sum of e and the differential is only the last value - the new one,
What is your reference for this equation?

An equation without describing all the variable worth nothing!

Here, for example, everything is explained:

https://rn-wissen.de/wiki/index.php/Regelungstechnik

My explanations are in German, but there are many English explanations on the internet.
And as I said, you really don't need a library for a simple PID control.

davek0974
Posts: 196
Joined: Mon Jul 22, 2019 1:52 pm

Re: PID library ??

Sun May 16, 2021 5:30 am

Brilliant, thanks for the info, i will read those links.

I have used PID many times before when setting up my CNC machinery with AC servos so I have a good grasp of what each number does but only in relation to a software front-end that asks for PID values.

User avatar
OneMadGypsy
Posts: 258
Joined: Wed Apr 28, 2021 1:57 am
Location: New Orleans, Louisiana
Contact: Website

Re: PID library ??

Sun May 16, 2021 6:49 am

@danjperron


y = Kp * e + Ki * Ta * esum + Kd * (e - ealt) / Ta

samplingTime = (now - lastTime)
Kp * error + Ki * samplingTime * errorSum + Kd * (error - lastError) / samplingTime
"Focus is a matter of deciding what things you're not going to do." ~ John Carmack

danjperron
Posts: 3790
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: PID library ??

Sun May 16, 2021 9:00 pm

y = Kp * e + Ki * Ta * esum + Kd * (e - ealt) / Ta

samplingTime = (now - lastTime)
Kp * error + Ki * samplingTime * errorSum + Kd * (error - lastError) / samplingTime
Thanks way better! now everybody know what it means!

davek0974
Posts: 196
Joined: Mon Jul 22, 2019 1:52 pm

Re: PID library ??

Sun May 16, 2021 9:07 pm

danjperron wrote:
Sun May 16, 2021 9:00 pm
y = Kp * e + Ki * Ta * esum + Kd * (e - ealt) / Ta

samplingTime = (now - lastTime)
Kp * error + Ki * samplingTime * errorSum + Kd * (error - lastError) / samplingTime
Thanks way better! now everybody know what it means!
Easy for you to say :D :D :D

Return to “MicroPython”