top of page

Fast PID controller implementation:

Aim of the lesson:

In this lesson, we will learn how to set up the dsPIC environment with the help of the MCC, and how to design a PID controller which uses the DSP specific instructions.

The hardware:

Our hardware consists of a brushed DC motor coupled with a gearbox, a quadrature encoder, and a switchable load. The dsPIC can't source enough current to drive the DC motor, so a driver circuit with Mikroelektronika Click compatible header will be used.

System overview:

syst-1.png

The image above shows a block diagram of the closed loop system containing the motor, the encoders with the ADC, the controller, the actuator with the DAC. The workflow of the project can be decomposed to a few sub-tasks:

  1. The implementation and testing of the discrete time PID control algorithm.

  2. The implementation of the coefficient generator algorithm.

  3. The implementation of the project wrapper containing the speed calculation from encoder pulses, the PWM generation based on the calculated duty (control signal).

The PID algorithm:

We can write the discrete PID algorithm from the continuous form - which was shown on multiple occasions in the control theory courses: 

equation-sum.png

Or in a more convoluted form, using the output from the previous step:

equation-1.png

From this equation we can see that 3 variables will be needed in the X memory space - the coefficients, and we will use 3 variables in the Y memory space for the error terms.

​

The coefficient generator function expects the kp, kiTs, kd/Ts values, and outputs the three coefficients shown above.

​

You can download a starter asm file from here - this file must contain the above mentioned two functions.

If you simulate the function with a step signal as input, what will you get as output?

The Microchip Code Configurator (MCC) GUI:

The MCC is a neat tool in the MPLAB X program, which lets us to configure the microcontrollers peripheral graphically, and generates the necessary C functions for the initialization, register set-up and interrupt calls.

The interface is started from Tools/Embedded/MPLAB Code Configurator, or from the blue MCC icon.

Untitled-1.jpg

The System module window can be used to set the MCU frequency to 140MHz with the primary oscillator. Enable clock switching to only use the PRI, when the frequency is stable.

 

An external interrupt on rising edges will be used to count the encoder pulses (Pin B5). We will use a timer with 5kHz interrupts as the control loop. We will generate the PWM signal with the help of the Output Compare module in Edge Aligned PWM mode (Pin F1). The primary compare count is 0x0, the secondary is 0x3FF (why this number?).

The LED pins are A8, B4, C0, C1, E12, E13, E14, while the switches are on pins C7, C8, D5, D6.

​

The EXT_INT interrupt routine should increase a global variable.

In the TMR2 interrupt we need to calculate the speed of the motor (by integrating the encoder pulses - fs = 5000Hz, N = 500), Calculate the error term, saturate the control duty, and modify the OC primary counter value based on the duty.

​

if for some odd reasons the MCC won't start here you can find the necessary files.

These LCD files will let you use printf for useful information display.

bottom of page