Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.74 KB | None | 0 0
  1. #include "PWM.h"
  2.  
  3. /*
  4.  
  5.  Mimics the fade example but with an extra parameter for frequency. It should dim but with a flicker
  6.  because the frequency has been set low enough for the human eye to detect. This flicker is easiest to see when
  7.  the LED is moving with respect to the eye and when it is between about 20% - 60% brighness. The library
  8.  allows for a frequency range from 1Hz - 2MHz on 16 bit timers and 31Hz - 2 MHz on 8 bit timers. When
  9.  SetPinFrequency()/SetPinFrequencySafe() is called, a bool is returned which can be tested to verify the
  10.  frequency was actually changed.
  11.  
  12.  This example runs on mega and uno.
  13.  */
  14.  
  15. int32_t frequency = 20000; //Frequency of the PWM to the H-Bridge in Hz
  16. #define pwmpin1 9
  17. #define pwmpin2 10
  18. #define buckboostpin 3
  19. #define opto_pin 4
  20. #define ammeterpin A0
  21. #define voltmeterpin A1
  22.  
  23. double analogValue;
  24. double analogValue1, analogValue2;
  25. double wavePeriod = 20000; //Period of 1 sine wave in microseconds, 50Hz -> 20ms/wave -> 20 000 micro seconds
  26. double hardOffset = 8600; //Number of microseconds the optocoupler input lags behind grid sine                    //EXTEND TO INCLUDE FILTER AND OTHER DELAY
  27.  
  28. int opto_prevVal = 0, opto_curVal = 0;
  29. double opto_totalTime;
  30. double opto_t0, opto_t1, opto_startTime;
  31. int opto_counter = 0, opto_counterLimit = 5; //The higher this value, the more accurate the average wavePeriod becomes
  32. int opto_measureFlag = 0;
  33. double opto_syncDelay = 0, opto_syncDelayDiff = 0;
  34. double avgPhaseTimeOffset = 0;
  35. double currentPhaseTimeOffset = 0;
  36.  
  37. double prevPower = 0, curPower;
  38. double buckboostDutyCycle = 10.0;
  39.  
  40. void setup() {
  41.   Serial.begin(115200);
  42.   InitTimersSafe();
  43.   //sets the frequency for the specified pin
  44.   bool success1 = SetPinFrequencySafe(pwmpin1, frequency);
  45.   bool success2 = SetPinFrequencySafe(pwmpin2, frequency);
  46.   bool success3 = SetPinFrequencySafe(buckboostpin, frequency);
  47.  
  48.   if((success1) && (success2) && (success3))
  49.     Serial.println("Frequency of all pins configured correctly");
  50.   else
  51.   {
  52.     if(!(success1))
  53.       Serial.println("Frequency of pin 1 not set correctly");
  54.     if(!(success2))
  55.       Serial.println("Frequency of pin 2 not set correctly");
  56.     if(!(success3))
  57.       Serial.println("Frequency of pin 3 not set correctly");
  58.   }
  59. }
  60.  
  61. void loop() {
  62.  
  63.   //--------------------------------------vv Phase locked loop vv--------------------------------------
  64.   opto_curVal= digitalRead(opto_pin);
  65.   if(opto_curVal == 1 && opto_prevVal == 0) //Check when optocoupler signal goes high
  66.   {
  67.     if(opto_measureFlag == 0) //Starting the time counter
  68.     {
  69.       opto_t0 = micros();
  70.       avgPhaseTimeOffset = avgPhaseTimeOffset + (1/(double(opto_counterLimit)))*sin(2*3.1415*(opto_t0 - opto_syncDelay)/wavePeriod);
  71.       opto_measureFlag = 2;
  72.     }
  73.     if(opto_measureFlag == 1) //Check when optocoupler signal goes high (again) after timer is started and after signal has gone low
  74.     {
  75.       opto_t1 = micros();
  76.       opto_measureFlag = 0;
  77.       opto_totalTime = opto_totalTime + (opto_t1 - opto_t0);
  78.       opto_counter++;
  79.       if(opto_counter == opto_counterLimit)
  80.       {
  81.         //Sync the delay between startpoints (opto_SyncDelay) of grid wave and H-Bridge wave and the difference in frequency of the two waves (->Correcting H-Bridge PWM frequency wavePeriod)
  82.         opto_syncDelay = opto_syncDelay + avgPhaseTimeOffset*1000;//syncDelay is in microseconds, this line shifts the delay left or right with a value that decreases as the gap closes
  83.         //wavePeriod = wavePeriod - (wavePeriod - opto_totalTime/5)*0.01; //If waveperiod of grid is larger than wavePeriod, wavePeriod increases by difference * factor
  84.  
  85.         opto_counter = 0;
  86.         opto_totalTime = 0;
  87.         avgPhaseTimeOffset = 0;
  88.       }
  89.     }
  90.   }
  91.   if(opto_measureFlag == 2 && opto_prevVal == 1 && opto_curVal == 0) //Check when optoCoupler signal goes LOW
  92.     opto_measureFlag = 1;
  93.  
  94.   opto_prevVal = opto_curVal;
  95.  
  96.  
  97.  
  98.   //--------------------------------------^^ Phase locked loop ^^--------------------------------------
  99.   //--------------------------------------vv H-Bridge control vv--------------------------------------
  100.  
  101.   analogValue = 255*sin(((double(micros())+(wavePeriod - opto_syncDelay - hardOffset))/wavePeriod)*2*3.141592);
  102.   //H-Bridge consists of 2 drivers controlling either the top or bottom half of sinewave. Idle driver receives 0, working driver recieves (positive) SPWM
  103.   if(analogValue > 0)
  104.   {
  105.     analogValue2 = 0;
  106.     analogValue1 = analogValue;
  107.   }
  108.   else
  109.   {
  110.     analogValue1 = 0;
  111.     analogValue2 = -analogValue;
  112.   }
  113.   pwmWrite(pwmpin1, analogValue1);
  114.   pwmWrite(pwmpin2, analogValue2);
  115.  
  116.   //--------------------------------------^^ H-Bridge control ^^--------------------------------------
  117.   //--------------------------------------vv Buck/Boost converter control and MPPT vv--------------------------------------
  118.   /*
  119.   if(opto_counter == 3) //Value is arbitrarily chosen to spread the calculations
  120.   {
  121.     //curpower = current * voltage
  122.     curPower = ((2.52 - (double(analogRead(ammeterpin))*(5/1023)) * 1.112) * (double((analogRead(A1)));               //DEFINE MEASURING RANGES OF BOTH VOLTMETER AND AMMETER
  123.     if(prevPower > curPower)
  124.       buckboostDutyCycle = buckboostDutyCycle + 0.1;                                                                  //SIGNS PROBABLY INCORRECT -> DEPENDS ON BUCK OR BOOST CONVERTER
  125.     if(prevPower < curPower)
  126.       buckboostDutyCycle = buckboostDutyCycle - 0.1;
  127.     pwmWrite(buckboostpin, buckboostDutyCycle);
  128.     prevPower = curPower;  
  129.      
  130.     //EXTEND CODE TO INCLUDE OPTIMIZATION FOR HARDPHASEDELAY (Alternating? Simultaneously? Consecutively?)
  131.   }
  132.   */
  133.   //--------------------------------------^^ Buck/Boost converter control and MPPT ^^--------------------------------------
  134.  
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement