-
Notifications
You must be signed in to change notification settings - Fork 0
/
robit.js
290 lines (250 loc) · 8.55 KB
/
robit.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
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
/* global require, console, every */
/* jshint node:true */
var ARDUINO_PORT = '/dev/ttyACM0';
var Cylon = require('cylon');
var sleep = require('sleep').sleep;
var Promise = require("bluebird");
var rasp2c = require('rasp2c');
var exec = require('shelljs/global').exec;
Cylon.api('http', {
ssl: false,
host: '0.0.0.0',
serveDir: '../robeaux'
});
Cylon.robot({
name: 'cylon_magellan',
connections: {
joystick: {
adaptor: 'joystick',
devices: {
controller: {
driver: 'dualshock-3'
}
}
},
loopback: {
adaptor: 'loopback',
devices: {
ping: {
driver: 'ping'
}
}
},
servoConnection: {
adaptor: 'raspi',
pin: 27,
devices: {
servo: {
driver: 'servo',
pin: 27
}
}
},
pilotConnection: {
adaptor: 's3pilot',
serialPort: ARDUINO_PORT,
devices: {
pilot: {
driver: 's3pilot',
geometry: {
//"TPR": 60,
//"Diam": 175.01,
//"Base": 220.01,
ticksPerMeter: 240,
mMax: 450
},
ahrsCalibration: {
a_x: -2114,
a_y: -578,
a_z: 566,
g_x: 92,
g_y: -7,
g_z: 53
}
}
}
},
//TODO: Make Srf08 Cylon Device
//i2c: {
// adaptor: 'firmata',
// port: '/dev/i2c-1',
// devices: {
// srf08: {
//
// }
// }
//}
},
work: function (my) {
//var version = exec('node --version', {silent:true}).output;
//console.log(version);
my.detectI2cDevices();
my.controller.details.leftStick = 0;
my.controller.details.rightStick = 0;
['square', 'circle', 'x', 'triangle'].forEach(function (button) {
my.controller.on(button + ':press', function () {
console.log('Button ' + button + ' pressed. \n ROTATE!');
//Rot
//
my.pilot.sendSerial({
"Cmd": "Rot",
"Rel": 90.5
});
});
my.controller.on(button + ':release', function () {
console.log('Button ' + button + ' released.');
});
});
my.controller.on('left_y:move', function (pos) {
my.controller.details.rightStick = pos;
});
my.controller.on('right_y:move', function (pos) {
my.controller.details.leftStick = pos;
});
var idleFor = 0; //how many cycles have we been idle for?
var angle = 90;
var increment = 10;
every((0.1).second(), function () {
var leftPower = Math.floor(my.controller.details.leftStick * 100);
var rightPower = Math.floor(my.controller.details.rightStick * 100);
var idle = Math.abs(leftPower) < 10 && Math.abs(rightPower) < 10;
if (idle && idleFor < 10) {
idleFor++;
my.setMotorPowers(0, 0);
} else if (!idle) {
idleFor = 0;
my.setMotorPowers(leftPower, rightPower);
}
});
every((1).second(), function () {
my.readSrf08(function (result) {
//FIXME: stop that
my.pilot.details = my.pilot.details || {};
my.pilot.details.distance = result.distance;
my.pilot.details.lightLevel = result.lightLevel;
});
angle += increment;
if ((angle <= 30) || (angle >= 150)) { increment = -increment; }
my.servo.angle(angle);
console.log(my.servo);
console.log('servo angle: '+my.servo.currentAngle());
});
},
setMotorPowers: function (leftPower, rightPower) {
this.pilot.enableMotors();
//TODO: Change this when pilot can handle ints. For now:
//Force powers to be floating point.
//Zero doesn't need this treatment apparently.
if (leftPower || rightPower) {
leftPower += 0.001;
rightPower += 0.001;
}
console.log('at power L:' + leftPower + '%\tR:' + rightPower + '%');
this.pilot.sendSerial({
"Cmd": "Pwr",
"M1": -rightPower,
"M2": -leftPower
});
},
commands: function () {
var myPowerMinPromise = powerMinPromise.bind(this);
return {
findInMotionPowerMin: myPowerMinPromise(70, 0, 1, 1000, 'inMotion', function (lastHeading, newHeading) {
return Math.abs(lastHeading - newHeading) < 1;
}),
findFromStallPowerMin: myPowerMinPromise(0, 70, 1, 1000, 'fromStall', function (lastHeading, newHeading) {
return (typeof lastHeading !== 'undefined') && Math.abs(lastHeading - newHeading) > 1;
})
}
},
detectI2cDevices: function () {
console.log('detecting i2c devices');
rasp2c.detect(function (err, result) {
if (err) {
console.error(err);
} else {
console.log('detected');
console.log(result);
}
});
},
readSrf08: function (callback) {
//TODO: something
var ADDRESS = '0x70';
rasp2c.dump(ADDRESS, '0x01-0x03', function (err, result) {
//pong . . .
if (err) {
console.log('Problem Reading SRF08');
console.error(err);
} else {
var sobright = result[0];
var highByte = result[1];
var lowByte = result[2];
var distance = (highByte << 8) + lowByte;
if ('function' === typeof callback) {
callback({
lightLevel: sobright,
distance: distance
});
}
}
});
//address, register, value
rasp2c.set(ADDRESS, '0x00', '0x51', function () {
//ping . . .
});
}
});
function powerMinPromise(_from, to, stepSize, stepDuration, resultName, stopWhen) {
return function () {
//TODO: Verify pilot board is ready?
var my = this;
console.log('Finding Min Power (' + resultName + ')');
var steps = toStepArray(_from, to, stepSize);
//TODO: this is whackeroni
if (resultName == 'fromStall') {
for (var i = steps.length - 1; i > 1; i--) {
steps.splice(i, 0, 0);
}
}
var lastHeading, lastPower;
var power = steps.shift();
my.setMotorPowers.call(my, power, -power);
return new Promise(function (resolve, reject) {
var intervalId = setInterval(function () {
var newHeading = my.devices.pilot.details.pose.h;
var power = steps.shift();
if (typeof power === 'undefined') {
my.setMotorPowers(0, 0);
clearInterval(intervalId);
var result = {};
result[resultName] = 0;
result['never_stopped'] = true;
reject(result);
return;
}
if (stopWhen(lastHeading, newHeading)) {
console.log('Found Min Power: ' + lastPower);
var result = {};
result[resultName] = lastPower;
resolve(result);
my.setMotorPowers(0, 0);
clearInterval(intervalId);
} else {
my.setMotorPowers(power, -power);
lastHeading = newHeading;
lastPower = power === 0 ? lastPower : power;
}
}, stepDuration);
});
};
}
function toStepArray(_from, to, stepSize) {
stepSize = (_from < to) ? stepSize : -stepSize;
var stepArray = [];
for (var i = _from; ((i <= to) && (stepSize > 0)) || ((i >= to) && (stepSize < 0)); i += stepSize) {
stepArray.push(i);
}
return stepArray;
}
Cylon.start();