forked from euphy/polargraph_server_a1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.ino
243 lines (205 loc) · 4.88 KB
/
util.ino
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
/**
* Polargraph Server. - CORE
* Written by Sandy Noble
* Released under GNU License version 3.
* http://www.polargraph.co.uk
* https://github.com/euphy/polargraph_server_a1
Util.
This is one of the core files for the polargraph server program.
This has all the methods that let the rest actually work, including
the geometry routines that convert from the different coordinates
systems, and do transformations.
*/
long multiplier(int in)
{
return multiplier((long) in);
}
long multiplier(long in)
{
return in * stepMultiplier;
}
float multiplier(float in)
{
return in * stepMultiplier;
}
long divider(long in)
{
return in / stepMultiplier;
}
//void changeLength(long tA, long tB)
//{
// changeLength((float)tA, (float)tB);
//}
void changeLength(long tAl, long tBl)
{
float tA = float(tAl);
float tB = float(tBl);
// Serial.println("changeLenth-float");
lastOperationTime = millis();
// impl_transform(tA,tB);
float currSpeedA = motorA.speed();
float currSpeedB = motorB.speed();
// Serial.print("A pos: ");
// Serial.print(motorA.currentPosition());
// Serial.print(", A target: ");
// Serial.println(tA);
// Serial.print("B pos: ");
// Serial.print(motorB.currentPosition());
// Serial.print(", B target: ");
// Serial.println(tB);
motorA.setSpeed(0.0);
motorB.setSpeed(0.0);
motorA.moveTo(tA);
motorB.moveTo(tB);
if (!usingAcceleration)
{
// The moveTo() function changes the speed in order to do a proper
// acceleration. This counteracts it. Ha.
if (motorA.speed() < 0)
currSpeedA = -currSpeedA;
if (motorB.speed() < 0)
currSpeedB = -currSpeedB;
// Serial.print("Setting A speed ");
// Serial.print(motorA.speed());
// Serial.print(" back to ");
// Serial.println(currSpeedA);
// Serial.print("Setting B speed ");
// Serial.print(motorB.speed());
// Serial.print(" back to ");
// Serial.println(currSpeedB);
motorA.setSpeed(currSpeedA);
motorB.setSpeed(currSpeedB);
}
while (motorA.distanceToGo() != 0 || motorB.distanceToGo() != 0)
{
// Serial.print("dA:");
// Serial.print(motorA.distanceToGo());
// Serial.print(", dB:");
// Serial.println(motorB.distanceToGo());
impl_runBackgroundProcesses();
if (currentlyRunning)
{
if (usingAcceleration)
{
motorA.run();
motorB.run();
}
else
{
// Serial.print("Run speed..");
// Serial.println(motorA.speed());
motorA.runSpeedToPosition();
motorB.runSpeedToPosition();
}
}
}
reportPosition();
}
void changeLengthRelative(float tA, float tB)
{
changeLengthRelative((long) tA, (long)tB);
}
void changeLengthRelative(long tA, long tB)
{
lastOperationTime = millis();
motorA.move(tA);
motorB.move(tB);
while (motorA.distanceToGo() != 0 || motorB.distanceToGo() != 0)
{
//impl_runBackgroundProcesses();
if (currentlyRunning)
{
if (usingAcceleration)
{
motorA.run();
motorB.run();
}
else
{
motorA.runSpeedToPosition();
motorB.runSpeedToPosition();
}
}
}
reportPosition();
}
long getMaxLength()
{
if (maxLength == 0)
{
// float length = getMachineA(pageWidth, pageHeight);
maxLength = long(getMachineA(pageWidth, pageHeight)+0.5);
Serial.print(F("maxLength: "));
Serial.println(maxLength);
}
return maxLength;
}
float getMachineA(float cX, float cY)
{
float a = sqrt(sq(cX)+sq(cY));
return a;
}
float getMachineB(float cX, float cY)
{
float b = sqrt(sq((pageWidth)-cX)+sq(cY));
return b;
}
void moveAxis(AccelStepper &m, int dist)
{
m.move(dist);
while (m.distanceToGo() != 0)
{
impl_runBackgroundProcesses();
if (currentlyRunning)
m.run();
}
lastOperationTime = millis();
}
void reportPosition()
{
if (reportingPosition)
{
Serial.print(OUT_CMD_SYNC_STR);
Serial.print(divider(motorA.currentPosition()));
Serial.print(COMMA);
Serial.print(divider(motorB.currentPosition()));
Serial.println(CMD_END);
}
}
//void engageMotors()
//{
// impl_engageMotors();
//}
//
//void releaseMotors()
//{
// impl_releaseMotors();
//}
//
//
float getCartesianXFP(float aPos, float bPos)
{
float calcX = (sq((float)pageWidth) - sq((float)bPos) + sq((float)aPos)) / ((float)pageWidth*2.0);
return calcX;
}
float getCartesianYFP(float cX, float aPos)
{
float calcY = sqrt(sq(aPos)-sq(cX));
return calcY;
}
long getCartesianX(float aPos, float bPos)
{
long calcX = long((pow(pageWidth, 2) - pow(bPos, 2) + pow(aPos, 2)) / (pageWidth*2));
return calcX;
}
long getCartesianX() {
long calcX = getCartesianX(motorA.currentPosition(), motorB.currentPosition());
return calcX;
}
long getCartesianY() {
return getCartesianY(getCartesianX(), motorA.currentPosition());
}
long getCartesianY(long cX, float aPos) {
long calcY = long(sqrt(pow(aPos,2)-pow(cX,2)));
return calcY;
}