【问题标题】:3-phase voltage monitor / Phase sequence using Arduino + EmonLib使用 Arduino + EmonLib 的三相电压监控器/相序
【发布时间】:2017-12-21 22:20:09
【问题描述】:

我正在尝试学习/构建一个基于 Arduino Mega 的三相系统监控设备,它允许用户通过相序检测来监控 220V/50Hz 三相系统电压和电流,以确保正确的接线顺序( L1, l2, l3)。

我从engineer experiences 关注这个项目,并在硬件部分取得了不错的成绩(原理图和电路来自上面的链接)。
对于软件,我使用 EmonLib 进行电压和电流测量。我从 Arduino 得到了正确的读数,我想我不明白与在代码中添加计时器循环相关的部分。

这是我修改后的代码:

#include "EmonLib.h" // Include Emon Library
EnergyMonitor emon0; // L1 Instance
EnergyMonitor emon1; // L2 Instance
EnergyMonitor emon2; // L3 Instance

int s1 = analogRead(A3);
int s2 = analogRead(A4);
int s3 = analogRead(A5);
unsigned int g=0;
int i; // For for loops
unsigned int a22;

void setup() {
  Serial.begin(9600);
  Serial.println("3 phase voltage");
  delay(1000);
  emon0.voltage(0, 225.5, 1.7); // Voltage: input pin, calibration, phase_shift
  emon1.voltage(1, 225.5, 1.7); // Voltage: input pin, calibration, phase_shift
  emon2.voltage(2, 225.5, 1.7); // Voltage: input pin, calibration, phase_shift
  emon0.current(6, 111.1); // Current: input pin, calibration.
  emon1.current(7, 111.1); // Current: input pin, calibration.
  emon2.current(8, 111.1); // Current: input pin, calibration.
}

//Code Start
//Function for angle calculations
int calculations() {
  unsigned int k=0;
  //To complete number of counts
  g=g+1;
  //To convert  into seconds
  float pf=(float)g/1000000;
  //To convert seconds into degrees
  pf=pf*50*360;//here frequency = 50hz
  k = pf;
  return k;
}

void tloop1() {
  while(1) {
    if (s2 != 0) {
      TCNT1=0;
      TCCR1B = 0x01; // Start timer1 at Fcpu/1
      break;
    } else {
      continue;
    }
  }

  while(1) {
    if (s3 != 0){
      TCCR1B = 0x00;//stop timer1
      g=TCNT1;//getting number of counts
      break;
    } else {
      continue;
    }
  }
}

void tloop2() {
  while(1) {
    if (s3 != 0){
      TCNT1=0;
      TCCR1B = 0x01; // Start timer1 at Fcpu/1
      break;
    } else {
      continue;
    }
  }

  while(1) {
    if (s1 != 0){
      TCCR1B = 0x00;//stop timer1
      g=TCNT1;//getting number of counts
      break;
    } else {
      continue;
    }
  }
}

float fmap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

void phase1() {
  unsigned int a1=0;
  unsigned int v1=0;
  emon0.calcVI(20,2000); // Calculate all. No.of wavelengths, time-out
  float L1supplyVoltage = emon0.Vrms; //extract Vrms into Variable
  float Irms0 = emon0.Irms; //extract Irms into Variable
  float cablibratedi0 = 0;
  Serial.print("phase 1: V = ");Serial.println(L1supplyVoltage); // Print out all variables
  cablibratedi0 = fmap(Irms0, 0.0, 1024.0, 0.0, 200.0);
  Serial.print("phase 1: A = ");Serial.println(cablibratedi0); // Print out all variables
  a1 = calculations();
  Serial.print("phase 1: D = ");Serial.println(a1);
  Serial.print("Line to Next Line Voltage:");Serial.println(L1supplyVoltage * 1.732);
}

void phase2() {
  tloop1();
  unsigned int a2=0;
  unsigned int v2=0;
  delay(20);
  emon1.calcVI(20,2000); // Calculate all. No.of wavelengths, time-out
  float L2supplyVoltage = emon1.Vrms; //extract Vrms into Variable
  float Irms1 = emon1.Irms; //extract Irms into Variable
  float cablibratedi1 = 0;
  Serial.print("phase 2: V = ");Serial.println(L2supplyVoltage); // Print out all variables
  cablibratedi1 = fmap(Irms1, 0.0, 1024.0, 0.0, 200.0);
  Serial.print("phase 2: A = ");Serial.println(cablibratedi1); // Print out all variables
  a2 = calculations(); 
  a22 = a2;
  Serial.print("phase 2: D = ");Serial.println(a2);
  Serial.print("Line to Next Line Voltage:");Serial.println(L2supplyVoltage * 1.732);
  delay(700);
}

