本文共 3691 字,大约阅读时间需要 12 分钟。
代码背景:
动态试验台流程之一.阶跃响应试验需求简而言之,就是信号发生器给试验对象一阶跃信号,位移传感器检测试验对象得到信号后的位移状态。
为了达到目的,位移传感器读取数据的频率要尽可能地快,但是C#的计时器Timer在10ms之内就会出现数据丢失,不过这不是我们要考虑的重点,重点是Timer的使用:信号发生器一次,传感器按信号发生器的周期进行。//动态油缸子类 public class Dynamic_Cylinder : SharedBase { //阶跃响应试验参数 public static int step_time = 0; public static double[] Array3 = new double[1000];//横坐标时间 public static double[] Array4 = new double[1000]; //纵坐标位移 //阶跃响应专用计时器 public System.Timers.Timer stepTimer3 = new System.Timers.Timer(); public System.Timers.Timer stepTimer4 = new System.Timers.Timer(); // 阶跃响应试验方法 public void StepResponseTest() { //此时液压缸已在中位,让液压缸以最高速度运行总行程的40%,即12.8mm stepTimer3.Start(); } public int shortTime = 0; public double step = 8; //阶跃响应试验信号发生计时器,给一个最高速度一半*10ms的方波 public void timer6_Tick(object sender, EventArgs e) { if (shortTime < 1) { server.InstantWrite("比例伺服阀VDS1_a", 5); shortTime++; } else { stepTimer3.Stop(); stepTimer4.Start(); } } //采集阶跃响应频率 public void timer7_Tick(object sender, EventArgs e) { //根据方波,液压缸行程应该是从16mm到28.8mm if (Dynamic_Cylinder.液压缸行程 <28.8) { //把液压缸响应曲线的很纵坐标点分别采集到两个数组里 Dynamic_Cylinder.Array3[Dynamic_Cylinder.Steprecorder] = Dynamic_Cylinder.step_time; Dynamic_Cylinder.Array4[Dynamic_Cylinder.Steprecorder] = Dynamic_Cylinder.液压缸行程; ++Dynamic_Cylinder.Steprecorder; //Ignore:以下两行代码在正式连硬件试验时可以用最后一行注释代替 ++Dynamic_Cylinder.step_time; Dynamic_Cylinder.液压缸行程 = Dynamic_Cylinder.液压缸行程 + step; step = step*4/5; // Dynamic_Cylinder.液压缸行程 = server.InstantRead ("位移传感器LVDT"); } else { stepTimer4.Stop(); for (int j = 0; j <= Dynamic_Cylinder.Steprecorder; j++)//绘图 { //即testGraphInfo.List.Add(MainForm.ps1, MainForm.wy1 / (MainForm.ps1 * a))纵坐标就是wy1/(ps1*a) string str = string.Format("采集第{0}组数据", j); LOG.Info(str); testGraphInfo.List.Add(Dynamic_Cylinder.Array3[j], Dynamic_Cylinder.Array4[j]); } TestEndEvent(); }
c#里的Timer是自带线程的,所以调试的时候也需要考虑到这个问题。
另外,我在试验类中开了一个试验线程,在这个线程的run()函数体中做了Timer的事件绑定,为了简化,我把其它试验内容都删掉了 (Test==11是阶跃响应试验的flag)public override void Run(Object stateInfo) { LOG.Info("试验开始..."); //recorder.StartRecord(); //开始试验记录 try { LOG.Info("开始记录数据"); //如果是阶跃响应试验 if (MainForm.test == 10) { stepTimer1.Enabled = true; stepTimer1.Interval = 100; stepTimer1.Elapsed += timer4_Tick;//事件绑定 stepTimer2.Enabled = true; stepTimer2.Interval = 10; stepTimer2.Elapsed += timer5_Tick;//事件绑定 } //获取实验数据包 istested = true; //停止记录试验 // recorder.EndRecort(); // LOG.Info("试验结束,请打印实验报告..."); } catch (Exception e) { LOG.Debug(e); throw e; } finally { if (MainForm.test == 11) StepResponseTest(); else TestEndEvent(); } }
转载地址:http://lbrsi.baihongyu.com/