-
Notifications
You must be signed in to change notification settings - Fork 20
/
stepper_2wire.html
337 lines (278 loc) · 9.16 KB
/
stepper_2wire.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link type="text/css" href="../libs/jquery-ui/jquery-ui.custom.css" rel="stylesheet" />
<style type="text/css">
body {
margin: 10px;
font-family: sans-serif;
font-size: 16px;
color: #00AEFF;
width: 300px;
}
h2 {
font-size: 22px;
padding-bottom: 10px;
}
h3 {
color: #666;
font-size: 18px;
margin: 0;
padding-bottom: 10px;
}
section {
margin-bottom: 20px;
}
section:last-child {
margin-bottom: 0px;
}
button {
padding: 10px;
font-size: 16px;
width: 280px;
color: #00AEFF;
}
label {
color: #666;
font-size: 14px;
margin-bottom: 10px;
}
input {
font-size: 16px;
width: 150px;
}
input[type="number"] {
margin-top: 10px;
}
#link {
padding: 0;
margin: 0;
width: 25px;
}
.container {
background-color: #f7f7f7;
padding: 10px;
border: 1px dotted #CCC;
width: 280px;
margin: 20px 0 20px 0;
padding-top: 10px;
}
.configure {
display: block;
}
.control {
display: none;
}
.slider {
margin-top: 10px;
width: 280px;
}
.note {
color: #666;
font-size: 14px;
line-height: 1.5em;
}
</style>
<title>Stepper Example (2 wire)</title>
<!-- The following (socket.io.js) is only required when using the node_server -->
<script src="../../socket.io/socket.io.js"></script>
<script src="../../dist/Breakout.min.js"></script>
<script src="../libs/jquery.min.js"></script>
<script src="../libs/jquery-ui/jquery-ui.custom.min.js"></script>
<script src="../libs/jquery.ui.touch-punch.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// Declare these variables so you don't have
// to type the full namespace
var IOBoard = BO.IOBoard;
var IOBoardEvent = BO.IOBoardEvent;
var Stepper = BO.io.Stepper;
var Event = JSUTILS.Event;
// Set to true to print debug messages to console
BO.enableDebugging = true;
// If you are not serving this file from the same computer
// that the Arduino board is connected to, replace
// window.location.hostname with the IP address or hostname
// of the computer that the Arduino board is connected to.
var host = window.location.hostname;
// if the file is opened locally, set the host to "localhost"
if (window.location.protocol.indexOf("file:") === 0) {
host = "localhost";
}
var arduino = new IOBoard(host, 8887);
// Variables
var stepper;
// Listen for the IOBoard READY event which indicates the IOBoard
// is ready to send and receive data
arduino.addEventListener(IOBoardEvent.READY, onReady);
function onReady(event) {
// Remove the listener because it is no longer needed
arduino.removeEventListener(IOBoardEvent.READY, onReady);
configureStepper();
}
function configureStepper() {
var stepsPerRev = 200; // adjust to match your particular stepper
$('#stepsPerRev').change(function(evt) {
if (this.value >= 10 && this.value <= 1000) {
stepsPerRev = this.value;
}
});
// When the configure button is clicked, create a new instance
// of a Stepper motor object.
$('button#configure').on('click', function() {
// Parameters: board, motor interface, num steps per rev,
// motor pin1, motor pin2
stepper = new Stepper(arduino,
Stepper.TWO_WIRE,
stepsPerRev,
arduino.getDigitalPin(2),
arduino.getDigitalPin(3));
// Event.COMPLETE will fire when the step sequence is complete
stepper.addEventListener(Event.COMPLETE, onStepperComplete);
$('.configure').css('display', 'none');
$('.control').css('display', 'block');
initGUIListeners();
});
}
function initGUIListeners() {
var numSteps = 2000; // 2000 steps
var speed = 15.0 // rad/sec
var acceleration = 20.0 // rad/sec^2
var deceleration = 20.0 // rad/sec^2
var link = true;
var rpmPerRadSec = 9.55; // 1 rad/sec = 9.55 rpm
$('button#start').on('click', function() {
// acceleration and deceleration are optional parameters
if (acceleration > 0 && deceleration > 0) {
// set the number of steps, speed and accel and decel parameters
stepper.step(numSteps, speed, acceleration, deceleration);
} else {
// set the number of steps and speed (accel and decel are optional)
stepper.step(numSteps, speed);
}
$('.status').text("Stepper " + stepper.id + " sequence running");
});
$('#numSteps').change(function(evt) {
if (this.value <= 2097151 && this.value >= -2097151) {
numSteps = this.value;
}
});
$('#speedSlider').slider({
min: 1,
max: 50,
step: 0.1,
slide: function(event, ui) {
speed = ui.value;
$('#stepSpeed').text(speed + " (" + (speed * rpmPerRadSec).toFixed(2) + " RPM)");
},
value: speed // set initial speed
});
$('#accelSlider').slider({
min: 0,
max: 100,
step: 0.1,
slide: function(event, ui) {
acceleration = ui.value;
$('#accel').text(acceleration > 0 ? acceleration : "none");
if (link) {
deceleration = acceleration;
$('#decelSlider').slider({
value: deceleration
});
$('#decel').text(deceleration > 0 ? deceleration : "none");
}
},
value: acceleration // set initial acceleration
});
$('#decelSlider').slider({
min: 0,
max: 100,
step: 0.1,
slide: function(event, ui) {
deceleration = ui.value;
$('#decel').text(deceleration > 0 ? deceleration : "none");
if (link) {
acceleration = deceleration;
$('#accelSlider').slider({
value: acceleration
});
$('#accel').text(acceleration > 0 ? acceleration : "none");
}
},
value: deceleration // set initial deceleration
});
$('#link').change(function(evt) {
if ($(this).attr('checked')) {
link = true;
} else {
link = false;
}
});
}
function onStepperComplete(evt) {
// Each stepper instance is automatically assigned an id
// when it is created, starting with 0. You can get the
// id like this but it cannot be set.
$('.status').text("Stepper " + evt.target.id + " sequence complete");
}
});
</script>
</head>
<body>
<h2>Stepper Example</h2>
<section class="configure">
<div class="container">
<h3>Configure your Stepper Motor</h3>
<section>
<label for="stepsPerRev">Number of Steps per Revolution: </label>
<input id="stepsPerRev" type="number" value="200" min="50" max="1000" step="10" />
</section>
<section>
<button type="button" id="configure">Configure</button>
</section>
</div>
<section class="note">
<p>In order for this example to work correctly you must make sure the number of steps per revolution is set for your stepper motor. This is the number of steps it takes for the motor to make one revolution. You can get this value from you motor's datasheet (if the number is not specified, divide the step angle by 360 degrees to get the number of steps per rev). This value usually ranges from 100 to 400 depending on the motor. The default value for this example is 200 stepsPerRev.</p>
</section>
</section>
<section class="control">
<div class="container">
<section>
<label for="numSteps">Number of Steps to Move (+ CW, - CCW): </label>
<input id="numSteps" type="number" value="2000" min="-2097150" max="2097150" step="10" />
</section>
<section>
<label>Speed (rad/sec): </label>
<label id="stepSpeed">15.0 (143.25 RPM)</label>
<div class="slider" id="speedSlider"></div>
</section>
<section>
<label>Acceleration (rad/sec<sup>2</sup>): </label>
<label id="accel">20.0</label>
<div class="slider" id="accelSlider"></div>
</section>
<section>
<label>Deceleration (rad/sec<sup>2</sup>): </label>
<label id="decel">20.0</label>
<div class="slider" id="decelSlider"></div>
</section>
<section>
<input id="link" type="checkbox" checked="checked" />
<label for="link">Link Acceleration and Deceleration</label>
</section>
<section>
<button type="button" id="start">Run Step Sequence</button>
</section>
</div>
<section>
<p class="status"> </p>
</section>
<section class="note">
<p>Depending on your motor, you may not be able to drive the stepper at speeds higher than 25 rad/sec (240 RPM).</p>
<p>To remove acceleration and deceleration, move the acceleration and deceleration sliders all the way to the left. When using accel and decel you must set both values.</p>
</section>
</section>
</body>
</html>