FrmPusicanTest.cs
20.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
using PUSICANLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSA_V.Common;
using TSA_V.DeviceLibrary;
using TSA_V.LoadCSVLibrary;
namespace TSA_V
{
public partial class FrmPusicanTest : FrmBase
{
public FrmPusicanTest()
{
InitializeComponent();
}
private void FrmPositionList_Load(object sender, EventArgs e)
{
FormStatus(false);
LanguageProcess();
List<TSAVPosition> list=new List<TSAVPosition> (CSVPositionReader<TSAVPosition>.allPositionMap.Values);
List<NodeInfo> nodeList = new List<NodeInfo>();
//nodeList.Add(new NodeInfo(TSAVBean.Node_X, "红外X轴", "红外X轴"));
//nodeList.Add(new NodeInfo(TSAVBean.Node_Y, "红外Y轴", "红外Y轴"));
foreach (Dictionary<int, NodeInfo> map in TSAVBean.RotateMap.Values)
{
foreach (NodeInfo nodeid in map.Values)
{
nodeList.Add(nodeid);
}
}
if (!IOBase.NoLine)
{
uint chaSlv = (uint)Setting_NInit.Line_NodeAddr.Val;
//nodeList.Add(new NodeInfo(chaSlv, "调宽电机_" + chaSlv, "调宽电机_" + chaSlv));
nodeList.Add(new NodeInfo(chaSlv, "Node: " + chaSlv, "Node: " + chaSlv));
}
this.cmbNode.DataSource = nodeList;
this.cmbNode.DisplayMember = "NodeName";
this.cmbNode.ValueMember = "NodeId";
if (PUSICANControl.Open().Equals(""))
{
lblCanMsg.Text =ResourceCulture.GetString(ResourceCulture.DeviceInConnect, "设备连接成功");
FormStatus(true);
}
else
{
lblCanMsg .Text= ResourceCulture.GetString(ResourceCulture.DeviceNotOkMsg, "设备未连接");
FormStatus(false);
}
SetScreen();
cmbNode.SelectedIndex = cmbNode.Items.Count - 1;
}
private void FormStatus(bool open)
{
timer1.Enabled = open;
btnReadAll.Enabled = open;
btnWriteAll.Enabled = open;
btnStopMove.Enabled = open;
btnSpeedMove.Enabled = open;
btnRelMove.Enabled = open;
btnGetStatus.Enabled = open;
btnAbsMove.Enabled = open;
btnClearStatus.Enabled = open;
lkSetMaxSpeed.Enabled = open;
checkBox1.Enabled = open;
btnSetPosition.Enabled = open;
btnHomeMove.Enabled = open;
llblClearp.Enabled = open;
linkP1.Enabled = open;
btnjog.Enabled = open;
btnjogF.Enabled = open;
linkSetP1.Enabled = true;
lblP1.Enabled = true;
txtCurrP1.Enabled = true;
}
private void btnBack_Click(object sender, EventArgs e)
{
this.Close();
}
private uint GetSelectNode()
{
return FormUtil.GetuIntValue(txtNode);
}
private void showConfigParam()
{
uint nodeId = GetSelectNode();
int addSpeed = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_AddSpeed, nodeId.ToString());
int delSpeed = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_DelSpeed, nodeId.ToString());
int maxPhase = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_MaxPhaseCurrent, nodeId.ToString());
int startSpeed = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_HomeStartSpeed, nodeId.ToString());
int stopSpeed = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_HomeStopSpeed, nodeId.ToString());
int maxSpeed = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_MaxSpeed, nodeId.ToString());
int microValue = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_MicroStepping, nodeId.ToString());
int delValue = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_DelPhaseCurrent, nodeId.ToString());
if (nodeId.Equals(LWidthManager.Line_NodeAddr))
{
if (ConfigAppSettings.GetIntValue(Setting_Init.Line_HomeStartSpeed, nodeId.ToString()) != 0)
{
startSpeed = ConfigAppSettings.GetIntValue(Setting_Init.Line_HomeStartSpeed, nodeId.ToString());
stopSpeed = ConfigAppSettings.GetIntValue(Setting_Init.Line_HomeStopSpeed, nodeId.ToString());
maxSpeed = ConfigAppSettings.GetIntValue(Setting_Init.Line_MaxSpeed, nodeId.ToString());
}
if (ConfigAppSettings.GetIntValue(Setting_Init.Line_MaxPhaseCurrent, nodeId.ToString()) > 0)
{
addSpeed = ConfigAppSettings.GetIntValue(Setting_Init.Line_AddSpeed, nodeId.ToString());
delSpeed = ConfigAppSettings.GetIntValue(Setting_Init.Line_DelSpeed, nodeId.ToString());
maxPhase = ConfigAppSettings.GetIntValue(Setting_Init.Line_MaxPhaseCurrent, nodeId.ToString());
microValue = ConfigAppSettings.GetIntValue(Setting_Init.Line_MicroStepping, nodeId.ToString());
delValue = ConfigAppSettings.GetIntValue(Setting_Init.Line_DelPhaseCurrent, nodeId.ToString());
}
}
this.nudMaxSpeed.Text = maxSpeed.ToString();
this.nudAddSpeed.Text = addSpeed.ToString();
this.nudDelSpeed.Text = delSpeed.ToString();
this.nudStartSpeed.Text = startSpeed.ToString();
this.nudStopSpeed.Text = stopSpeed.ToString();
this.cmbXiFen.Text = microValue.ToString();
this.nudFengzhi.Text = maxPhase.ToString();
this.nudShuaijian.Text = delValue.ToString();
}
private void btnReadAll_Click(object sender, EventArgs e)
{
uint nodeid = GetSelectNode();
this.txtGetPosition.Text = PUSICANControl.GetPosition(nodeid).ToString();
this.nudMaxSpeed.Text = PUSICANControl.GetMaxSpeed(nodeid).ToString();
this.nudAddSpeed.Text = PUSICANControl.GetAddSpeed(nodeid).ToString();
this.nudDelSpeed.Text = PUSICANControl.GetDelSpeed(nodeid).ToString();
this.nudStartSpeed.Text = PUSICANControl.GetStartSpeed(nodeid).ToString();
this.nudStopSpeed.Text = PUSICANControl.GetStopSpeed(nodeid).ToString();
this.cmbXiFen.SelectedIndex = PUSICANControl.getXifenList().IndexOf(PUSICANControl.GetMicroStepping(nodeid).ToString());
this.nudFengzhi.Text = PUSICANControl.GetMaxPhaseCurrent(nodeid).ToString();
this.nudShuaijian.Text = PUSICANControl.GetDelPhaseCurrent(nodeid).ToString();
}
private void btnWriteAll_Click(object sender, EventArgs e)
{
uint nodeid = GetSelectNode();
int value = Convert.ToInt32(this.nudAddSpeed.Value);
PUSICANControl.SetAddSpeed(nodeid, value);
value = FormUtil.GetIntValue(this.txtGetPosition);
PUSICANControl.SetPosition(nodeid, value);
value = (int)this.nudDelSpeed.Value;
PUSICANControl.SetDelSpeed(nodeid, value);
value = (int)this.nudStartSpeed.Value;
PUSICANControl.SetStartSpeed(nodeid, value);
value = (int)this.nudStopSpeed.Value;
PUSICANControl.SetStopSpeed(nodeid, value);
value = Convert.ToInt32(this.cmbXiFen.Text);
PUSICANControl.SetMicroStepping(nodeid, value);
value = Convert.ToInt32(this.nudMaxSpeed.Text);
PUSICANControl.SetMaxSpeed(nodeid, value);
value = Convert.ToInt32(this.nudShuaijian.Text);
PUSICANControl.SetDelPhaseCurrent(nodeid, value);
value = Convert.ToInt32(this.nudFengzhi.Text);
PUSICANControl.SetMaxPhaseCurrent(nodeid, value);
}
private void btnSpeedMove_Click(object sender, EventArgs e)
{
uint nodeid = GetSelectNode();
int speed = Convert.ToInt32(nudMaxSpeed.Text);
PUSICANControl.VolMove(nodeid, speed);
}
private void btnRelMove_Click(object sender, EventArgs e)
{
uint nodeid = GetSelectNode();
int position = FormUtil.GetIntValue(txtPosition);
PUSICANControl.RelMove(nodeid, position);
}
private void btnAbsMove_Click(object sender, EventArgs e)
{
uint nodeid = GetSelectNode();
int position = FormUtil.GetIntValue(txtPosition);
PUSICANControl.AbsMove(nodeid, position);
}
private void btnStopMove_Click(object sender, EventArgs e)
{
uint nodeid = GetSelectNode();
PUSICANControl.StopMove(nodeid);
}
private void btnGetStatus_Click(object sender, EventArgs e)
{
uint nodeid = GetSelectNode();
int value = 0;
if (PUSIResult.RET_SUCCESS == PUSICANControl.ReadSDO(nodeid, CAN_Address.ControlStatus, out value))
{
string str = Convert.ToString(value, 2).PadLeft(5, '0');
lblControlStatus.Text = ("status:stop 3:[" + str.Substring(0, 1) + "],busy:[" + str.Substring(1, 1) + "],Locked-rotor state:["
+ str.Substring(2, 1) + "],stop 2:[" + str.Substring(3, 1) + "],stop 1:[" + str.Substring(4, 1) + "]");
}
lblControlStatus.Text += PUSICANControl.GetGPIO(nodeid);
}
private void btnClearStatus_Click(object sender, EventArgs e)
{
uint nodeid = GetSelectNode();
if (PUSIResult.RET_SUCCESS == PUSICANControl.WriteSDO(nodeid, CAN_Address.ControlStatus, 1))
{
lblControlStatus.Text = ("write CAN_Address.ControlStatus sdo ok.");
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (PUSICANControl.Open().Equals(""))
{
uint nodeid = GetSelectNode();
txtGetPosition.Text = PUSICANControl.GetPosition(nodeid).ToString();
}
}
private void lkSetMaxSpeed_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
uint nodeid = GetSelectNode();
if (PUSIResult.RET_SUCCESS == PUSICANControl.WriteSDO(nodeid, CAN_Address.MaxSpeed, Convert.ToInt32(this.nudMaxSpeed.Text)))
{
lblControlStatus.Text = ("write CAN_Address.MaxSpeed sdo ok.");
}
}
private void cmbNode_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbNode.SelectedItem != null)
{
NodeInfo node = (NodeInfo)cmbNode.SelectedItem;
txtNode.Text = node.NodeId.ToString();
uint nodeid = GetSelectNode();
if (nodeid.Equals(LWidthManager.Line_NodeAddr))
{
txtHomeMoveSpeed.Text = ConfigAppSettings.GetValue(Setting_Init.Line_HomeSpeed);
ShowP1(false,(int)nodeid);
}
else
{
txtHomeMoveSpeed.Text = ConfigAppSettings.GetValue(Setting_Init.RotateNode_HomeSpeed);
ShowP1(true,(int)nodeid);
}
showConfigParam();
if (btnReadAll.Enabled)
{
btnReadAll_Click(null, null);
}
}
}
private void ShowP1(bool v, int nodeId)
{
linkP1.Visible = v;
linkSetP1.Visible = v;
lblP1.Visible = v;
txtCurrP1.Visible = v;
lblPosL.Visible = v;
cmbPostionList.Visible = v;
txtPosValue.Visible = v;
linkPosSave.Visible = v;
if (v)
{
txtCurrP1.Text = Setting_NInit.getRotateMotorP1(nodeId).ToString();
//查找所有位置
List<TSAVPosition> positions = new List<TSAVPosition>();
List<TSAVPosition> allPos = CSVPositionReader<TSAVPosition>.getPositionList();
foreach (TSAVPosition pos in allPos)
{
if (pos.PositionType == 1)
{
NodeInfo node = pos.GetNode(TSAVBean.RotateMap);
if (((int)node.NodeId).Equals(nodeId))
{
positions.Add(pos);
}
}
}
txtPosValue.Text = "";
cmbPostionList.DataSource = null;
cmbPostionList.Items.Clear();
cmbPostionList.DataSource = positions;
cmbPostionList.DisplayMember = "PositionName";
if (positions.Count > 0)
{
cmbPostionList.SelectedIndex = 0;
}
if (Setting_NInit.App_IsDebug)
{
}
else
{
linkSetP1.Visible = false;
linkP1.Visible = false;
linkPosSave.Visible = false;
}
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
timer1.Enabled = checkBox1.Checked;
}
private void FrmPusicanTest_Shown(object sender, EventArgs e)
{
SetSkin(this);
lblCanMsg.ForeColor = Color.Red;
lblControlStatus.ForeColor = Color.Red;
}
private void btnSetPosition_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
uint nodeid = GetSelectNode();
int value = FormUtil.GetIntValue(txtGetPosition);
PUSICANControl.SetPosition(nodeid, value);
}
private void btnHomeMove_Click(object sender, EventArgs e)
{
uint nodeid = GetSelectNode();
if (nodeid.Equals(LWidthManager.Line_NodeAddr))
{
int homeSpeed = FormUtil.GetIntValue(txtHomeMoveSpeed);
PUSICANControl.HomeMove(nodeid, true, homeSpeed);
}
else
{
int homeSpeed = FormUtil.GetIntValue(txtHomeMoveSpeed);
PUSICANControl.HomeMove(nodeid,false, homeSpeed);
}
}
private void llblClearp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
uint nodeid = GetSelectNode();
PUSICANControl.SetPosition(nodeid, 0);
}
private void lblControlStatus_Click(object sender, EventArgs e)
{
}
private void linkP1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
uint nodeid = GetSelectNode();
int pos = Convert.ToInt32(txtGetPosition.Text);
string msg = ResourceCulture.GetString("SureSaveToP1","是否要将位置{0}设为轴{1}的P1点?", pos, nodeid);
DialogResult result=MessageBox.Show(msg, ResourceCulture.GetString("提示", "提示"), MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result.Equals(DialogResult.Yes))
{
Setting_NInit.UpdateRotateMotorP1((int)nodeid, pos);
LogUtil.info($"点击:{linkP1.Text} 设置 轴{nodeid} 的待机点={pos}");
txtCurrP1.Text = Setting_NInit.getRotateMotorP1((int)nodeid).ToString();
}
}
private void linkSetP1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
uint nodeid = GetSelectNode();
int pos = Convert.ToInt32(txtCurrP1.Text);
string msg = ResourceCulture.GetString("SureSaveToP1", "是否要将位置{0}设为轴{1}的P1点?", pos, nodeid);
DialogResult result = MessageBox.Show(msg, ResourceCulture.GetString("提示", "提示"), MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result.Equals(DialogResult.Yes))
{
Setting_NInit.UpdateRotateMotorP1((int)nodeid, pos);
LogUtil.info($"点击:{linkP1.Text} 设置 轴{nodeid} 的待机点={pos}");
txtCurrP1.Text = Setting_NInit.getRotateMotorP1((int)nodeid).ToString();
MessageBox.Show(ResourceCulture.GetString(ResourceCulture.SaveOk, "保存成功"));
}
}
private void cmbPostionList_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbPostionList.SelectedIndex != -1)
{
string text=cmbPostionList.Text;
List<TSAVPosition> allPos = CSVPositionReader<TSAVPosition>.getPositionList();
List<TSAVPosition> positions = (from m in allPos where m.PositionName.Equals(text) select m).ToList();
if(positions!=null&& positions.Count > 0)
{
TSAVPosition tSAVPosition = positions[0];
if (tSAVPosition != null)
{
txtPosValue.Text = tSAVPosition.RotatePosition.ToString();
}
}
}
}
private void linkPosSave_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (cmbPostionList.SelectedIndex != -1)
{
try
{
string text = cmbPostionList.Text;
int value = Convert.ToInt32(txtPosValue.Text);
LogUtil.info("点击按钮,保存库位" + text + "的位置=" + value);
List<TSAVPosition> allPos = CSVPositionReader<TSAVPosition>.getPositionList();
TSAVPosition tSAVPosition = (from m in allPos where m.PositionName.Equals(text) select m).First();
if (tSAVPosition != null)
{
tSAVPosition.RotatePosition = value;
string positionFileName = CSVPositionReader<TSAVPosition>.getPositionFilePath();
CSVPositionReader<TSAVPosition>.SavePostion(positionFileName, tSAVPosition);
MessageBox.Show(ResourceCulture.GetString(ResourceCulture.SaveOk, "保存成功"));
}
}
catch(Exception ex)
{
LogUtil.error(ex.ToString());
}
}
}
private void btnjogF_MouseDown(object sender, MouseEventArgs e)
{
if (btnjogF.BackColor.Equals(Color.Black))
{
uint nodeid = GetSelectNode();
int speed = Math.Abs((int)nudMaxSpeed.Value)/5*-1;
if (speed == 0)
{
return;
}
btnjogF.BackColor = Color.Green;
PUSICANControl.VolMove(nodeid, speed);
}
}
private void btnjogF_MouseUp(object sender, MouseEventArgs e)
{
if (btnjogF.BackColor == Color.Green)
{
btnjogF.BackColor = Color.Black;
PUSICANControl.StopMove(GetSelectNode());
}
}
private void btnjog_MouseDown(object sender, MouseEventArgs e)
{
if (btnjog.BackColor.Equals(Color.Black))
{
uint nodeid = GetSelectNode();
int speed = Math.Abs(Convert.ToInt32(nudMaxSpeed.Text)) /5* 1;
if (speed == 0)
{
return;
}
btnjog.BackColor = Color.Green;
PUSICANControl.VolMove(nodeid, speed);
}
}
private void btnjog_MouseUp(object sender, MouseEventArgs e)
{
if (btnjog.BackColor == Color.Green)
{
btnjog.BackColor = Color.Black;
PUSICANControl.StopMove(GetSelectNode());
}
}
}
}