New example for train loop with station stop
This commit is contained in:
parent
37118d5c1e
commit
e4809ce420
@ -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!");
|
||||
}
|
||||
|
||||
|
@ -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",
|
||||
|
59
examples/train_loop_with_station_stop.js
Normal file
59
examples/train_loop_with_station_stop.js
Normal 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;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
});
|
@ -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;
|
||||
|
@ -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!");
|
||||
}
|
||||
|
||||
|
@ -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!
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user