From e4809ce4200ebbb017edcb9d622b26d6760ef3e0 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Thu, 3 Jan 2019 10:08:30 -0800 Subject: [PATCH] New example for train loop with station stop --- examples/batmobile_remote.js | 4 +- examples/train_controller.js | 8 ++-- examples/train_loop_with_station_stop.js | 59 ++++++++++++++++++++++++ examples/train_reverse.js | 4 +- examples/vernie_remote.js | 4 +- examples/vernie_roaming.js | 2 +- 6 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 examples/train_loop_with_station_stop.js diff --git a/examples/batmobile_remote.js b/examples/batmobile_remote.js index ec7b325..05cc851 100644 --- a/examples/batmobile_remote.js +++ b/examples/batmobile_remote.js @@ -85,8 +85,8 @@ poweredUP.on("discover", async (hub) => { // Wait to Batmobile and Remote } if (batmobile && remote) { - batmobile.setLEDColor(PoweredUP.Consts.Colors.WHITE); - remote.setLEDColor(PoweredUP.Consts.Colors.RED); + batmobile.setLEDColor(PoweredUP.Consts.Color.WHITE); + remote.setLEDColor(PoweredUP.Consts.Color.RED); console.log("You're now ready to go!"); } diff --git a/examples/train_controller.js b/examples/train_controller.js index bcdd6b8..e4eaaec 100644 --- a/examples/train_controller.js +++ b/examples/train_controller.js @@ -5,7 +5,7 @@ const poweredUP = new PoweredUP.PoweredUP() const trains = [ { name: "Maersk Intermodal", - color: PoweredUP.Consts.Colors.LIGHT_BLUE, + color: PoweredUP.Consts.Color.LIGHT_BLUE, hubs: [ { name: "NK_Maersk", @@ -15,7 +15,7 @@ const trains = [ }, { name: "Horizon Express", - color: PoweredUP.Consts.Colors.ORANGE, + color: PoweredUP.Consts.Color.ORANGE, hubs: [ { name: "NK_Horizon_1", @@ -32,7 +32,7 @@ const trains = [ }, { name: "Emerald Night", - color: PoweredUP.Consts.Colors.GREEN, + color: PoweredUP.Consts.Color.GREEN, hubs: [ { name: "NK_Emerald", @@ -44,7 +44,7 @@ const trains = [ }, { name: "Metroliner", - color: PoweredUP.Consts.Colors.WHITE, + color: PoweredUP.Consts.Color.WHITE, hubs: [ { name: "NK_Metroliner", diff --git a/examples/train_loop_with_station_stop.js b/examples/train_loop_with_station_stop.js new file mode 100644 index 0000000..033daf0 --- /dev/null +++ b/examples/train_loop_with_station_stop.js @@ -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; + } + } + }); + +}); \ No newline at end of file diff --git a/examples/train_reverse.js b/examples/train_reverse.js index 038f39a..946faa3 100644 --- a/examples/train_reverse.js +++ b/examples/train_reverse.js @@ -4,8 +4,8 @@ const poweredUP = new PoweredUP.PoweredUP(); poweredUP.scan(); // Start scanning for trains // Change these to make the train behave as you want -const FORWARD_DIRECTION_COLOR = PoweredUP.Consts.Colors.YELLOW; -const BACKWARDS_DIRECTION_COLOR = PoweredUP.Consts.Colors.RED; +const FORWARD_DIRECTION_COLOR = PoweredUP.Consts.Color.YELLOW; +const BACKWARDS_DIRECTION_COLOR = PoweredUP.Consts.Color.RED; const TRAIN_SPEED = 40; const STOPPING_SPEED = 2000; const STOP_DELAY = 2000; diff --git a/examples/vernie_remote.js b/examples/vernie_remote.js index 0bc48ba..c9da37c 100644 --- a/examples/vernie_remote.js +++ b/examples/vernie_remote.js @@ -67,8 +67,8 @@ poweredUP.on("discover", async (hub) => { // Wait to discover Vernie and Remote } if (vernie && remote) { - vernie.setLEDColor(PoweredUP.Consts.Colors.BLUE); - remote.setLEDColor(PoweredUP.Consts.Colors.BLUE); + vernie.setLEDColor(PoweredUP.Consts.Color.BLUE); + remote.setLEDColor(PoweredUP.Consts.Color.BLUE); console.log("You're now ready to go!"); } diff --git a/examples/vernie_roaming.js b/examples/vernie_roaming.js index dd042f2..b1ad26e 100644 --- a/examples/vernie_roaming.js +++ b/examples/vernie_roaming.js @@ -79,7 +79,7 @@ poweredUP.on("discover", async (vernie) => { // Wait to discover Vernie await vernie.connect(); console.log("Connected to Vernie!"); - vernie.setLEDColor(PoweredUP.Consts.Colors.BLUE); + vernie.setLEDColor(PoweredUP.Consts.Color.BLUE); await vernie.sleep(1000); vernie.setMotorSpeed("AB", 50); // Start moving!