-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathRobotControl.cs
276 lines (252 loc) · 9.48 KB
/
RobotControl.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Robot;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using Serial;
using Router;
using Commands;
namespace GUI
{
public partial class RobotControl : UserControl, IOpenGLDrawable
{
private Router.Router router;
private Robot.Robot robot;
private SerialPortWrapper serial;
private COMPortForm comPortForm = null;
public RobotControl()
{
InitializeComponent();
serial = new SerialPortWrapper();
}
public void AssignRouter(Router.Router router)
{
this.router = router;
this.robot = new Robot.Robot(serial);
this.robot.onRobotStatusChange += new EventHandler(RobotStatusUpdate);
}
public Robot.Robot GetRobot()
{
return robot;
}
void RobotStatusUpdate(object o, EventArgs e)
{
if (this.InvokeRequired && !this.Disposing)
{
try
{
this.Invoke(new EventHandler(RobotStatusUpdate), new object[] { o, e });
}
catch (ObjectDisposedException)
{
}
}
else
{
IRobotCommandWithStatus status = o as IRobotCommandWithStatus;
if (status != null)
{
this.runButton.Enabled = true;
this.steppersEnabledBox.Enabled = true;
this.zbox.Enabled = true;
this.zGo.Enabled = true;
this.steppersEnabledBox.Checked = status.SteppersEnabled;
if (status.Pausing)
{
pause_resume_button.Text = "Pausing...";
pause_resume_button.Enabled = false;
}
else
{
if (status.Paused)
{
if (pause_resume_button.Text != "Resume")
{
cancelButton.Enabled = true;
pause_resume_button.Text = "Resume";
}
}
else
{
if (pause_resume_button.Text != "Pause")
{
pause_resume_button.Text = "Pause";
}
}
pause_resume_button.Enabled = true;
}
}
}
}
void IOpenGLDrawable.Draw()
{
// Draw the tool location as a cone
Vector3 position = robot.GetPosition();
GL.Color3(Color.Silver);
Polyhedra.DrawCone(position + new Vector3(0, 0, router.ToolDiameter), position, router.ToolDiameter / 2.0f);
Vector3 physicalPosition = robot.GetPhysicalPosition();
GL.Color3(Color.Black);
Polyhedra.DrawCone(physicalPosition + new Vector3(0, 0, router.ToolDiameter), physicalPosition, router.ToolDiameter / 2.0f);
//// Draw the past positions & velocity graph
//float lastTime = 0;
//Vector3 lastPos = new Vector3(0, 0, 0);
//float lastVel = 0;
//bool lastIsGood = false;
//GL.Disable(EnableCap.Lighting);
//lock (previousPoints)
//{
// Vector3 lastpoint = new Vector3(0, 0, 0);
// for (int i = 0; i < previousPoints.Count(); i++)
// {
// PreviousPoint point = previousPoints[i];
// float age_delta = point.createTime - lastTime;
// float time = age_delta / 1000.0f; // Age is microseconds, time is seconds
// float pos_delta = (point.location - lastPos).Length;
// float vel = pos_delta / time; // Inches per second
// Vector3 atpoint = new Vector3(point.location.X * 1000, point.location.Y * 1000, point.location.Z * 1000);
// if (lastIsGood)
// {
// GL.LineWidth(1);
// GL.Begin(PrimitiveType.Lines);
// GL.Color3(Color.LightGray);
// for (int j = 0; j < 5; j++)
// {
// GL.Vertex3(lastpoint + new Vector3(0, 0, j * 10));
// GL.Vertex3(lastpoint + new Vector3(0, 0, j * 10 + 10));
//
// GL.Vertex3(lastpoint + new Vector3(0, 0, j * 10));
// GL.Vertex3(atpoint + new Vector3(0, 0, j * 10));
// }
// GL.End();
// GL.LineWidth(2);
// GL.Begin(PrimitiveType.Lines);
// GL.Color3(Color.Orange);
// GL.Vertex3(lastpoint + new Vector3(0, 0, lastVel * lastVel * 200));
// GL.Vertex3(atpoint + new Vector3(0, 0, vel * vel * 200));
// GL.End();
// }
// lastVel = vel;
// lastpoint = atpoint;
//
// lastPos = point.location;
// lastTime = point.createTime;
// lastIsGood = true;
// }
//}
//GL.Enable(EnableCap.Lighting);
//GL.LineWidth(1);
}
private void pause_resume_button_Click(object sender, EventArgs e)
{
if (pause_resume_button.Text == "Pause")
{
robot.SendPauseCommand();
}
else if (pause_resume_button.Text == "Resume")
{
robot.SendResumeCommand();
cancelButton.Enabled = false;
}
pause_resume_button.Enabled = false;
}
private void cancelButton_Click(object sender, EventArgs e)
{
robot.CancelPendingCommands();
Vector3 position = robot.GetPosition();
robot.AddCommand(new MoveTool(new Vector3(position.X, position.Y, router.MoveHeight), MoveTool.SpeedType.Rapid));
robot.AddCommand(new MoveTool(new Vector3(0, 0, router.MoveHeight), MoveTool.SpeedType.Rapid));
robot.SendResumeCommand();
this.pause_resume_button.Enabled = false;
this.cancelButton.Enabled = false;
}
private void steppersEnabledBox_Click(object sender, EventArgs e)
{
if (steppersEnabledBox.Checked)
{
robot.DisableMotors();
}
else
{
robot.EnableMotors();
}
}
private void runButton_Click(object sender, EventArgs e)
{
foreach (ICommand command in router.GetCommands())
{
robot.AddCommand(command);
}
}
private void zGo_Click(object sender, EventArgs e)
{
float f = float.Parse(zbox.Text);
MoveTool move = new MoveTool(new Vector3(0, 0, f), MoveTool.SpeedType.Rapid);
robot.AddCommand(move);
}
private void button4_Click(object sender, EventArgs e)
{
if (comPortForm == null || comPortForm.IsDisposed)
{
comPortForm = new COMPortForm(serial);
}
if (!comPortForm.Visible)
{
comPortForm.Show(null);
}
else
{
comPortForm.Focus();
}
}
private void button1_Click(object sender, EventArgs e)
{
robot.Zero();
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
robot.z_offset = (float)numericUpDown1.Value;
this.zGo_Click(null, EventArgs.Empty);
}
//private List<PreviousPoint> previousPoints = new List<PreviousPoint>();
//public class PreviousPoint
//{
// public PreviousPoint(float time, Vector3 location)
// {
// this.createTime = time;
// this.location = location;
// }
// public float createTime;
// public Vector3 location;
//}
//void RouterPositionUpdate(object o, EventArgs e)
//{
// StatusCommand status = o as StatusCommand;
// if (status != null)
// {
// Vector3 position = status.CurrentPosition;
// float time = status.time;
// float distance = (lastPosition - position).Length;
//
// if ((lastPosition - position).Length > 0.0001f)
// {
// lock (previousPoints)
// {
// //Console.WriteLine("{0},{1}", time, distance);
// while (previousPoints.Count > 1000)
// {
// previousPoints.RemoveAt(0);
// }
// previousPoints.Add(new PreviousPoint(time, position));
// lastPosition = position;
// }
// }
// }
//}
}
}