-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHotwireControl.cs
More file actions
75 lines (60 loc) · 1.82 KB
/
HotwireControl.cs
File metadata and controls
75 lines (60 loc) · 1.82 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
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hotwire
{
public class HotwireControl
{
public event EventHandler AfterMovement;
private Configuration _configuration;
private HotwirePort _port;
public HotwireControl(Configuration configuration, HotwirePort port)
{
_configuration = configuration;
_port = port;
}
public static short Invert(decimal value, bool configuration, bool invert)
{
short steps = (short)value;
if ((invert && !configuration) || (configuration && !invert))
steps = (short)(-steps);
return steps;
}
public void MoveRelative(double x, double y)
{
x = _configuration.X + x;
y = _configuration.Y + y;
MoveAbsolute(x, y);
}
public void MoveAbsolute(double x, double y)
{
Vector2 vec = new Vector2(x, y) - new Vector2(-_configuration.SpoolDiameter / 2, 0);
double newLenA = vec.Length;
vec = new Vector2(x, y) - new Vector2(_configuration.LeftDistance + _configuration.SpoolDiameter / 2, 0);
double newLenB = vec.Length;
double lenDiffA = newLenA - _configuration.LengthA;
double lenDiffB = newLenB - _configuration.LengthB;
// Invert due to orientation of steppers
double stepsA = -(lenDiffA / 10 /*cm*/ ) * _configuration.StepsA;
double stepsB = -(lenDiffB / 10 /*cm*/ ) * _configuration.StepsB;
if (_configuration.ReverseA)
stepsA = -stepsA;
if (_configuration.ReverseB)
stepsB = -stepsB;
_port.MoveMotors((short)stepsA, (short)stepsB, 0, 0);
_configuration.X = x;
_configuration.Y = y;
_configuration.LengthA = newLenA;
_configuration.LengthB = newLenB;
OnAfterMovement();
}
protected virtual void OnAfterMovement()
{
if (AfterMovement != null)
AfterMovement(this, EventArgs.Empty);
}
}
}