dsPIC PID Control Basics

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 7.

Moderator: Benj

Post Reply
User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

dsPIC PID Control Basics

Post by Jay Dee »

Hi Guys,
I am trying to get to grips with the P, PI and PID DSP components. Using ECIO dsPIC
I have tweaked the PI example, (an possibly broken!) adding a display. I am then playing with the result using basic hardware on the bench.
I will be using;
>DC Motor driven linear actuator
>Suitable motor controller to drive the actuator
>a Pot to define the set-point.
>a feedback pot attached to the linear actuators output shaft.

My expectation of basic P control is,
a comparison between set-point and feedback, resulting in an error value. The DSP control buffer will result in a +/- gain value that is a function of the PID settings and the error value.

If I have a setpoint of 128 and feedback of 100, I expect a positive gain to drive the output forwards to the setpoint value.
If I have a setpoint of 128 and the feedback is 150, then I should get a negative gain to drive the output backwards to the setpoint value.

However my 'control' output variable is always positive. Again I've played around but not making much progress.

See my basic FC code so far..
dsPIC_ECIO_PI_Test.fcfx
(40.77 KiB) Downloaded 362 times

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

Re: dsPIC PID Control Basics

Post by Jay Dee »

OK, so if you change the DSP system components sign, to signed. You do get positive and negative numbers.

I disabled the simulation 2nd and 3rd order responses, as (for me) they were just confusing the situation in simulation.
I can see the values in Console and these seem to what I would expect.

Setting the 'P' value to something like 2 gives a more easily understandable result. (SetPoint-FeedBack)xP = Control say, (128 - 120)x2 = 16

But playing about I noticed;
The data recorder seems to have fixed values of 0-5, and wont show negative values.
The system debugger, when showing the working values, show the Setpoint and Feedback values but displays the Control value in an un-formatted 2's compliment, it does not display an actual Negatives value. Is this a feature or bug or other ? :) J.

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: dsPIC PID Control Basics

Post by Steve »

Hi J,

Ben is our resident PID expert but he's not around at the moment. He's away for another week but should be able to answer when he is back. Hopefully another user can help if you are desperate for an answer...

Regards,
Steve.

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

Re: dsPIC PID Control Basics

Post by Jay Dee »

Hi,
:EDIT: I know Ben is away so I may just have to wait it out. :)
So have been having another play..
By using Signed Integers I've made some progress, however the values output to the gLCD in sim and hardware are not what I would expect and are different to those shown in the console during simulation.
'Control' value shown on the gLCD wont go negative.

'Control' value shown in console is inverted from what I'd expect,
>large values when Setpoint is close to Feedback
>small value when Setpoint is far from Feedback

Is this a feature, bug or operator error? :)
I've attached my test FC, and disables all the stuff that is not really relevant ( the i2C motor controller macros)
dsPIC_ECIO_MD04_Test.fcfx
Test FC program
(53.9 KiB) Downloaded 331 times
Any help appreciated.
J.

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: dsPIC PID Control Basics

Post by Steve001 »

Not wanting to hijack a post a PID tutorial would be good

Steve
Success always occurs in private and failure in full view.

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

Re: dsPIC PID Control Basics

Post by Jay Dee »

No Hijack required!. I also need all the help I can get!
From my side. as I make progress, I'l try to post anything relavent;
I know the Matrix guys are flat out at the moment but for sure, some additional guidance on these areas would be great. :)
J.

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

Re: dsPIC PID Control Basics

Post by Jay Dee »

Apologies for a blatant bump, I'm getting a better feel for some of the basics of the PID but still struggling to understand why the output values do not swing between the ranges I would expect? Regards, John.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: dsPIC PID Control Basics

Post by Benj »

Hello,

I made a P, PI and PID example here which may help.
https://www.matrixtsl.com/wikiv7/index. ... 43d3715cbe

There is also an overview of the DSP components and how they work here.
https://www.matrixtsl.com/wikiv7/index.php?title=DSP

