Test program for 20A 24V power stagefor sine commutation of BLDC motor. PID loop and communication will foolow
main.cpp@0:924cdaeea469, 2013-05-23 (annotated)
- Committer:
- hkiela
- Date:
- Thu May 23 16:28:28 2013 +0000
- Revision:
- 0:924cdaeea469
- Child:
- 1:57efa2224451
PWM test fase 1: potmeters, sinustable, pwm out
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 | 0:924cdaeea469 | 8 | |
hkiela | 0:924cdaeea469 | 9 | Serial pc(USBTX, USBRX); // tx, rx |
hkiela | 0:924cdaeea469 | 10 | DigitalOut led1(LED1); // main led |
hkiela | 0:924cdaeea469 | 11 | DigitalOut led2(LED2); // Interrupt led |
hkiela | 0:924cdaeea469 | 12 | PwmOut led3(LED3), aPwm0(p23); |
hkiela | 0:924cdaeea469 | 13 | |
hkiela | 0:924cdaeea469 | 14 | Ticker t; |
hkiela | 0:924cdaeea469 | 15 | |
hkiela | 0:924cdaeea469 | 16 | Timer tmr1; |
hkiela | 0:924cdaeea469 | 17 | AnalogIn AD1(p20), AD2(p19); |
hkiela | 0:924cdaeea469 | 18 | |
hkiela | 0:924cdaeea469 | 19 | unsigned int ServoCnt, BackgrCnt, ServoTime; |
hkiela | 0:924cdaeea469 | 20 | float Potm1, Potm2; |
hkiela | 0:924cdaeea469 | 21 | |
hkiela | 0:924cdaeea469 | 22 | int TableIndex, SineOut0; |
hkiela | 0:924cdaeea469 | 23 | |
hkiela | 0:924cdaeea469 | 24 | int PwmTime_us = 10000; |
hkiela | 0:924cdaeea469 | 25 | int SineTable[360] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 |
hkiela | 0:924cdaeea469 | 26 | , 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 |
hkiela | 0:924cdaeea469 | 27 | , 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 |
hkiela | 0:924cdaeea469 | 28 | , 30, 31, 32, 33, 34, 35, 36, 37, 38, 39 |
hkiela | 0:924cdaeea469 | 29 | , 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 |
hkiela | 0:924cdaeea469 | 30 | , 50, 51, 52, 53, 54, 55, 56, 57, 58, 59 |
hkiela | 0:924cdaeea469 | 31 | , 60, 61, 62, 63, 64, 65, 66, 67, 68, 69 |
hkiela | 0:924cdaeea469 | 32 | , 70, 71, 72, 73, 74, 75, 76, 77, 78, 79 |
hkiela | 0:924cdaeea469 | 33 | , 80, 81, 82, 83, 84, 85, 86, 87, 88, 89 |
hkiela | 0:924cdaeea469 | 34 | , 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 |
hkiela | 0:924cdaeea469 | 35 | , 100, 101, 102, 103, 104, 105, 106, 107, 108, 109 |
hkiela | 0:924cdaeea469 | 36 | , 110, 111, 112, 113, 114, 115, 116, 117, 118, 119 |
hkiela | 0:924cdaeea469 | 37 | , 120, 121, 122, 123, 124, 125, 126, 127, 128, 129 |
hkiela | 0:924cdaeea469 | 38 | , 130, 131, 132, 133, 134, 135, 136, 137, 138, 139 |
hkiela | 0:924cdaeea469 | 39 | , 140, 141, 142, 143, 144, 145, 146, 147, 148, 149 |
hkiela | 0:924cdaeea469 | 40 | , 150, 151, 152, 153, 154, 155, 156, 157, 158, 159 |
hkiela | 0:924cdaeea469 | 41 | , 160, 161, 162, 163, 164, 165, 166, 167, 168, 169 |
hkiela | 0:924cdaeea469 | 42 | , 170, 171, 172, 173, 174, 175, 176, 177, 178, 179 |
hkiela | 0:924cdaeea469 | 43 | , 180, 181, 182, 183, 184, 185, 186, 187, 188, 189 |
hkiela | 0:924cdaeea469 | 44 | , 190, 191, 192, 193, 194, 195, 196, 197, 198, 199 |
hkiela | 0:924cdaeea469 | 45 | , 200, 201, 202, 203, 204, 205, 206, 207, 208, 209 |
hkiela | 0:924cdaeea469 | 46 | , 210, 211, 212, 213, 214, 215, 216, 217, 218, 219 |
hkiela | 0:924cdaeea469 | 47 | , 220, 221, 222, 223, 224, 225, 226, 227, 228, 229 |
hkiela | 0:924cdaeea469 | 48 | , 230, 231, 232, 233, 234, 235, 236, 237, 238, 239 |
hkiela | 0:924cdaeea469 | 49 | , 240, 241, 242, 243, 244, 245, 246, 247, 248, 249 |
hkiela | 0:924cdaeea469 | 50 | , 250, 251, 252, 253, 254, 255, 256, 257, 258, 259 |
hkiela | 0:924cdaeea469 | 51 | , 260, 261, 262, 263, 264, 265, 266, 267, 268, 269 |
hkiela | 0:924cdaeea469 | 52 | , 270, 271, 272, 273, 274, 275, 276, 277, 278, 279 |
hkiela | 0:924cdaeea469 | 53 | , 280, 281, 282, 283, 284, 285, 286, 287, 288, 289 |
hkiela | 0:924cdaeea469 | 54 | , 290, 291, 292, 293, 294, 295, 296, 297, 298, 299 |
hkiela | 0:924cdaeea469 | 55 | , 300, 301, 302, 303, 304, 305, 306, 307, 308, 309 |
hkiela | 0:924cdaeea469 | 56 | , 310, 311, 312, 313, 314, 315, 316, 317, 318, 319 |
hkiela | 0:924cdaeea469 | 57 | , 320, 321, 322, 323, 324, 325, 326, 327, 328, 329 |
hkiela | 0:924cdaeea469 | 58 | , 330, 331, 332, 333, 334, 335, 336, 337, 338, 339 |
hkiela | 0:924cdaeea469 | 59 | , 340, 341, 342, 343, 344, 345, 346, 347, 348, 349 |
hkiela | 0:924cdaeea469 | 60 | , 350, 351, 352, 353, 354, 355, 356, 357, 358, 359}; |
hkiela | 0:924cdaeea469 | 61 | |
hkiela | 0:924cdaeea469 | 62 | // A class for flip()-ing a DigitalOut |
hkiela | 0:924cdaeea469 | 63 | class Flipper { |
hkiela | 0:924cdaeea469 | 64 | public: |
hkiela | 0:924cdaeea469 | 65 | Flipper(PinName pin) : _pin(pin) { |
hkiela | 0:924cdaeea469 | 66 | _pin = 0; |
hkiela | 0:924cdaeea469 | 67 | } |
hkiela | 0:924cdaeea469 | 68 | void flip() |
hkiela | 0:924cdaeea469 | 69 | {int starttime, endtime; |
hkiela | 0:924cdaeea469 | 70 | |
hkiela | 0:924cdaeea469 | 71 | starttime = tmr1.read_us(); // Measure time 1 |
hkiela | 0:924cdaeea469 | 72 | _pin = !_pin; // toggle led 2 |
hkiela | 0:924cdaeea469 | 73 | |
hkiela | 0:924cdaeea469 | 74 | Potm1 = AD1.read(); Potm2 = AD2.read(); // Get potmeters |
hkiela | 0:924cdaeea469 | 75 | led3.pulsewidth_us(Potm1 * PwmTime_us); // Set duty cycle for LED |
hkiela | 0:924cdaeea469 | 76 | aPwm0.pulsewidth_us(Potm1 * PwmTime_us); // Set DC amp out 0 |
hkiela | 0:924cdaeea469 | 77 | |
hkiela | 0:924cdaeea469 | 78 | TableIndex = ceil(Potm1 * 359); |
hkiela | 0:924cdaeea469 | 79 | SineOut0 = SineTable[TableIndex]; // Get sine value from table |
hkiela | 0:924cdaeea469 | 80 | |
hkiela | 0:924cdaeea469 | 81 | ServoCnt++; // Servo counter |
hkiela | 0:924cdaeea469 | 82 | endtime = tmr1.read_us(); // Measure time 2 |
hkiela | 0:924cdaeea469 | 83 | ServoTime = endtime - starttime; |
hkiela | 0:924cdaeea469 | 84 | } |
hkiela | 0:924cdaeea469 | 85 | private: |
hkiela | 0:924cdaeea469 | 86 | DigitalOut _pin; |
hkiela | 0:924cdaeea469 | 87 | }; |
hkiela | 0:924cdaeea469 | 88 | |
hkiela | 0:924cdaeea469 | 89 | Flipper f(LED2); |
hkiela | 0:924cdaeea469 | 90 | |
hkiela | 0:924cdaeea469 | 91 | // Init program |
hkiela | 0:924cdaeea469 | 92 | void DoInit(void) { |
hkiela | 0:924cdaeea469 | 93 | pc.baud(115200); |
hkiela | 0:924cdaeea469 | 94 | tmr1.start(); |
hkiela | 0:924cdaeea469 | 95 | |
hkiela | 0:924cdaeea469 | 96 | t.attach_us(&f, &Flipper::flip, 100000); // the address of the object, member function, and interval |
hkiela | 0:924cdaeea469 | 97 | // t.attach_us(&f, &Flipper::flip, 25); // the address of the object, member function, and interval |
hkiela | 0:924cdaeea469 | 98 | // 25 us = 40kHz |
hkiela | 0:924cdaeea469 | 99 | // led3 = 1; |
hkiela | 0:924cdaeea469 | 100 | led3.period_us(PwmTime_us); // Set the period to 10mS = 100Hz |
hkiela | 0:924cdaeea469 | 101 | // aPwm0.period_ms(PwmTime_us); // Set the period for the pwm output to amplifier |
hkiela | 0:924cdaeea469 | 102 | |
hkiela | 0:924cdaeea469 | 103 | pc.printf("The Init time was %f seconds\n\r", tmr1.read()); |
hkiela | 0:924cdaeea469 | 104 | // Return 0; |
hkiela | 0:924cdaeea469 | 105 | } |
hkiela | 0:924cdaeea469 | 106 | |
hkiela | 0:924cdaeea469 | 107 | |
hkiela | 0:924cdaeea469 | 108 | int main() { |
hkiela | 0:924cdaeea469 | 109 | DoInit(); |
hkiela | 0:924cdaeea469 | 110 | |
hkiela | 0:924cdaeea469 | 111 | while(1) { |
hkiela | 0:924cdaeea469 | 112 | BackgrCnt++; |
hkiela | 0:924cdaeea469 | 113 | led1 = 1; |
hkiela | 0:924cdaeea469 | 114 | wait(0.2); |
hkiela | 0:924cdaeea469 | 115 | led1 = 0; |
hkiela | 0:924cdaeea469 | 116 | wait(0.2); |
hkiela | 0:924cdaeea469 | 117 | pc.printf("Servotm: %i, ServoCnt: %i , BackgrCnt: %i , Potm1: ,%.2f, Potm2: ,%.2f, TableIndex %i, SineOut0 %i\r", |
hkiela | 0:924cdaeea469 | 118 | ServoTime, ServoCnt, BackgrCnt, Potm1, Potm2, TableIndex, SineOut0 ); |
hkiela | 0:924cdaeea469 | 119 | } |
hkiela | 0:924cdaeea469 | 120 | } |
hkiela | 0:924cdaeea469 | 121 |