Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of BoxBrovoEcho_OCt3 by
main.cpp@1:fa78d980522c, 2018-09-04 (annotated)
- Committer:
- rafael210
- Date:
- Tue Sep 04 16:18:52 2018 +0000
- Revision:
- 1:fa78d980522c
- Parent:
- 0:b265ed6faf6c
- Child:
- 2:98bc0c7668e3
kkk
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| rafael210 | 0:b265ed6faf6c | 1 | #include "mbed.h" |
| rafael210 | 1:fa78d980522c | 2 | #include "math.h" |
| rafael210 | 0:b265ed6faf6c | 3 | |
| rafael210 | 0:b265ed6faf6c | 4 | Ticker scheduler1; |
| rafael210 | 0:b265ed6faf6c | 5 | Ticker scheduler2; |
| rafael210 | 0:b265ed6faf6c | 6 | Ticker scheduler3; |
| rafael210 | 0:b265ed6faf6c | 7 | Ticker scheduler4; |
| rafael210 | 0:b265ed6faf6c | 8 | |
| rafael210 | 1:fa78d980522c | 9 | Serial pc(USBTX, USBRX); // tx, rx |
| rafael210 | 1:fa78d980522c | 10 | |
| rafael210 | 1:fa78d980522c | 11 | DigitalOut task_1_pin(p8, 0); |
| rafael210 | 1:fa78d980522c | 12 | DigitalOut task_2_pin(p9, 0); |
| rafael210 | 1:fa78d980522c | 13 | |
| rafael210 | 0:b265ed6faf6c | 14 | DigitalOut THY_S(p5, 0); |
| rafael210 | 1:fa78d980522c | 15 | DigitalOut IGBT_G(p6, 0); |
| rafael210 | 1:fa78d980522c | 16 | |
| rafael210 | 0:b265ed6faf6c | 17 | DigitalOut led1(LED1, 0); |
| rafael210 | 0:b265ed6faf6c | 18 | DigitalOut led2(LED2, 0); |
| rafael210 | 1:fa78d980522c | 19 | DigitalOut led3(LED3, 0); |
| rafael210 | 0:b265ed6faf6c | 20 | |
| rafael210 | 1:fa78d980522c | 21 | DigitalIn ButtonPress(p21); |
| rafael210 | 1:fa78d980522c | 22 | |
| rafael210 | 1:fa78d980522c | 23 | AnalogIn current(p15); |
| rafael210 | 1:fa78d980522c | 24 | |
| rafael210 | 1:fa78d980522c | 25 | |
| rafael210 | 1:fa78d980522c | 26 | |
| rafael210 | 1:fa78d980522c | 27 | |
| rafael210 | 1:fa78d980522c | 28 | |
| rafael210 | 1:fa78d980522c | 29 | |
| rafael210 | 1:fa78d980522c | 30 | float i_load = 0; |
| rafael210 | 1:fa78d980522c | 31 | int c_i = 0; |
| rafael210 | 1:fa78d980522c | 32 | |
| rafael210 | 1:fa78d980522c | 33 | // ------------------------- Ramp up parameters ----------------------------- // |
| rafael210 | 1:fa78d980522c | 34 | float N = 10; // number of steps |
| rafael210 | 1:fa78d980522c | 35 | float tramp = 5000; // ramp time in ms |
| rafael210 | 1:fa78d980522c | 36 | float tstep = 0; // step time |
| rafael210 | 1:fa78d980522c | 37 | float toff = 0; // cycle off time |
| rafael210 | 1:fa78d980522c | 38 | float toff_sat = 1; // off time saturation value - determines the final duty cycle |
| rafael210 | 1:fa78d980522c | 39 | float ton = 0; // fixed on time |
| rafael210 | 1:fa78d980522c | 40 | float d = 0; // duty cycle starting point |
| rafael210 | 1:fa78d980522c | 41 | float d_sat = 0.9; // determines the final duty cycle |
| rafael210 | 1:fa78d980522c | 42 | int c = 0; // step counter |
| rafael210 | 1:fa78d980522c | 43 | int i = 0; // cycle counter |
| rafael210 | 1:fa78d980522c | 44 | int Ncycles = 0; // Number of cycles |
| rafael210 | 1:fa78d980522c | 45 | |
| rafael210 | 1:fa78d980522c | 46 | |
| rafael210 | 1:fa78d980522c | 47 | float resistance = 15.0; |
| rafael210 | 1:fa78d980522c | 48 | float V_batt = 160.0; |
| rafael210 | 1:fa78d980522c | 49 | float I_out = 10.0; |
| rafael210 | 1:fa78d980522c | 50 | float V_out = 0.0; |
| rafael210 | 1:fa78d980522c | 51 | float freq = 50; //switching frequency in Hz |
| rafael210 | 1:fa78d980522c | 52 | |
| rafael210 | 1:fa78d980522c | 53 | // -------------------------- RampUp RunTime ------------------------------- // |
| rafael210 | 1:fa78d980522c | 54 | int RunTime = 300; |
| rafael210 | 1:fa78d980522c | 55 | int X = 0; |
| rafael210 | 1:fa78d980522c | 56 | int Y = 0; |
| rafael210 | 1:fa78d980522c | 57 | int RampUp = 0; |
| rafael210 | 1:fa78d980522c | 58 | |
| rafael210 | 1:fa78d980522c | 59 | |
| rafael210 | 1:fa78d980522c | 60 | |
| rafael210 | 1:fa78d980522c | 61 | |
| rafael210 | 1:fa78d980522c | 62 | |
| rafael210 | 1:fa78d980522c | 63 | |
| rafael210 | 1:fa78d980522c | 64 | // Current reading and limit testing |
| rafael210 | 0:b265ed6faf6c | 65 | void task1() { |
| rafael210 | 1:fa78d980522c | 66 | task_1_pin = !task_1_pin; |
| rafael210 | 1:fa78d980522c | 67 | i_load = current.read(); |
| rafael210 | 1:fa78d980522c | 68 | i_load = i_load*3.3; |
| rafael210 | 1:fa78d980522c | 69 | |
| rafael210 | 1:fa78d980522c | 70 | if(i_load > 1.6) |
| rafael210 | 1:fa78d980522c | 71 | { |
| rafael210 | 1:fa78d980522c | 72 | c_i++; |
| rafael210 | 1:fa78d980522c | 73 | if(c_i == 5) |
| rafael210 | 1:fa78d980522c | 74 | { |
| rafael210 | 1:fa78d980522c | 75 | led3 = 1; |
| rafael210 | 1:fa78d980522c | 76 | c_i = 0; |
| rafael210 | 1:fa78d980522c | 77 | } |
| rafael210 | 1:fa78d980522c | 78 | } |
| rafael210 | 1:fa78d980522c | 79 | else |
| rafael210 | 1:fa78d980522c | 80 | { |
| rafael210 | 1:fa78d980522c | 81 | led3 = 0; |
| rafael210 | 1:fa78d980522c | 82 | } |
| rafael210 | 0:b265ed6faf6c | 83 | } |
| rafael210 | 0:b265ed6faf6c | 84 | |
| rafael210 | 1:fa78d980522c | 85 | //Serial communication |
| rafael210 | 0:b265ed6faf6c | 86 | void task2() { |
| rafael210 | 1:fa78d980522c | 87 | task_2_pin = !task_2_pin; |
| rafael210 | 1:fa78d980522c | 88 | pc.printf("\n\r Current = %f", i_load ); |
| rafael210 | 1:fa78d980522c | 89 | |
| rafael210 | 0:b265ed6faf6c | 90 | } |
| rafael210 | 0:b265ed6faf6c | 91 | |
| rafael210 | 1:fa78d980522c | 92 | // |
| rafael210 | 0:b265ed6faf6c | 93 | void task3() { |
| rafael210 | 0:b265ed6faf6c | 94 | led1 = !led1; |
| rafael210 | 0:b265ed6faf6c | 95 | } |
| rafael210 | 0:b265ed6faf6c | 96 | |
| rafael210 | 0:b265ed6faf6c | 97 | void task4() { |
| rafael210 | 0:b265ed6faf6c | 98 | led2 = !led2; |
| rafael210 | 0:b265ed6faf6c | 99 | } |
| rafael210 | 0:b265ed6faf6c | 100 | |
| rafael210 | 0:b265ed6faf6c | 101 | int main() { |
| rafael210 | 1:fa78d980522c | 102 | pc.baud (115200); |
| rafael210 | 1:fa78d980522c | 103 | NVIC_SetPriority(TIMER3_IRQn, 0); // set mbed tickers to higher priority than other things |
| rafael210 | 1:fa78d980522c | 104 | |
| rafael210 | 1:fa78d980522c | 105 | task_1_pin = 0; |
| rafael210 | 1:fa78d980522c | 106 | task_2_pin = 0; |
| rafael210 | 0:b265ed6faf6c | 107 | THY_S = 0; |
| rafael210 | 0:b265ed6faf6c | 108 | IGBT_G = 0; |
| rafael210 | 0:b265ed6faf6c | 109 | led1 = 0; |
| rafael210 | 1:fa78d980522c | 110 | led2 = 0; |
| rafael210 | 1:fa78d980522c | 111 | led3 = 0; |
| rafael210 | 0:b265ed6faf6c | 112 | |
| rafael210 | 1:fa78d980522c | 113 | scheduler1.attach(&task1, 0.0001); // Four independent tasks |
| rafael210 | 1:fa78d980522c | 114 | scheduler2.attach(&task2, 0.2); // |
| rafael210 | 1:fa78d980522c | 115 | //scheduler3.attach(&task3, 0.02); // |
| rafael210 | 1:fa78d980522c | 116 | //scheduler4.attach(&task4, 0.01); // |
| rafael210 | 0:b265ed6faf6c | 117 | |
| rafael210 | 0:b265ed6faf6c | 118 | |
| rafael210 | 0:b265ed6faf6c | 119 | while(1) { |
| rafael210 | 0:b265ed6faf6c | 120 | |
| rafael210 | 1:fa78d980522c | 121 | d = 0.5; |
| rafael210 | 1:fa78d980522c | 122 | ton = d/freq; |
| rafael210 | 1:fa78d980522c | 123 | toff = (1/freq)-ton; |
| rafael210 | 1:fa78d980522c | 124 | ton = ton*1000; |
| rafael210 | 1:fa78d980522c | 125 | toff = toff*1000; |
| rafael210 | 1:fa78d980522c | 126 | if(led3 == 1) |
| rafael210 | 1:fa78d980522c | 127 | { |
| rafael210 | 1:fa78d980522c | 128 | break; |
| rafael210 | 1:fa78d980522c | 129 | } |
| rafael210 | 1:fa78d980522c | 130 | |
| rafael210 | 1:fa78d980522c | 131 | IGBT_G= 1; // set IGBT Ground side pin to high |
| rafael210 | 1:fa78d980522c | 132 | wait_us(50); |
| rafael210 | 1:fa78d980522c | 133 | THY_S= 1; |
| rafael210 | 1:fa78d980522c | 134 | wait_us(10); |
| rafael210 | 1:fa78d980522c | 135 | THY_S= 0; |
| rafael210 | 1:fa78d980522c | 136 | wait_us(200); |
| rafael210 | 1:fa78d980522c | 137 | wait_ms(ton); |
| rafael210 | 1:fa78d980522c | 138 | IGBT_G.write(0); |
| rafael210 | 1:fa78d980522c | 139 | wait_ms(toff); |
| rafael210 | 1:fa78d980522c | 140 | |
| rafael210 | 0:b265ed6faf6c | 141 | } |
| rafael210 | 0:b265ed6faf6c | 142 | } |
