davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

C PID library for the Pi?

Fri Mar 18, 2016 6:24 pm

hey,
is there a PID C library for the Pi, e.g. like the PID_v1 lib for Arduino?
https://github.com/br3ttb/Arduino-PID-Library
Last edited by davenull on Sat Mar 19, 2016 8:21 am, edited 2 times in total.
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: PID library for the Pi?

Fri Mar 18, 2016 7:25 pm

There have been topics about PID controlling like this one:

viewtopic.php?f=37&t=63714

... you might consider writing a library for PI. Or, you might consider using Beauregard's Arduino library (and the Uno) to actually do the 'controlling' and use the RPI for analytics, communications, or what-not.

Try searching the python archives; somebody has probably been working on a python module for this RPI purpose... maybe even Beauregard!

Cheers
Last edited by MarkHaysHarris777 on Fri Mar 18, 2016 7:50 pm, edited 1 time in total.
marcus
:ugeek:

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: C PID library for the Pi?

Fri Mar 18, 2016 7:33 pm

I know about the basics of a PID controller, but there are those nitty-gritty details which make the simple basic algorithms complicated, and which has been partially described in the comments to the PID_v1 Arduino lib.

Python is weird and absurd to me, I hate this so-called "programming language" and don't want to look into it.

So for C/C++ I wouldn't like to invent the wheel anew if there is already a C/C++ PID controller lib available, and there also are drawbacks for the Arduino PID_v1 lib, so also out-sourcing to an Arduino would not resolve all issues.

That's why I would prefer to use a tested and trusted lib for the Pi iteslf.
Last edited by davenull on Sat Mar 19, 2016 8:20 am, edited 1 time in total.
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: PID library for the Pi?

Fri Mar 18, 2016 8:17 pm

I hear what you're saying about python--- learning curves are tough; however, the python learning curve isn't that bad, really. I would recommend at least giving python a shot, because that's where you'll find the most community support (especially for GPIO coding) and that's where you're more likely than not to find some help with a functional library (beta or not).

The following link is a dissertation.pdf discussing PID controlling using the Raspberry PI and Python. Its a cool read, even if you're not going to use his stuff. Anyway, give python a try; its ubiquitous these days (almost all platforms) and I'd say its the primary language of the RPI community.

https://studentnet.cs.manchester.ac.uk/ ... lltext.pdf

Best of luck
marcus
:ugeek:

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: C PID library for the Pi?

Sat Mar 19, 2016 7:57 am

thank you, I appreciate your input and friendlyness to help very much, but about Python - no chance.
If I just see it I immediatly feel like I have to vomite.
(actually there is just 1 PL in the world with an even more nausea-causing power to me than Python, i.e. Java.)

I'm programming just in ANSI C (only partially using C++ objects written by other programmers): because C is structured, clear, straight, and powerful. I need pointers and parameters to be passed to functions by value oder by reference, by a procedural syntax.
(No obfuscations though, even if possible.)

And as I don't speak Python it's supposed to be much more complicated and unreasonable to me to first learn a hated crappy "language" just to port the code afterwards to C which's syntax is again completely different.

But back to topic, I can't actually imagine that I as a beginner to Raspi would be the first one to ask for an already existing PID control lib in C for the Raspi, but that instead I had to invent that stuff from the scratch by myself because no one ever did it succesfully before - that would be unlikely IMO.

It simply cannot be true that there is no tested and trusted PID lib for the Pi on this entire planet, or is it?
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: C PID library for the Pi?

Sat Mar 19, 2016 12:55 pm

there will be one ...

... in the release after the next of wiringPi.

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: C PID library for the Pi?

Sat Mar 19, 2016 1:55 pm

davenull wrote:thank you, I appreciate your input and friendlyness to help very much ...

I can't actually imagine that I as a beginner to Raspi would be the first one to ask for an already existing PID control lib in C for the Raspi, but that instead I had to invent that stuff from the scratch by myself because no one ever did it succesfully before - that would be unlikely IMO, ... or is it?
Under most circumstances, in my opinion, you are correct; always shop around to see if someone else has at least framed your idea in code so that you can either learn from it, or hopefully borrow from it (open, libre).

