-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlotter.java
More file actions
246 lines (213 loc) · 5.35 KB
/
Plotter.java
File metadata and controls
246 lines (213 loc) · 5.35 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
import lejos.hardware.lcd.LCD;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.hardware.port.Port;
import lejos.hardware.port.SensorPort;
import lejos.hardware.sensor.EV3ColorSensor;
import lejos.hardware.sensor.EV3TouchSensor;
import lejos.hardware.sensor.SensorMode;
public class Plotter
{
EV3LargeRegulatedMotor XYMotors[] = { new EV3LargeRegulatedMotor(MotorPort.D),
new EV3LargeRegulatedMotor(MotorPort.C) };
Coordinate coords;
EV3ColorSensor colorSensor;
SensorMode redColorSensor;
EV3TouchSensor touchSensor;
SensorMode touchSensorTouch;
boolean initialised = false;
Printhead printhead = new Printhead();
public Plotter()
{
}
public void init()
{
colorSensor = new EV3ColorSensor(SensorPort.S1);
colorSensor.setFloodlight(true); // initialisierung Farbsensor
redColorSensor = colorSensor.getRedMode();
touchSensor = new EV3TouchSensor(SensorPort.S2);
touchSensorTouch = touchSensor.getTouchMode();
try
{
homing();
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
initialised = true;
}
public void close()
{
XYMotors[0].close();
XYMotors[1].close();
}
private static void fahre(double streckeInMM, double zeit, EV3LargeRegulatedMotor m, Port port)
{
double durchmesserReifen = 41;
if (port.equals(MotorPort.A))
{
durchmesserReifen = 40;
} else if (port.equals(MotorPort.C))
{
durchmesserReifen = 43.2;
} else
{
return;
}
double umfangReifen = durchmesserReifen * Math.PI;
double umdrehungenReifen = streckeInMM / umfangReifen;
int zahnrad1 = 36;
int zahnrad2 = 12;
double uebersetzungsVerhaetlnis = (double) zahnrad1 / zahnrad2;
double umdrehungenMotor = umdrehungenReifen * uebersetzungsVerhaetlnis;
double gradMotor = umdrehungenMotor * 360;
double gradProSekunde = gradMotor / zeit;
m.setSpeed((float) gradProSekunde);
m.rotate(((int) Math.round(gradMotor)));
}
private static void fahre_mmSec(double streckeInMM, double mmSec, EV3LargeRegulatedMotor m, Port port)
{
double zeit = streckeInMM / mmSec;
fahre(streckeInMM, zeit, m, port);
}
private static void hypo(final double x, final double y, double mmSec, final EV3LargeRegulatedMotor XYMotors[])
throws InterruptedException
{
double hypo = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
double SecMm = 1 / mmSec;
final double seconds = SecMm * hypo;
// double seconds = mmSec / hypo;
Thread thread1 = new Thread(new Runnable()
{
@Override
public void run()
{
fahre(x, seconds, XYMotors[0], MotorPort.A);
}
});
Thread thread2 = new Thread(new Runnable()
{
@Override
public void run()
{
fahre(y, seconds, XYMotors[1], MotorPort.C);
}
});
thread1.start();
thread2.start();
Thread.sleep((long) (seconds * 1000));
}
/**
* Method that zeros the position of the Plotter
*/
public boolean homing() throws InterruptedException
{
final EV3LargeRegulatedMotor XYMotors[] = this.XYMotors;
final SensorMode redColorSensor = this.redColorSensor;
final SensorMode touchSensorTouch = this.touchSensorTouch;
LCD.clear();
LCD.drawString("homing in progress", 0, 0);
LCD.refresh();
Thread colorSensing = new Thread(new Runnable()
{
@Override
public void run()
{
float[] sensorVals = new float[redColorSensor.sampleSize()];
XYMotors[1].setSpeed(500);
XYMotors[1].backward();
while (sensorVals[0] < 0.6)
{
redColorSensor.fetchSample(sensorVals, 0);
try
{
Thread.sleep(1);
} catch (InterruptedException error)
{
error.printStackTrace();
}
}
XYMotors[1].stop();
XYMotors[1].resetTachoCount();
}
});
Thread touchSensing = new Thread(new Runnable()
{
@Override
public void run()
{
float[] sensorVals = new float[touchSensorTouch.sampleSize()];
XYMotors[0].setSpeed(500);
XYMotors[0].forward();
while (sensorVals[0] == 0.0f)
{
touchSensorTouch.fetchSample(sensorVals, 0);
try
{
Thread.sleep(1);
} catch (InterruptedException error)
{
error.printStackTrace();
}
}
XYMotors[0].stop();
XYMotors[0].resetTachoCount();
}
});
touchSensing.start();
colorSensing.start();
coords = new Coordinate(0, 0);
printhead.setCoords(new float[] { 0, 0 });
return true;
}
/**
* Draws a rectangle needs some work, but should be deprecated
*
* @param coordinate 1
* @param coordinate 2
*/
public void dRect(Coordinate c1, Coordinate c2)
{
if (!initialised)
{
return;
}
}
/**
* Draws all kinds of Paths TODO needs Parameters and Code
*/
public void dPath()
{
if (!initialised)
{
return;
}
}
/**
* Draws a hypotenuse with xy coords and speed, does this relative always ONLY
* USE for Testing TODO should be private also
*
* @param x
* @param y
* @param mmSec
*/
public void dHypo(double x, double y, double mmSec)
{
if (!initialised)
{
return;
}
try
{
this.hypo(x, y, mmSec, XYMotors);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void logCoords()
{
}
}