practice for PID control

Dependencies:   PID QEI USBDevice mbed

Committer:
NT32
Date:
Tue Apr 15 02:10:03 2014 +0000
Revision:
1:6d5c35b995fb
Parent:
0:b2973a157cb6
Child:
2:b46f1a4c42fb
configurable parameters are maximam input limit and PID gain.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NT32 0:b2973a157cb6 1 #include "mbed.h"
NT32 0:b2973a157cb6 2 #include "USBSerial.h"
NT32 0:b2973a157cb6 3 #include "QEI.h"
NT32 0:b2973a157cb6 4 #include "PID.h"
NT32 0:b2973a157cb6 5
NT32 0:b2973a157cb6 6 #define PIDRATE 0.01
NT32 0:b2973a157cb6 7 #define CW 0x01
NT32 0:b2973a157cb6 8 #define CCW 0x02
NT32 0:b2973a157cb6 9 #define STOP 0x03
NT32 0:b2973a157cb6 10 #define FREE 0x00
NT32 0:b2973a157cb6 11
NT32 0:b2973a157cb6 12 #define kc 2.8
NT32 0:b2973a157cb6 13 #define ti 16.0
NT32 0:b2973a157cb6 14 #define td 0.0
NT32 0:b2973a157cb6 15
NT32 0:b2973a157cb6 16 USBSerial vcom;
NT32 0:b2973a157cb6 17 SPI spi(P0_21, NC, P1_20);
NT32 0:b2973a157cb6 18 DigitalOut cs(P1_23);
NT32 0:b2973a157cb6 19
NT32 0:b2973a157cb6 20 Timer timer;
NT32 0:b2973a157cb6 21
NT32 0:b2973a157cb6 22 PID controller(kc, ti, td, PIDRATE);
NT32 0:b2973a157cb6 23
NT32 0:b2973a157cb6 24 QEI wheel (P1_15, P0_19, NC, 1, QEI::X4_ENCODING);
NT32 0:b2973a157cb6 25
NT32 0:b2973a157cb6 26 BusOut gled(P0_8, P0_9);
NT32 0:b2973a157cb6 27 BusOut mdrv(P1_27, P1_26);
NT32 0:b2973a157cb6 28
NT32 0:b2973a157cb6 29 DigitalIn SW(P0_1);
NT32 0:b2973a157cb6 30
NT32 0:b2973a157cb6 31 void pidsetup();
NT32 0:b2973a157cb6 32
NT32 0:b2973a157cb6 33 union MCP4922
NT32 0:b2973a157cb6 34 {
NT32 0:b2973a157cb6 35 uint16_t command;
NT32 0:b2973a157cb6 36 struct
NT32 0:b2973a157cb6 37 {
NT32 0:b2973a157cb6 38 //DAC data bits
NT32 0:b2973a157cb6 39 uint16_t D :12;
NT32 0:b2973a157cb6 40 //Output power down control bit
NT32 0:b2973a157cb6 41 uint8_t SHDN:1;
NT32 0:b2973a157cb6 42 //Outout gain select bit
NT32 0:b2973a157cb6 43 uint8_t GA :1;
NT32 0:b2973a157cb6 44 //Vref input buffer Control bit
NT32 0:b2973a157cb6 45 uint8_t BUF :1;
NT32 0:b2973a157cb6 46 //DACa or DACb select bit
NT32 0:b2973a157cb6 47 uint8_t AB :1;
NT32 0:b2973a157cb6 48 }bit;
NT32 0:b2973a157cb6 49 };
NT32 0:b2973a157cb6 50
NT32 0:b2973a157cb6 51 union MCP4922 dac = {0xF7F};
NT32 0:b2973a157cb6 52
NT32 0:b2973a157cb6 53 int main()
NT32 0:b2973a157cb6 54 {
NT32 0:b2973a157cb6 55 uint8_t i = 0;
NT32 0:b2973a157cb6 56 int epls[2] = {0, 0};
NT32 0:b2973a157cb6 57 float rps = 0;
NT32 0:b2973a157cb6 58
NT32 0:b2973a157cb6 59 SW.mode(PullUp);
NT32 0:b2973a157cb6 60 mdrv = CW;
NT32 0:b2973a157cb6 61 cs = 1;
NT32 0:b2973a157cb6 62 dac.bit.AB = 0;
NT32 0:b2973a157cb6 63 dac.bit.BUF = 1;
NT32 0:b2973a157cb6 64 dac.bit.GA = 1;
NT32 0:b2973a157cb6 65 dac.bit.SHDN = 1;
NT32 0:b2973a157cb6 66 dac.bit.D = 0;
NT32 0:b2973a157cb6 67 spi.format(16,0);
NT32 0:b2973a157cb6 68 spi.frequency(20000000);
NT32 0:b2973a157cb6 69 cs = 0;
NT32 0:b2973a157cb6 70 spi.write(dac.command);
NT32 0:b2973a157cb6 71 cs = 1;
NT32 0:b2973a157cb6 72 //Revolution per second input from 0.0 to 50.0rev/sec
NT32 0:b2973a157cb6 73 controller.setInputLimits(0.0, 30.0);
NT32 0:b2973a157cb6 74 //DAC output from 0.0 to 4096.0
NT32 0:b2973a157cb6 75 controller.setOutputLimits(0.0, 4095.0);
NT32 0:b2973a157cb6 76 //If there's a bias.
NT32 0:b2973a157cb6 77 controller.setBias(1000.0);
NT32 0:b2973a157cb6 78 controller.setMode(AUTO_MODE);
NT32 0:b2973a157cb6 79 //We want the process variable to be 12rev/sec
NT32 0:b2973a157cb6 80 controller.setSetPoint(12.0);
NT32 0:b2973a157cb6 81 while(SW == 1)
NT32 0:b2973a157cb6 82 {
NT32 0:b2973a157cb6 83 if(i == 100)
NT32 0:b2973a157cb6 84 {
NT32 0:b2973a157cb6 85 i = 0;
NT32 0:b2973a157cb6 86 vcom.printf("\033[%d;%dH", 0, 0);
NT32 0:b2973a157cb6 87 vcom.printf("DAC.d :%012d\n", dac.bit.D);
NT32 0:b2973a157cb6 88 vcom.printf("rps :%12.4f", rps);
NT32 0:b2973a157cb6 89 }
NT32 0:b2973a157cb6 90 i++;
NT32 0:b2973a157cb6 91 gled = i;
NT32 0:b2973a157cb6 92
NT32 0:b2973a157cb6 93 epls[1] = wheel.getPulses();
NT32 0:b2973a157cb6 94 rps = ((float)(epls[1] - epls[0]) / PIDRATE) / 3600;
NT32 0:b2973a157cb6 95 epls[0] = epls[1];
NT32 0:b2973a157cb6 96 controller.setProcessValue(rps);
NT32 0:b2973a157cb6 97 dac.bit.D = (int)controller.compute();
NT32 0:b2973a157cb6 98 cs = 0;
NT32 0:b2973a157cb6 99 spi.write(dac.command);
NT32 0:b2973a157cb6 100 cs = 1;
NT32 0:b2973a157cb6 101 wait(PIDRATE);
NT32 0:b2973a157cb6 102 }
NT32 0:b2973a157cb6 103 pidsetup();
NT32 0:b2973a157cb6 104 i = 0;
NT32 0:b2973a157cb6 105 rps = 0;
NT32 0:b2973a157cb6 106 epls[0] = 0;
NT32 0:b2973a157cb6 107 timer.reset();
NT32 0:b2973a157cb6 108 timer.start();
NT32 0:b2973a157cb6 109 while(1)
NT32 0:b2973a157cb6 110 {
NT32 0:b2973a157cb6 111 if(SW == 0)
NT32 0:b2973a157cb6 112 {
NT32 0:b2973a157cb6 113 timer.stop();
NT32 0:b2973a157cb6 114 pidsetup();
NT32 0:b2973a157cb6 115 timer.reset();
NT32 0:b2973a157cb6 116 timer.start();
NT32 0:b2973a157cb6 117 }
NT32 0:b2973a157cb6 118 if(i == 10)
NT32 0:b2973a157cb6 119 {
NT32 0:b2973a157cb6 120 vcom.printf("%d,%d\n" , timer.read_ms(), dac.bit.D);
NT32 0:b2973a157cb6 121 i = 0;
NT32 0:b2973a157cb6 122 }
NT32 0:b2973a157cb6 123 i++;
NT32 0:b2973a157cb6 124 gled = i;
NT32 0:b2973a157cb6 125 epls[1] = wheel.getPulses();
NT32 0:b2973a157cb6 126 rps = ((float)(epls[1] - epls[0]) / PIDRATE) / 3600;
NT32 0:b2973a157cb6 127 epls[0] = epls[1];
NT32 0:b2973a157cb6 128 controller.setProcessValue(rps);
NT32 0:b2973a157cb6 129 dac.bit.D = (int)controller.compute();
NT32 0:b2973a157cb6 130 cs = 0;
NT32 0:b2973a157cb6 131 spi.write(dac.command);
NT32 0:b2973a157cb6 132 cs = 1;
NT32 0:b2973a157cb6 133 wait(PIDRATE);
NT32 0:b2973a157cb6 134
NT32 0:b2973a157cb6 135 }
NT32 0:b2973a157cb6 136
NT32 0:b2973a157cb6 137 }
NT32 0:b2973a157cb6 138
NT32 0:b2973a157cb6 139 void pidsetup()
NT32 0:b2973a157cb6 140 {
NT32 0:b2973a157cb6 141 char str[64] = {'\0'}, *erstr;
NT32 0:b2973a157cb6 142 float tmp[3];
NT32 0:b2973a157cb6 143 dac.bit.D = 0;
NT32 0:b2973a157cb6 144 cs = 0;
NT32 0:b2973a157cb6 145 spi.write(dac.command);
NT32 0:b2973a157cb6 146 cs = 1;
NT32 0:b2973a157cb6 147 controller.reset();
NT32 0:b2973a157cb6 148 vcom.printf("\033[2J");
NT32 0:b2973a157cb6 149 vcom.printf("\033[%d;%dH", 0, 0);
NT32 0:b2973a157cb6 150
NT32 0:b2973a157cb6 151 //Output bias
NT32 1:6d5c35b995fb 152 // vcom.printf("Input the bias for the controller output\n");
NT32 1:6d5c35b995fb 153 // vcom.printf("within 15 figures.\n");
NT32 1:6d5c35b995fb 154 // vcom.scanf("%s", str);
NT32 1:6d5c35b995fb 155 // tmp[0] = strtof(str, &erstr);
NT32 1:6d5c35b995fb 156 // controller.setBias(tmp[0]);
NT32 1:6d5c35b995fb 157 // vcom.printf("Output bias : %15f\n", tmp[0]);
NT32 1:6d5c35b995fb 158 controller.setBias(200);
NT32 0:b2973a157cb6 159
NT32 0:b2973a157cb6 160 //Input limits
NT32 1:6d5c35b995fb 161 // vcom.printf("Input the minimum inputlimit\n");
NT32 1:6d5c35b995fb 162 // vcom.printf("within 15 figures.\n");
NT32 1:6d5c35b995fb 163 // vcom.scanf("%s", str);
NT32 1:6d5c35b995fb 164 // tmp[0] = strtof(str, &erstr);
NT32 1:6d5c35b995fb 165 // vcom.printf("Minimum input limit : %15f\n", tmp[0]);
NT32 0:b2973a157cb6 166 vcom.printf("Input the maximum inputlimit\n");
NT32 0:b2973a157cb6 167 vcom.printf("within 15 figures.\n");
NT32 0:b2973a157cb6 168 vcom.scanf("%s", str);
NT32 0:b2973a157cb6 169 tmp[1] = strtof(str, &erstr);
NT32 0:b2973a157cb6 170 vcom.printf("Maximum input limit : %15f\n", tmp[1]);
NT32 1:6d5c35b995fb 171 // controller.setInputLimits(tmp[0], tmp[1]);
NT32 1:6d5c35b995fb 172 controller.setInputLimits(0,tmp[1]);
NT32 0:b2973a157cb6 173
NT32 0:b2973a157cb6 174 //Output limits
NT32 1:6d5c35b995fb 175 // vcom.printf("Input the minimum outputlimit\n");
NT32 1:6d5c35b995fb 176 // vcom.printf("within 15 figures.\n");
NT32 1:6d5c35b995fb 177 // vcom.scanf("%s", str);
NT32 1:6d5c35b995fb 178 // tmp[0] = strtof(str, &erstr);
NT32 1:6d5c35b995fb 179 // vcom.printf("Minimum output limit : %15f\n", tmp[0]);
NT32 1:6d5c35b995fb 180 // vcom.printf("Input the maximum outputlimit\n");
NT32 1:6d5c35b995fb 181 // vcom.printf("within 15 figures.\n");
NT32 1:6d5c35b995fb 182 // vcom.scanf("%s", str);
NT32 1:6d5c35b995fb 183 // tmp[1] = strtof(str, &erstr);
NT32 1:6d5c35b995fb 184 // vcom.printf("Maximum output limit : %15f\n", tmp[1]);
NT32 1:6d5c35b995fb 185 // controller.setOutputLimits(tmp[0], tmp[1]);
NT32 1:6d5c35b995fb 186 controller.setOutputLimits(0, 4095);
NT32 0:b2973a157cb6 187
NT32 0:b2973a157cb6 188 //Setpoint
NT32 0:b2973a157cb6 189 vcom.printf("Input the setpoint\n");
NT32 0:b2973a157cb6 190 vcom.printf("within 15 figures.\n");
NT32 0:b2973a157cb6 191 vcom.scanf("%s", str);
NT32 0:b2973a157cb6 192 tmp[0] = strtof(str, &erstr);
NT32 0:b2973a157cb6 193 controller.setSetPoint(tmp[0]);
NT32 0:b2973a157cb6 194 vcom.printf("Setpoint : %15f\n", tmp[0]);
NT32 0:b2973a157cb6 195
NT32 0:b2973a157cb6 196 //tuning parameter
NT32 0:b2973a157cb6 197 vcom.printf("Input the proportional gain\n");
NT32 0:b2973a157cb6 198 vcom.printf("within 15 figures.\n");
NT32 0:b2973a157cb6 199 vcom.scanf("%s", str);
NT32 0:b2973a157cb6 200 tmp[0] = strtof(str, &erstr);
NT32 0:b2973a157cb6 201 vcom.printf("proportional gain : %15f\n", tmp[0]);
NT32 0:b2973a157cb6 202 vcom.printf("Input the integral gain\n");
NT32 0:b2973a157cb6 203 vcom.printf("within 15 figures.\n");
NT32 0:b2973a157cb6 204 vcom.scanf("%s", str);
NT32 0:b2973a157cb6 205 tmp[1] = strtof(str, &erstr);
NT32 0:b2973a157cb6 206 vcom.printf("integral gain : %15f\n", tmp[1]);
NT32 0:b2973a157cb6 207 vcom.printf("Input the derivative gain\n");
NT32 0:b2973a157cb6 208 vcom.printf("within 15 figures.\n");
NT32 0:b2973a157cb6 209 vcom.scanf("%s", str);
NT32 0:b2973a157cb6 210 tmp[2] = strtof(str, &erstr);
NT32 0:b2973a157cb6 211 vcom.printf("derivative gain : %15f\n", tmp[2]);
NT32 0:b2973a157cb6 212 controller.setTunings(tmp[0], tmp[1], tmp[2]);
NT32 0:b2973a157cb6 213
NT32 0:b2973a157cb6 214 //interval
NT32 0:b2973a157cb6 215 // vcom.printf("Input the interval seconds\n");
NT32 0:b2973a157cb6 216 // vcom.printf("within 15 figures.\n");
NT32 0:b2973a157cb6 217 // vcom.scanf("%s", str);
NT32 0:b2973a157cb6 218 // tmp[0] = strtof(str, &erstr);
NT32 0:b2973a157cb6 219 controller.setInterval(PIDRATE);
NT32 0:b2973a157cb6 220 vcom.printf("\033[2J");
NT32 0:b2973a157cb6 221
NT32 0:b2973a157cb6 222
NT32 0:b2973a157cb6 223
NT32 0:b2973a157cb6 224 //PID mode
NT32 0:b2973a157cb6 225 controller.setMode(AUTO_MODE);
NT32 0:b2973a157cb6 226 }