|
| 1 | + var five = require('johnny-five'), |
| 2 | + dualshock = require('dualshock-controller'); |
| 3 | + |
| 4 | +var board, lServo, rServo, ds; |
| 5 | + |
| 6 | +ds = dualshock({ |
| 7 | + config: 'dualShock3' |
| 8 | +}); |
| 9 | + |
| 10 | +ds.on('error', function (data) { |
| 11 | + console.log('ruh roh something broke'); |
| 12 | +}); |
| 13 | + |
| 14 | +board = new five.Board({ |
| 15 | + port: '/dev/tty.usbserial-AH00156H' |
| 16 | +}); |
| 17 | + |
| 18 | +board.on("ready", function () { |
| 19 | + rServo = new five.Servo({ |
| 20 | + pin: 10, |
| 21 | + type: 'continuous' |
| 22 | + }); |
| 23 | + lServo = new five.Servo({ |
| 24 | + pin: 11, |
| 25 | + type: 'continuous' |
| 26 | + }); |
| 27 | + |
| 28 | + lServo.stop(); |
| 29 | + rServo.stop(); |
| 30 | + |
| 31 | + var moveSpeed = 0.1; |
| 32 | + |
| 33 | + function stop() { |
| 34 | + lServo.stop(); |
| 35 | + rServo.stop(); |
| 36 | + } |
| 37 | + |
| 38 | + function turn (rightOn, leftOn, timeout) { |
| 39 | + if (rightOn) { |
| 40 | + rServo.cw(moveSpeed); |
| 41 | + } else { |
| 42 | + rServo.ccw(moveSpeed); |
| 43 | + } |
| 44 | + |
| 45 | + if (leftOn) { |
| 46 | + lServo.ccw(moveSpeed); |
| 47 | + } else { |
| 48 | + lServo.cw(moveSpeed); |
| 49 | + } |
| 50 | + |
| 51 | + if (timeout) { |
| 52 | + setTimeout(stop, timeout); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + function turnLeft (timeout) { |
| 57 | + console.log('turning left!'); |
| 58 | + turn (false, true, timeout); |
| 59 | + } |
| 60 | + |
| 61 | + function turnRight (timeout) { |
| 62 | + console.log('turning right!'); |
| 63 | + turn (true, false, timeout); |
| 64 | + } |
| 65 | + |
| 66 | + function goStraight (timeout) { |
| 67 | + console.log('going straight!'); |
| 68 | + turn (true, true, timeout); |
| 69 | + } |
| 70 | + |
| 71 | + function goBack (timeout) { |
| 72 | + console.log('back it up!'); |
| 73 | + turn (false, false, timeout); |
| 74 | + } |
| 75 | + |
| 76 | + ds.on('square:press', function () { |
| 77 | + turnLeft(); |
| 78 | + }); |
| 79 | + |
| 80 | + ds.on('square:release', function () { |
| 81 | + stop(); |
| 82 | + }); |
| 83 | + |
| 84 | + ds.on('circle:press', function () { |
| 85 | + turnRight(); |
| 86 | + }); |
| 87 | + |
| 88 | + ds.on('circle:release', function () { |
| 89 | + stop(); |
| 90 | + }); |
| 91 | + |
| 92 | + ds.on('triangle:press', function () { |
| 93 | + goStraight(); |
| 94 | + }); |
| 95 | + |
| 96 | + ds.on('triangle:release', function () { |
| 97 | + stop(); |
| 98 | + }); |
| 99 | + |
| 100 | + ds.on('x:press', function () { |
| 101 | + goBack(); |
| 102 | + }); |
| 103 | + |
| 104 | + ds.on('x:release', function () { |
| 105 | + stop(); |
| 106 | + }); |
| 107 | + |
| 108 | +}); |
| 109 | + |
| 110 | +ds.connect(); |
0 commit comments