Test program for 20A 24V power stagefor sine commutation of BLDC motor. PID loop and communication will foolow

Dependencies:   mbed

Committer:
hkiela
Date:
Fri May 24 08:39:03 2013 +0000
Revision:
1:57efa2224451
Parent:
0:924cdaeea469
Child:
2:5eacc17e53eb
Loopt met sinustable en multiply

Who changed what in which revision?

UserRevisionLine numberNew 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 0:924cdaeea469 13 PwmOut led3(LED3), aPwm0(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 0:924cdaeea469 22 unsigned int ServoCnt, BackgrCnt, ServoTime;
hkiela 0:924cdaeea469 23 float Potm1, Potm2;
hkiela 0:924cdaeea469 24
hkiela 1:57efa2224451 25 int TableIndex;
hkiela 1:57efa2224451 26 int SineOut0, SineOut1, SineOut2; // The Sine output values
hkiela 1:57efa2224451 27 float mSineOut0, mSineOut1, mSineOut2; // The multiplied Sine output values
hkiela 1:57efa2224451 28
hkiela 0:924cdaeea469 29
hkiela 0:924cdaeea469 30 int PwmTime_us = 10000;
hkiela 1:57efa2224451 31 int Sin1Offset = 120, Sin2Offset = 240; // Offsets for two other phase outputs
hkiela 1:57efa2224451 32
hkiela 1:57efa2224451 33 int SineTable[360] = {0, 17, 35, 52, 70, 87, 105, 122, 139, 156
hkiela 1:57efa2224451 34 ,174, 191, 208, 225, 242, 259, 276, 292, 309, 326
hkiela 1:57efa2224451 35 ,342, 358, 375, 391, 407, 423, 438, 454, 469, 485
hkiela 1:57efa2224451 36 ,500, 515, 530, 545, 559, 574, 588, 602, 616, 629
hkiela 1:57efa2224451 37 ,643, 656, 669, 682, 695, 707, 719, 731, 743, 755
hkiela 1:57efa2224451 38 ,766, 777, 788, 799, 809, 819, 829, 839, 848, 857
hkiela 1:57efa2224451 39 ,866, 875, 883, 891, 899, 906, 914, 921, 927, 934
hkiela 1:57efa2224451 40 ,940, 946, 951, 956, 961, 966, 970, 974, 978, 982
hkiela 1:57efa2224451 41 ,985, 988, 990, 993, 995, 996, 998, 999, 999, 1000
hkiela 1:57efa2224451 42 ,1000, 1000, 999, 999, 998, 996, 995, 993, 990, 988
hkiela 1:57efa2224451 43 ,985, 982, 978, 974, 970, 966, 961, 956, 951, 946
hkiela 1:57efa2224451 44 ,940, 934, 927, 921, 914, 906, 899, 891, 883, 875
hkiela 1:57efa2224451 45 ,866, 857, 848, 839, 829, 819, 809, 799, 788, 777
hkiela 1:57efa2224451 46 ,766, 755, 743, 731, 719, 707, 695, 682, 669, 656
hkiela 1:57efa2224451 47 ,643, 629, 616, 602, 588, 574, 559, 545, 530, 515
hkiela 1:57efa2224451 48 ,500, 485, 469, 454, 438, 423, 407, 391, 375, 358
hkiela 1:57efa2224451 49 ,342, 326, 309, 292, 276, 259, 242, 225, 208, 191
hkiela 1:57efa2224451 50 ,174, 156, 139, 122, 105, 87, 70, 52, 35, 17
hkiela 1:57efa2224451 51 ,0, -17, -35, -52, -70, -87, -105, -122, -139, -156
hkiela 1:57efa2224451 52 ,-174, -191, -208, -225, -242, -259, -276, -292, -309, -326
hkiela 1:57efa2224451 53 ,-342, -358, -375, -391, -407, -423, -438, -454, -469, -485
hkiela 1:57efa2224451 54 ,-500, -515, -530, -545, -559, -574, -588, -602, -616, -629
hkiela 1:57efa2224451 55 ,-643, -656, -669, -682, -695, -707, -719, -731, -743, -755
hkiela 1:57efa2224451 56 ,-766, -777, -788, -799, -809, -819, -829, -839, -848, -857
hkiela 1:57efa2224451 57 ,-866, -875, -883, -891, -899, -906, -914, -921, -927, -934
hkiela 1:57efa2224451 58 ,-940, -946, -951, -956, -961, -966, -970, -974, -978, -982
hkiela 1:57efa2224451 59 ,-985, -988, -990, -993, -995, -996, -998, -999, -999, -1000
hkiela 1:57efa2224451 60 ,-1000, -1000, -999, -999, -998, -996, -995, -993, -990, -988
hkiela 1:57efa2224451 61 ,-985, -982, -978, -974, -970, -966, -961, -956, -951, -946
hkiela 1:57efa2224451 62 ,-940, -934, -927, -921, -914, -906, -899, -891, -883, -875
hkiela 1:57efa2224451 63 ,-866, -857, -848, -839, -829, -819, -809, -799, -788, -777
hkiela 1:57efa2224451 64 ,-766, -755, -743, -731, -719, -707, -695, -682, -669, -656
hkiela 1:57efa2224451 65 ,-643, -629, -616, -602, -588, -574, -559, -545, -530, -515
hkiela 1:57efa2224451 66 ,-500, -485, -469, -454, -438, -423, -407, -391, -375, -358
hkiela 1:57efa2224451 67 ,-342, -326, -309, -292, -276, -259, -242, -225, -208, -191
hkiela 1:57efa2224451 68 ,-174, -156, -139, -122, -105, -87, -70, -52, -35, -17
hkiela 1:57efa2224451 69 };
hkiela 1:57efa2224451 70
hkiela 0:924cdaeea469 71
hkiela 0:924cdaeea469 72 // A class for flip()-ing a DigitalOut
hkiela 0:924cdaeea469 73 class Flipper {
hkiela 0:924cdaeea469 74 public:
hkiela 0:924cdaeea469 75 Flipper(PinName pin) : _pin(pin) {
hkiela 0:924cdaeea469 76 _pin = 0;
hkiela 0:924cdaeea469 77 }
hkiela 0:924cdaeea469 78 void flip()
hkiela 0:924cdaeea469 79 {int starttime, endtime;
hkiela 0:924cdaeea469 80
hkiela 0:924cdaeea469 81 starttime = tmr1.read_us(); // Measure time 1
hkiela 0:924cdaeea469 82 _pin = !_pin; // toggle led 2
hkiela 0:924cdaeea469 83
hkiela 0:924cdaeea469 84 Potm1 = AD1.read(); Potm2 = AD2.read(); // Get potmeters
hkiela 0:924cdaeea469 85 led3.pulsewidth_us(Potm1 * PwmTime_us); // Set duty cycle for LED
hkiela 0:924cdaeea469 86 aPwm0.pulsewidth_us(Potm1 * PwmTime_us); // Set DC amp out 0
hkiela 0:924cdaeea469 87
hkiela 0:924cdaeea469 88 TableIndex = ceil(Potm1 * 359);
hkiela 0:924cdaeea469 89 SineOut0 = SineTable[TableIndex]; // Get sine value from table
hkiela 1:57efa2224451 90 mSineOut0 = SineOut0 * Potm2; // Multiply sine with potmeter
hkiela 0:924cdaeea469 91
hkiela 0:924cdaeea469 92 ServoCnt++; // Servo counter
hkiela 0:924cdaeea469 93 endtime = tmr1.read_us(); // Measure time 2
hkiela 0:924cdaeea469 94 ServoTime = endtime - starttime;
hkiela 0:924cdaeea469 95 }
hkiela 0:924cdaeea469 96 private:
hkiela 0:924cdaeea469 97 DigitalOut _pin;
hkiela 0:924cdaeea469 98 };
hkiela 0:924cdaeea469 99
hkiela 0:924cdaeea469 100 Flipper f(LED2);
hkiela 0:924cdaeea469 101
hkiela 0:924cdaeea469 102 // Init program
hkiela 0:924cdaeea469 103 void DoInit(void) {
hkiela 0:924cdaeea469 104 pc.baud(115200);
hkiela 0:924cdaeea469 105 tmr1.start();
hkiela 0:924cdaeea469 106
hkiela 0:924cdaeea469 107 t.attach_us(&f, &Flipper::flip, 100000); // the address of the object, member function, and interval
hkiela 0:924cdaeea469 108 // t.attach_us(&f, &Flipper::flip, 25); // the address of the object, member function, and interval
hkiela 0:924cdaeea469 109 // 25 us = 40kHz
hkiela 0:924cdaeea469 110 // led3 = 1;
hkiela 0:924cdaeea469 111 led3.period_us(PwmTime_us); // Set the period to 10mS = 100Hz
hkiela 0:924cdaeea469 112 // aPwm0.period_ms(PwmTime_us); // Set the period for the pwm output to amplifier
hkiela 0:924cdaeea469 113
hkiela 0:924cdaeea469 114 pc.printf("The Init time was %f seconds\n\r", tmr1.read());
hkiela 0:924cdaeea469 115 // Return 0;
hkiela 0:924cdaeea469 116 }
hkiela 0:924cdaeea469 117
hkiela 0:924cdaeea469 118
hkiela 0:924cdaeea469 119 int main() {
hkiela 0:924cdaeea469 120 DoInit();
hkiela 0:924cdaeea469 121
hkiela 0:924cdaeea469 122 while(1) {
hkiela 0:924cdaeea469 123 BackgrCnt++;
hkiela 0:924cdaeea469 124 led1 = 1;
hkiela 0:924cdaeea469 125 wait(0.2);
hkiela 0:924cdaeea469 126 led1 = 0;
hkiela 0:924cdaeea469 127 wait(0.2);
hkiela 1:57efa2224451 128 pc.printf("Servotm: %i, ServoCnt: %i , BackgrCnt: %i , Potm1: %.2f, Potm2: %i, TableIndex :%i, SineOut0: %i , mSineOut0: %.2f\r",
hkiela 1:57efa2224451 129 ServoTime, ServoCnt, BackgrCnt, Potm1, Potm2, TableIndex, SineOut0, mSineOut0 );
hkiela 0:924cdaeea469 130 }
hkiela 0:924cdaeea469 131 }
hkiela 0:924cdaeea469 132