PID control with DSP

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 5.
To post in this forum you must have a registered copy of Flowcode 5 or higher.

Moderator: Benj

Post Reply
benp
Posts: 155
Joined: Sat Mar 28, 2009 5:44 pm
Location: LYON FRANCE
Has thanked: 3 times
Been thanked: 41 times
Contact:

PID control with DSP

Post by benp »

I will test in a short time on hardware the pid control with DSP control on flowcode DsPIC.
There is no macro which allow to live control the P, I and D parameters.
Adjusting PID with software(with DSP control properties) is not a recommended method.

Can matrix staff do a macro to live control PID?

It is probably possible to do it in custom code but it will be much more easy with a macro for my students(have somebody already do that?).

If you want to learn how to live adjust you PID parameters with almost no theory, PIDSTOP is a very good website.
You can practice with chopper for example:
http://www.pidstop.com/index.php?r_id=641
Regards
INSA 1er cycle GCP projects with or without eblocks:
http://www.youtube.com/user/INSAgcp

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: PID control with DSP

Post by Benj »

Hello,

The P,I and D parameters can be edited by doing the following.

Select the DSP control component and then click on the custom code.

Select the defines function and click the edit button.

Find these lines of code and comment them out by adding "//" to the start of the line.

Code: Select all

#define %a_PROPORTIONAL         %h
#define %a_INTEGRAL             %i
#define %a_DIFFERENTIAL         %j
Should look like this once it has been done.

Code: Select all

//#define %a_PROPORTIONAL         %h
//#define %a_INTEGRAL             %i
//#define %a_DIFFERENTIAL         %j
Below these commented out lines add the following lines of code.

Code: Select all

#define join_dsp(b)           (%a##b)
MX_UINT8 %a_PROPORTIONAL =        %h
MX_UINT8 %a_INTEGRAL =            %i
MX_UINT8 %a_DIFFERENTIAL =        %j

#define MXP  join_dsp(_PROPORTIONAL)
#define MXI  join_dsp(_INTEGRAL)
#define MXD  join_dsp(_DIFFERENTIAL)
You can then update the parameters at any time in your Flowcode program by using a C icon to copy values from Flowcode variables P, I and D into the C variables.

Code: Select all

MXP = FCV_P;
MXI = FCV_I;
MXD = FCV_D;
Let me know how you get on.

benp
Posts: 155
Joined: Sat Mar 28, 2009 5:44 pm
Location: LYON FRANCE
Has thanked: 3 times
Been thanked: 41 times
Contact:

PID with DSP: not ready to use

Post by benp »

I performed some test on motor and as5030 encoder.
The P control work properly but not de PI control!

Here is a simple test program:
virtual pidV02bug.fcf_pic16
(33.53 KiB) Downloaded 250 times
With a static error you should have an increasing output. That's not the case.

You write in your C code:

Code: Select all

Out = P * (Error - Prev_Error + (Error / I))
But that's not true. See here:
http://electronics.stackexchange.com/qu ... lemenation

You should write:

Code: Select all

Out = OLD out+P * (Error - Prev_Error + (Error / I))
It is possible to correct the C code with:

Code: Select all

%a_OUTPUT_NAME[MXDSP_idx] = OLDval+%a_PROPORTIONAL * (error - errorm1 + (error / %a_INTEGRAL));
         OLDval =%a_OUTPUT_NAME[MXDSP_idx];
The result in flowcode is here:
virtual pidV02bugcor.fcf_pic16
(37.67 KiB) Downloaded 238 times
That better but still not good.
See in a next post...
Regards
INSA 1er cycle GCP projects with or without eblocks:
http://www.youtube.com/user/INSAgcp

benp
Posts: 155
Joined: Sat Mar 28, 2009 5:44 pm
Location: LYON FRANCE
Has thanked: 3 times
Been thanked: 41 times
Contact:

Re: PID control with DSP

Post by benp »

virtual pidV04bugcor.fcf_pic16
(38.09 KiB) Downloaded 278 times
With the code from last post, the results are better: the output increase with a static error.
There are still some problems to solve:
- There is no limit for the output
- error /I=0 when error is small and I is big with integer calculation. This mean that a small static error will not increase the output(we use I especially for small static error).
Example: If error=2 and I=10 then error/I=0.

One solution to solve both problems this is:

Code: Select all

//Out = OLD out+P * (Error - Prev_Error + (Error / I))

		 NEWval = OLDval+ %a_PROPORTIONAL * %a_INTEGRAL *(error - errorm1) + %a_PROPORTIONAL*error;
         
         tempval=NEWval/%a_INTEGRAL;
         if (tempval<-126) {tempval=-126; NEWval=-126*%a_INTEGRAL; }
         if (tempval>126) {tempval=126; NEWval=126*%a_INTEGRAL; }
		 %a_OUTPUT_NAME[MXDSP_idx]=tempval;

		//Shift error for next iteration
        OLDval =NEWval;		
        errorm1 = error;
Here is the flowcode example:
virtual pidV04bugcor.fcf_pic16
(38.09 KiB) Downloaded 278 times
This solve only PI for Process_Tick. There is more work to do on PID and process PI and PID.

It would be a good idea to add the ouput limit in the DSP control properties.

I don't have time to look at the FFT code:
http://www.matrixmultimedia.com/mmforum ... 009#p46372
I hope that's not the same...
Regards
INSA 1er cycle GCP projects with or without eblocks:
http://www.youtube.com/user/INSAgcp

benp
Posts: 155
Joined: Sat Mar 28, 2009 5:44 pm
Location: LYON FRANCE
Has thanked: 3 times
Been thanked: 41 times
Contact:

Re: PID control with DSP=not ready to use

Post by benp »

Still no news from that post!
Can you correct the PID component: only P is working properly.

Our prototypes could be good DSP demonstrators if you correct your bug quickly.
3 prototypes with PID will be manufacture this year. The students already implement PID without the flowcode DSP components because you didn't correct the bugs...
You still have one possible demonstration with FFT:
http://www.matrixmultimedia.com/mmforum ... 009#p46845
Regards
INSA 1er cycle GCP projects with or without eblocks:
http://www.youtube.com/user/INSAgcp

Post Reply