summaryrefslogtreecommitdiff
path: root/trunk/users/adrian/host/src/org/reprap/gui/botConsole/StepperPositionJPanel.java
blob: 6ca81ed2c72e82157d5d84d74c28b9629bb7f0d0 (plain)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * StepperPositionJPanel.java
 *
 * Created on June 30, 2008, 4:59 PM
 */

package org.reprap.gui.botConsole;
import java.io.IOException;
import javax.swing.JOptionPane;
import org.reprap.Preferences;
import org.reprap.Printer;
//import org.reprap.AxisMotor;
//import org.reprap.comms.Communicator;
//import org.reprap.comms.snap.SNAPAddress;
//import org.reprap.devices.GenericStepperMotor;

/**
 *
 * @author  ensab
 */
public class StepperPositionJPanel extends javax.swing.JPanel {
	private static final long serialVersionUID = 1L;
    //private Communicator communicator;
    private int motorID;
    private String axis;
    //private GenericStepperMotor motor;
    private Printer printer;

    private double currentSpeed;
    @SuppressWarnings("unused")
	private double motorStepsPerMM;
    private double axisLength;
    private double nudgeSize;
    private double newTargetAfterNudge;
    private BotConsoleFrame parentBotConsoleFrame = null;
    /**
     * Really set it up under control...
     * @param m
     * @throws java.io.IOException
     */
    public void postSetUp(int m) throws IOException {
        
        //communicator = org.reprap.Main.getCommunicator();
        motorID = m;
        printer = org.reprap.Main.gui.getPrinter();

        switch(motorID)
        {
        case 1:
                axis = "X";
                //motor = printer.getXMotor();
                axisLength = 160; // TODO: Replace with Prefs when Java3D parameters work for small wv's.
                break;
        case 2:
                axis = "Y";
                //motor = printer.getYMotor();
                axisLength = 160; // TODO: Replace with Prefs when Java3D parameters work for small wv's.
                break;
        case 3:
                axis = "Z";
                //motor = printer.getZMotor();
                axisLength = 80; // TODO: Replace with Prefs when Java3D parameters work for small wv's.
                break;
        default:
                axis = "X";
        		//motor = printer.getXMotor();
                System.err.println("StepperPanel - dud axis id:" + motorID);
        }
        
//        if(!motor.isAvailable())
//        {
//            deactivateMotorPanel();  
//            return;
//        }
        
        targetPosition.setEnabled(true);
        stepDownButton.setEnabled(true);
        stepUpButton.setEnabled(true);
        axisLabel.setText(axis);
        targetPosition.setText("0");
        storedPosition.setText("0");
        
        //int address = Preferences.loadGlobalInt(axis + "Axis" + "Address");
        
        //motor = new GenericStepperMotor(communicator, new SNAPAddress(address), Preferences.getGlobalPreferences(), motorID);

        motorStepsPerMM = Preferences.loadGlobalDouble(axis + "AxisScale(steps/mm)");
	
//      TODO: Activate this code when the Java3D parameters allow a small enough working volume. Currently I get a black screen.
//      axisLength = Preferences.loadGlobalDouble("Working" + axis + "(mm)");

    }
    
    public void setConsoleFrame(BotConsoleFrame b)
    {
    	parentBotConsoleFrame = b;
    }
    
//    private void deactivateMotorPanel() {
//            //motor.dispose();
//            //motor = null;
//            axisLabel.setEnabled(false);
//            targetPosition.setEnabled(false);
//            //endButton.setEnabled(false);
//            homeButton.setEnabled(false);
//            storeButton.setEnabled(false);
//            recallButton.setEnabled(false);
//            stepDownButton.setEnabled(false);
//            stepUpButton.setEnabled(false);
//    }
    
//    private int getSpeedFromFeed(double f)
//    {
//    	if (axis.equals("Z"))
//    		return printer.convertFeedrateToSpeedZ(f);
//    	else
//    		return printer.convertFeedrateToSpeedXY(f);
//    }
    
    public void zeroBox()
    {
    	targetPosition.setText("0");
    }
    
