sandeep03edu

Mann Morrision Arduino Code

May 4th, 2023 (edited)
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.07 KB | None | 0 0
  1. float prevLast = -100;
  2. float last = -100;
  3. float f = 0.25;
  4.  
  5. float timeDelay = max(0.001, 1/(40*f));
  6. float pi = 3.14159265359;
  7.  
  8. int analogPinPositive = A0;
  9.  
  10. int rst = 0;
  11. int rstPin = 7;
  12. float offSet = 2.5;
  13. int sample = 0;
  14. float cutoff = 2.5;
  15. float VpeakReal = 2;
  16.  
  17.  
  18. void setup() {
  19.   // put your setup code here, to run once:
  20.     Serial.begin(9600);
  21.     Serial.print("Setup completed!!\n");
  22. }
  23.  
  24. // Mann-Morrision algorithm approach for peak voltage calculation
  25. float getPeakVol(float curr){
  26.   float vDash = curr - prevLast;
  27.   vDash = vDash/2;
  28.   vDash = vDash/timeDelay;
  29.  
  30.   float omega = 2*pi*f;
  31.   float last2 = last*last;
  32.  
  33.   float term2 = vDash/omega;
  34.   term2 = term2*term2;
  35.  
  36.   float vPeak = last2 + term2;
  37.   vPeak = sqrt(vPeak);
  38.   return vPeak;
  39. }
  40.  
  41. // Mann-Morrision algorithm approach for phase calculation
  42. float getPhaseDeg(float curr){
  43.   float vDash = curr - prevLast;
  44.   vDash = vDash/2;
  45.   vDash = vDash/timeDelay;
  46.  
  47.   float omega = 2*pi*f;
  48.   float term1 = vDash/omega;
  49.   float radPhase = atan2(last, term1) - omega*sample*timeDelay;
  50.   float degPhase = radPhase*(180/pi);
  51.  
  52.   Serial.print(degPhase, 2);
  53.   Serial.print(" ");
  54.  
  55.   return degPhase;
  56. }
  57.  
  58. // Two sampling method to calculate phase
  59. float getPhaseTwoSample(float currVolt, float currI, float peakV, float peakI) {
  60.   float omega = 2 * pi * f;
  61.   float theta = omega * timeDelay;
  62.   float deno = peakV * peakI * sin(theta) * sin(theta);
  63.   float term1 = prevVolt * prevI + currVolt * currI;
  64.   float term2 = (prevI * currVolt + prevVolt * currI) * cos(theta);
  65.   float num = term1 - term2;
  66.   float phase = num / deno;
  67.   phase = acosf(phase);
  68.   float degPhase = phase * (180 / pi);
  69.   return degPhase;
  70. }
  71.  
  72.  
  73.  
  74. void loop() {
  75.   // put your main code here, to run repeatedly:
  76.   delay(timeDelay*1000);
  77.  
  78.   rst = digitalRead(rstPin);
  79.  
  80. // Reset the circuit values if reset is enabled
  81.   if(rst){
  82.     prevLast = 0;
  83.     last = 0;
  84.     sample = 0;
  85.     return;
  86.   }
  87.  
  88.   // Scanning user input
  89.   int sensorValue = analogRead(analogPinPositive); // read the analog input value
  90.   float voltage = sensorValue * (5.0 / 1023.0); // convert the sensor value to voltage (assuming 5V reference voltage)
  91.   voltage = voltage - offSet;
  92.  
  93.   // Input wave
  94.   Serial.print(voltage, 2); // print the input voltage value with 2 decimal places
  95.   Serial.print(" ");
  96.  
  97.   // Setting default values
  98.   if(prevLast ==-100){
  99.     prevLast = voltage;
  100.     return;
  101.   }
  102.  
  103.   // Setting default values
  104.   if(last==-100){
  105.     last = voltage;
  106.     return;
  107.   }
  108.  
  109.   // Average peak voltage
  110.   float rmsVol = getPeakVol(voltage);
  111.   Serial.print(rmsVol, 2); // print the peak rms value with 2 decimal places
  112.  
  113.   // Avergae phase
  114.   float degPhase = getPhaseDeg(voltage);
  115.   Serial.print(degPhase, 2);
  116.  
  117.   Serial.print(" ");
  118.   float error = VpeakReal - peakVol;
  119.   Serial.print(error);
  120.   Serial.print(" ");
  121.  
  122.  
  123.   if(peakVol>cutoff){
  124.     // Fault issue
  125.     Serial.print(" Fault ");
  126.   }
  127.   else{
  128.     // Input voltage is under range
  129.     Serial.print(" NoFault ");
  130.   }
  131.  
  132.   prevLast = last;
  133.   last = voltage;
  134.   Serial.print("\n");
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment