Added example of slow speed train control with a Medium Linear Motor (Boost Motor)

This commit is contained in:
Nathan Kellenicki 2020-01-29 14:54:14 -08:00
parent 407accedc8
commit c6f8015154
2 changed files with 83 additions and 5 deletions

View 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!");
}
}
});

View File

@ -134,11 +134,11 @@ export const ColorNames = Color;
* @property {number} STOP 4
*/
export enum ButtonState {
PRESSED = 0,
RELEASED = 1,
UP = 2,
DOWN = 3,
STOP = 4
PRESSED = 2,
RELEASED = 0,
UP = 1,
DOWN = 255,
STOP = 127
}