    public void homeAxis() {
    	parentBotConsoleFrame.suspendPolling();
        try {
            setSpeed();
			switch(motorID)
			{
			case 1:
				printer.homeToZeroX();
				break;
			case 2:
				printer.homeToZeroY();
				break;
			case 3:
				printer.homeToZeroZ();
				break;
			default:
				System.err.println("StepperPositionPanel - homeReset.  Dud motor id: " + motorID);
			}
            //motor.homeReset(getSpeedFromFeed(currentSpeed));
            zeroBox();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Could not home motor: " + ex);
        }
        parentBotConsoleFrame.resumePolling();
    }
    
    public void setSpeed() {
        if (motorID == 3)
        {
        	currentSpeed = Double.parseDouble(org.reprap.gui.botConsole.XYZTabPanel.zSpeedField.getText());
        	//printer.setFeedrate(currentSpeed);
        } else
        {
       		currentSpeed = Double.parseDouble(org.reprap.gui.botConsole.XYZTabPanel.xySpeedField.getText());
       		//printer.setFeedrate(currentSpeed);
        }
    }
    
    public double getTargetPositionInMM() {
        double targetMM = Double.parseDouble(targetPosition.getText());
        if (targetMM > axisLength) {
            targetMM = axisLength;
            targetPosition.setText("" + round(targetMM, 2));
        }
        if (targetMM < 0) {
            targetMM = 0;
            targetPosition.setText("" + round(targetMM, 2));
        }
        return targetMM;
    }
    
//    private int getTargetPositionInSteps() {
//        double targetMM = getTargetPositionInMM();
//        return (int)Math.round(targetMM * motorStepsPerMM);
//    }
//    
//    private double stepsToMM(int steps)
//    {
//        return (double)steps/motorStepsPerMM;
//    }
    
    public void moveToTarget() {
    	parentBotConsoleFrame.suspendPolling();
    	    setSpeed();
    		double x, y, z, p;
    		x = printer.getX();
    		y = printer.getY();
    		z = printer.getZ();
    		p = getTargetPositionInMM();
    		try
    		{
    			switch(motorID)
    			{
    			case 1:
    				x = p;
    				break;
    			case 2:
    				y = p;
    				break;
    			case 3:
    				z = p;
    				break;
    			default:
    				System.err.println("moveToTarget()  Dud motor id: " + motorID);
    			}
    			printer.singleMove(x, y, z, currentSpeed);
    			//printer.moveTo(x, y, z, currentSpeed, false, false);
    		} catch (Exception ex)
    		{}
//	    	try {
//	        	motor.seek(getSpeedFromFeed(currentSpeed), getTargetPositionInSteps());
//	        } 
//	        catch (Exception ex) {
//	            JOptionPane.showMessageDialog(null, axis + " motor could not seek: " + ex);
//	        }
    		parentBotConsoleFrame.resumePolling();
    }
    
//    public void moveToTargetBlocking() {
//    	    setSpeed();
//    		try {
//                motor.seekBlocking(getSpeedFromFeed(currentSpeed), getTargetPositionInSteps());
//            } 
//            catch (Exception ex) {
//                JOptionPane.showMessageDialog(null, axis + " motor could not block: " + ex);
//            }
//    }
    
    public double round(double Rval, int r2dp) {
        double p = (Double)Math.pow(10,r2dp);
        Rval = Rval * p;
        float tmp = Math.round(Rval);
        return (double)tmp/p;
    }
    
//    public void updateCurrentPositionLabels() {
//        currentPositionLabel.setText("(" + getTargetPositionInMM() + ")");
//         
//    }
    
    public void setNudgeSize(double size) {
        nudgeSize = size;
    }
    
    public void setTargetPositionField(int coord) {
    	targetPosition.setText("" + coord);
    }

    
    /** Creates new form StepperPositionJPanel */
    public StepperPositionJPanel() {

      
        initComponents();
        

    }

    
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        stepUpButton = new javax.swing.JButton();
        stepDownButton = new javax.swing.JButton();
        homeButton = new javax.swing.JButton();
        mmLabel1 = new javax.swing.JLabel();
        targetPosition = new javax.swing.JTextField();
        axisLabel = new javax.swing.JLabel();
        storeButton = new javax.swing.JButton();
        recallButton = new javax.swing.JButton();
        storedPosition = new javax.swing.JLabel();
        mmLabel2 = new javax.swing.JLabel();

