Guy and Seth on Simulink

Implementing a PID Controller on an Arduino Board 12

Posted by Guy Rouleau,

This week my colleague Pravallika is back to continue her motor control story!

Pravallikota Vinnakota, guest blogger

In my previous post, we saw how to estimate continuous transfer functions with System Identification Toolbox. We estimated the following transfer function for a simple DC Motor using tfest:

Transfer function identified from a DC Motor

For this transfer function, we designed the following controller using pidtune:

Controller generated by pidtune

We will now implement the controller on the Arduino Uno and see how the DC motor fares with this controller. To deploy the controller on the hardware, we will use Simulink’s capability to generate an executable and run it on selected hardware.

Deploying controller to the Arduino board

You probably noticed that the controller shown above is in a continuous form. To use it on our target, the first thing to do is to discretize it using the c2d function:

Discretizing the controller generated by pidtune

Then we grab the PID block from the Simulink Library and configure it.

PID block configuration

To keep the PID controller’s output within the limits of the hardware, we go to the PID Advanced tab and enable output saturation along with anti-windup protection.

Advanced PID block configuration

To test the controller on the hardware, we created a Simulink model using blocks from the Arduino Support Package.

As you can see, we receive the desired motor position from the serial port and compare it to the measured position from the Analog Input. The position error goes through the PID block which generates a voltage to be sent to the motor. We also send the measured position through the serial port.

Controller model to be used on the Arduino board

Hardware response

We ran the model on the target, sent it some commands and logged the data transmitted through the serial port. Here is what it looks like:

Data logged from our controller

So, it does a pretty good job of tracking the reference signal. When we compare the response from the hardware to that of the simulation, we observe that they are very close! The System Identification Toolbox model is quite good.

Now it is your turn

How are you designing controllers when a system is difficult to model? Have you tried the Run on Target Hardware capability in Simulink to run your models on the supported boards? Let us know by leaving a comment here.

12 CommentsOldest to Newest

Sorry about a question outside the topic.
How did you make your photos frame like that? I really interested in it.
Thanks!

Hey, this is great! thanks for this.

But you could have realised this in a better manner, by using only 1 output pin. Just check the sign of the outputting PID control signal and basing on that, control the PWM signal by negating it etc.

Hi. How are you representing the angle? 0-360 degrees or 0..+1..+2…+179..+180..-179..-178..-2..-1..0 degrees.

Is this command ‘pidtune’ new introduced in 2013a?

Because in my version 2010a, doc pidtune doesn’t work.

Hi,

I don’t understand how it was processed the PID output signal:

How can you transform the PID control law to PWM sequence? What is there inside “Convert to PWM” blocks?

Hi,
1. I don’t understand why also send the measured position through the “serial port 0″?
2. we receive the desired motor position from the “serial port 0″,and send the measured position through the same serial port?
3. receive the measured position by USB ?if i want to get the data from arduino serial port,which block should be used?serial receive block or serial transmit block?
thank you!

I have a question about the “Convert to PWM” block , I can’t understand how did you do it
thanks.

Add A Comment

What is 1 + 6?

Preview: hide

These postings are the author's and don't necessarily represent the opinions of MathWorks.