How to implement PID control in Arduino w/o using loop function?

In summary, the project was about designing an autonomous and mobile robot that picks up 9 glass tiles from a stack point and place them into a 3x3 matrix with minimum tolerance. The robot uses a DC motor with an infinite turn potentiometer for closed loop feedback. However, implementing PID control in the actual project was a challenge as the robot has specific tasks. The solution was to write a separate function with a while loop, but this caused a loss of PID functionality. To overcome this, global variables were set up and a regular interrupt was used to keep them up to date. Another solution could be using a second Arduino to handle all other tasks while one controls the motor. Additionally, a cooperative multitasking scheme could be implemented.
  • #1
mastermechanic
108
15
Last week I encountered a problem in my graduate project. The project was about designing an autonomous and mobile robot that picks up 9 glass tiles from a stack point and place them into a 3x3 matrix with minimum tolerance.

I am using a DC motor with an infinite turn potentiometer for closed loop feedback. For testing purposes I wrote a different Arduino sketch to test PID code and in that sketch the code was inside loop function but in the actual project the robot has specific number of tasks, therefore, I wrote my functions inside setup function so that it does it only once. However, PID control code must run all the time so that it can detect disturbances and make corrections but as you can imagine I cannot write it inside loop function due the reasons I mentioned above.

What I did was, I wrote a separate function and wrote the PID code inside a while (true) loop, when the actual position is within 99-101% of the target position it breaks the loop. But in this case, we lose the PID functionality since I am manually stopping the operation.

My question is, is there any method for arduino to implement PID loop function while other codes are running. I was thinking about interrupts but couldn't find what I want. First of all, is it possible? or do we need to run this PID code in another board? How it is done in industrial projects?

I am waiting your thoughts., thank you in advance.
 
Last edited:
Engineering news on Phys.org
  • #2
mastermechanic said:
But in this case, we lose the PID functionality since I am manually stopping the operation.
You need to set up some global variables; Prop, Integ and Differ. Also set up a regular interrupt, with code that keeps those variables up to date, and calculates the response each time.
I expect the interrupt rate will be somewhere between 10 per sec and 1k per sec.
 
  • #3
mastermechanic said:
The project was about designing an autonomous and mobile robot that picks up 9 glass tiles from a stack point and place them into a 3x3 matrix with minimum tolerance.
Wow, that's a complex project. Do you need to implement machine vision to find the tubes and direct the arm to do the pickups and placements in the holder? Or is there some simplifying mechanical track that you will follow instead?
mastermechanic said:
I am using a DC motor with an infinite turn potentiometer for closed loop feedback.
What's an infinite turn potentiometer? And you don't usually control the speed of a DC motor with a potentiometer -- using PWM will give you much better performance.

When you do the calculations & experiments using interrupts for your control loop, you may find that most of the bandwidth of your single Arduino processor is consumed controlling that loop. If so, consider adding a 2nd Arduino so that one can control the motor and the other can handle all of the other tasks. You will need to figure out the best way to communicate between the two processors, like using a mailbox scheme or Master/Slave communication.
 
  • #5
berkeman said:
Wow, that's a complex project. Do you need to implement machine vision to find the tubes and direct the arm to do the pickups and placements in the holder? Or is there some simplifying mechanical track that you will follow instead?
Sorry for late reply, I am already graduated and I have done it in a simpler manner by using IR distance sensor, when the measured distance equals the target distance motor stops. So, I didn't use PID controller. Here is the robot if you wish to see,

IMG_4713.PNG
IMG_4658.jpg


I am not much happy with the result though :) Because I couldn't use my engineering skills, it was like a Youtuber content. I am now searching for solutions about the challenges I encountered.

berkeman said:
What's an infinite turn potentiometer? And you don't usually control the speed of a DC motor with a potentiometer -- using PWM will give you much better performance.
Infinite turn potentiometer is pretty much the same with usual potentiometer but without a 180 or 270 degree limitations. As you turn it, it outputs an analog signal between 0-1023, after 1023 it returns to 0. By writing a few lines of code you can obtain a signal value that constanstly increases or decreases as you turn it.