        stepUpButton.setText(">");
        stepUpButton.setEnabled(false);
        stepUpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stepUpButtonActionPerformed(evt);
            }
        });

        stepDownButton.setText("<");
        stepDownButton.setEnabled(false);
        stepDownButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stepDownButtonActionPerformed(evt);
            }
        });

        homeButton.setText("Home");
        homeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                homeButtonActionPerformed(evt);
            }
        });

        mmLabel1.setFont(new java.awt.Font("Tahoma", 0, 12));
        mmLabel1.setText("mm");

        targetPosition.setColumns(4);
        targetPosition.setFont(targetPosition.getFont().deriveFont((float)12));
        targetPosition.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        targetPosition.setText("0");
        targetPosition.setEnabled(false);

        axisLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        axisLabel.setText("X");

        storeButton.setText("Sto");
        storeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                storeButtonActionPerformed(evt);
            }
        });

        recallButton.setText("Rcl");
        recallButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                recallButtonActionPerformed(evt);
            }
        });

        storedPosition.setFont(new java.awt.Font("Tahoma", 0, 12));
        storedPosition.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        storedPosition.setText("(?)");
        storedPosition.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        storedPosition.setMaximumSize(new java.awt.Dimension(50, 15));
        storedPosition.setPreferredSize(new java.awt.Dimension(50, 15));

        mmLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        mmLabel2.setText("mm");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(axisLabel)
                .add(4, 4, 4)
                .add(targetPosition, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(3, 3, 3)
                .add(mmLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(stepDownButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(stepUpButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(homeButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(storeButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(recallButton)
                .add(3, 3, 3)
                .add(storedPosition, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(mmLabel2)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(axisLabel)
                    .add(targetPosition, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(mmLabel1)
                    .add(stepDownButton)
                    .add(stepUpButton)
                    .add(homeButton)
                    .add(storeButton)
                    .add(recallButton)
                    .add(storedPosition, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 15, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(mmLabel2))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void stepUpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stepUpButtonActionPerformed
        setSpeed();
        newTargetAfterNudge = getTargetPositionInMM() + nudgeSize;
        targetPosition.setText("" + round(newTargetAfterNudge, 2));
        moveToTarget();
}//GEN-LAST:event_stepUpButtonActionPerformed

private void stepDownButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stepDownButtonActionPerformed
        setSpeed();
        //System.out.println(nudgeSize);
        double newTargetAfterNudge = getTargetPositionInMM() - nudgeSize;
        targetPosition.setText("" + round(newTargetAfterNudge, 2));
        moveToTarget();
}//GEN-LAST:event_stepDownButtonActionPerformed

private void homeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_homeButtonActionPerformed
    homeAxis();
}//GEN-LAST:event_homeButtonActionPerformed

public void store()
{
    double pos = 0;
		switch(motorID)
		{
		case 1:
			pos = printer.getX();
			break;
		case 2:
			pos = printer.getY();
			break;
		case 3:
			pos = printer.getZ();
			break;
		default:
			System.err.println("store()  Dud motor id: " + motorID);
		}
//    try
//    {   
//        moPos = motor.getPosition();
//    } catch (Exception ex) {}
//    double pos = stepsToMM(moPos);
    storedPosition.setText("" + round(pos, 2));    
}

private void storeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_storeButtonActionPerformed
    store();
}//GEN-LAST:event_storeButtonActionPerformed

public void recall()
{
    targetPosition.setText(storedPosition.getText());
}

private void recallButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_recallButtonActionPerformed
    recall();
    
}//GEN-LAST:event_recallButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel axisLabel;
    private javax.swing.JButton homeButton;
    private javax.swing.JLabel mmLabel1;
    private javax.swing.JLabel mmLabel2;
    private javax.swing.JButton recallButton;
    private javax.swing.JButton stepDownButton;
    private javax.swing.JButton stepUpButton;
    private javax.swing.JButton storeButton;
    private javax.swing.JLabel storedPosition;
    private javax.swing.JTextField targetPosition;
    // End of variables declaration//GEN-END:variables

}