But, this is a little like supply and demand. Most controllers don´t need to be quite as robust as PID controlling; so, it [PID] gets reinvented by the next ´user´ who needs a solution, but who isn´t also really interested in starting a project to distribute a library which will also have very little demand. As Gordon pointed out, sooner or later, someone will do it. BUT, that will happen only after the ´need´ is demonstrated and the interest is sparked (then everyone will benefit from it; however infrequently).

A little searching on google will show you a host of other folks who have reinvented the wheel on PID... several using the RPI. It would be interesting to see a study sometime for the break-even point where someone decides that a library is needed. Usually, these days, its a corporate entity which wants to release a product and also releases an API to match. Keep searching there might be an industrial application that produces an open API for their own product that you could learn from, and or copy and tweak; you never know!
marcus
:ugeek:

rzusman
Posts: 347
Joined: Fri Jan 01, 2016 10:27 pm

Re: C PID library for the Pi?

Sat Mar 19, 2016 4:14 pm

A PID is like, what, 5 lines of C code?

What makes a PID for the Pi special?

Here's some sample code in C:
http://brettbeauregard.com/blog/2011/04 ... tive-kick/

Just compile is on the Pi and use it.

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: C PID library for the Pi?

Sat Mar 19, 2016 6:12 pm

rzusman wrote:A PID is like, what, 5 lines of C code?

What makes a PID for the Pi special?
It's not wrapped in a nice little package like what Arduino ones are..

-Gordon
--
Gordons projects: https://projects.drogon.net/

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: C PID library for the Pi?

Mon Mar 21, 2016 8:33 am

@rzusman:
not just this simple wrap, but also the features which are provided by this API lib, like partially provided by the Arduino PID_v1 lib:
e.g., there are issues, if you use PID control for quickly rotating rotary encoder motors under varying positive or negative load to precisely target an encoder value, and to avoid inertial passive overrun when the encoder target once has been reached and the motor already has been switched off (then automatically reactivate PID control and re-allign to the target within a predefined time of latency).
Then, controlling motor rotations both by approaching an encoder target and simultaneously approaching a rotation speed during this mission for either motor.
Then, to provide a simple instantiation of a set of PID motor controls for either single motor control purposes even for 6-10 motors..
Then, when having to control several motors, starting and stopping simultaneous PID controls by different PID control settings, and all "PID motor control instances" have to be computed automatically independendly in parallel tasks or added or cancelled to already running tasks.
Then a PID control should provide different set modes, e.g. to reach a target and then switch off or to hold the target point actively by infinite re-adjusting until being disabled.
Then, when a motor has not reached a target precisely but just quite closely (e.g., stalling under heavy load close to the target), either set the control mode to try approaching it repetively or enable switch-off after a predefined time of latency and/or within a predefined target-gap nevertheless.
Then, the ability to change PID-settings temporarily even if the control already has been started, overwriting the automatic controls (PID_v1: "manual" vs. "automatic").
Then having all-over runtime control of all single current motor states like
idle
busy
ramp-up
ramp-down
off
coast
brake
hold (target)
to be able to start single motor control missions depending on the run state of one or more different other motor run states (e.g., wait for PID motor A+B to ramp down before starting PID motor D, or to synchronize motors 5+6+7 by adjusting current PID settings for motor speed during runtime.
Then, setting different ramp-up and ramp-down constants for either motor to smoothly start and stop motors.
Then, last but not least, have a way to set PID control update intervals to adjust to slow low-priority control modes (e.g., 500ms relay-switching) up to high speed+high priority controls (e.g., 50-100µs for a quickly drawing 3DOF plotter or a 6DOF assembling robot).

And after all, providing a auto-tune-module for automatical empirical computing of most appropriate kp, ki, kd settings under different runtime-circumstances.


These are examples of functional purposes which are needed to have by a PID API lib but which are not provided by the simple PID algorithm based on the formula
Image
gordon@drogon.net wrote:there will be one ...

... in the release after the next of wiringPi.

-Gordon
that indeed sounds very promising ! 8-)


.
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

Return to “C/C++”