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
|
/* @author Ed Sells 27 July 2007
*
* Dialogue for fast head positioning (maintenance/research scenarios)
*
* Status: Working on it. Appologies for bosnian code -
* one of my first ever coding attempts. Advice welcome!
* en0es@bath.ac.uk
*
* Code borrowed from org.reprap.gui.steppertest.Main v818
*
*/
package org.reprap.gui.steppertest;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import org.reprap.Preferences;
import org.reprap.comms.Communicator;
import org.reprap.comms.snap.SNAPAddress;
import org.reprap.comms.snap.SNAPCommunicator;
import org.reprap.devices.GenericExtruder;
import org.reprap.devices.GenericStepperMotor;
import org.reprap.gui.Utility;
import java.awt.Color;
public class MaintenancePositionsFrame extends JFrame {
// Panel globals
private JLabel warning;
private JLabel area;
private JLabel status;
private final int fastSpeed = 245;
// 'Talk to bot' globals
private final int localNodeNumber = 0;
Communicator communicator;
// Operation globals
GenericStepperMotor motorX, motorY;
boolean homePositionAlreadyFound = false;
public MaintenancePositionsFrame()
{
/*
* TODO: Trying to recognise null/cartesian status: Won't work?
*
* //Establish connection type
String connection = "nullcartesian";
try {
connection = Preferences.loadGlobalString("Geometry");
}
catch (Exception e){
JOptionPane.showMessageDialog(null, "Can't establish 'Geometry parameter'" + e);
return;
}
//Initialise comms with bot
if (connection == "cartesian"){*/
try {
talkToBot();
}
catch (Exception e){
JOptionPane.showMessageDialog(null, "Can't talk to bot: " + e);
return;
}
//}
//Establish motors
try {
motorX = new GenericStepperMotor(communicator, new SNAPAddress(Preferences.loadGlobalInt("XAxisAddress")), Preferences.getGlobalPreferences(), 1);
motorY = new GenericStepperMotor(communicator, new SNAPAddress(Preferences.loadGlobalInt("YAxisAddress")), Preferences.getGlobalPreferences(), 2);
}
catch (Exception e){
JOptionPane.showMessageDialog(null, "Couldn't initialise motors" + e);
return;
}
//Build frame
setTitle("Maintenance positions");
warning = new JLabel("WARNING: Use Working Volume Probe to establish WorkingAxis(mm) preferences first.");
area = new JLabel("Finding working area...");
status = new JLabel("To activate the sector buttons, click home...");
status.setForeground(Color.red);
JPanel text = new JPanel();
text.setLayout(new GridLayout(3,1));
text.add(warning);
text.add(area);
text.add(status);
getContentPane().add(text, BorderLayout.NORTH);
//Add grid of buttons for sector positioning of head
try {
addSectorGrid();
}
catch (Exception e){
JOptionPane.showMessageDialog(null, "Couldn't add controls" + e);
return;
}
pack();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
show();
}
public void talkToBot() throws Exception {
SNAPAddress myAddress = new SNAPAddress(localNodeNumber);
String port = Preferences.loadGlobalString("Port(name)");
String err = "";
try {
communicator = new SNAPCommunicator(port, myAddress);
}
catch (gnu.io.NoSuchPortException e)
{
err = "There was an error opening " + port + ".\n\n";
err += "Check to make sure that is the right path.\n";
err += "Check that you have your serial connector plugged in.";
throw new Exception(err);
}
catch (gnu.io.PortInUseException e)
{
err = "The " + port + " port is already in use by another program.";
throw new Exception(err);
}
}
private void addSectorGrid() throws Exception {
double stepsPerMMX = Preferences.loadGlobalDouble("XAxisScale(steps/mm)");
double axisLengthX = Preferences.loadGlobalDouble("WorkingX(mm)");
int maxStepsX = (int)Math.round(stepsPerMMX*axisLengthX);
double stepsPerMMY = Preferences.loadGlobalDouble("YAxisScale(steps/mm)");
double axisLengthY = Preferences.loadGlobalDouble("WorkingY(mm)");
int maxStepsY = (int)Math.round(stepsPerMMY*axisLengthY);
area.setText("Current working area = " + axisLengthX + " mm x " + axisLengthY + " mm ("
+ maxStepsX + " steps x " + maxStepsY + " steps)");
area.repaint();
JPanel sectorXY = new JPanel();
sectorXY.setLayout(new GridLayout(3,3));
JButton topLeft = makeSectorXYButton(0, maxStepsY, 0, axisLengthY, "", false);
JButton topMiddle = makeSectorXYButton(maxStepsX/2, maxStepsY, axisLengthX/2, axisLengthY, "^", false);
JButton topRight = makeSectorXYButton(maxStepsX, maxStepsY, axisLengthX, axisLengthY, "", false);
JButton middleLeft = makeSectorXYButton(0, maxStepsY/2, 0, axisLengthY/2, "<", true);
JButton middle = makeSectorXYButton(maxStepsX/2, maxStepsY/2, axisLengthX/2, axisLengthY/2, "X", true);
JButton middleRight = makeSectorXYButton(maxStepsX, maxStepsY/2, axisLengthX, axisLengthY/2, ">", false);
JButton home = homeButton();
JButton bottomMiddle= makeSectorXYButton(maxStepsX/2, 0, axisLengthX/2, 0, "V", true);
JButton bottomRight = makeSectorXYButton(maxStepsX, 0, axisLengthX, 0, "", false);
sectorXY.add(topLeft);
sectorXY.add(topMiddle);
sectorXY.add(topRight);
sectorXY.add(middleLeft);
sectorXY.add(middle);
sectorXY.add(middleRight);
sectorXY.add(home);
sectorXY.add(bottomMiddle);
sectorXY.add(bottomRight);
getContentPane().add(sectorXY, BorderLayout.SOUTH);
}
private JButton makeSectorXYButton(int xSteps, int ySteps, double xmm, double ymm, String pointer, boolean activate)
{
final int xCoord = xSteps;
final int yCoord = ySteps;
final double xMM = xmm;
final double yMM = ymm;
final boolean active = activate;
final JButton sector = new JButton(pointer);
sector.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (active)
{
if (homePositionAlreadyFound)
{
status.setText("Travelling...");
status.repaint();
try {
motorX.seek(fastSpeed, xCoord);
motorY.seek(fastSpeed, yCoord);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Could not position motor: " + ex);
}
status.setText("Head position: " + xMM
+ ", " + yMM);
status.repaint();
}
else {
status.setText("Find the home position before sectors can activate.");
status.repaint();
}
}
else
{
status.setText("Button has already been DEACTIVATED for safety. Reactivate in source if happy with working volume.");
status.repaint();
}
}
});
return sector;
}
private JButton homeButton()
{
JButton home = new JButton("Home");
home.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
status.setText("Homing... ");
status.repaint();
try {
motorX.homeReset(fastSpeed);
motorY.homeReset(fastSpeed);
homePositionAlreadyFound = true;
}
catch (Exception ex) {
homePositionAlreadyFound = false;
JOptionPane.showMessageDialog(null, "Could not home motor: " + ex);
}
if (homePositionAlreadyFound = true)
{
status.setText("Axes homed. You may now click any sector button...");
status.repaint();
}
}
});
return home;
}
}
|