-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
227 lines (189 loc) · 7.69 KB
/
utils.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
const sleep = require('es7-sleep');
const Motor = require('./objects/motor');
const Sensor = require('./objects/sensor');
const RESET_MOTOR_LIMIT = {
CURRENT_POSITION: 1,
FORWARD_LIMIT: 2,
BACKWARD_LIMIT: 3,
MIDPOINT_LIMIT: 4
};
/**
* Resets the motors offset encoder to the given type of offset.
*
* Valid limit values:
* * RESET_MOTOR_LIMIT.CURRENT_POSITION: The current position becomes the 0-point.
* * RESET_MOTOR_LIMIT.FORWARD_LIMIT: It rotates forward until it detects an obstacle and uses this as the new 0-point.
* * RESET_MOTOR_LIMIT.BACKWARD_LIMIT: It rotates backward until it detects an obstacle and uses this as the new 0-point.
* * RESET_MOTOR_LIMIT.MIDPOINT_LIMIT: It first rotates forwards and searches for an obstacle, then rotates backward and
* searches for an obstacle. The middle between those two extreme points becomes the new 0-point.
*
* @param {Object} brickPiInstance Instance of the BrickPi class in which the motor is plugged in
* @param {number} motorPort The port of the motor to be manipulated (brickPiInstance.PORT_A, .PORT_B, .PORT_C, .PORT_D)
* @param {number} limitType Search for this point. See above description.
* @param {number} newOffset Set the found point to this offset value.
* @param {number} maxPower When the power of the motor drops below this value, it is considered to be an obstacle. (WARNING: power is currently "dps", because I couldn't find a way to get the current power of the motor.. get_motor_status states that it returnes the power, but instead returnes the speed :/)
* @param {number} timeLimit If no obstacle is found within this time limit, the promise gets rejected.
* @param {number} motorPower Power of the motor (lower for smoother finding)
* @return {Promise} When the new offset is set, the promise resolves.
*/
const resetMotorEncoder = async (brickPiInstance, motorPort, limitType = RESET_MOTOR_LIMIT.CURRENT_POSITION, newOffset = 0, maxPower = 25, timeLimit = 10000, motorPower = 100) => {
let startTime = Date.now();
const checkPower = async () => {
while (Date.now() - startTime <= timeLimit) {
await sleep(20);
let status = await brickPiInstance.get_motor_status(motorPort);
if (Math.abs(status[3]) <= maxPower) {
await brickPiInstance.set_motor_power(motorPort, 0);
return status[2];
}
}
await brickPiInstance.set_motor_power(motorPort, 0);
throw new Error('resetMotorEncoder: timeLimit exceeded');
};
if (limitType === RESET_MOTOR_LIMIT.CURRENT_POSITION) {
let offset = await brickPiInstance.get_motor_encoder(motorPort);
await brickPiInstance.offset_motor_encoder(motorPort, offset - newOffset);
} else if (limitType === RESET_MOTOR_LIMIT.FORWARD_LIMIT || limitType === RESET_MOTOR_LIMIT.BACKWARD_LIMIT) {
let power = motorPower;
if (limitType === RESET_MOTOR_LIMIT.BACKWARD_LIMIT) power = -motorPower;
await brickPiInstance.set_motor_power(motorPort, power);
let offset = await checkPower();
await brickPiInstance.offset_motor_encoder(motorPort, offset - newOffset);
} else if (limitType === RESET_MOTOR_LIMIT.MIDPOINT_LIMIT) {
await brickPiInstance.set_motor_power(motorPort, motorPower);
let offsetForward = await checkPower();
await brickPiInstance.set_motor_power(motorPort, -motorPower);
let offsetBackward = await checkPower();
await brickPiInstance.offset_motor_encoder(motorPort, offsetBackward + (offsetForward - offsetBackward) / 2 - newOffset);
} else {
throw new Error('resetMotorEncoder: Invalid limitType.');
}
};
let resetBrickPis = [];
let shutdownHandlerRegistered = false;
const resetAllWhenFinished = (brickPiInstance) => {
if (!shutdownHandlerRegistered) {
shutdownHandlerRegistered = true;
async function exitHandler(err) {
if (err) console.log(err.stack);
for (let i = 0; i < resetBrickPis.length; i++) {
await resetBrickPis[i].reset_all();
}
process.exit();
}
process.stdin.resume();
process.on('exit', exitHandler);
process.on('SIGINT', exitHandler);
process.on('uncaughtException', exitHandler);
}
resetBrickPis.push(brickPiInstance);
};
/**
* Sets the motors position and resolves, when the final position is reached
* @deprecated Will be removed in 1.0.0. Use brickpi3.utils.getMotor(BP, port).setPosition(targetPosition);
*
* @param brickPiInstance
* @param motorPort
* @param targetPosition
* @return {Promise}
*/
const setMotorPosition = async (brickPiInstance, motorPort, targetPosition) => {
await brickPiInstance.set_motor_position(motorPort, targetPosition);
let lastEncoder = null;
while (true) {
await sleep(20);
let encoder = await brickPiInstance.get_motor_encoder(motorPort);
if (lastEncoder !== null && lastEncoder === encoder) {
return;
}
lastEncoder = encoder;
}
};
/**
* Waits for a simple sensor to become a given value. Works with all sensors, which return a scalar value (and not an
* array). If the sensor returns the value, the promise is resolved.
* @deprecated Will be removed in 1.0.0. Use brickpi3.utils.getSensor(BP, BP.PORT_1).waitFor(1);
*
* @param {Object} brickPiInstance
* @param {*} sensorPort
* @param {number} targetValue
* @param {number} timeLimit
* @return {Promise}
*/
const waitForSensor = async (brickPiInstance, sensorPort, targetValue, timeLimit = 10000) => {
let startTime = Date.now();
while (Date.now() - startTime <= timeLimit) {
await sleep(10);
let value = await brickPiInstance.get_sensor(sensorPort, timeLimit);
if (value === targetValue) {
return;
}
}
throw new Error('waitForSensor: timeLimit exceeded');
};
const getMotor = (brickPiInstance, motorPort) => {
return new Motor(brickPiInstance, motorPort);
};
const getSensor = (brickPiInstance, sensorPort, timeLimit=null) => {
return new Sensor(brickPiInstance, sensorPort, timeLimit);
};
const Gear = function(teeth) {
this._joinedParent = null;
this._drivenParent = null;
/**
*
* @param {number|Gear} teethOrGear
* @return {Gear}
*/
this.drive = (teethOrGear) => {
if(!(teethOrGear instanceof Gear)) {
teethOrGear = new Gear(teethOrGear);
}
teethOrGear._drivenParent = this;
return teethOrGear;
};
/**
*
* @param {number|Gear} teethOrGear
* @return {Gear}
*/
this.connect = (teethOrGear) => {
if(!(teethOrGear instanceof Gear)) {
teethOrGear = new Gear(teethOrGear);
}
teethOrGear._joinedParent = this;
return teethOrGear;
};
/**
* @return {number}
*/
this.getTeeth = () => {
return teeth;
};
/**
* @return {number}
*/
this.getFactor = () => {
let currentGear = this;
let currentFactor = 1;
while (currentGear._joinedParent !== null || currentGear._drivenParent !== null) {
if (currentGear._drivenParent !== null) {
currentFactor *= -1 * currentGear._drivenParent.getTeeth() / currentGear.getTeeth();
currentGear = currentGear._drivenParent;
} else {
currentGear = currentGear._joinedParent;
}
}
return currentFactor;
}
};
module.exports = {
RESET_MOTOR_LIMIT: RESET_MOTOR_LIMIT,
resetMotorEncoder: resetMotorEncoder,
resetAllWhenFinished: resetAllWhenFinished,
setMotorPosition: setMotorPosition,
waitForSensor: waitForSensor,
getMotor: getMotor,
getSensor: getSensor,
Gear: Gear
};