This repository has been archived by the owner on May 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
robot.js
227 lines (195 loc) · 5.64 KB
/
robot.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Licensed under the GNU GPL V3 License
* (C) Seravo Oy 2014-2015
* Contributors: Otto Kekäläinen
*
* This Node.js code is designed to be compatible with the JavaScript
* pseudo-code that the Blockly visual programming environment produces.
* You can for example code a figure that the Artist at code.org can draw,
* and then run that exact code with this Node.js module so that
* it is executed by the Ryantek robot.
*
* Async queue is used to force the code run in sequense as it does in Blockly
*/
var async = require('async');
/*
* This code is build using Cylon, so in theory it could run on multiple
* different hardware platforms.
*
* GPIO pin configuration:
* 11 = right wheel forwards (BCM 17)
* 12 = right wheel backwards (BCM 18)
* 15 = left wheel forwards (BCM 22)
* 16 = left wheel backwards (BCM 23)
*
* See also http://cylonjs.com/documentation/platforms/raspberry-pi/
*
* On the Ryantek robot on full 360 degrees turn is about 1.52 seconds
* of power for the electric motors:
*/
var circleTime = 1.52;
/*
* 100 pixels equal 2 seconds of power for forward or backward movement
* 1 pixel is 0.02 seconds
*/
var pixelTime = 2/100;
/*
* Make a short 0.5 second pause between each step
*/
var pauseTime = 0.5;
var Cylon = require('cylon');
Cylon.robot({
connection: {
name: 'raspi',
adaptor: 'raspi'
},
devices: [
{
name: 'rwheelf',
driver: 'led',
pin: 11
},
{
name: 'rwheelb',
driver: 'led',
pin: 12
},
{
name: 'lwheelf',
driver: 'led',
pin: 15
},
{
name: 'lwheelb',
driver: 'led',
pin: 16
},
],
work: function(my) {
function runStep(stepName, stepParameter, callback) {
switch (stepName) {
case 'moveForward':
my.rwheelf.turnOn();
my.lwheelf.turnOn();
// 100 pixels equal 1 second
after((stepParameter*pixelTime).seconds(), function () {
my.rwheelf.turnOff();
my.lwheelf.turnOff();
after(pauseTime.seconds(), function () {
callback();
});
});
break;
case 'moveBackward':
my.rwheelb.turnOn();
my.lwheelb.turnOn();
// 100 pixels equal 1 second
after((stepParameter*pixelTime).seconds(), function () {
my.rwheelb.turnOff();
my.lwheelb.turnOff();
after(pauseTime.seconds(), function () {
callback();
});
});
break;
case 'turnLeft':
my.rwheelf.turnOn();
my.lwheelb.turnOn();
// 90 degrees equal 0.38 seconds
// 360 degrees equal 1.52 seconds
after((stepParameter/360*circleTime).seconds(), function () {
my.rwheelf.turnOff();
my.lwheelb.turnOff();
after(pauseTime.seconds(), function () {
callback();
});
});
break;
case 'turnRight':
my.rwheelb.turnOn();
my.lwheelf.turnOn();
// 90 degrees equal 0.38 seconds
// 360 degrees equal 1.52 seconds
after((stepParameter/360*circleTime).seconds(), function () {
my.rwheelb.turnOff();
my.lwheelf.turnOff();
after(pauseTime.seconds(), function () {
callback();
});
});
break;
}
}
function moveForward(pixels) {
if (pixels === undefined) {
pixels = 100; // default 100 pixels
}
q.push({name: 'moveForward', parameter: pixels});
}
function moveBackward(pixels) {
if (pixels === undefined) {
pixels = 100; // default 100 pixels
}
q.push({name: 'moveBackward', parameter: pixels});
}
function turnLeft(degrees) {
if (degrees === undefined) {
degrees = 90; // default 90 degrees
}
q.push({name: 'turnLeft', parameter: degrees});
}
function turnRight(degrees) {
if (degrees === undefined) {
degrees = 90; // default 90 degrees
}
q.push({name: 'turnRight', parameter: degrees});
}
// dummy functions that might be called but will not do anything
function colour_random() {}
function penColour() {}
function penWidth() {}
// Create queue that processes one task at the time
var q = async.queue(function (task, callback) {
console.log(task.name + '(' + task.parameter + ');');
runStep(task.name, task.parameter, callback);
}, 1);
// assign a callbacks
q.saturated = function() {
console.log('Queue max concurrency.');
}
q.drain = function() {
console.log('Program completed.');
}
// Use same syntax for movement as in code.org tutorials and Blockly
// ** EDIT BELOW TO MAKE YOUR OWN MOVEMENT PATTERN **
console.log('Running: \n' + Cylon.commands.code);
eval(Cylon.commands.code);
// ** EDIT ABOVE TO MAKE YOUR OWN MOVEMENT PATTERN **
// Remember to run this as sudo!
// Reset all pins, just to be sure
my.rwheelf.turnOff();
my.lwheelf.turnOff();
my.rwheelb.turnOff();
my.lwheelb.turnOff();
process.on('SIGINT', function() {
console.log("Caught interrupt signal");
my.rwheelf.turnOff();
my.lwheelf.turnOff();
my.rwheelb.turnOff();
my.lwheelb.turnOff();
process.exit();
});
process.on('exit', function() {
console.log("Program exiting");
my.rwheelf.turnOff();
my.lwheelf.turnOff();
my.rwheelb.turnOff();
my.lwheelb.turnOff();
process.exit();
});
}
});
Cylon.commands = {
code: 'moveForward(10);', // Initialize empty
};
module.exports = Cylon;