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

Click For Summary

Discussion Overview

The discussion revolves around implementing PID control in an Arduino project without using the loop function. Participants explore various methods to maintain PID functionality while executing other tasks in an autonomous mobile robot designed to manipulate glass tiles.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their project involving a mobile robot that uses a DC motor with an infinite turn potentiometer for closed-loop feedback and expresses the challenge of running PID control code continuously without the loop function.
  • Another participant suggests using global variables for PID components and setting up a regular interrupt to keep these variables updated, proposing an interrupt rate between 10 Hz and 1 kHz.
  • There is a question about the necessity of machine vision for the robot's operation, with some participants discussing alternatives like mechanical tracks or IR distance sensors.
  • One participant explains the concept of an infinite turn potentiometer and argues that it provides better precision for position control compared to rotary encoders.
  • Another participant proposes a cooperative multitasking scheme as a potential solution for managing PID control alongside other tasks, providing links to relevant resources.
  • Concerns are raised about the bandwidth consumption of a single Arduino processor when controlling PID loops, with suggestions to use a second Arduino for task separation and communication methods.
  • One participant discusses the implementation of interrupt-based code to enhance microcontroller throughput and multitasking capabilities, mentioning the potential for establishing a Real-Time Operating System (RTOS) on Arduino devices.
  • A later reply acknowledges the suggestions provided and expresses a willingness to study multitasking further.

Areas of Agreement / Disagreement

Participants express a variety of approaches and solutions to the problem, with no clear consensus on the best method for implementing PID control without the loop function. Multiple competing views and suggestions remain present throughout the discussion.

Contextual Notes

Participants discuss the limitations of using a single Arduino for PID control and other tasks, highlighting the need for potential hardware solutions or software strategies like interrupts or multitasking. Some assumptions about the capabilities of the hardware and the nature of the tasks are not fully explored.

Who May Find This Useful

This discussion may be useful for robotics enthusiasts, Arduino users, and those interested in control systems, particularly in the context of implementing PID control in embedded systems.

mastermechanic
Messages
107
Reaction score
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
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.
 
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.
 
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.
 
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   Reactions: sysprog and mastermechanic
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   Reactions: berkeman

Similar threads

  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
9
Views
3K
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
6K