# [PIC]DSP-PID for PIC33EP512MU814

Status
Not open for further replies.

#### urnuj

##### Junior Member level 3
Hi all

I am doing up a project that requires PID control and I am trying to use the dsp to achieve the PID control.
i have included the files #include <dsp.h> #include <libq.h> in my source, declared the followings:

Code:
tPID fooPID;
fractional abcCoefficient[3] __attribute__ ((section ("xmemory,bss")));
fractional controlHistory[3] __attribute__ ((section ("ymemory,bss")));
fractional kCoeffs[] = {0,0,0};

and i have the initialization:

Code:
fooPID.abcCoefficients = &abcCoefficient[0];
fooPID.controlHistory = &controlHistory[0];
PIDInit(&fooPID);
kCoeffs[0] = Q15(0.7);
kCoeffs[1] = Q15(0.2);
kCoeffs[2] = Q15(0.07);
PIDCoeffCalc(&kCoeffs[0], &fooPID);

However, when i build the project,it failed and the make file could not be created.
The following errors occured:

build/default/production/tempControl.o(.text+0x4a4): In function .LSM164':
: undefined reference to _PIDInit'
build/default/production/tempControl.o(.text+0x4b6): In function .LSM168':
: undefined reference to _PIDCoeffCalc'
make[2]: *** [dist/default/production/thermalControl.X.production.hex] Error 255
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
I have tried to put the libq-dsp-coff.a and the libdsp-coff.a file in the library folder but it still doesn't help.

I am using MPLABX IDE with xc16 compiler. Is dsp PID supported with this or do I have to port over to C30 complier?
Is there a way to get around this problem without having to switch complier?

Thanks a lot for any help from you guys.

Regards

Last edited by a moderator:

#### Aussie Susan

The thing you have not mentioned is telling the IDE to include the library when it is performing the linking operation. Putting the library file in the right place is not enough - you need to add the "-library libname" or similar to the command line. This is typically done by adding an entry to the linker section options within the IDE. (I'm at work right and I can't remember the exact path to the place to enter the option but it is part of the linker section of the project options.)
Some libraries are included in the link by default but not all.
Susan

urnuj

### urnuj

Points: 2

#### bigdogguru

Resolving this issue depends largely on the specific compiler toolset and the associated IDE utilized to develop your code.

If you are using the XC16 compiler in combination with the MPLAB X, simply adding the libraries to the current project library folder may or may not automatically cue the MPLAB X IDE to insert the proper -l options into for linker command line configuration. However, as Susan pointed out they need to be present, one way or another, therefore check the actual linkage commands issued in the appropriate Output window or check the appropriate Project Properties settings, XC16-ld, Option Categories: Libraries -> Libraries and Library Directories.

Please note, as these are COFF libraries, you will need to ensure the Object Model Format is set to COFF, not the default ELF/DWARF.

You can change these settings under the Project Properties Settings Window:

On the right hand side, Categories: XC16 Global Options -> On the left hand side, Options for XC16-GCC, Option Categories: drop down menu, Global Options, beneath, Output File Format -> drop down menu, COFF.

Or you could elect to utilize the ELF versions, libq-dsp-elf.a and the libdsp-elf.a instead and retain the default OMF of ELF/DWARF.

BigDog

urnuj

### urnuj

Points: 2

#### urnuj

##### Junior Member level 3
I have changed it to the elf file and it is able to build successfully. Thank you all for the help. Thanks for pointing out to me the difference in coff and elf and how to change the options for the compiler which helps a lot.

And for MPLABX with xc-16, it is automatically linked and all that has to be done is to add the library in the folder. The only thing is to use the correct object model.

Thanks again.

#### urnuj

##### Junior Member level 3
Hi

Now that I am able to compile the file, there is a problem when I run the program.

as [Target halted due to a break instruction in user code] keeps occurring. It happens when it runs through the

Code:
PID(&fooPID);

Can anyone help me with what is wrong with the code?

Could it be possibly due this declaration? :

Code:
tPID fooPID;
[COLOR="#FF0000"]fractional abcCoefficient[3] __attribute__ ((section ("xmemory,bss")));
fractional controlHistory[3] __attribute__ ((section ("ymemory,bss"))); [/COLOR]
fractional kCoeffs[] = {0,0,0};

#### Aussie Susan

Without knowing the PID function at all, the error you have shown is unlikely to be from the declaration (as long as it conforms to whatever the documentation says) but could well be because there is an initialisation or similar set that could be missing.
I have looked at the Microchip web site and the references to PID (other than with respect to USB) are AN937 and AN964. The assembler source code that comes with AN937 seems to have a suite of functions.
For example, you define "abcCoefficient", "controlHistory" and "kCoeffs" as well as "fooPID" but are these correctly initialised and (perhaps - really guessing here) referenced in the "fooPID" strcuture?
What I can imagine is happening is that the 'PID' function expects the 'fooPID' (or something) to have a pointer to a function that is not set correctly; therefore the CPU is trying to execute code from soem random part of memory and is therefore seeing a code sequence that it is interpreting as a "breakpoint".
Susan

urnuj

### urnuj

Points: 2

#### bigdogguru

As Susan alluded to, these types of issues are very difficult to troubleshoot without access to the entire source code. Therefore, I would recommend you either post the source code or ZIP up the project folder and upload it to this forum's servers using the Manage Attachment feature.

Once the source code is available for complete analysis, we can then attempt to resolve the issue.

BigDog

urnuj

### urnuj

Points: 2

#### urnuj

##### Junior Member level 3
I have changed the declaration of abcCoefficients and controlHistory by changing the data allocation to it with this:

Code:
fractional abcCoefficient[3] __attribute__ ((space (xmemory)));
fractional controlHistory[3] __attribute__ ((eds,space(ymemory)));

Now, it is able to work, and the controlOutput can be calculated out.

Thank you all for the help.

- - - Updated - - -

Another question is that if I am to have a gain larger than 1, and I manually scale the value down to a value smaller than 1, say for example, scaling Kp of 20 down to 0.2 by dividing it by 100 and scaling up the controlOutput by multiplying it by 100, is it a possible way that would not affect the result of the PID function?

I would scale the inputs by the same factor and the output will be scale by the same factor.

Thanks

Status
Not open for further replies.