This repository has been archived by the owner on Apr 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
car.js
142 lines (105 loc) · 3.32 KB
/
car.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
var Config = require('./config.json');
var five = require('johnny-five');
var events = require('events');
var Logger = require('./logger.js');
var boardOptions = {};
if (Config.boardType == "raspi") {
var raspio = require('raspi-io');
boardOptions = {
io: new raspio(),
repl: false
}
}
var board = new five.Board(boardOptions);
var Car = function () {
this.speed = 0;
this.emitter = new events.EventEmitter;
};
Car.prototype = {};
var theCar = new Car();
board.on('ready', function () {
const ENGINE_MIN = 43,
ENGINE_MAX = 93,
BRAKE_TIME = 900,
BRAKE_INTERVAL = 300,
ACCELERATION_TIME = 1000,
ACCELERATION_INTERVAL = 200;
var motor, steering;
init();
function init() {
motor = new five.ESC({
pin: Config.car.motor,
device: 'FORWARD_REVERSE',
range: [0, ENGINE_MAX],
neutral: ENGINE_MIN
});
steering = new five.Servo({
pin: Config.car.steering,
center: true
});
//let everyone know the car is ready.
theCar.emitter.emit('CarReady');
}
theCar.emitter.on('Stop', function () {
stop();
});
theCar.emitter.on('Go', function () {
go();
});
theCar.emitter.on('SetSteering', function (value) {
Logger.debug('Steering value: ' + value);
var direction = (value + 50) / 100 * 180;
steering.to(direction);
});
theCar.emitter.on('SetMotorSpeed', setSpeed);
function setSpeed(value) {
theCar.speed = value;
var actualSpeed = translateSpeed(value);
Logger.debug('Speed value: ' + actualSpeed);
motor.speed(actualSpeed);
}
/**
* Translates the speed percentage to an actual value to be sent to the ESC.
* @param speed Speed in percentage (0-100%)
* @returns {number} Translated speed value adjusted to the scale of the ESC.
*/
function translateSpeed(speed) {
return speed / 100 * (ENGINE_MAX - ENGINE_MIN) + ENGINE_MIN
}
function stop() {
console.log('Stopping car');
var currentSpeed = theCar.speed,
brakeStepSize = currentSpeed / (BRAKE_TIME / BRAKE_INTERVAL);
slowDown();
function slowDown() {
setTimeout(function () {
var newSpeed = currentSpeed - brakeStepSize;
console.log('Slowing down to: ' + newSpeed);
setSpeed(newSpeed);
currentSpeed = theCar.speed;
if (currentSpeed > 0) {
slowDown();
}
}, BRAKE_INTERVAL);
}
}
function go() {
console.log('Speeding car');
var MAX_SPEED = 100,
currentSpeed = theCar.speed,
accelerateStepSize = MAX_SPEED / (ACCELERATION_TIME / ACCELERATION_INTERVAL);
speedUp();
function speedUp() {
setTimeout(function () {
var newSpeed = currentSpeed + accelerateStepSize;
console.log('Speeding up to: ' + newSpeed);
setSpeed(newSpeed);
currentSpeed = theCar.speed;
if (currentSpeed < MAX_SPEED) {
speedUp();
}
}, ACCELERATION_INTERVAL);
}
}
});
module.exports = theCar;