summaryrefslogtreecommitdiff
path: root/tags/host/0.8.1/src/org/reprap/gui/steppertest/StepperPanel.java
blob: 7665ec27a9132eba9f05a9ab32af8adfa25a9ce3 (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
package org.reprap.gui.steppertest;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.reprap.Preferences;
import org.reprap.comms.Communicator;
import org.reprap.comms.snap.SNAPAddress;
import org.reprap.devices.GenericStepperMotor;

/// TODO There is a bug in this app that can cause the stepper to skip some steps
/// and lose its place.  This can occur if you slowly drag the position slider.  Multiple
/// updates will be rapidly sent to the stepper telling it to change position. This
/// will actually cause it to perform a step immediately on each request so it
/// can cause steps to occur faster than they normally would.  If this happens
/// too quickly, it will be beyond the safe torque speed and it will skip.  This
/// should be resolved with a timer or something in the gui.  If an update just
/// occurred the new one should not be sent immedidately.  Instead the event
/// should be queued up and only send after a safe amount of time has elapsed.
/// This would also allow multiple movement events to be coallesced into a single
/// request if they occur very quickly.  It still has to be sent eventually
/// or else the motor will not go to the correct location represented in the gui.
/// One way to avoid this is for the short term to use keys instead of the mouse
/// (eg pgup, pgdown) and don't press too quickly.  This problem has no
/// consequences for normal software driven operation, just interactive use.

public class StepperPanel extends JPanel implements ChangeListener {

	private static final long serialVersionUID = 6262697694879478425L;

	private JSlider externalSpeedSlider;  // Externally maintained speed slider
	private JSlider positionRequest;      // Slider to control position
	private JSlider positionActual;       // Slider to indicate position
	
	private JCheckBox torque;            // If motor is driving or not
	
	private JLabel rangeLabel;
	
	private GenericStepperMotor motor; 
	private boolean moving = false;   // True if (as far as we know) the motor is seeking
	private boolean waiting = false;  // True if already waiting for a timer to complete (so we don't start another one)

	private Timer updateTimer;
	
	private int minValue = 0;
	private int maxValue = 30000;
	private int startingPosition = 5000;
	
	private boolean monitoring = false;
	
	public StepperPanel(String name, int motorId, JSlider externalSpeedSlider, Communicator communicator) throws IOException {
		super();
		

		String axis;
		switch(motorId)
		{
		case 1:
			axis = "X";
			break;
		case 2:
			axis = "Y";
			break;
		case 3:
			axis = "Z";
			break;
		default:
			axis = "X";
			System.err.println("StepperPanel - dud axis id: " + motorId);
				
		}
		int address = Preferences.loadGlobalInt(axis + "Axis" + "Address");
		
		double stepsPerMM = Preferences.loadGlobalDouble(axis + "AxisScale(steps/mm)");
		double axisLength = Preferences.loadGlobalDouble("Working" + axis + "(mm)");
		maxValue = (int)Math.round(stepsPerMM*axisLength);
		startingPosition = maxValue/6;
		
		updateTimer = new Timer();
		
		motor = new GenericStepperMotor(communicator, new SNAPAddress(address), Preferences.getGlobalPreferences(), motorId);
		
		this.externalSpeedSlider = externalSpeedSlider;
		
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		c.insets.bottom = c.insets.top = 0;
		c.ipady = 0;
		c.gridx = 0;
		c.gridy = 0;
		
		add(new JLabel("Set " + name + " axis position"), c);
		positionRequest = new JSlider(SwingConstants.HORIZONTAL, minValue, maxValue, 0);
		positionRequest.setValue(startingPosition);
		positionRequest.addChangeListener(this);
		c.gridy = 1;
		add(positionRequest, c);
		
		c.gridy = 2;
		add(new JLabel("Actual " + name + " axis position"), c);
		positionActual = new JSlider(SwingConstants.HORIZONTAL, minValue, maxValue, 0);
		positionActual.setValue(startingPosition);
		positionActual.setEnabled(false);
		c.gridy = 3;
		add(positionActual, c);
		
		c.gridx = 1;
		c.gridy = 0;
		rangeLabel = new JLabel();
		updateRange();
		add(rangeLabel, c);
		
		c.gridy = 1;
		JButton calibrate = new JButton("Calibrate");
		calibrate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				onCalibrate();
			}
		});
		add(calibrate, c);
		
		c.gridx = 2;
		JButton home = new JButton("Home");
		home.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				onHomeReset();
			}
		});
		add(home, c);
		
		c.gridx = 3;
		JButton stepForward = new JButton("Step +");
		stepForward.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				onOneStep(true);
			}
		});
		add(stepForward, c);
		
		c.gridx = 4;
		JButton stepBackward = new JButton("Step -");
		stepBackward.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				onOneStep(false);
			}
		});
		add(stepBackward, c);
		
		c.gridx = 1;
		c.gridy = 2;
		torque = new JCheckBox("Torque");
		torque.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				onTorqueUpdate();
        	}
        });
        add(torque, c);
        
		try {
			motor.setPosition(startingPosition);
		} catch (Exception ex) {
			motor.dispose();
			motor = null;
			positionRequest.setEnabled(false);
			calibrate.setEnabled(false);
			torque.setEnabled(false);
			home.setEnabled(false);
			stepForward.setEnabled(false);
			stepBackward.setEnabled(false);
			return;
		}

	}

	/**
	 * Utility function to update the range display and 
	 * slider end points
	 */
	private void updateRange() {
		rangeLabel.setText(minValue + " to " + maxValue);
		
		positionRequest.setMinimum(minValue);
		positionRequest.setMaximum(maxValue);
		positionActual.setMinimum(minValue);
		positionActual.setMaximum(maxValue);
		
        int range = maxValue - minValue;
        positionRequest.setMajorTickSpacing(range/8);  // Ten circles
        positionRequest.setMinorTickSpacing(range/80);   // A full circle
        positionRequest.setPaintTicks(true);
	}

	/**
	 * Calibrate button handler
	 */
	protected void onCalibrate() {
		try {
			GenericStepperMotor.Range range = motor.getRange(externalSpeedSlider.getValue());
			minValue = range.minimum;
			maxValue = range.maximum;
			// We could re-request current position, but for now we know
			// that we're at the max position, so update to reflect this
			moving = false;
			updateRange();
			positionRequest.setValue(maxValue);
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(null, "Problem during calibration: " + ex);
		}
	}

	/**
	 * Callback when the torque checkbox is clicked
	 */
	protected void onTorqueUpdate()
	{
		try {
			if (torque.isSelected()) {
				// You can't currently do this
				torque.setSelected(false);
			} else {
				motor.setIdle();
				moving = false;
			}
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(null, "Could not idle motor: " + ex);
		}
			
	}
	
	/**
	 * Callback when the Home button is clicked
	 */
	protected void onHomeReset()
	{
		try {
			motor.homeReset(externalSpeedSlider.getValue());
			positionRequest.setValue(0);
			positionActual.setValue(0);
			torque.setSelected(true);
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(null, "Could not home motor: " + ex);
		}
	}
	
	/**
	 * Callback when the Step + button is clicked
	 */
	protected void onOneStep(boolean forward)
	{
		try {
			if(forward)
				motor.stepForward();
			else
				motor.stepBackward();
			setDisplayPosition();
			torque.setSelected(true);
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(null, "Could not step motor: " + ex);
		}
	}

	/**
	 * Callback when a slider is changed
	 */
	public void stateChanged(ChangeEvent evt) {
		try {
			Object srcObj = evt.getSource();
			
			if (srcObj instanceof JSlider) {
				JSlider src = (JSlider)srcObj;
				if (src == positionRequest)
					seekToSelectedPosition();
			}
		} catch (Exception ex) {
    		JOptionPane.showMessageDialog(null, "Update exception: " + ex);
		} 
	}

	/**
	 * Queue a timer event for the near future
	 */
	private void startUpdates() {
		if (!waiting && (moving || monitoring)) {    // If there is already one, don't create another
			waiting = true;
			TimerTask task = new TimerTask() {
				public void run() {
					Thread.currentThread().setName("Stepper position poll");
					waiting = false;
					updatePosition();
				}			
			};
			updateTimer.schedule(task, 200);
		}
	}

	/**
	 * Request the current position and display it
	 */
	private void setDisplayPosition() throws IOException {
		int position = motor.getPosition();
		if (monitoring)
			positionRequest.setValue(position);
		positionActual.setValue(position);
		if (position == positionRequest.getValue())
			moving = false;
	}	

	/**
	 * Called when the slider is moved to seek the motor
	 * @throws IOException
	 */
	private void seekToSelectedPosition() throws IOException {
		motor.seek(externalSpeedSlider.getValue(), positionRequest.getValue());
		torque.setSelected(true);
		moving = true;
		startUpdates();
	}

	/**
	 * Called when the speed slider is changed
	 * @throws IOException
	 */
	public void updateSpeed() throws IOException {
		// If we're not moving, changing the speed does nothing.
		// Otherwise, we just re-seek to the current position
		// with the new speed for it to take effect.
		if (moving)
			seekToSelectedPosition();
	}

	/**
	 * This method is called on the timer event to get the current
	 * position and display it.
	 */
	protected void updatePosition()
	{
		try {
			setDisplayPosition();
			if (moving || monitoring)  // If we're moving, start another timer
				startUpdates();
		} catch (IOException ex) {
			// Ignore these if they happen
			System.out.println("Ignored IO exception in position update: " + ex);
		}
	}

	/**
	 * @return Returns the motor.
	 */
	public GenericStepperMotor getMotor() {
		return motor;
	}

	public void monitor(boolean enable) {
		monitoring = enable;
		if (monitoring)
			startUpdates();
			
	}
	
	public void setMoved() {
		torque.setSelected(true);
	}

	public void loadPosition() throws IOException {
		monitor(false);
		positionActual.setValue(motor.getPosition());
		positionRequest.setValue(motor.getPosition());
		torque.setSelected(true);
	}
	
	public void dispose() {
		if (motor != null)
			motor.dispose();
		updateTimer.cancel();
	}
}