This repository has been archived by the owner on Apr 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathDrawMachine.js
207 lines (177 loc) · 9.03 KB
/
DrawMachine.js
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
/*
Copyright 2013 Google Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
require('mootools');
var ConfigParams = require('../../ConfigParams').ConfigParams;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// DrawMachine
///// Coverts Draw commands into steps for the motion controller.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
exports.DrawMachine = new Class({
Implements: [Events, Options, process.EventEmitter],
/**
* This class is required to emit only these events:
* 'sensorError' - if there is a downstream error which means that sensor values are unreliable or unavailable
* 'robotConnectedToMotionController' - when we have a successful connection between robot and motion controller
* 'turntableMotionComplete' - when the draw machine has finished moving the turntable (e.g. to wipe the sand)
* 'robotAtHome' - when the draw machine's robotics are in their home position
* 'readyForPicture' - when the draw machine is in a state where it is appropriate to capture an image of the current drawing
* 'robotCalibrated' - when the draw machine has finished its self-calibration routine
* 'timeEstimate' - when the draw machine has a new or updated estimate on the time that its current drawing will be complete. The payload of the event should be the time estimate, expressed as a Unix timestamp.
* 'drawingComplete' - when the draw machine is done with the current drawing
*
*/
options: {},
/*
* public variables
*
*/
buff_type: new Array(), //will contain an array of integers, one element for each slot in the current command buffer; each element in the array will have one of these values: 0=point, 1=line, 2=bezier
buff_vel: new Array(), //will contain an array of numbers, one element for each slot in the current command buffer; each element is the velocity of one command
buff_acc: new Array(), //will contain an array of numbers, one element for each slot in the current command buffer; each element is the acceleration of one command
buff_dist: new Array(), //will contain an array of numbers, one element for each slot in the current command buffer; each element is the distance parameter of one command
buff_cart: [
new Array(), //buf_cart[0] will contain an array of numbers, one element for each slot in the current command buffer; each element contains the X coordinate for the target point of that command
new Array(), //buf_cart[1] will contain an array of numbers, one element for each slot in the current command buffer; each element contains the Y coordinate for the target point of that command
new Array(), //buf_cart[2] will contain an array of numbers, one element for each slot in the current command buffer; each element contains the Z coordinate ("lift" or position of the drawing machine's tool above the sand) for the target point of that command
],
buff_control: [
new Array(), //buff_control[0] will contain an array of numbers, one element for each slot in the current command buffer; each element contains the X1 coordinate for the control point of that command (see buff_type)
new Array(), //buff_control[1] will contain an array of numbers, one element for each slot in the current command buffer; each element contains the Y1 coordinate for the control point of that command (see buff_type)
new Array(), //buff_control[2] will contain an array of numbers, one element for each slot in the current command buffer; each element contains the X2 coordinate for the control point of that command (see buff_type)
new Array(), //buff_control[3] will contain an array of numbers, one element for each slot in the current command buffer; each element contains the Y2 coordinate for the control point of that command (see buff_type)
],
maxBufferIndex: 0, //an integer indicating the maximum populated index for all of the buff_* variables
currentBufferIndex: 0, //an integer indicating the read position in the command buffer (this should always be <= maxBufferIndex)
/*
* public functions
*
*/
/**
* initialize the DrawMachine class. Do not set up machine communication here. This is for state initialization.
*
*/
initialize: function(options) {
this.setOptions(options);
this.BUFFER_SIZE = 3000;
this.maxBufferIndex = 0;
this.currentBufferIndex = 0;
this.buff_vel = new Array(this.BUFFER_SIZE);
this.buff_acc = new Array(this.BUFFER_SIZE);
this.buff_dist = new Array(this.BUFFER_SIZE);
this.buff_cart = new Array(3);
this.buff_cart[0] = new Array(this.BUFFER_SIZE);
this.buff_cart[1] = new Array(this.BUFFER_SIZE);
this.buff_cart[2] = new Array(this.BUFFER_SIZE);
this.buff_control = new Array(3);
this.buff_control[0] = new Array(this.BUFFER_SIZE);
this.buff_control[1] = new Array(this.BUFFER_SIZE);
this.buff_control[2] = new Array(this.BUFFER_SIZE);
this.buff_control[3] = new Array(this.BUFFER_SIZE);
this.buff_type = new Array(this.BUFFER_SIZE);
//Do NOT initialize communication with the machine here
//instead do that in createRobot()
//TODO - other initialization
this.zero(); //always call this at the end of initialize()
},
/**
* causes the internal representation of the machine to be created. Put your communication initialization in here.
* Should eventually cause a 'robotConnectedToMotionController' event
*
*/
createRobot: function() {
//TODO - set up a connection to the machine
this._simulateMachineEvent('robotConnectedToMotionController');
},
/**
* causes the machine to rotate its turntable.
* Should eventually cause a 'turntableMotionComplete' event
*
*/
moveTurntable: function() {
//TODO - move the turntable
this._simulateMachineEvent('turntableMotionComplete');
},
/**
* causes the machine to completely reset its state, the meaning of which is machine-specific.
*/
reset: function() {
//TODO - reset the machine
},
/**
* causes the machine to return to its home position.
* Should eventually cause a 'robotAtHome' event
*
*/
goHome: function() {
//TODO - home the machine
this._simulateMachineEvent('robotAtHome');
},
/**
* Causes the machine to start drawing.
* Should eventually cause 'timeEstimate', and 'drawingComplete' events, in that order
*
*/
start: function() {
//TODO - start drawing
this._simulateMachineEvent('timeEstimate', 1000, (new Date().getTime() / 1000) + 60); //simulate 60 second drawings
this._simulateMachineEvent('drawingComplete', 2000);
this._simulateMachineEvent('readyForPicture', 3000);
},
/**
* zeros out the software state of the machine to assume that it is at its home position.
* IMPORTANT: This is about state inside node ONLY. This should NOT move the machine!
*
*/
zero: function() {
//TODO - zero the machine state
},
/**
* Calibrates the machine, the meaning of which is machine-specific.
* Eventually should cause a 'robotCalibrated' event
*
*/
calibrate: function() {
//TODO - calibrate the robot
this._simulateMachineEvent('robotCalibrated');
},
/**
* indicates that the machine should do "initial" calibration, which could be more involved than subsequent calibration.
* If the machine does nothing different on initial or subsequent calibration, then this function should just call calibrate()
* See calibrate().
* Eventually should cause a 'robotCalibrated' event
*
*/
doInitialCalibration: function() {
this.calibrate();
},
/**
* Causes the machine to perform a partial wipe, usually done right after calibration completes.
* Should eventually cause a 'turntableMotionComplete' event.
*
*/
partialWipe: function() {
//TODO - perform partial wipe
this._simulateMachineEvent('turntableMotionComplete');
},
/*
* private functions
*
*/
_simulateMachineEvent: function(eventName, delay, obj) {
if (!delay) delay = 1000;
if (!obj) obj = null;
setTimeout(function() {
this.emit(eventName, obj);
}.bind(this), delay);
},
});