inverter

code for an inverter controler
git clone git://git.yotsev.xyz/inverter.git
Log | Files | Refs | README | LICENSE

inverter.ino (3163B)


      1 #include <math.h>
      2 
      3 #define p1 7
      4 #define p2 6
      5 #define n1 5
      6 #define n2 4
      7 
      8 /* specify the frequency of AC */
      9 /* =========================== */
     10 constexpr float freq = 10.0f;
     11 /* =========================== */
     12 
     13 // resolution / number of roots
     14 constexpr int res = 220;
     15 
     16 // temporary pointer for the top of an array
     17 unsigned int index = 0;
     18 float roots[res];
     19 int time_intervals[res - 1]; // in microseconds
     20 
     21 constexpr float pi = 3.141592653589793;
     22 
     23 float Wave(const float& x)
     24 {
     25     return (2.1f / pi) * asin(sin(float(res) * pi * x))
     26         + sin(2.0f * pi * x - pi / 2.0f);
     27 }
     28 
     29 bool led_is_on = false;
     30 
     31 void Converge(
     32     const float& lower,
     33     const float& upper,
     34     const int& exponent)
     35 {
     36     // base case, maximum float precision reached for root
     37     if (exponent < -7) {
     38         roots[index] = (lower + upper) / 2;
     39         index++;
     40         // flash builtin LED
     41         digitalWrite(LED_BUILTIN, (led_is_on) ? LOW : HIGH);
     42         led_is_on = !led_is_on;
     43         return;
     44     }
     45 
     46     // record if the wave function is positive or negative before it
     47     // crosses the x-axis
     48     bool is_positive = (Wave(lower) > 0) ? true : false;
     49     // define the incremental step
     50     float step = 1.0f * pow(10, exponent);
     51 
     52     // scan for a change of sign in Wave() between upper and lower bound
     53     for (float func, x = lower; x < upper + step; x += step) {
     54         // record output of Wave() to avoid multiple calls for same x
     55         func = Wave(x);
     56         // sign of the output has changed / root is found
     57         if ((func > 0 && !is_positive) || (func < 0 && is_positive)) {
     58             // go one magnitude lower for more precision
     59             Converge(x - step, x, exponent - 1);
     60             is_positive = !is_positive;
     61         }
     62     }
     63 }
     64 
     65 void setup()
     66 {
     67     pinMode(p1, OUTPUT);
     68     pinMode(p2, OUTPUT);
     69     pinMode(n1, OUTPUT);
     70     pinMode(n2, OUTPUT);
     71 
     72     pinMode(LED_BUILTIN, OUTPUT);
     73 
     74     // find roots
     75     Converge(0, 1, -4);
     76 
     77     digitalWrite(LED_BUILTIN, LOW);
     78 
     79     // calculate intervals between roots
     80     index = 0;
     81     for (int i = 1; i < res; ++i) {
     82         time_intervals[index]
     83             = int(1000000.0f * (roots[i] - roots[i - 1]) / (freq * 2.0f));
     84         index++;
     85     }
     86 }
     87 
     88 void loop()
     89 {
     90     // set other set of transistors off to avoid feedback
     91     digitalWrite(p2, LOW);
     92     digitalWrite(n1, HIGH);
     93     // set first transistor (from +) on
     94     digitalWrite(p1, HIGH);
     95 
     96     for (int i = 0; i < res - 1; i += 2) {
     97         // set second transistor (from +) on
     98         digitalWrite(n2, LOW);
     99         delayMicroseconds(time_intervals[i]);
    100         // set second transistor (from +) off
    101         digitalWrite(n2, HIGH);
    102         delayMicroseconds(time_intervals[i + 1]);
    103     }
    104 
    105     // set other set of transistors off to avoid feedback
    106     digitalWrite(p1, LOW);
    107     digitalWrite(n2, HIGH);
    108     // set first transistor (from +) on
    109     digitalWrite(p2, HIGH);
    110 
    111     for (int i = 0; i < res - 1; i += 2) {
    112         // set second transistor (from +) on
    113         digitalWrite(n1, LOW);
    114         delayMicroseconds(time_intervals[i]);
    115         // set second transistor (from +) off
    116         digitalWrite(n1, HIGH);
    117         delayMicroseconds(time_intervals[i + 1]);
    118     }
    119 }