Test program for 20A 24V power stagefor sine commutation of BLDC motor. PID loop and communication will foolow
main.cpp@2:5eacc17e53eb, 2013-05-29 (annotated)
- Committer:
- hkiela
- Date:
- Wed May 29 07:36:36 2013 +0000
- Revision:
- 2:5eacc17e53eb
- Parent:
- 1:57efa2224451
Test program 3 Phase BLDC Sine drive power stage
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hkiela | 0:924cdaeea469 | 1 | // May 2013 |
hkiela | 0:924cdaeea469 | 2 | // HJK Test PWM sine generator |
hkiela | 0:924cdaeea469 | 3 | // Reads 2 potmeter (value 0 to 1 float) and generates a sine wave with amplidute and freq. |
hkiela | 0:924cdaeea469 | 4 | // The resulting value is sent to Led 3 and PWM output 1 |
hkiela | 0:924cdaeea469 | 5 | |
hkiela | 0:924cdaeea469 | 6 | #include "mbed.h" |
hkiela | 0:924cdaeea469 | 7 | //#include "math.h" |
hkiela | 1:57efa2224451 | 8 | //#include "Tables.cpp" |
hkiela | 0:924cdaeea469 | 9 | |
hkiela | 0:924cdaeea469 | 10 | Serial pc(USBTX, USBRX); // tx, rx |
hkiela | 0:924cdaeea469 | 11 | DigitalOut led1(LED1); // main led |
hkiela | 0:924cdaeea469 | 12 | DigitalOut led2(LED2); // Interrupt led |
hkiela | 2:5eacc17e53eb | 13 | PwmOut led3(LED3), aPwm0(p21), aPwm1(p22), aPwm2(p23); |
hkiela | 0:924cdaeea469 | 14 | |
hkiela | 0:924cdaeea469 | 15 | Ticker t; |
hkiela | 1:57efa2224451 | 16 | |
hkiela | 1:57efa2224451 | 17 | //int SineTable tSineTable; |
hkiela | 0:924cdaeea469 | 18 | |
hkiela | 0:924cdaeea469 | 19 | Timer tmr1; |
hkiela | 0:924cdaeea469 | 20 | AnalogIn AD1(p20), AD2(p19); |
hkiela | 0:924cdaeea469 | 21 | |
hkiela | 2:5eacc17e53eb | 22 | unsigned int ServoCnt =0, BackgrCnt =0, ServoTime =0; |
hkiela | 0:924cdaeea469 | 23 | float Potm1, Potm2; |
hkiela | 2:5eacc17e53eb | 24 | float Potm1Offset = 0.5; // Center position is speed zero |
hkiela | 0:924cdaeea469 | 25 | |
hkiela | 2:5eacc17e53eb | 26 | int TableIndex0 =0, TableIndex1 =0, TableIndex2 =0; // Index in sine table |
hkiela | 2:5eacc17e53eb | 27 | float fTableIndex0 =0, fTableIndex1 =0, fTableIndex2 =0, SpeedIndex =0; // Speedindex is calculated dependinding on cycle speed. fTableIndex0 is the floating point index progressing |
hkiela | 2:5eacc17e53eb | 28 | |
hkiela | 1:57efa2224451 | 29 | int SineOut0, SineOut1, SineOut2; // The Sine output values |
hkiela | 1:57efa2224451 | 30 | float mSineOut0, mSineOut1, mSineOut2; // The multiplied Sine output values |
hkiela | 2:5eacc17e53eb | 31 | float MotorSpeed, CycleSpeed; // Motor speed in cycles per second derived from potm1 |
hkiela | 2:5eacc17e53eb | 32 | float MaxMotorSpeed = 10; |
hkiela | 2:5eacc17e53eb | 33 | float MaxCycleSpeed = 10; |
hkiela | 0:924cdaeea469 | 34 | |
hkiela | 2:5eacc17e53eb | 35 | int Rti_us = 100000; // Real time interrupt time in us |
hkiela | 2:5eacc17e53eb | 36 | float Rti_sec = 0.33; // The cycle time |
hkiela | 2:5eacc17e53eb | 37 | |
hkiela | 2:5eacc17e53eb | 38 | // Change PWM freq HERE |
hkiela | 2:5eacc17e53eb | 39 | int PwmTime_us = 50, PwmPulsWidth0 = 0, PwmPulsWidth1 = 0, PwmPulsWidth2 = 0; // Reference time Pwm and actual puls time |
hkiela | 2:5eacc17e53eb | 40 | //int PwmTime_us = 10000, PwmPulsWidth0 = 0, PwmPulsWidth1 = 0, PwmPulsWidth2 = 0; // Reference time Pwm and actual puls time |
hkiela | 1:57efa2224451 | 41 | int Sin1Offset = 120, Sin2Offset = 240; // Offsets for two other phase outputs |
hkiela | 1:57efa2224451 | 42 | |
hkiela | 1:57efa2224451 | 43 | int SineTable[360] = {0, 17, 35, 52, 70, 87, 105, 122, 139, 156 |
hkiela | 1:57efa2224451 | 44 | ,174, 191, 208, 225, 242, 259, 276, 292, 309, 326 |
hkiela | 1:57efa2224451 | 45 | ,342, 358, 375, 391, 407, 423, 438, 454, 469, 485 |
hkiela | 1:57efa2224451 | 46 | ,500, 515, 530, 545, 559, 574, 588, 602, 616, 629 |
hkiela | 1:57efa2224451 | 47 | ,643, 656, 669, 682, 695, 707, 719, 731, 743, 755 |
hkiela | 1:57efa2224451 | 48 | ,766, 777, 788, 799, 809, 819, 829, 839, 848, 857 |
hkiela | 1:57efa2224451 | 49 | ,866, 875, 883, 891, 899, 906, 914, 921, 927, 934 |
hkiela | 1:57efa2224451 | 50 | ,940, 946, 951, 956, 961, 966, 970, 974, 978, 982 |
hkiela | 1:57efa2224451 | 51 | ,985, 988, 990, 993, 995, 996, 998, 999, 999, 1000 |
hkiela | 1:57efa2224451 | 52 | ,1000, 1000, 999, 999, 998, 996, 995, 993, 990, 988 |
hkiela | 1:57efa2224451 | 53 | ,985, 982, 978, 974, 970, 966, 961, 956, 951, 946 |
hkiela | 1:57efa2224451 | 54 | ,940, 934, 927, 921, 914, 906, 899, 891, 883, 875 |
hkiela | 1:57efa2224451 | 55 | ,866, 857, 848, 839, 829, 819, 809, 799, 788, 777 |
hkiela | 1:57efa2224451 | 56 | ,766, 755, 743, 731, 719, 707, 695, 682, 669, 656 |
hkiela | 1:57efa2224451 | 57 | ,643, 629, 616, 602, 588, 574, 559, 545, 530, 515 |
hkiela | 1:57efa2224451 | 58 | ,500, 485, 469, 454, 438, 423, 407, 391, 375, 358 |
hkiela | 1:57efa2224451 | 59 | ,342, 326, 309, 292, 276, 259, 242, 225, 208, 191 |
hkiela | 1:57efa2224451 | 60 | ,174, 156, 139, 122, 105, 87, 70, 52, 35, 17 |
hkiela | 1:57efa2224451 | 61 | ,0, -17, -35, -52, -70, -87, -105, -122, -139, -156 |
hkiela | 1:57efa2224451 | 62 | ,-174, -191, -208, -225, -242, -259, -276, -292, -309, -326 |
hkiela | 1:57efa2224451 | 63 | ,-342, -358, -375, -391, -407, -423, -438, -454, -469, -485 |
hkiela | 1:57efa2224451 | 64 | ,-500, -515, -530, -545, -559, -574, -588, -602, -616, -629 |
hkiela | 1:57efa2224451 | 65 | ,-643, -656, -669, -682, -695, -707, -719, -731, -743, -755 |
hkiela | 1:57efa2224451 | 66 | ,-766, -777, -788, -799, -809, -819, -829, -839, -848, -857 |
hkiela | 1:57efa2224451 | 67 | ,-866, -875, -883, -891, -899, -906, -914, -921, -927, -934 |
hkiela | 1:57efa2224451 | 68 | ,-940, -946, -951, -956, -961, -966, -970, -974, -978, -982 |
hkiela | 1:57efa2224451 | 69 | ,-985, -988, -990, -993, -995, -996, -998, -999, -999, -1000 |
hkiela | 1:57efa2224451 | 70 | ,-1000, -1000, -999, -999, -998, -996, -995, -993, -990, -988 |
hkiela | 1:57efa2224451 | 71 | ,-985, -982, -978, -974, -970, -966, -961, -956, -951, -946 |
hkiela | 1:57efa2224451 | 72 | ,-940, -934, -927, -921, -914, -906, -899, -891, -883, -875 |
hkiela | 1:57efa2224451 | 73 | ,-866, -857, -848, -839, -829, -819, -809, -799, -788, -777 |
hkiela | 1:57efa2224451 | 74 | ,-766, -755, -743, -731, -719, -707, -695, -682, -669, -656 |
hkiela | 1:57efa2224451 | 75 | ,-643, -629, -616, -602, -588, -574, -559, -545, -530, -515 |
hkiela | 1:57efa2224451 | 76 | ,-500, -485, -469, -454, -438, -423, -407, -391, -375, -358 |
hkiela | 1:57efa2224451 | 77 | ,-342, -326, -309, -292, -276, -259, -242, -225, -208, -191 |
hkiela | 1:57efa2224451 | 78 | ,-174, -156, -139, -122, -105, -87, -70, -52, -35, -17 |
hkiela | 1:57efa2224451 | 79 | }; |
hkiela | 1:57efa2224451 | 80 | |
hkiela | 0:924cdaeea469 | 81 | |
hkiela | 0:924cdaeea469 | 82 | // A class for flip()-ing a DigitalOut |
hkiela | 0:924cdaeea469 | 83 | class Flipper { |
hkiela | 0:924cdaeea469 | 84 | public: |
hkiela | 0:924cdaeea469 | 85 | Flipper(PinName pin) : _pin(pin) { |
hkiela | 0:924cdaeea469 | 86 | _pin = 0; |
hkiela | 0:924cdaeea469 | 87 | } |
hkiela | 0:924cdaeea469 | 88 | void flip() |
hkiela | 0:924cdaeea469 | 89 | {int starttime, endtime; |
hkiela | 0:924cdaeea469 | 90 | |
hkiela | 0:924cdaeea469 | 91 | starttime = tmr1.read_us(); // Measure time 1 |
hkiela | 0:924cdaeea469 | 92 | |
hkiela | 0:924cdaeea469 | 93 | Potm1 = AD1.read(); Potm2 = AD2.read(); // Get potmeters |
hkiela | 2:5eacc17e53eb | 94 | // led3.pulsewidth_us(Potm1 * PwmTime_us); // Set duty cycle for LED |
hkiela | 2:5eacc17e53eb | 95 | |
hkiela | 2:5eacc17e53eb | 96 | CycleSpeed = (Potm1 - Potm1Offset) * 30 * MaxCycleSpeed; // Potmeter -0.5 to +0.5 |
hkiela | 2:5eacc17e53eb | 97 | SpeedIndex = CycleSpeed; |
hkiela | 2:5eacc17e53eb | 98 | fTableIndex0 = fTableIndex0 + SpeedIndex; |
hkiela | 2:5eacc17e53eb | 99 | fTableIndex1 = fTableIndex0+120; // calculate offset angles other two phases |
hkiela | 2:5eacc17e53eb | 100 | fTableIndex2 = fTableIndex1+120; |
hkiela | 2:5eacc17e53eb | 101 | |
hkiela | 2:5eacc17e53eb | 102 | while (fTableIndex0 < 0) { fTableIndex0 += 360; }; // Caclulate modulo 360 index Ph0 |
hkiela | 2:5eacc17e53eb | 103 | while (fTableIndex0 > 359) { fTableIndex0 -= 360; }; |
hkiela | 2:5eacc17e53eb | 104 | // fTableIndex1 = fTableIndex1 % 359; |
hkiela | 2:5eacc17e53eb | 105 | while (fTableIndex1 < 0) { fTableIndex1 += 360; }; // Caclulate modulo 360 index ph1 |
hkiela | 2:5eacc17e53eb | 106 | while (fTableIndex1 > 359) { fTableIndex1 -= 360; }; |
hkiela | 2:5eacc17e53eb | 107 | while (fTableIndex2 < 0) { fTableIndex2 += 359; }; // Caclulate modulo 360 index ph2 |
hkiela | 2:5eacc17e53eb | 108 | while (fTableIndex2 > 359) { fTableIndex2 -= 360; }; |
hkiela | 0:924cdaeea469 | 109 | |
hkiela | 2:5eacc17e53eb | 110 | TableIndex0 = std::floor(fTableIndex0); |
hkiela | 2:5eacc17e53eb | 111 | TableIndex1 = std::floor(fTableIndex1); |
hkiela | 2:5eacc17e53eb | 112 | TableIndex2 = std::floor(fTableIndex2); |
hkiela | 2:5eacc17e53eb | 113 | |
hkiela | 2:5eacc17e53eb | 114 | SineOut0 = SineTable[TableIndex0]; // Get sine value from table ph0 |
hkiela | 2:5eacc17e53eb | 115 | mSineOut0 = SineOut0 * Potm2; // Multiply sine with potmeter |
hkiela | 2:5eacc17e53eb | 116 | SineOut1 = SineTable[TableIndex1]; // Get sine value from table ph1 |
hkiela | 2:5eacc17e53eb | 117 | mSineOut1 = SineOut1 * Potm2; // Multiply sine with potmeter |
hkiela | 2:5eacc17e53eb | 118 | SineOut2 = SineTable[TableIndex2]; // Get sine value from table ph2 |
hkiela | 2:5eacc17e53eb | 119 | mSineOut2 = SineOut2 * Potm2; // Multiply sine with potmeter |
hkiela | 2:5eacc17e53eb | 120 | |
hkiela | 2:5eacc17e53eb | 121 | // PwmPulsWidth = PwmTime_us * TableIndex0/360; |
hkiela | 2:5eacc17e53eb | 122 | PwmPulsWidth0 = PwmTime_us * (mSineOut0 + 1000) /2000; // output based on sine table via degree index multiplied by potm2 and offset 50% |
hkiela | 2:5eacc17e53eb | 123 | PwmPulsWidth1 = PwmTime_us * (mSineOut1 + 1000) /2000; // output based on sine table via degree index multiplied by potm2 and offset 50% |
hkiela | 2:5eacc17e53eb | 124 | PwmPulsWidth2 = PwmTime_us * (mSineOut2 + 1000) /2000; // output based on sine table via degree index multiplied by potm2 and offset 50% |
hkiela | 2:5eacc17e53eb | 125 | aPwm0.pulsewidth_us(PwmPulsWidth0); // Set DC amp out 0 |
hkiela | 2:5eacc17e53eb | 126 | aPwm1.pulsewidth_us(PwmPulsWidth1); // Set DC amp out 0 |
hkiela | 2:5eacc17e53eb | 127 | aPwm2.pulsewidth_us(PwmPulsWidth2); // Set DC amp out 0 |
hkiela | 2:5eacc17e53eb | 128 | //aPwm0.pulsewidth_us(Potm1 * PwmTime_us); // Set DC amp out 0 |
hkiela | 2:5eacc17e53eb | 129 | // led3.pulsewidth_us(0.4 * PwmTime_us); // Set duty cycle for LED |
hkiela | 2:5eacc17e53eb | 130 | led3.pulsewidth_us(PwmPulsWidth0); // Set duty cycle for LED |
hkiela | 2:5eacc17e53eb | 131 | // led3.pulsewidth_us(Potm1 * PwmTime_us); // Set duty cycle for LED |
hkiela | 2:5eacc17e53eb | 132 | |
hkiela | 2:5eacc17e53eb | 133 | // TableIndex0 = ceil(Potm1 * 359); |
hkiela | 2:5eacc17e53eb | 134 | SineOut0 = SineTable[TableIndex0]; // Get sine value from table |
hkiela | 1:57efa2224451 | 135 | mSineOut0 = SineOut0 * Potm2; // Multiply sine with potmeter |
hkiela | 0:924cdaeea469 | 136 | |
hkiela | 0:924cdaeea469 | 137 | ServoCnt++; // Servo counter |
hkiela | 2:5eacc17e53eb | 138 | if ((ServoCnt % 10) == 0) {_pin = !_pin;}; // toggle led 2 |
hkiela | 2:5eacc17e53eb | 139 | |
hkiela | 0:924cdaeea469 | 140 | endtime = tmr1.read_us(); // Measure time 2 |
hkiela | 0:924cdaeea469 | 141 | ServoTime = endtime - starttime; |
hkiela | 0:924cdaeea469 | 142 | } |
hkiela | 0:924cdaeea469 | 143 | private: |
hkiela | 0:924cdaeea469 | 144 | DigitalOut _pin; |
hkiela | 0:924cdaeea469 | 145 | }; |
hkiela | 0:924cdaeea469 | 146 | |
hkiela | 0:924cdaeea469 | 147 | Flipper f(LED2); |
hkiela | 0:924cdaeea469 | 148 | |
hkiela | 0:924cdaeea469 | 149 | // Init program |
hkiela | 0:924cdaeea469 | 150 | void DoInit(void) { |
hkiela | 0:924cdaeea469 | 151 | pc.baud(115200); |
hkiela | 0:924cdaeea469 | 152 | tmr1.start(); |
hkiela | 2:5eacc17e53eb | 153 | // Rti_sec = Rti_us / 1000000; |
hkiela | 2:5eacc17e53eb | 154 | |
hkiela | 2:5eacc17e53eb | 155 | t.attach_us(&f, &Flipper::flip, Rti_us); // the address of the object, member function, and interval |
hkiela | 0:924cdaeea469 | 156 | // t.attach_us(&f, &Flipper::flip, 25); // the address of the object, member function, and interval |
hkiela | 0:924cdaeea469 | 157 | // 25 us = 40kHz |
hkiela | 0:924cdaeea469 | 158 | // led3 = 1; |
hkiela | 0:924cdaeea469 | 159 | led3.period_us(PwmTime_us); // Set the period to 10mS = 100Hz |
hkiela | 2:5eacc17e53eb | 160 | // aPwm0.period_us(PwmTime_us); // Set the period for the pwm output to amplifier |
hkiela | 2:5eacc17e53eb | 161 | // aPwm1.period_us(PwmTime_us); // Set the period for the pwm output to amplifier |
hkiela | 2:5eacc17e53eb | 162 | // aPwm2.period_us(PwmTime_us); // Set the period for the pwm output to amplifier |
hkiela | 0:924cdaeea469 | 163 | |
hkiela | 2:5eacc17e53eb | 164 | pc.printf("The Init time was %f seconds, Cycletime: %f\n\r", tmr1.read(), Rti_sec); |
hkiela | 0:924cdaeea469 | 165 | // Return 0; |
hkiela | 0:924cdaeea469 | 166 | } |
hkiela | 0:924cdaeea469 | 167 | |
hkiela | 0:924cdaeea469 | 168 | |
hkiela | 0:924cdaeea469 | 169 | int main() { |
hkiela | 0:924cdaeea469 | 170 | DoInit(); |
hkiela | 0:924cdaeea469 | 171 | |
hkiela | 0:924cdaeea469 | 172 | while(1) { |
hkiela | 0:924cdaeea469 | 173 | BackgrCnt++; |
hkiela | 0:924cdaeea469 | 174 | led1 = 1; |
hkiela | 0:924cdaeea469 | 175 | wait(0.2); |
hkiela | 0:924cdaeea469 | 176 | led1 = 0; |
hkiela | 0:924cdaeea469 | 177 | wait(0.2); |
hkiela | 2:5eacc17e53eb | 178 | pc.printf("Servotm: %i, ServoCnt: %i , BackgrCnt: %i , Potm1: %.2f, Potm2: %.2f \r", |
hkiela | 2:5eacc17e53eb | 179 | ServoTime, ServoCnt, BackgrCnt, Potm1, Potm2); |
hkiela | 2:5eacc17e53eb | 180 | pc.printf("TableIndex0 :%i, TableIndex0 :%i, TableIndex0 :%i, SineOut0: %i , mSineOut0: %.2f, mSineOut1: %.2f, mSineOut2: %.2f \r", |
hkiela | 2:5eacc17e53eb | 181 | ServoTime, TableIndex0, TableIndex1, TableIndex2, SineOut0, mSineOut0, mSineOut1 , mSineOut2 ); |
hkiela | 2:5eacc17e53eb | 182 | pc.printf("CycleSpeed: %.2f, fTableIndex0: %.2f, fTableIndex1: %.2f, fTableIndex2: %.2f \r", |
hkiela | 2:5eacc17e53eb | 183 | CycleSpeed, fTableIndex0, fTableIndex1, fTableIndex2); |
hkiela | 2:5eacc17e53eb | 184 | pc.printf("PwmPulsWidth0: %i, PwmPulsWidth1: %i ,, PwmPulsWidth2: %i \r\r", |
hkiela | 2:5eacc17e53eb | 185 | PwmPulsWidth0, PwmPulsWidth1, PwmPulsWidth2 ); |
hkiela | 0:924cdaeea469 | 186 | } |
hkiela | 0:924cdaeea469 | 187 | } |
hkiela | 0:924cdaeea469 | 188 |