JayThree Balancing Car Project — Part 5/5

PID without a PhD

J3
Jungletronics
7 min readOct 11, 2017

--

Here we go again: How to control my car using PID without a Ph.D.:|

Here is Our Final Gool accomplished! See the final code now!

Thanks to J. Brokking!

J3 BALANCING CAR PROJECT: We want the angle gyro output as near zero as possible. So, I suppose the car will be in balance at that point, right?

We did whatever was necessary to balance this car… at the point in this championship giving up was not an option…ever and never!

Here is the my JayThree Balancing Car under PID Control. WooooHoooo\o/

Notes:

  • First run this code _35_J3_BCP_Hardware_Test_19.ino for correction annotation of the GY-521 board; Take note of Balance value from output;
  • Then open the code _35_J3_Balancing_Car_Project_20.ino and change the variable int acc_calibration_value igual to the annotated previous value found;
  • Now be patient to set the PID value by trial and error. These variables are:
    float pid_p_gain = a; // Gain setting for the P-controller (15)
    float pid_i_gain = b; // Gain setting for the I-controller (1.5)
    float pid_d_gain = c; // Gain setting for the D-controller (30)
    float turning_speed = d; // Turning speed (20)
    float max_target_speed = e;
  • To get the level keep the robot perfect standing upright!
  • Turn the code as a horizontal robot and place it vertically after the LEDs go out. There is a blinking sequence until it is calibrated;
  • After several attempts, I had a problem loading new codes. I had to replace my Arduino Pro Mini with a new one! (that’s why is better to buy on real supplier).
  • For the Arduino UNO that does the remote control load this code:_35_Balancing_robot_remote_21.ino ;
  • An interesting test loads this code _35_Receiving_Serial_Data_11.ino in the Arduino Pro Mini and sees the output of the Serial Monitor from Arduino UNO; Do this and you will have a great discovery for your understanding once and for all of this solution :D
  • I bought my Arduino pro mini from an unknown supplier. It happens that they sold an 8MHz instead of 16 Mhz. To prove when opening the serial port via Arduino IDE Serial Monitor, you will receive garbage :( I changed the speed to 4800 and everything worked. That is, expected 9600 came only 4800 (?). Please, buy your Arduino Pro Mini Serial board from a reliable supplier;
  • Another situation is that my step motors are heating up a lot (?) maybe the wiring deserves a revision. The fact is that warming the Arduino enters into safe mode. In order to work again take some time and try again…

That’s it! There were 5 posts published about this project.

In total were 3 months of study research some family setbacks but the delivery is done!

If you still want to read, follow some theory about PID. Thanks for following this project. If you liked, favorite, share, send your message. I will be happy to interact with the DIY community. Version 2 is on the way…Goodbye!

What’s going to be the next project? JayThree X Drone. Can I get high now?

Before we say goodbye shall we do some terminologies associated with PID?

J3BCP v1.0, Schema- Rev 0.1- OCT, 2017
J3BCP MindMap Updated! It Took me 147 days Uf!— Download all Time Report from my GoogleDrive Directory

More PID Theory

Defining the parameters of a PID controller:

Proportional-Integral-Derivative (PID) control is the most common control algorithm used in industry and has been universally accepted in industrial control.

What’s behind a PID Controller?

The basic idea behind a PID controller is to read a sensor, then compute the desired actuator output by calculating proportional, integral, and derivative responses and summing those three components to compute the output.

Thank God we have an IMU. This is Closed Loop Configuration:

Closed Loop Control System (CLCS) is the process of reading sensors to provide constant feedback and calculating the desired actuator output is repeated continuously and at a fixed loop rate .

Our goal is to tune a PID controller to reduce overshoot in reference tracking or to improve rejection of a disturbance at the plant input.

What is disturbance rejection? From MacMynowski:

The term “disturbance rejection” was used a number of times in lecture today to describe the purpose of feedback. With feedback, the controller is able to use the output to shape the input of the system.

And how about Nonlinear System:

Nonlinear System (NLS) is a system in which the control parameters that produce a desired response at one operating point might not produce a satisfactory response at another operating point.

I read that simulation is an important tool for getting PID right.

I already did this in the previous post.

The Setpoint (SP) is the value that we want the process to be.

The PID controller looks at the setpoint and compares it with the actual value of the Process Variable (PV).

Here’s the Block Diagram we used before, with the labels changed to represent the ‘car-on-windy-freeway control’ loop.

Pure PID

Thankfully we have feedback from the angle returned by IMU.
Notice how important closing the feedback loop is. If we removed the feedback loop we would be in open loop control, and would have to control the car’s angle with our eyes closed!

We are interested here in what the black box actually does,
Here are my options, called the “Modes of Control”:

Proportional (P)

Integral (I)

Derivative (D)

Here’s a simplified text of what the PID controller does:
These 3 modes are used in different combinations:

P — Sometimes used

PI — Most often used

PID — Sometimes used

PD — rare as hen’s teeth but can be useful for controlling servo motors.

Derivative (D): is just a mathematical term meaning rate-of-change. The derivative component causes the output to decrease if the process variable is increasing rapidly. The derivative response is proportional to the rate of change of the process variable. Increasing the derivative time (Td) parameter will cause the control system to react more strongly to changes in the error term and will increase the speed of the overall control system response.
Integral (I): from school, turns out to be the area under the curve. The integral component sums the error term over time. The result is that even a small error term will cause the integral component to increase slowly. The integral response will continually increase over time unless the error is zero, so the effect is to drive the Steady-State error to zero.
Proportional (P): simply multiplies the Error by the Proportional Gain (Kp) to get the controller output. In general, increasing the proportional gain will increase the speed of the control system response. However, if the proportional gain is too large, the process variable will begin to oscillate

Under The Hood Of The PID Controller — Tuning

You will find two final codes in my repository (look for prefix _35 Proj). One using the GY-85 and another using the GY-521.

All codes worked but each plant has its peculiarities. I left the serials prints scattered in the key places that you should analyze when running this project.

The process of setting the optimal gains for P, I and D to get an ideal response from a control system is called tuning.

The gains of a PID controller can be obtained by trial and error method. This is my approach to the solution.

I started with PD. This way distorting the most common occurrences in the literature.

The car balances for a few seconds but delays the acceleration when it needs it most. And fell!

The tricky thing about Integral Action is that it will really screw up your process unless you know exactly how much Integral
action to apply.

The way to adjust how much Integral Action you have is by adjusting a term called “minutes per repeat”.

So where does this strange name come from? It is a measure of how long it will take for the Integral Action to match the Proportional Action.

In other words, if the output of the proportional box on the diagram above is 20%, the repeat time is the time it will take for the output of the Integral box to get to 20% too.

Derivative action improves the controller action because it predicts what is yet to happen. Of course, you can try and filter the noise out, but my advice is that, unless PI control is really slow, don’t worry about switching D on.

Another Method:

The Ziegler-Nichols method is another popular method of tuning a PID controller. It is very similar to the trial and error method wherein I and D are set to zero and P is increased until the loop starts to oscillate. Once oscillation starts, the critical gain Kc and the period of oscillations Pc are noted. The P, I and D are then adjusted in a tabular column.

Bye!

Until the JayThree X Drone!

Cheers!

Download All Files From Google Drive

Part 1 — — Part 2 — — Part3Part 4 — -Part5

Related Posts

23° ArduSeries — PID sample for Arduino — HowTo Control devices with PIDLibrary

5/5 Part JayThree Balancing Car Project — PID without a PhD

15º Lego Episode — PID — The Ultimate Line Follower — Algorithm for your EV3 PID Line Follower Robot

References & Credits:

https://diyhacking.com/build-arduino-self-balancing-robot/
https://www.monocilindro.com/2016/06/04/how-to-calculate-tait-bryan-angles-acceleration-and-gyroscope-sensors-signal-fusion/
http://nxtotherway.webs.com/
http://www.ece.utah.edu/~ece3510/lab_Inverted%20Pendulum_S05.pdf
http://free-electrons.com/labs/doc/nunchuk.pdf
http://wiibrew.org/wiki/Wiimote/Extension_Controllers
http://blog.jumpear.com.br/2010/10/29/comunicando-com-o-nunchuk/
https://www.arduino.cc/en/Reference/PortManipulation
https://github.com/GabrielBianconi/arduino-nunchuk/blob/master/ArduinoNunchuk/ArduinoNunchuk.cpp
https://www.csimn.com/CSI_pages/PIDforDummies.html
http://www.ni.com/white-paper/3782/en/

— edited at Oct 2018 — Grammar corrections w/ Grammarly

--

--

J3
Jungletronics

Hi, Guys o/ I am J3! I am just a hobby-dev, playing around with Python, Django, Ruby, Rails, Lego, Arduino, Raspy, PIC, AI… Welcome! Join us!