高精度记录过零检测零点位置
This commit is contained in:
parent
5f47a501da
commit
fa93b4a1da
|
@ -10,7 +10,6 @@ ACZero::ACZero()
|
|||
Period = 10000;
|
||||
Width = 0;
|
||||
Count = 0;
|
||||
Last = 0;
|
||||
}
|
||||
|
||||
ACZero::~ACZero()
|
||||
|
@ -65,16 +64,11 @@ void ACZero::OnHandler(InputPort& port, bool down)
|
|||
return;
|
||||
}
|
||||
|
||||
auto now = Sys.Ms();
|
||||
Count++;
|
||||
|
||||
// 每256*10ms修正一次周期
|
||||
if (Last > 0 && (Count & 0xFF) == 0)
|
||||
//if (Last > 0 && (Count & 0xFF) == 0)
|
||||
if (Count++ > 0 && (Count & 0xFF) == 0)
|
||||
{
|
||||
// 两次零点
|
||||
int ms = now - Last;
|
||||
int us = ms * 1000;
|
||||
int us = Last.Elapsed();
|
||||
|
||||
// 零点信号可能有毛刺或者干扰,需要避开
|
||||
//if (us <= Period / 2 || us >= Period * 2) return;
|
||||
|
@ -86,23 +80,22 @@ void ACZero::OnHandler(InputPort& port, bool down)
|
|||
debug_printf("OnHandler us=%d Period=%d Width=%d _Delay=%d \r\n", us, Period, Width, _Delay);
|
||||
}
|
||||
|
||||
Last = now;
|
||||
Last.Reset();
|
||||
}
|
||||
|
||||
// 等待下一次零点,需要考虑继电器动作延迟
|
||||
bool ACZero::Wait(int usDelay) const
|
||||
{
|
||||
if (Count == 0 || Last == 0) return false;
|
||||
if (Count == 0) return false;
|
||||
|
||||
// 计算上一次零点后过去的时间
|
||||
UInt64 now = Sys.Ms();
|
||||
int ms = now - Last;
|
||||
if (ms < 0 && ms > 40) return false;
|
||||
int us = Last.Elapsed();
|
||||
if (us < 0 && us > 40000) return false;
|
||||
|
||||
Sys.Trace(4);
|
||||
|
||||
// 计算下一次零点什么时候到来
|
||||
int us = Period - ms * 1000;
|
||||
us = Period - us;
|
||||
// 继电器动作延迟
|
||||
us -= usDelay;
|
||||
|
||||
|
@ -110,17 +103,17 @@ bool ACZero::Wait(int usDelay) const
|
|||
while (us < 0) us += d;
|
||||
while (us > d) us -= d;
|
||||
|
||||
Sys.Trace();
|
||||
//Sys.Trace();
|
||||
//debug_printf("ACZero::Wait 周期=%dus 等待=%dus Width=%dms Count=%d Last=%d Now=%d", Period, us, Width, Count, (int)Last, (int)now);
|
||||
_Delay = us;
|
||||
|
||||
TimeCost tc;
|
||||
//TimeCost tc;
|
||||
|
||||
Sys.Trace();
|
||||
//Sys.Trace();
|
||||
//Sys.Delay(us);
|
||||
Time.Delay(us);
|
||||
|
||||
us = tc.Elapsed();
|
||||
//us = tc.Elapsed();
|
||||
Sys.Trace(4);
|
||||
//debug_printf(" Now2=%d Cost=%d \r\n", (int)Sys.Ms(), us);
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef __ACZero_H__
|
||||
#define __ACZero_H__
|
||||
|
||||
#include "Kernel\TTime.h"
|
||||
#include "Device\Port.h"
|
||||
|
||||
// 交流过零检测
|
||||
|
@ -11,7 +12,7 @@ public:
|
|||
int Period; // 周期us
|
||||
int Width; // 零点信号宽度ms
|
||||
uint Count; // 累计次数
|
||||
UInt64 Last; // 最后一次零点
|
||||
TimeCost Last; // 上一次零点
|
||||
|
||||
ACZero();
|
||||
~ACZero();
|
||||
|
|
|
@ -69,8 +69,9 @@ public:
|
|||
|
||||
TimeCost();
|
||||
|
||||
int Elapsed(); // 逝去的时间,微秒
|
||||
void Show(cstring format = nullptr);
|
||||
void Reset();
|
||||
int Elapsed() const; // 逝去的时间,微秒
|
||||
void Show(cstring format = nullptr) const;
|
||||
};
|
||||
|
||||
/*
|
||||
|
|
|
@ -177,13 +177,18 @@ bool TimeWheel::Expired()
|
|||
/************************************************ TimeCost ************************************************/
|
||||
|
||||
TimeCost::TimeCost()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
void TimeCost::Reset()
|
||||
{
|
||||
Start = Time.Current();
|
||||
StartTicks = Time.CurrentTicks();
|
||||
}
|
||||
|
||||
// 逝去的时间,微秒
|
||||
int TimeCost::Elapsed()
|
||||
int TimeCost::Elapsed() const
|
||||
{
|
||||
int ts = (int)(Time.CurrentTicks() - StartTicks);
|
||||
int ms = (int)(Time.Current() - Start);
|
||||
|
@ -198,7 +203,7 @@ int TimeCost::Elapsed()
|
|||
return ms * 1000 - Time.TicksToUs(-ts);
|
||||
}
|
||||
|
||||
void TimeCost::Show(cstring format)
|
||||
void TimeCost::Show(cstring format) const
|
||||
{
|
||||
if (!format) format = "执行 %dus\r\n";
|
||||
debug_printf(format, Elapsed());
|
||||
|
|
Loading…
Reference in New Issue