You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

370 lines
12 KiB
C#

using Rs.Framework;
using Rs.Motion.Base;
using Rs.MotionPlat.Flow;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace Rs.MotionPlat
{
public partial class OldTest : BaseForm
{
bool m_bPause = false;
string configFilePath = "config/oldtest.xml";
private OldTestConfig config;
public OldTest()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
private void OldTest_Load(object sender, EventArgs e)
{
config = LoadConfig();
dataGridView1.AutoGenerateColumns = false;
//添加第一列
DataGridViewTextBoxColumn column1 = new DataGridViewTextBoxColumn();
column1.ReadOnly = true;
column1.Name = "cboxAxis";
column1.HeaderText = "轴名称";
column1.Width = 150;
column1.DataPropertyName = "AxisName";
dataGridView1.Columns.Add(column1);
//添加第二列
DataGridViewTextBoxColumn column2 = new DataGridViewTextBoxColumn();
column2.Name = "axisSpeed";
column2.HeaderText = "速度比例";
column2.DataPropertyName = "Speed";
dataGridView1.Columns.Add(column2);
//添加第三列
DataGridViewTextBoxColumn column3 = new DataGridViewTextBoxColumn();
column3.Name = "repeatTimes";
column3.HeaderText = "循环次数";
column3.DataPropertyName = "RepeatTimes";
dataGridView1.Columns.Add(column3);
//添加第四列
DataGridViewTextBoxColumn column4 = new DataGridViewTextBoxColumn();
column4.Name = "sleepTime";
column4.HeaderText = "间隔时间";
column4.DataPropertyName = "SleepTime";
dataGridView1.Columns.Add(column4);
//添加第五列
DataGridViewTextBoxColumn column5 = new DataGridViewTextBoxColumn();
column5.Name = "startPoint";
column5.HeaderText = "起点";
column5.DataPropertyName = "StartPos";
dataGridView1.Columns.Add(column5);
//添加第六列
DataGridViewTextBoxColumn column6 = new DataGridViewTextBoxColumn();
column6.Name = "endPoint";
column6.HeaderText = "终点";
column6.DataPropertyName = "EndPos";
dataGridView1.Columns.Add(column6);
//添加第七列
DataGridViewButtonColumn column7 = new DataGridViewButtonColumn();
column7.Name = "axisSpeed";
column7.HeaderText = "启动";
column7.DefaultCellStyle.NullValue = "启动";
//column7.Width = 50;
//column7.FillWeight = 20;
dataGridView1.Columns.Add(column7);
dataGridView1.DataSource = config.MovePoints;
}
protected override void OnClosing(CancelEventArgs e)
{
quit = true;
base.OnClosing(e);
}
private OldTestConfig LoadConfig()
{
OldTestConfig config = new OldTestConfig();
XmlSerializerHelper.Instance.Deserialize<OldTestConfig>(configFilePath, out config);
if(config==null)
{
config = new OldTestConfig();
Type type = typeof(AxisControl);
PropertyInfo[] pis = type.GetProperties(BindingFlags.Public | BindingFlags.Static);
foreach (var item in pis)
{
MoveParameter move = new MoveParameter();
move.AxisName = item.Name;
move.Speed = 5;
move.RepeatTimes = 10;
move.SleepTime = 100;
move.StartPos = 10;
move.EndPos = 20;
config.MovePoints.Add(move);
}
XmlSerializerHelper.Instance.Serialize<OldTestConfig>(configFilePath, config);
}
else
{
Type type = typeof(AxisControl);
PropertyInfo[] pis = type.GetProperties(BindingFlags.Public | BindingFlags.Static);
foreach (var item in pis)
{
if(config.MovePoints.Where(m=>m.AxisName==item.Name).Count()>0)
{
continue;
}
MoveParameter move = new MoveParameter();
move.AxisName = item.Name;
move.Speed = 5;
move.RepeatTimes = 10;
move.SleepTime = 100;
move.StartPos = 10;
move.EndPos = 20;
config.MovePoints.Add(move);
}
}
return config;
}
private void btnAddAxis_Click(object sender, EventArgs e)
{
int rowIndex = dataGridView1.Rows.Add();
DataGridViewComboBoxCell cell = dataGridView1.Rows[rowIndex].Cells[0] as DataGridViewComboBoxCell;
dataGridView1.Rows[rowIndex].Cells[1].Value = 5;//速度
dataGridView1.Rows[rowIndex].Cells[2].Value = 10;//次数
dataGridView1.Rows[rowIndex].Cells[3].Value = 100;//间隔
dataGridView1.Rows[rowIndex].Cells[4].Value = 10;//起点
dataGridView1.Rows[rowIndex].Cells[5].Value = 20;//终点
dataGridView1.Rows[rowIndex].Cells[6].Value = "启动";
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if(e.ColumnIndex==6)
{
string axisName = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
IAxis axis = AxisControl.GetAxis(axisName);
if(axis!=null)
{
if(axis.HomeStatus== Motion.EHomeStatus.Finished)
{
//开启一个线程,开始运动
Task task = new Task(() =>
{
MoveParameter mp = config[axisName];
int workStep = 0;
while (true && mp.RepeatTimes > 0 && quit==false)
{
if(m_bPause)
{
Thread.Sleep(100);
continue;
}
switch (workStep)
{
case 0://到起点
axis.MovePos(mp.StartPos, mp.Speed);
workStep++;
break;
case 1:
axis.IsStop(out bool bStop);
if (bStop)
{
workStep++;
Thread.Sleep(mp.SleepTime);
//设置
//double targetPos = -105;
//targetPos-
axis.SetPosCompare(1, new double[] { -20,-80 });
}
break;
case 2:
axis.MovePos(mp.EndPos, mp.Speed);
workStep++;
break;
case 3:
axis.IsStop(out bStop);
if (bStop)
{
mp.RepeatTimes--;
workStep = 0;
axis.SetPosCompare(1, new double[] { 20, 80 });
Thread.Sleep(mp.SleepTime);
}
break;
default:
break;
}
}
});
task.Start();
}
}
}
}
private void btnSave_Click(object sender, EventArgs e)
{
try
{
if (File.Exists(configFilePath))
File.Delete(configFilePath);
if(config!=null)
{
if( XmlSerializerHelper.Instance.Serialize<OldTestConfig>(configFilePath, config))
{
MessageBox.Show("Save sucess","Info",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
}
catch (Exception)
{
}
}
private void btnStop_Click(object sender, EventArgs e)
{
m_bPause = true;
}
private void btnStart_Click(object sender, EventArgs e)
{
m_bPause = false;
}
private void button1_Click(object sender, EventArgs e)
{
//AxisControl.LoadX.SetPosCompare(1, new double[] { -20,-40});
}
}
public class OldTestConfig
{
public BindingList<MoveParameter> MovePoints { get; set; } = new BindingList<MoveParameter>();
public MoveParameter this[string axisName]
{
get
{
if (MovePoints.Where(m => m.AxisName == axisName).Count()>0)
return MovePoints.Where(m => m.AxisName == axisName).First();
return null;
}
}
}
public class MoveParameter:INotifyPropertyChanged
{
private string _AxisName;
/// <summary>
/// 轴名称
/// </summary>
public string AxisName
{
get { return _AxisName; }
set {
NotifyPropertyChange(ref _AxisName, value);
}
}
private int _Speed;
/// <summary>
/// 运动速度(1=100)
/// </summary>
public int Speed
{
get { return _Speed; }
set {
NotifyPropertyChange(ref _Speed, value);
}
}
private int _RepeatTimes;
/// <summary>
/// 重复次数
/// </summary>
public int RepeatTimes
{
get { return _RepeatTimes; }
set
{
NotifyPropertyChange(ref _RepeatTimes, value);
}
}
private int _SleepTime;
/// <summary>
/// 等待时间
/// </summary>
public int SleepTime
{
get { return _SleepTime; }
set {
NotifyPropertyChange(ref _SleepTime, value);
}
}
private double _StartPos;
/// <summary>
/// 起点
/// </summary>
public double StartPos
{
get { return _StartPos; }
set {
NotifyPropertyChange(ref _StartPos, value);
}
}
private double _EndPos;
/// <summary>
/// 终点
/// </summary>
public double EndPos
{
get { return _EndPos; }
set {
NotifyPropertyChange(ref _EndPos, value);
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChange<T>(ref T oldValue, T newValue, [CallerMemberName] string propertyName = "")
{
if (Object.Equals(oldValue, newValue))
return;
oldValue = newValue;
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}