Added example of slow speed train control with a Medium Linear Motor (Boost Motor)
This commit is contained in:
parent
407accedc8
commit
c6f8015154
78
examples/slow_train_remote.js
Normal file
78
examples/slow_train_remote.js
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
*
|
||||||
|
* This example allows you to connect Vernie and a Powered UP Remote Control to your laptop, and enables the control of Vernie with the Remote.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
const PoweredUP = require("..");
|
||||||
|
|
||||||
|
const poweredUP = new PoweredUP.PoweredUP();
|
||||||
|
poweredUP.scan(); // Start scanning
|
||||||
|
|
||||||
|
console.log("Looking for train and remote...");
|
||||||
|
|
||||||
|
const TRAIN_LED_COLOR = PoweredUP.Consts.Color.PURPLE;
|
||||||
|
|
||||||
|
let trainHub = null;
|
||||||
|
let trainMotor = null;
|
||||||
|
let remoteHub = null;
|
||||||
|
let remoteButtonLeft = null;
|
||||||
|
let remoteButtonRight = null;
|
||||||
|
|
||||||
|
let currentSpeed = 0;
|
||||||
|
|
||||||
|
poweredUP.on("discover", async (hub) => { // Wait to discover Vernie and Remote
|
||||||
|
|
||||||
|
if (hub.type === PoweredUP.Consts.HubType.HUB) {
|
||||||
|
|
||||||
|
trainHub = hub;
|
||||||
|
await trainHub.connect();
|
||||||
|
const led = await trainHub.waitForDeviceByType(PoweredUP.Consts.DeviceType.HUB_LED);
|
||||||
|
trainMotor = await trainHub.waitForDeviceByType(PoweredUP.Consts.DeviceType.MEDIUM_LINEAR_MOTOR);
|
||||||
|
led.setColor(TRAIN_LED_COLOR);
|
||||||
|
console.log(`Connected to train (${trainHub.name})!`);
|
||||||
|
if (trainHub && remoteHub) {
|
||||||
|
console.log("You're now ready to go!");
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (hub.type === PoweredUP.Consts.HubType.REMOTE_CONTROL) {
|
||||||
|
remoteHub = hub;
|
||||||
|
await remoteHub.connect();
|
||||||
|
const led = await remoteHub.waitForDeviceByType(PoweredUP.Consts.DeviceType.HUB_LED);
|
||||||
|
remoteButtonLeft = await remoteHub.waitForDeviceAtPort("LEFT");
|
||||||
|
remoteButtonRight = await remoteHub.waitForDeviceAtPort("RIGHT");
|
||||||
|
led.setColor(TRAIN_LED_COLOR);
|
||||||
|
|
||||||
|
remoteButtonLeft.on("remoteButton", ({ event }) => {
|
||||||
|
if (trainMotor) {
|
||||||
|
if (event === PoweredUP.Consts.ButtonState.UP) {
|
||||||
|
currentSpeed += 10;
|
||||||
|
} else if (event === PoweredUP.Consts.ButtonState.DOWN) {
|
||||||
|
currentSpeed -= 10;
|
||||||
|
} else if (event === PoweredUP.Consts.ButtonState.STOP) {
|
||||||
|
currentSpeed = 0;
|
||||||
|
}
|
||||||
|
trainMotor.setSpeed(currentSpeed);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
remoteButtonRight.on("remoteButton", ({ event }) => {
|
||||||
|
if (trainMotor) {
|
||||||
|
if (event === PoweredUP.Consts.ButtonState.UP) {
|
||||||
|
currentSpeed += 1;
|
||||||
|
} else if (event === PoweredUP.Consts.ButtonState.DOWN) {
|
||||||
|
currentSpeed -= 1;
|
||||||
|
} else if (event === PoweredUP.Consts.ButtonState.STOP) {
|
||||||
|
currentSpeed = 0;
|
||||||
|
}
|
||||||
|
trainMotor.setSpeed(currentSpeed);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`Connected to remote (${remoteHub.name})!`);
|
||||||
|
if (trainHub && remoteHub) {
|
||||||
|
console.log("You're now ready to go!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
@ -134,11 +134,11 @@ export const ColorNames = Color;
|
|||||||
* @property {number} STOP 4
|
* @property {number} STOP 4
|
||||||
*/
|
*/
|
||||||
export enum ButtonState {
|
export enum ButtonState {
|
||||||
PRESSED = 0,
|
PRESSED = 2,
|
||||||
RELEASED = 1,
|
RELEASED = 0,
|
||||||
UP = 2,
|
UP = 1,
|
||||||
DOWN = 3,
|
DOWN = 255,
|
||||||
STOP = 4
|
STOP = 127
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user