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

Using Arduino-PID-Library on Raspbian

Tue Nov 19, 2019 9:08 pm

I got aware of Arduino-PID-library years ago when I did more stuff with Arduinos.
https://playground.arduino.cc/Code/PIDLibrary/
Especially the article series describing in detail the design decisions impressed me:
http://brettbeauregard.com/blog/2011/04 ... roduction/
So it was clear that I would do my first PID project with that library.
Only now I really have to do my first PID controller.
...
https://forum.arduino.cc/index.php?topic=649071.0

Code: Select all

$ g++ -Wall -Wextra PID_test.cpp Arduino.cpp Arduino-PID-Library/PID_v1.cpp -o PID_test
$ ./PID_test
0
100
70.000000
$

Code: Select all

$ cat PID_test.cpp 
#include <stdio.h>
#include <unistd.h>

#include "Arduino.h"
#include "Arduino-PID-Library/PID_v1.h"

double Setpoint, Input, Output;

double Kp=2, Ki=5, Kd=1;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

int main()
{
  printf("%ld\n",millis());
  usleep(100000);
  printf("%ld\n",millis());

  Input = 42;
  Setpoint = 100;

  myPID.SetMode(AUTOMATIC);

  Input = 48;
  myPID.Compute();
  printf("%lf\n",Output);

  return 0;
}
$ 
⇨https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/github_repo_i420toh264
https://github.com/Hermann-SW/fork-raspiraw
https://twitter.com/HermannSW

dsyleixa123
Posts: 462
Joined: Mon Jun 11, 2018 11:22 am

Re: Using Arduino-PID-Library on Raspbian

Wed Nov 20, 2019 11:32 am

thank you very much for your contribution!
JTM, many people are using wiringPi for Raspi GPIOs, but wiringPi is already providing a millis() function.
Perhaps one will need some #ifdef...#elseif to avoid ambiguous millis() in the code, or what do you think?

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

Re: Using Arduino-PID-Library on Raspbian

Wed Nov 20, 2019 4:08 pm

dsyleixa123 wrote:
Wed Nov 20, 2019 11:32 am
many people are using wiringPi for Raspi GPIOs, but wiringPi is already providing a millis() function.
Perhaps one will need some #ifdef...#elseif to avoid ambiguous millis() in the code, or what do you think?
Easier than that -- in case you get millis() function from wiringPi, just make Arduino.h an empty file and delete Arduino.cpp in directory above Arduino-PID-library.


I switched to using pigpio some months ago because of the micosecond precision waveforms.
https://github.com/Hermann-SW/Raspberry ... shots-tool
This is global shutter multiple exposure frame captured with "./shots 5 9 241" (five 9µs duration pulses, 241µs apart) of very fast rotating mini drone propeller:
Image
⇨https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/github_repo_i420toh264
https://github.com/Hermann-SW/fork-raspiraw
https://twitter.com/HermannSW

dsyleixa123
Posts: 462
Joined: Mon Jun 11, 2018 11:22 am

Re: Using Arduino-PID-Library on Raspbian

Wed Nov 20, 2019 6:21 pm

HermannSW wrote:
Wed Nov 20, 2019 4:08 pm
dsyleixa123 wrote:
Wed Nov 20, 2019 11:32 am
many people are using wiringPi for Raspi GPIOs, but wiringPi is already providing a millis() function.
Perhaps one will need some #ifdef...#elseif to avoid ambiguous millis() in the code, or what do you think?
Easier than that -- in case you get millis() function from wiringPi, just make Arduino.h an empty file and delete Arduino.cpp in directory above Arduino-PID-library.
woudn't it be possible then better to delete the line
#include "Arduino.h"
and completely delete all Arduino.* files completely?

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

Re: Using Arduino-PID-Library on Raspbian

Wed Nov 20, 2019 7:19 pm

sure, if you link with wiringPi library.
⇨https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/github_repo_i420toh264
https://github.com/Hermann-SW/fork-raspiraw
https://twitter.com/HermannSW

dsyleixa123
Posts: 462
Joined: Mon Jun 11, 2018 11:22 am

Re: Using Arduino-PID-Library on Raspbian

Wed Nov 20, 2019 7:37 pm

so 1st,
IIUC, you are using
- the original Arduino PID_v1.h/cpp https://github.com/br3ttb/Arduino-PID-Library
- plus Arduino.h/cpp , altenatively wiringPi for millis()

or which different files to download?

2nd,
what does the first line of your program source code mean?
$ cat PID_test.cpp

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

Re: Using Arduino-PID-Library on Raspbian

Thu Nov 21, 2019 12:24 pm

On 1:
Without WiringPI:
1)
a) "git clone https://github.com/Hermann-SW2/userland.git"
b) or download "https://github.com/Hermann-SW2/userland ... master.zip" and unpack
2)
go to "host_applications/linux/apps/hello_pi/i420toh264" directory
3)
use Arduino.cpp, Arduino.h and Arduino-PID-Library subdirectory
4)
Compile PID_test.cpp as I showed

With WiringPi:
1)
git clone or download zip from original repo: https://github.com/br3ttb/Arduino-PID-Library/
2)
edit Arduino-PID-Library/PID_v1.cpp
a) delete lines 8-12
b) change
#include <PID_v1.h>
to
#include "PID_v1.h"


On 2:
Just print contents of file PID_Test.cpp:
http://www.man7.org/linux/man-pages/man1/cat.1.html
⇨https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/github_repo_i420toh264
https://github.com/Hermann-SW/fork-raspiraw
https://twitter.com/HermannSW

dsyleixa123
Posts: 462
Joined: Mon Jun 11, 2018 11:22 am

Re: Using Arduino-PID-Library on Raspbian

Thu Nov 21, 2019 12:32 pm

thank you very much!

Return to “C/C++”