New example for train loop with station stop

This commit is contained in:
Nathan Kellenicki 2019-01-03 10:08:30 -08:00
parent 37118d5c1e
commit e4809ce420
6 changed files with 70 additions and 11 deletions

View File

@ -85,8 +85,8 @@ poweredUP.on("discover", async (hub) => { // Wait to Batmobile and Remote
} }
if (batmobile && remote) { if (batmobile && remote) {
batmobile.setLEDColor(PoweredUP.Consts.Colors.WHITE); batmobile.setLEDColor(PoweredUP.Consts.Color.WHITE);
remote.setLEDColor(PoweredUP.Consts.Colors.RED); remote.setLEDColor(PoweredUP.Consts.Color.RED);
console.log("You're now ready to go!"); console.log("You're now ready to go!");
} }

View File

@ -5,7 +5,7 @@ const poweredUP = new PoweredUP.PoweredUP()
const trains = [ const trains = [
{ {
name: "Maersk Intermodal", name: "Maersk Intermodal",
color: PoweredUP.Consts.Colors.LIGHT_BLUE, color: PoweredUP.Consts.Color.LIGHT_BLUE,
hubs: [ hubs: [
{ {
name: "NK_Maersk", name: "NK_Maersk",
@ -15,7 +15,7 @@ const trains = [
}, },
{ {
name: "Horizon Express", name: "Horizon Express",
color: PoweredUP.Consts.Colors.ORANGE, color: PoweredUP.Consts.Color.ORANGE,
hubs: [ hubs: [
{ {
name: "NK_Horizon_1", name: "NK_Horizon_1",
@ -32,7 +32,7 @@ const trains = [
}, },
{ {
name: "Emerald Night", name: "Emerald Night",
color: PoweredUP.Consts.Colors.GREEN, color: PoweredUP.Consts.Color.GREEN,
hubs: [ hubs: [
{ {
name: "NK_Emerald", name: "NK_Emerald",
@ -44,7 +44,7 @@ const trains = [
}, },
{ {
name: "Metroliner", name: "Metroliner",
color: PoweredUP.Consts.Colors.WHITE, color: PoweredUP.Consts.Color.WHITE,
hubs: [ hubs: [
{ {
name: "NK_Metroliner", name: "NK_Metroliner",

View File

@ -0,0 +1,59 @@
const PoweredUP = require("..");
const poweredUP = new PoweredUP.PoweredUP();
poweredUP.scan(); // Start scanning for trains
// Change these to make the train behave as you want
const STATION_STOP_COLOR = PoweredUP.Consts.Color.RED;
const TRAIN_SPEED = 55;
const STOPPING_SPEED = 4000;
const STOP_DELAY = 6000;
const LOOPS_WITHOUT_STOPPING = 3;
const TRAIN_MOTOR_PORT = "A";
let currentSpeed = 0;
let currentLoop = 0;
console.log("Looking for trains...");
poweredUP.on("discover", async (hub) => { // Wait to discover a train
let sensing = true;
await hub.connect(); // Connect to train
console.log(`Connected to ${hub.name}!`);
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.name}`);
hub.on("colorAndDistance", async (port, color, distance) => {
if (color === STATION_STOP_COLOR && distance > 5 && distance < 15 && sensing) { // If color is seen, stop the train, wait, then go again
if (currentLoop >= LOOPS_WITHOUT_STOPPING - 1) {
console.log(`Looped ${hub.name} (${currentLoop + 1})`);
console.log(`Stopping ${hub.name}`);
sensing = false;
await hub.rampMotorSpeed(TRAIN_MOTOR_PORT, currentSpeed, 0, STOPPING_SPEED);
currentSpeed = 0;
currentLoop = 0;
console.log(`Stopped ${hub.name}`);
await hub.sleep(STOP_DELAY);
console.log(`Starting ${hub.name}`);
await hub.rampMotorSpeed(TRAIN_MOTOR_PORT, currentSpeed + 10, TRAIN_SPEED, STOPPING_SPEED);
currentSpeed = TRAIN_SPEED;
await hub.sleep(2000);
sensing = true;
} else {
console.log(`Looped ${hub.name} (${currentLoop + 1})`);
sensing = false;
currentLoop++;
await hub.sleep(2000);
sensing = true;
}
}
});
});

View File

@ -4,8 +4,8 @@ const poweredUP = new PoweredUP.PoweredUP();
poweredUP.scan(); // Start scanning for trains poweredUP.scan(); // Start scanning for trains
// Change these to make the train behave as you want // Change these to make the train behave as you want
const FORWARD_DIRECTION_COLOR = PoweredUP.Consts.Colors.YELLOW; const FORWARD_DIRECTION_COLOR = PoweredUP.Consts.Color.YELLOW;
const BACKWARDS_DIRECTION_COLOR = PoweredUP.Consts.Colors.RED; const BACKWARDS_DIRECTION_COLOR = PoweredUP.Consts.Color.RED;
const TRAIN_SPEED = 40; const TRAIN_SPEED = 40;
const STOPPING_SPEED = 2000; const STOPPING_SPEED = 2000;
const STOP_DELAY = 2000; const STOP_DELAY = 2000;

View File

@ -67,8 +67,8 @@ poweredUP.on("discover", async (hub) => { // Wait to discover Vernie and Remote
} }
if (vernie && remote) { if (vernie && remote) {
vernie.setLEDColor(PoweredUP.Consts.Colors.BLUE); vernie.setLEDColor(PoweredUP.Consts.Color.BLUE);
remote.setLEDColor(PoweredUP.Consts.Colors.BLUE); remote.setLEDColor(PoweredUP.Consts.Color.BLUE);
console.log("You're now ready to go!"); console.log("You're now ready to go!");
} }

View File

@ -79,7 +79,7 @@ poweredUP.on("discover", async (vernie) => { // Wait to discover Vernie
await vernie.connect(); await vernie.connect();
console.log("Connected to Vernie!"); console.log("Connected to Vernie!");
vernie.setLEDColor(PoweredUP.Consts.Colors.BLUE); vernie.setLEDColor(PoweredUP.Consts.Color.BLUE);
await vernie.sleep(1000); await vernie.sleep(1000);
vernie.setMotorSpeed("AB", 50); // Start moving! vernie.setMotorSpeed("AB", 50); // Start moving!