![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
code for bidirectional DC-DC converter
main.cpp@0:a9dbf85a571b, 2018-10-02 (annotated)
- Committer:
- amarendra23
- Date:
- Tue Oct 02 19:20:10 2018 +0000
- Revision:
- 0:a9dbf85a571b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
amarendra23 | 0:a9dbf85a571b | 1 | #include "mbed.h" |
amarendra23 | 0:a9dbf85a571b | 2 | |
amarendra23 | 0:a9dbf85a571b | 3 | PwmOut led1(PB_4); |
amarendra23 | 0:a9dbf85a571b | 4 | AnalogIn sense(PA_1); |
amarendra23 | 0:a9dbf85a571b | 5 | PwmOut led2(PB_3); |
amarendra23 | 0:a9dbf85a571b | 6 | float Kp_set = 0.00005f; |
amarendra23 | 0:a9dbf85a571b | 7 | float kp_set = 0.00002f; |
amarendra23 | 0:a9dbf85a571b | 8 | float Ki=0.0000001f; |
amarendra23 | 0:a9dbf85a571b | 9 | double sum_error_per_buck=0.0f; |
amarendra23 | 0:a9dbf85a571b | 10 | double sum_error_per_boost=0.0f; |
amarendra23 | 0:a9dbf85a571b | 11 | double pre_error_per_buck=0.0f; |
amarendra23 | 0:a9dbf85a571b | 12 | double pre_error_per_boost=0.0f; |
amarendra23 | 0:a9dbf85a571b | 13 | float Kp = 0.0f,Kd=0.00001f; |
amarendra23 | 0:a9dbf85a571b | 14 | int flag=1; |
amarendra23 | 0:a9dbf85a571b | 15 | |
amarendra23 | 0:a9dbf85a571b | 16 | int main() |
amarendra23 | 0:a9dbf85a571b | 17 | { |
amarendra23 | 0:a9dbf85a571b | 18 | |
amarendra23 | 0:a9dbf85a571b | 19 | led1.period(0.00001f); |
amarendra23 | 0:a9dbf85a571b | 20 | led2.period(0.00001f); |
amarendra23 | 0:a9dbf85a571b | 21 | // led2.write(0.40f); |
amarendra23 | 0:a9dbf85a571b | 22 | while(1) |
amarendra23 | 0:a9dbf85a571b | 23 | { |
amarendra23 | 0:a9dbf85a571b | 24 | float duty_boost=0.15f; |
amarendra23 | 0:a9dbf85a571b | 25 | |
amarendra23 | 0:a9dbf85a571b | 26 | |
amarendra23 | 0:a9dbf85a571b | 27 | while(flag==1) |
amarendra23 | 0:a9dbf85a571b | 28 | { |
amarendra23 | 0:a9dbf85a571b | 29 | // wait(0.001); |
amarendra23 | 0:a9dbf85a571b | 30 | led2.write(0.0f); |
amarendra23 | 0:a9dbf85a571b | 31 | uint16_t setpoint = 0x9777; |
amarendra23 | 0:a9dbf85a571b | 32 | double error_per = 0.0f; |
amarendra23 | 0:a9dbf85a571b | 33 | uint16_t bus_voltage = sense.read_u16(); |
amarendra23 | 0:a9dbf85a571b | 34 | for(int i=0;i<10;i++) |
amarendra23 | 0:a9dbf85a571b | 35 | { |
amarendra23 | 0:a9dbf85a571b | 36 | uint16_t bus_voltage = sense.read_u16(); |
amarendra23 | 0:a9dbf85a571b | 37 | int32_t error = setpoint - bus_voltage; |
amarendra23 | 0:a9dbf85a571b | 38 | error_per += (double(setpoint)-double(bus_voltage))/double(setpoint)*100.0f; |
amarendra23 | 0:a9dbf85a571b | 39 | } |
amarendra23 | 0:a9dbf85a571b | 40 | error_per = error_per/10.0f; |
amarendra23 | 0:a9dbf85a571b | 41 | sum_error_per_boost+=error_per; |
amarendra23 | 0:a9dbf85a571b | 42 | |
amarendra23 | 0:a9dbf85a571b | 43 | if (duty_boost <= 0.700f) |
amarendra23 | 0:a9dbf85a571b | 44 | { |
amarendra23 | 0:a9dbf85a571b | 45 | |
amarendra23 | 0:a9dbf85a571b | 46 | // if (error_per > 5 || error_per < (-5)) |
amarendra23 | 0:a9dbf85a571b | 47 | // Kp = Kp_set*10; |
amarendra23 | 0:a9dbf85a571b | 48 | |
amarendra23 | 0:a9dbf85a571b | 49 | // else |
amarendra23 | 0:a9dbf85a571b | 50 | Kp = Kp_set; |
amarendra23 | 0:a9dbf85a571b | 51 | |
amarendra23 | 0:a9dbf85a571b | 52 | // if (error_per > 2 ||error_per < (-2)) |
amarendra23 | 0:a9dbf85a571b | 53 | //{ |
amarendra23 | 0:a9dbf85a571b | 54 | duty_boost = duty_boost + Kp*error_per+Ki*sum_error_per_boost*0.0002f+Kd*(error_per-pre_error_per_boost); |
amarendra23 | 0:a9dbf85a571b | 55 | pre_error_per_boost=error_per; |
amarendra23 | 0:a9dbf85a571b | 56 | // } |
amarendra23 | 0:a9dbf85a571b | 57 | |
amarendra23 | 0:a9dbf85a571b | 58 | } |
amarendra23 | 0:a9dbf85a571b | 59 | |
amarendra23 | 0:a9dbf85a571b | 60 | else duty_boost = 0.700f; |
amarendra23 | 0:a9dbf85a571b | 61 | led1.write(duty_boost); |
amarendra23 | 0:a9dbf85a571b | 62 | // printf("%f %f \n ",error_per,duty_boost); |
amarendra23 | 0:a9dbf85a571b | 63 | if(error_per<=0.0f&&duty_boost<=0.2) |
amarendra23 | 0:a9dbf85a571b | 64 | { |
amarendra23 | 0:a9dbf85a571b | 65 | //printf("buck hona chahiye"); |
amarendra23 | 0:a9dbf85a571b | 66 | flag=0; |
amarendra23 | 0:a9dbf85a571b | 67 | |
amarendra23 | 0:a9dbf85a571b | 68 | led2.write(0.0f); |
amarendra23 | 0:a9dbf85a571b | 69 | led1.write(0.0f); |
amarendra23 | 0:a9dbf85a571b | 70 | sum_error_per_buck=0.0f; |
amarendra23 | 0:a9dbf85a571b | 71 | sum_error_per_boost=0.0f; |
amarendra23 | 0:a9dbf85a571b | 72 | pre_error_per_boost= 0.0f; |
amarendra23 | 0:a9dbf85a571b | 73 | pre_error_per_buck = 0.0f; |
amarendra23 | 0:a9dbf85a571b | 74 | } |
amarendra23 | 0:a9dbf85a571b | 75 | |
amarendra23 | 0:a9dbf85a571b | 76 | // float Voltagef = float(bus_voltage)*3.3f; |
amarendra23 | 0:a9dbf85a571b | 77 | // printf(" %f %d \n", Voltagef , flag); |
amarendra23 | 0:a9dbf85a571b | 78 | |
amarendra23 | 0:a9dbf85a571b | 79 | } |
amarendra23 | 0:a9dbf85a571b | 80 | |
amarendra23 | 0:a9dbf85a571b | 81 | float duty_buck=0.0f; |
amarendra23 | 0:a9dbf85a571b | 82 | while(flag==0) |
amarendra23 | 0:a9dbf85a571b | 83 | { |
amarendra23 | 0:a9dbf85a571b | 84 | led1.write(0.0f); |
amarendra23 | 0:a9dbf85a571b | 85 | |
amarendra23 | 0:a9dbf85a571b | 86 | //printf("buck ho rha hai"); |
amarendra23 | 0:a9dbf85a571b | 87 | uint16_t setpoint = 0xA1B3; |
amarendra23 | 0:a9dbf85a571b | 88 | float error_per = 0.0f; |
amarendra23 | 0:a9dbf85a571b | 89 | uint16_t bus_voltage = sense.read_u16(); |
amarendra23 | 0:a9dbf85a571b | 90 | for(int i=0;i<5;i++) |
amarendra23 | 0:a9dbf85a571b | 91 | { |
amarendra23 | 0:a9dbf85a571b | 92 | uint16_t bus_voltage = sense.read_u16(); |
amarendra23 | 0:a9dbf85a571b | 93 | int32_t error = bus_voltage - setpoint; |
amarendra23 | 0:a9dbf85a571b | 94 | error_per += (float(bus_voltage)-float(setpoint))/float(setpoint)*100.0f; |
amarendra23 | 0:a9dbf85a571b | 95 | } |
amarendra23 | 0:a9dbf85a571b | 96 | error_per = error_per/5.0f; |
amarendra23 | 0:a9dbf85a571b | 97 | |
amarendra23 | 0:a9dbf85a571b | 98 | // if (error_per > 5 || error_per < (-5)) |
amarendra23 | 0:a9dbf85a571b | 99 | // Kp = kp_set*10; |
amarendra23 | 0:a9dbf85a571b | 100 | |
amarendra23 | 0:a9dbf85a571b | 101 | //else |
amarendra23 | 0:a9dbf85a571b | 102 | Kp = kp_set; |
amarendra23 | 0:a9dbf85a571b | 103 | |
amarendra23 | 0:a9dbf85a571b | 104 | //if (error_per > 2 ||error_per < (-2)) |
amarendra23 | 0:a9dbf85a571b | 105 | |
amarendra23 | 0:a9dbf85a571b | 106 | duty_buck = duty_buck + Kp*error_per+Ki*sum_error_per_buck*0.0002f+Kd*(error_per-pre_error_per_buck); |
amarendra23 | 0:a9dbf85a571b | 107 | pre_error_per_buck=error_per; |
amarendra23 | 0:a9dbf85a571b | 108 | |
amarendra23 | 0:a9dbf85a571b | 109 | led2.write(duty_buck); |
amarendra23 | 0:a9dbf85a571b | 110 | |
amarendra23 | 0:a9dbf85a571b | 111 | |
amarendra23 | 0:a9dbf85a571b | 112 | // if(duty_buck>1)duty_buck=1; |
amarendra23 | 0:a9dbf85a571b | 113 | // printf("%f dbk= %f \n",error_per,duty_buck) ; |
amarendra23 | 0:a9dbf85a571b | 114 | if(error_per<0.0f&&duty_buck<0.29 ) |
amarendra23 | 0:a9dbf85a571b | 115 | { |
amarendra23 | 0:a9dbf85a571b | 116 | flag=1; |
amarendra23 | 0:a9dbf85a571b | 117 | led2.write(0.0f); |
amarendra23 | 0:a9dbf85a571b | 118 | led1.write(0.0f); |
amarendra23 | 0:a9dbf85a571b | 119 | sum_error_per_buck=0.0f; |
amarendra23 | 0:a9dbf85a571b | 120 | sum_error_per_boost=0.0f; |
amarendra23 | 0:a9dbf85a571b | 121 | pre_error_per_boost=0.0f; |
amarendra23 | 0:a9dbf85a571b | 122 | pre_error_per_boost=0.0f; |
amarendra23 | 0:a9dbf85a571b | 123 | |
amarendra23 | 0:a9dbf85a571b | 124 | } |
amarendra23 | 0:a9dbf85a571b | 125 | // float Voltagef = float(bus_voltage)*3.3f; |
amarendra23 | 0:a9dbf85a571b | 126 | // printf(" %f %d \n", Voltagef , flag); |
amarendra23 | 0:a9dbf85a571b | 127 | // printf(" %f %x %d %f \n", duty, input, error, error_per); |
amarendra23 | 0:a9dbf85a571b | 128 | |
amarendra23 | 0:a9dbf85a571b | 129 | } |
amarendra23 | 0:a9dbf85a571b | 130 | |
amarendra23 | 0:a9dbf85a571b | 131 | |
amarendra23 | 0:a9dbf85a571b | 132 | } |
amarendra23 | 0:a9dbf85a571b | 133 | } |