The reason I needed to use it was because I wasn't interested in the speed control but position control for feedback system. Using infinite turn potentiometer enables you to acquire much more precision compared to the cheap rotary encoders. So, that was the reason.

berkeman said:
When you do the calculations & experiments using interrupts for your control loop, you may find that most of the bandwidth of your single Arduino processor is consumed controlling that loop. If so, consider adding a 2nd Arduino so that one can control the motor and the other can handle all of the other tasks. You will need to figure out the best way to communicate between the two processors, like using a mailbox scheme or Master/Slave communication.
In fact, that's what I thought at first, I could have used this method but as I said I did it without PID. What I am wondering is, is this the method used in the industry? I mean do all robots in the world use different controller for their PID loop? Or is there any software solution (interrupts etc.) for this that I couldn't find.
 
  • #6
mastermechanic said:
Or is there any software solution (interrupts etc.) for this that I couldn't find.
There is simple interrupt based code that when implemented greatly increases the throughput of a microcontroller and allows multitasking. Because it establishes a regular time-base, several PID loops can execute as short digital background tasks, while the processor spends most of the time in an idle state, on reduced power.

If the processor turns a LED on when the real-time interrupt arrives, then turns the LED off when entering the idle state, the LED brightness will let you know if you need to check the processor work load. Examining that LED output with an oscilloscope will show you the duty cycle of the processor during software development. If the processor has not finished the list of tasks and become idle before it receives the next interrupt, then it can reduce the work load by priority, or signal a task overload.

Even the arduino trinket has all the handles needed to establish a Real-Time OS. It will take you a little time to read the documentation and understand how to write the interrupt task and list. You must first step back and approach the code from the RTOS point of view. I have a trinket here that asynchronously flashes away when I power it. But I don't know were my source code ended up.
As another example, long ago I wrote a minimum RTOS in assembly for the PIC16C84. If I remember correctly, the OS ran in about 37 instructions, while handling lists of separate asynchronous tasks.

As an experiment and to test micro-RTOS code, you should write tasks that simply flash two LEDs at different slow rates, while being based on the same real time interrupt rate. Flashing LEDs at different rates is very difficult without a RTOS, but becomes trivial with a RTOS.
 
  • Like
Likes sysprog and mastermechanic
  • #7
Thank you @Baluncore for your detailed response, what you're suggesting is I think, the same with what @DaveE suggested. I don't know about multitasking but I will study based on your suggestions. Thank you again!
 
  • Like
Likes berkeman

1. What is PID control and how does it work?

PID control stands for Proportional-Integral-Derivative control, and it is a control algorithm commonly used in industrial and scientific applications. It works by continuously measuring the error between a desired setpoint and the actual output of a system, and adjusting the control input to minimize this error.

2. Why would I want to implement PID control in my Arduino project?

PID control is useful for controlling systems that need to maintain a specific setpoint, such as temperature, speed, or position. It can also help to improve the accuracy and stability of a system, making it a valuable tool for many projects.

3. Can I use the built-in PID library in Arduino to implement PID control?

Yes, Arduino has a built-in PID library that can be used for implementing PID control. However, this library uses the loop function, which may not be suitable for certain projects. There are alternative methods for implementing PID control without using the loop function.

4. How can I implement PID control in Arduino without using the loop function?

One method is to use interrupts, which are hardware signals that can trigger a specific function to run. By setting up an interrupt to trigger at a specific interval, you can create a pseudo-loop that can be used for implementing PID control. Another method is to use a state-machine approach, where the control algorithm is broken down into smaller steps and executed sequentially.

5. Are there any limitations to implementing PID control in Arduino without using the loop function?

Yes, there are some limitations to consider when using alternative methods for implementing PID control. These may include reduced accuracy, slower response times, and increased complexity in code. It is important to carefully evaluate the requirements of your project and choose the best method for implementing PID control.

Similar threads

Replies
5
Views
3K
  • Mechanical Engineering
Replies
3
Views
1K
  • Mechanical Engineering
Replies
10
Views
2K
Replies
9
Views
2K
Replies
7
Views
1K
  • Electrical Engineering
Replies
10
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Electrical Engineering
Replies
5
Views
2K
  • General Engineering
Replies
2
Views
2K
  • General Engineering
Replies
2
Views
5K
Back
Top