void phase3() {
  tloop2();
  unsigned int a3=0;
  unsigned int v3=0;
  delay(20);
  emon2.calcVI(20,2000); // Calculate all. No.of wavelengths, time-out
  float L3supplyVoltage = emon2.Vrms; //extract Vrms into Variable
  float Irms2 = emon2.Irms; //extract Irms into Variable
  float cablibratedi2 = 0;
  Serial.print("phase 3: V = ");Serial.println(L3supplyVoltage);           // Print out all variables
  cablibratedi2 = fmap(Irms2, 0.0, 1024.0, 0.0, 200.0);
  Serial.print("phase 3: A = ");Serial.println(cablibratedi2); // Print out all variables
  a3 = calculations();
  a3 = a22 + a3;
  Serial.print("phase 3: D = ");Serial.println(a3);
  Serial.print("Line to Next Line Voltage:");Serial.println(L3supplyVoltage * 1.732);
}

void loop() {
  phase1();
  phase2();
  phase3();
  delay(1000);
}

代码编译没有错误,但是当我从 Arduino IDE 打开串行监视器时,只有 phase1(); 运行并显示一次。

【问题讨论】:

    标签: arduino embedded microcontroller


    【解决方案1】:
    int s1 = analogRead(A3);
    int s2 = analogRead(A4);
    int s3 = analogRead(A5);
    

    不要尝试在函数之外读取引脚。它行不通。变量将被初始化为 0。这意味着您的代码将卡在 while 循环中。

    你需要做的是:

    int s1;
    int s2;
    int s3;
    
    [...]
    
    void setup() {
      s1 = analogRead(A3);
      s2 = analogRead(A4);
      s3 = analogRead(A5);
    

    【讨论】:

      【解决方案2】:

      代码正在运行,这里是它的副本:

      int m; float n;
      int m1; float n1;
      int m2; float n2;
      
      void setup()
      {
        pinMode(A0,INPUT); // L1
        pinMode(A1,INPUT); // L2
        pinMode(A2,INPUT); // L3
        pinMode(A3,INPUT); // A
        pinMode(A4,INPUT); // B
        pinMode(A5,INPUT); // C
        Serial.begin(9600);
      }
      
      void loop()
      {
         m=analogRead(A0);// read analog values from pin A0 across capacitor
         n=(m* .304177);// converts analog value(x) into input ac supply value using this formula ( explained in woeking section)
      
         Serial.print("1   analaog input  " ) ; // specify name to the corresponding value to be printed
         Serial.print(m) ; // print input analog value on serial monitor
         Serial.print("   ac voltage  ") ; // specify name to the corresponding value to be printed
         Serial.print(n) ; // prints the ac value on Serial monitor
         Serial.println();
         delay(1000);
      
         m1=analogRead(A1);// read analog values from pin A0 across capacitor
         n1=(m1* .304177);// converts analog value(x) into input ac supply value using this formula ( explained in woeking section)
      
         Serial.print("2   analaog input  " ) ; // specify name to the corresponding value to be printed
         Serial.print(m1) ; // print input analog value on serial monitor
         Serial.print("   ac voltage  ") ; // specify name to the corresponding value to be printed
         Serial.print(n1) ; // prints the ac value on Serial monitor
         Serial.println();
         delay(1000);
      
         m2=analogRead(A2);// read analog values from pin A0 across capacitor
         n2=(m2* .304177);// converts analog value(x) into input ac supply value using this formula ( explained in woeking section)
      
         Serial.print("3   analaog input  " ) ; // specify name to the corresponding value to be printed
         Serial.print(m2) ; // print input analog value on serial monitor
         Serial.print("   ac voltage  ") ; // specify name to the corresponding value to be printed
         Serial.print(n2) ; // prints the ac value on Serial monitor
         Serial.println();
         delay(1000);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多