Tidied up train examples

This commit is contained in:
Nathan Kellenicki 2018-08-09 09:18:05 +01:00
parent 74a222fd8c
commit d40ba5cee6
2 changed files with 38 additions and 18 deletions

View File

@ -19,12 +19,12 @@ const trains = [
{
name: "NK_Horizon_1",
ports: ["A"],
lights: ["B"]
lights: ["B"],
reverse: ["A"]
},
{
name: "NK_Horizon_2",
ports: ["A"],
reverse: ["A"]
ports: ["A"]
}
]
},
@ -35,7 +35,8 @@ const trains = [
{
name: "NK_Emerald",
ports: ["A"],
lights: ["B"]
lights: ["B"],
reverse: ["A"]
}
]
},
@ -46,7 +47,8 @@ const trains = [
{
name: "NK_Metroliner",
ports: ["A"],
lights: ["B"]
lights: ["B"],
reverse: ["A"]
}
]
}
@ -57,7 +59,7 @@ poweredUP.on("discover", async (hub) => {
if (hub instanceof PoweredUP.PUPRemote) {
await hub.connect();
hub._currentTrain = 0;
hub._currentTrain = 2;
hub.on("button", (button, state) => {
if (button === "GREEN") {
@ -69,8 +71,7 @@ poweredUP.on("discover", async (hub) => {
hub.setLEDColor(trains[hub._currentTrain].color);
console.log(`Switched active train on remote ${hub.name} to ${trains[hub._currentTrain].name}`);
}
break;
} else if (button === "LEFT" || button === "RIGHT") {
} else if ((button === "LEFT" || button === "RIGHT") && state !== PoweredUP.Consts.ButtonStates.RELEASED) {
trains[hub._currentTrain]._speed = trains[hub._currentTrain]._speed || 0;
if (state === PoweredUP.Consts.ButtonStates.UP) {
trains[hub._currentTrain]._speed += 10;
@ -79,17 +80,19 @@ poweredUP.on("discover", async (hub) => {
}
} else if (state === PoweredUP.Consts.ButtonStates.DOWN) {
trains[hub._currentTrain]._speed -= 10;
if (trains[hub._currentTrain]._speed < 0) {
trains[hub._currentTrain]._speed = 0;
if (trains[hub._currentTrain]._speed < -100) {
trains[hub._currentTrain]._speed = -100;
}
} else if (state === PoweredUP.Consts.ButtonStates.STOP) {
trains[hub._currentTrain]._speed = 0;
}
for (let trainHub in trains[hub._currentTrain].hubs) {
trainHub = trains[hub._currentTrain].hubs[trainHub];
if (trainHub._hub) {
for (let port in trainHub.ports) {
port = trainHub.ports[port];
trainHub.reverse = trainHub.reverse || [];
trainHub._hub.setMotorSpeed(port, trainHub.reverse.indexOf(port) >= 0 ? -trains[hub._currentTrain].speed : trains[hub._currentTrain].speed);
trainHub._hub.setMotorSpeed(port, trainHub.reverse.indexOf(port) >= 0 ? -trains[hub._currentTrain]._speed : trains[hub._currentTrain]._speed);
}
}
}
@ -103,7 +106,9 @@ poweredUP.on("discover", async (hub) => {
}
for (let train in trains) {
train = trains[train];
for (let trainHub in train.hubs) {
trainHub = train.hubs[trainHub];
if (hub.name === trainHub.name) {
await hub.connect();
trainHub._hub = hub;

View File

@ -7,9 +7,12 @@ poweredUP.scan(); // Start scanning for trains
const FORWARD_DIRECTION_COLOR = PoweredUP.Consts.Colors.YELLOW;
const BACKWARDS_DIRECTION_COLOR = PoweredUP.Consts.Colors.RED;
const TRAIN_SPEED = 40;
const STOPPING_SPEED = 2000;
const STOP_DELAY = 2000;
const TRAIN_MOTOR_PORT = "A";
let currentSpeed = 0;
console.log("Looking for trains...");
poweredUP.on("discover", async (hub) => { // Wait to discover a train
@ -19,27 +22,39 @@ poweredUP.on("discover", async (hub) => { // Wait to discover a train
await hub.connect(); // Connect to train
console.log(`Connected to ${hub.name}!`);
await hub.wait(2000); // Wait two seconds before starting the train
await hub.sleep(2000); // Wait two seconds before starting the train
hub.setMotorSpeed(TRAIN_MOTOR_PORT, TRAIN_SPEED);
currentSpeed = TRAIN_SPEED;
console.log("Started moving");
hub.on("color", async (port, color) => {
if (color === FORWARD_DIRECTION_COLOR && moving) { // If yellow is seen, stop the train, wait seconds, and reverse direction
console.log("Slowing down");
moving = false;
hub.setMotorSpeed(TRAIN_MOTOR_PORT, 0);
await hub.rampMotorSpeed(TRAIN_MOTOR_PORT, currentSpeed, 0, STOPPING_SPEED);
currentSpeed = 0;
console.log("Stopped");
await hub.sleep(STOP_DELAY);
hub.setMotorSpeed(TRAIN_MOTOR_PORT, -TRAIN_SPEED);
await hub.sleep(2000);
console.log("Changing direction and speeding up");
await hub.rampMotorSpeed(TRAIN_MOTOR_PORT, currentSpeed + 10, TRAIN_SPEED, STOPPING_SPEED);
currentSpeed = TRAIN_SPEED;
await hub.sleep(STOPPING_SPEED + 1000);
moving = true;
} else if (color === BACKWARDS_DIRECTION_COLOR && moving) { // If red is seen, stop the train, wait seconds, and reverse direction
console.log("Slowing down");
moving = false;
hub.setMotorSpeed(TRAIN_MOTOR_PORT, 0);
await hub.rampMotorSpeed(TRAIN_MOTOR_PORT, currentSpeed, 0, STOPPING_SPEED);
currentSpeed = 0;
console.log("Stopped");
await hub.sleep(STOP_DELAY);
hub.setMotorSpeed(TRAIN_MOTOR_PORT, -TRAIN_SPEED);
await hub.sleep(2000);
console.log("Changing direction and speeding up");
await hub.rampMotorSpeed(TRAIN_MOTOR_PORT, -(currentSpeed + 10), -TRAIN_SPEED, STOPPING_SPEED);
currentSpeed = -TRAIN_SPEED;
await hub.sleep(STOPPING_SPEED + 1000);
moving = true;
}