Here are some examples taken from our motors course using the PID control.
5. PositionControl-PID-Control.fcfx
(32.03 KiB) Downloaded 385 times
5. SpeedControl-PID-Control.fcfx
(30.54 KiB) Downloaded 371 times
Sounds like there may be some overflowing going on either in your variables or in the DSP variables. I'm having a look at your program now to see if I can spot the cause of the problem.

...

Maybe try changing the DSPSystem buffer size property to 1 or change the DSPControl process property to Last Value, this way you are reacting to a single signal rather then 8 signals in an array.

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

Re: dsPIC PID Control Basics

Post by Jay Dee »

Hi Ben, Thanks for the info and additional motor examples.
I've had another play, starting at the basics of my FC.

Ahhh.... please scroll down attached screen shot. Are the the console and watch windows values confused some how?? :?
Can you replicate this behaviour? I get it on my laptop and desktop both running FC 7.2.0.5
Values
Values
FC_PID_Values.JPG (64.9 KiB) Viewed 13513 times
The 'feedback' value in the Console is showing the 'control' (error) value.
The 'control signal' value in the Console is showing the 'Feedback' value.

and the values in the Watch windows also don't seem to want to go negative..
Is this something I have messed up?
I don't think my control variable is overflowing as its a 16bit Signed.
J.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: dsPIC PID Control Basics

Post by Benj »

Hello,

The bytes could be having an effect, they won't go negative and maybe effecting the signed int calculation?

Maybe try changing all the variables to signed int type and this might help.

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

Re: dsPIC PID Control Basics

Post by Jay Dee »

Hi,
My results are only from the SIM but.
I've ensured, control, setpoint and feedback are all Signed Integers. The numbers show on sim are still not what I would expect;
however... is what I am expecting correct!! :D
If,
I have a set point ADC at 128 bits (0-255), and a feedback ADC at 100 bits (0-255)
I expect to see a Feedback value of 100 and a control (error) value of 28
assuming P control only and a P term of 1.0

This aside, the values seen in the Console dont match those shown in the Watch window.

Its possible I have screwed the basic setup.. my Feedback value is fed into DSPInput1 and the Setpoint value is fed into DSPControl1.
P_Control_Test_SINT.fcfx
(22.43 KiB) Downloaded 267 times
J.

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

Re: dsPIC PID Control Basics

Post by Jay Dee »

Hi Guys,
I'm back on this project start of next week and really need to make some progress quite soon.
If you think the problem we are seeing, are just down to my FC code, I'll keep working away and try other approaches
Just want to check the odd values I'm reporting are not bugs, before I commit more time as the schedule is already pretty tight.
Cheers, J.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: dsPIC PID Control Basics

Post by Benj »

Hello,

It might be worth a try on hardware and see what this does.
I have a set point ADC at 128 bits (0-255), and a feedback ADC at 100 bits (0-255)
I expect to see a Feedback value of 100 and a control (error) value of 28
assuming P control only and a P term of 1.0
This sounds correct to me. I did start having a play with your program but then got pulled off onto something else. I'll see if I can have another quick look for you now and remember where I got to.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: dsPIC PID Control Basics

Post by Benj »

Here is the version I was playing with, I disabled the timer interrupt as this currently has some problems in simulation in the latest version of v7. I instead call the interrupt macro inside the main loop.

For hardware this can go back to being interrupt driven to give you a stable sample rate.
P_Control_Test_SINT.fcfx
(26.34 KiB) Downloaded 227 times

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

Re: dsPIC PID Control Basics

Post by Jay Dee »

Thanks Ben,
I'll play with this in Hardware next week, using two basic pots and outputting the values to a screen.
Cheers for your efforts so far. J.

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

Re: dsPIC PID Control Basics

Post by Jay Dee »

Found it!!!!! rough working example attached for anyone following along!
If you want signed outputs... you need to use the signed macros for DSPOutput1 DOH!
it was ReadAsByte changed to ReadAsSINT
In the example below, I've changed all variable and macros to SINT (Signed Integer) types, I doubt its necessary for all macros but it got this basic example working.
P_Control_Working.fcfx
P_Control_Working
(23 KiB) Downloaded 293 times
Now working on implementing it on a MIAC dsPIC for some quick bench testing...

Post Reply