Removed examples with old API
This commit is contained in:
parent
c6f8015154
commit
e34eea98ba
@ -1,162 +0,0 @@
|
|||||||
// This example won't work by itself, this is the code used to automate this train layout: https://www.youtube.com/watch?v=Tyzo_hHFiUc
|
|
||||||
// It is included here as an example.
|
|
||||||
|
|
||||||
const PoweredUP = require("..");
|
|
||||||
|
|
||||||
const poweredUP = new PoweredUP.PoweredUP();
|
|
||||||
poweredUP.scan(); // Start scanning for hubs
|
|
||||||
|
|
||||||
console.log("Looking for Hubs...");
|
|
||||||
|
|
||||||
const maerskUuid = "e29b56ad7c74419a9c6462f13c58306c";
|
|
||||||
const horizonUuid = "2278310c5d3b47c087290d9dc745c017";
|
|
||||||
const outerUuid = "c72413db7ce24411967ff25184d4609a";
|
|
||||||
const innerUuid = "31c8ce1864f245d3ba5f87b33adb4b6d";
|
|
||||||
const stopUuid = "f4924139c6684be19840f97738c707f3";
|
|
||||||
|
|
||||||
let maersk = null;
|
|
||||||
let outer = null;
|
|
||||||
let inner = null;
|
|
||||||
let horizon = null;
|
|
||||||
let stop = null;
|
|
||||||
|
|
||||||
let state = 0;
|
|
||||||
|
|
||||||
let moving = true;
|
|
||||||
|
|
||||||
poweredUP.on("discover", async (hub) => { // Wait to discover hubs
|
|
||||||
|
|
||||||
console.log(hub.uuid);
|
|
||||||
|
|
||||||
if (hub.uuid === outerUuid) {
|
|
||||||
await hub.connect();
|
|
||||||
outer = hub;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hub.uuid === innerUuid) {
|
|
||||||
await hub.connect();
|
|
||||||
inner = hub;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hub.uuid === horizonUuid) {
|
|
||||||
await hub.connect();
|
|
||||||
horizon = hub;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hub.uuid === stopUuid) {
|
|
||||||
await hub.connect();
|
|
||||||
stop = hub;
|
|
||||||
|
|
||||||
stop.on("distance", async (port, distance) => {
|
|
||||||
if (distance < 100 && moving && state === 10) {
|
|
||||||
moving = false;
|
|
||||||
horizon.setMotorSpeed("A", 0);
|
|
||||||
await maersk.sleep(2000);
|
|
||||||
maersk.setMotorSpeed("A", -40);
|
|
||||||
setTimeout(() => {
|
|
||||||
state = 3;
|
|
||||||
moving = true;
|
|
||||||
}, 3000);
|
|
||||||
}
|
|
||||||
if (distance < 100 && moving && state === 11) {
|
|
||||||
moving = false;
|
|
||||||
horizon.setMotorSpeed("A", 0);
|
|
||||||
await maersk.sleep(2000);
|
|
||||||
maersk.setMotorSpeed("A", 50);
|
|
||||||
setTimeout(() => {
|
|
||||||
state = 0;
|
|
||||||
moving = true;
|
|
||||||
}, 3000);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hub.uuid === maerskUuid) {
|
|
||||||
await hub.connect();
|
|
||||||
maersk = hub;
|
|
||||||
maersk.setMotorSpeed("A", 50);
|
|
||||||
|
|
||||||
maersk.on("color", async (port, color) => {
|
|
||||||
console.log(color);
|
|
||||||
|
|
||||||
if (color === 7 && moving && state === 0) {
|
|
||||||
moving = false;
|
|
||||||
maersk.setMotorSpeed("A", 0);
|
|
||||||
await outer.setMotorSpeed("C", -100, 600);
|
|
||||||
await maersk.sleep(2000);
|
|
||||||
maersk.setMotorSpeed("A", -40);
|
|
||||||
setTimeout(() => {
|
|
||||||
state = 1;
|
|
||||||
moving = true;
|
|
||||||
}, 3000);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (color === 7 && moving && state === 1) {
|
|
||||||
moving = false;
|
|
||||||
maersk.setMotorSpeed("A", 0);
|
|
||||||
inner.setMotorSpeed("A", 100);
|
|
||||||
await inner.sleep(600);
|
|
||||||
inner.setMotorSpeed("A", 0);
|
|
||||||
await maersk.sleep(2000);
|
|
||||||
maersk.setMotorSpeed("A", 30);
|
|
||||||
setTimeout(() => {
|
|
||||||
state = 2;
|
|
||||||
moving = true;
|
|
||||||
}, 3000);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (color === 7 && moving && state === 2) {
|
|
||||||
moving = false;
|
|
||||||
maersk.setMotorSpeed("A", 0);
|
|
||||||
await outer.setMotorSpeed("C", 100, 600);
|
|
||||||
await maersk.sleep(2000);
|
|
||||||
horizon.setMotorSpeed("A", -60);
|
|
||||||
setTimeout(() => {
|
|
||||||
state = 10;
|
|
||||||
moving = true;
|
|
||||||
}, 6000);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (color === 7 && moving && state === 3) {
|
|
||||||
moving = false;
|
|
||||||
maersk.setMotorSpeed("A", 0);
|
|
||||||
inner.setMotorSpeed("A", -100);
|
|
||||||
outer.setMotorSpeed("C", -100, 600);
|
|
||||||
await inner.sleep(600);
|
|
||||||
inner.setMotorSpeed("A", 0);
|
|
||||||
await maersk.sleep(2000);
|
|
||||||
maersk.setMotorSpeed("A", 30);
|
|
||||||
setTimeout(() => {
|
|
||||||
state = 4;
|
|
||||||
moving = true;
|
|
||||||
setTimeout(() => {
|
|
||||||
maersk.setMotorSpeed("A", 50);
|
|
||||||
}, 4000);
|
|
||||||
}, 3000);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (color === 7 && moving && state === 4) {
|
|
||||||
moving = false;
|
|
||||||
await outer.setMotorSpeed("C", 100, 600);
|
|
||||||
setTimeout(() => {
|
|
||||||
state = 0;
|
|
||||||
moving = true;
|
|
||||||
}, 3000);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (color === 9 && moving && state === 0) {
|
|
||||||
moving = false;
|
|
||||||
maersk.setMotorSpeed("A", 0);
|
|
||||||
await maersk.sleep(2000);
|
|
||||||
horizon.setMotorSpeed("A", -60);
|
|
||||||
setTimeout(() => {
|
|
||||||
state = 11;
|
|
||||||
moving = true;
|
|
||||||
}, 6000);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
@ -1,93 +0,0 @@
|
|||||||
/*
|
|
||||||
*
|
|
||||||
* This example allows you to connect your Batmobile and a Powered UP Remote Control to your laptop, and enables the control of the Batmobile with the Remote.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
const PoweredUP = require("..");
|
|
||||||
|
|
||||||
const poweredUP = new PoweredUP.PoweredUP();
|
|
||||||
poweredUP.scan(); // Start scanning
|
|
||||||
|
|
||||||
console.log("Looking for Batmobile and Remote...");
|
|
||||||
|
|
||||||
let batmobile = null;
|
|
||||||
let remote = null;
|
|
||||||
|
|
||||||
let lastButton = null;
|
|
||||||
|
|
||||||
poweredUP.on("discover", async (hub) => { // Wait to Batmobile and Remote
|
|
||||||
|
|
||||||
if (hub instanceof PoweredUP.PUPHub) {
|
|
||||||
|
|
||||||
batmobile = hub;
|
|
||||||
await batmobile.connect();
|
|
||||||
console.log("Connected to Batmobile!");
|
|
||||||
|
|
||||||
} else if (hub instanceof PoweredUP.PUPRemote) {
|
|
||||||
|
|
||||||
remote = hub;
|
|
||||||
remote.on("button", async (button, state) => {
|
|
||||||
if (batmobile) {
|
|
||||||
switch (state) {
|
|
||||||
case PoweredUP.Consts.ButtonState.UP: // If up is pressed, move the wheels forward
|
|
||||||
{
|
|
||||||
lastButton = state;
|
|
||||||
batmobile.setMotorSpeed(button === "LEFT" ? "B" : "A", button === "LEFT" ? -100 : 100);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case PoweredUP.Consts.ButtonState.DOWN: // If down is pressed, move the wheels backwards
|
|
||||||
{
|
|
||||||
lastButton = state;
|
|
||||||
batmobile.setMotorSpeed(button === "LEFT" ? "B" : "A", button === "LEFT" ? 100 : -100);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case PoweredUP.Consts.ButtonState.RELEASED: // Stop the wheels when the button is released
|
|
||||||
{
|
|
||||||
if (lastButton === PoweredUP.Consts.ButtonState.UP || lastButton === PoweredUP.Consts.ButtonState.DOWN) {
|
|
||||||
batmobile.setMotorSpeed(button === "LEFT" ? "B" : "A", 0);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case PoweredUP.Consts.ButtonState.STOP: // When left red button is pressed, do a retreat. When right red button is pressed, scan the area.
|
|
||||||
{
|
|
||||||
lastButton = state;
|
|
||||||
if (button === "LEFT") {
|
|
||||||
await batmobile.setMotorSpeed("A", -100, 500);
|
|
||||||
await batmobile.setMotorSpeed("B", -100, 1000);
|
|
||||||
await batmobile.setMotorSpeed("A", -100, 1000);
|
|
||||||
await batmobile.setMotorSpeed("B", -100, 1000);
|
|
||||||
await batmobile.setMotorSpeed("A", -100, 500);
|
|
||||||
} else if (button === "RIGHT") {
|
|
||||||
await batmobile.setMotorSpeed("AB", 100, 1000);
|
|
||||||
await batmobile.setMotorSpeed("AB", -100, 2000);
|
|
||||||
await batmobile.setMotorSpeed("AB", 100, 2000);
|
|
||||||
await batmobile.setMotorSpeed("AB", -100, 1000);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case PoweredUP.Consts.ButtonState.PRESSED: // Do a wheelie when the green button is pressed
|
|
||||||
{
|
|
||||||
lastButton = state;
|
|
||||||
if (button === "GREEN") {
|
|
||||||
batmobile.setMotorSpeed("AB", [-100, 100]);
|
|
||||||
await batmobile.sleep(500);
|
|
||||||
await batmobile.setMotorSpeed("AB", [100, -100], 1000);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
await remote.connect();
|
|
||||||
console.log("Connected to Powered UP Remote!");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (batmobile && remote) {
|
|
||||||
batmobile.setLEDColor(PoweredUP.Consts.Color.WHITE);
|
|
||||||
remote.setLEDColor(PoweredUP.Consts.Color.RED);
|
|
||||||
console.log("You're now ready to go!");
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
@ -1,37 +0,0 @@
|
|||||||
/*
|
|
||||||
*
|
|
||||||
* This demonstrates connecting multiple hubs to your laptop. Once connected, all the hubs LED lights will cycle through the same colors simultaneously.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
const PoweredUP = require("..");
|
|
||||||
|
|
||||||
const poweredUP = new PoweredUP.PoweredUP();
|
|
||||||
poweredUP.scan(); // Start scanning for hubs
|
|
||||||
|
|
||||||
console.log("Looking for Hubs...");
|
|
||||||
|
|
||||||
poweredUP.on("discover", async (hub) => { // Wait to discover hubs
|
|
||||||
|
|
||||||
await hub.connect(); // Connect to hub
|
|
||||||
console.log(`Connected to ${hub.name}!`);
|
|
||||||
|
|
||||||
const motorA = await hub.waitForDeviceAtPort("A");
|
|
||||||
console.log("Got motor A");
|
|
||||||
const motorB = await hub.waitForDeviceAtPort("B");
|
|
||||||
console.log("Got motor B");
|
|
||||||
const motorC = await hub.waitForDeviceAtPort("C");
|
|
||||||
console.log("Got motor C");
|
|
||||||
const motorD = await hub.waitForDeviceAtPort("D");
|
|
||||||
console.log("Got motor D");
|
|
||||||
|
|
||||||
motorA.setPower(30);
|
|
||||||
motorB.setPower(30);
|
|
||||||
motorC.setPower(30);
|
|
||||||
motorD.setPower(30);
|
|
||||||
|
|
||||||
hub.on("disconnect", () => {
|
|
||||||
console.log("Hub disconnected");
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
@ -1,138 +0,0 @@
|
|||||||
/*
|
|
||||||
*
|
|
||||||
* This demonstrates connecting multiple hubs to your laptop. Once connected, all the hubs LED lights will cycle through the same colors simultaneously.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
const PoweredUP = require("..");
|
|
||||||
|
|
||||||
const poweredUP = new PoweredUP.PoweredUP();
|
|
||||||
poweredUP.scan(); // Start scanning for hubs
|
|
||||||
|
|
||||||
console.log("Looking for Hubs...");
|
|
||||||
|
|
||||||
poweredUP.on("discover", async (hub) => { // Wait to discover hubs
|
|
||||||
|
|
||||||
await hub.connect(); // Connect to hub
|
|
||||||
console.log(`Connected to ${hub.name}!`);
|
|
||||||
|
|
||||||
hub.on("attach", async (device) => {
|
|
||||||
|
|
||||||
console.log(`Attached device ${device.type} to ${device.port}`)
|
|
||||||
|
|
||||||
device.on("detach", () => {
|
|
||||||
console.log(`Detached device ${device.type} from ${device.port}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
if ((
|
|
||||||
device instanceof PoweredUP.SimpleMediumLinearMotor ||
|
|
||||||
device instanceof PoweredUP.TrainMotor ||
|
|
||||||
device instanceof PoweredUP.MediumLinearMotor ||
|
|
||||||
device instanceof PoweredUP.TechnicLargeLinearMotor ||
|
|
||||||
device instanceof PoweredUP.TechnicXLargeLinearMotor
|
|
||||||
) && hub.type === PoweredUP.Consts.HubType.WEDO2_SMART_HUB) {
|
|
||||||
const motor = device;
|
|
||||||
|
|
||||||
motor.on("rotate", (angle) => {
|
|
||||||
console.log(`Rotate ${angle}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
motor.setPower(40);
|
|
||||||
await hub.sleep(2000);
|
|
||||||
motor.setPower(0);
|
|
||||||
await hub.sleep(2000);
|
|
||||||
motor.setPower(-40);
|
|
||||||
await hub.sleep(2000);
|
|
||||||
motor.setPower(0);
|
|
||||||
await hub.sleep(2000);
|
|
||||||
motor.setPower(20);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((
|
|
||||||
// device instanceof PoweredUP.MoveHubMediumLinearMotor ||
|
|
||||||
device instanceof PoweredUP.MediumLinearMotor ||
|
|
||||||
device instanceof PoweredUP.TechnicLargeLinearMotor ||
|
|
||||||
device instanceof PoweredUP.TechnicXLargeLinearMotor
|
|
||||||
) && hub.type !== PoweredUP.Consts.HubType.WEDO2_SMART_HUB) {
|
|
||||||
const motor = device;
|
|
||||||
|
|
||||||
motor.on("rotate", (angle) => {
|
|
||||||
console.log(`Rotate ${angle}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
await motor.rotateByAngle(900, 50);
|
|
||||||
await motor.rotateByAngle(900, -50);
|
|
||||||
await motor.rotateByAngle(900, 50);
|
|
||||||
await motor.rotateByAngle(900, -50);
|
|
||||||
motor.setPower(20);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
device instanceof PoweredUP.SimpleMediumLinearMotor ||
|
|
||||||
device instanceof PoweredUP.TrainMotor
|
|
||||||
) {
|
|
||||||
const motor = device;
|
|
||||||
|
|
||||||
motor.setPower(20);
|
|
||||||
await hub.sleep(1000);
|
|
||||||
motor.setPower(40);
|
|
||||||
await hub.sleep(1000);
|
|
||||||
motor.setPower(60);
|
|
||||||
await hub.sleep(1000);
|
|
||||||
motor.setPower(80);
|
|
||||||
await hub.sleep(1000);
|
|
||||||
motor.setPower(100);
|
|
||||||
await hub.sleep(1000);
|
|
||||||
motor.setPower(60);
|
|
||||||
await hub.sleep(1000);
|
|
||||||
motor.setPower(20);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
device instanceof PoweredUP.Light
|
|
||||||
) {
|
|
||||||
const lights = device;
|
|
||||||
|
|
||||||
lights.setBrightness(100);
|
|
||||||
await hub.sleep(1000);
|
|
||||||
lights.setBrightness(0);
|
|
||||||
await hub.sleep(1000);
|
|
||||||
lights.setBrightness(100);
|
|
||||||
await hub.sleep(1000);
|
|
||||||
lights.setBrightness(0);
|
|
||||||
await hub.sleep(1000);
|
|
||||||
lights.setBrightness(100);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (device instanceof PoweredUP.ColorDistanceSensor) {
|
|
||||||
const sensor = device;
|
|
||||||
sensor.on("distance", (distance) => {
|
|
||||||
console.log(`Distance ${distance}`);
|
|
||||||
});
|
|
||||||
// sensor.on("color", (color) => {
|
|
||||||
// console.log(`Color ${color}`);
|
|
||||||
// });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (device instanceof PoweredUP.MotionSensor) {
|
|
||||||
const sensor = device;
|
|
||||||
sensor.on("distance", (distance) => {
|
|
||||||
console.log(`Distance ${distance}`);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (device instanceof PoweredUP.TiltSensor) {
|
|
||||||
const sensor = device;
|
|
||||||
sensor.on("tilt", (x, y) => {
|
|
||||||
console.log(`Tilt ${x} ${y}`);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
hub.on("disconnect", () => {
|
|
||||||
console.log("Hub disconnected");
|
|
||||||
})
|
|
||||||
|
|
||||||
});
|
|
@ -1,143 +0,0 @@
|
|||||||
const PoweredUP = require("..");
|
|
||||||
|
|
||||||
const poweredUP = new PoweredUP.PoweredUP()
|
|
||||||
|
|
||||||
const trains = [
|
|
||||||
{
|
|
||||||
name: "Maersk Intermodal",
|
|
||||||
color: PoweredUP.Consts.Color.LIGHT_BLUE,
|
|
||||||
hubs: [
|
|
||||||
{
|
|
||||||
name: "NK_Maersk",
|
|
||||||
ports: ["A"]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Horizon Express",
|
|
||||||
color: PoweredUP.Consts.Color.ORANGE,
|
|
||||||
hubs: [
|
|
||||||
{
|
|
||||||
name: "NK_Horizon_1",
|
|
||||||
ports: ["A"],
|
|
||||||
lights: ["B"],
|
|
||||||
reverse: ["A"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "NK_Horizon_2",
|
|
||||||
ports: ["A"],
|
|
||||||
lights: ["B"]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Emerald Night",
|
|
||||||
color: PoweredUP.Consts.Color.GREEN,
|
|
||||||
hubs: [
|
|
||||||
{
|
|
||||||
name: "NK_Emerald",
|
|
||||||
ports: ["A"],
|
|
||||||
lights: ["B"],
|
|
||||||
reverse: ["A"]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Metroliner",
|
|
||||||
color: PoweredUP.Consts.Color.WHITE,
|
|
||||||
hubs: [
|
|
||||||
{
|
|
||||||
name: "NK_Metroliner",
|
|
||||||
ports: ["A"],
|
|
||||||
lights: ["B"],
|
|
||||||
reverse: ["A"]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
|
|
||||||
poweredUP.on("discover", async (hub) => {
|
|
||||||
|
|
||||||
if (hub instanceof PoweredUP.PUPRemote) {
|
|
||||||
hub.on("button", (button, state) => {
|
|
||||||
|
|
||||||
if (button === "GREEN") {
|
|
||||||
if (state === PoweredUP.Consts.ButtonState.PRESSED) {
|
|
||||||
hub._currentTrain++;
|
|
||||||
if (hub._currentTrain >= trains.length) {
|
|
||||||
hub._currentTrain = 0;
|
|
||||||
}
|
|
||||||
hub.setLEDColor(trains[hub._currentTrain].color);
|
|
||||||
const batteryLevels = [];
|
|
||||||
const train = trains[hub._currentTrain];
|
|
||||||
for (let trainHub of train.hubs) {
|
|
||||||
if (trainHub._hub) {
|
|
||||||
batteryLevels.push(`${trainHub.name}: ${trainHub._hub.batteryLevel}%`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
console.log(`Switched active train on remote ${hub.name} to ${trains[hub._currentTrain].name} (${batteryLevels.join(", ")})`);
|
|
||||||
}
|
|
||||||
} else if ((button === "LEFT" || button === "RIGHT") && state !== PoweredUP.Consts.ButtonState.RELEASED) {
|
|
||||||
trains[hub._currentTrain]._speed = trains[hub._currentTrain]._speed || 0;
|
|
||||||
if (state === PoweredUP.Consts.ButtonState.UP) {
|
|
||||||
trains[hub._currentTrain]._speed += 10;
|
|
||||||
if (trains[hub._currentTrain]._speed > 100) {
|
|
||||||
trains[hub._currentTrain]._speed = 100;
|
|
||||||
}
|
|
||||||
} else if (state === PoweredUP.Consts.ButtonState.DOWN) {
|
|
||||||
trains[hub._currentTrain]._speed -= 10;
|
|
||||||
if (trains[hub._currentTrain]._speed < -100) {
|
|
||||||
trains[hub._currentTrain]._speed = -100;
|
|
||||||
}
|
|
||||||
} else if (state === PoweredUP.Consts.ButtonState.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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
console.log(`Set ${trains[hub._currentTrain].name} speed to ${trains[hub._currentTrain]._speed}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
await hub.connect();
|
|
||||||
hub._currentTrain = 2;
|
|
||||||
hub.setLEDColor(trains[hub._currentTrain].color);
|
|
||||||
console.log(`Connected to Powered UP remote (${hub.name})`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let train in trains) {
|
|
||||||
train = trains[train];
|
|
||||||
for (let trainHub in train.hubs) {
|
|
||||||
trainHub = train.hubs[trainHub];
|
|
||||||
if (hub.name === trainHub.name) {
|
|
||||||
hub.on("attach", (port, type) => {
|
|
||||||
if (trainHub.lights && trainHub.lights.indexOf(port) >= 0) {
|
|
||||||
hub.setLightBrightness(port, 100);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
hub.on("disconnect", () => {
|
|
||||||
console.log(`Disconnected from ${train.name} (${hub.name})`);
|
|
||||||
delete trainHub._hub;
|
|
||||||
})
|
|
||||||
await hub.connect();
|
|
||||||
trainHub._hub = hub;
|
|
||||||
hub.setLEDColor(train.color);
|
|
||||||
console.log(`Connected to ${train.name} (${hub.name})`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
poweredUP.scan(); // Start scanning for trains
|
|
||||||
console.log("Looking for trains...");
|
|
@ -1,59 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
@ -1,64 +0,0 @@
|
|||||||
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 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;
|
|
||||||
const TRAIN_MOTOR_PORT = "A";
|
|
||||||
|
|
||||||
let currentSpeed = 0;
|
|
||||||
|
|
||||||
console.log("Looking for trains...");
|
|
||||||
|
|
||||||
poweredUP.on("discover", async (hub) => { // Wait to discover a train
|
|
||||||
|
|
||||||
let moving = 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.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;
|
|
||||||
await hub.rampMotorSpeed(TRAIN_MOTOR_PORT, currentSpeed, 0, STOPPING_SPEED);
|
|
||||||
currentSpeed = 0;
|
|
||||||
console.log("Stopped");
|
|
||||||
await hub.sleep(STOP_DELAY);
|
|
||||||
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;
|
|
||||||
await hub.rampMotorSpeed(TRAIN_MOTOR_PORT, currentSpeed, 0, STOPPING_SPEED);
|
|
||||||
currentSpeed = 0;
|
|
||||||
console.log("Stopped");
|
|
||||||
await hub.sleep(STOP_DELAY);
|
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
@ -1,45 +0,0 @@
|
|||||||
/*
|
|
||||||
*
|
|
||||||
* 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 Vernie...");
|
|
||||||
|
|
||||||
const Modes = {
|
|
||||||
AVOIDING: 0,
|
|
||||||
MOVING: 1
|
|
||||||
}
|
|
||||||
|
|
||||||
poweredUP.on("discover", async (hub) => { // Wait to discover Vernie and Remote
|
|
||||||
|
|
||||||
const vernie = hub;
|
|
||||||
await vernie.connect();
|
|
||||||
console.log("Connected to Vernie!");
|
|
||||||
|
|
||||||
let mode = Modes.MOVING;
|
|
||||||
|
|
||||||
vernie.setMotorSpeed("AB", 50);
|
|
||||||
|
|
||||||
vernie.on("distance", async (port, distance) => {
|
|
||||||
|
|
||||||
if (distance < 180 && mode === Modes.MOVING) {
|
|
||||||
mode = Modes.AVOIDING;
|
|
||||||
await vernie.setMotorSpeed("AB", 0, 1000);
|
|
||||||
await vernie.setMotorSpeed("AB", -20, 1000);
|
|
||||||
await vernie.setMotorSpeed("AB", 0, 1000);
|
|
||||||
vernie.setMotorSpeed("A", 30, 500);
|
|
||||||
await vernie.setMotorSpeed("B", -30, 500);
|
|
||||||
await vernie.setMotorSpeed("AB", 0, 1000);
|
|
||||||
vernie.setMotorSpeed("AB", 50);
|
|
||||||
mode = Modes.MOVING;
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
@ -1,97 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
|
|
||||||
<head>
|
|
||||||
<title>Vernie / PlayStation DualShock 4 Remote Control</title>
|
|
||||||
<script src="https://cdn.jsdelivr.net/npm/node-poweredup@latest/dist/browser/poweredup.js"></script>
|
|
||||||
<link rel="stylesheet" type="text/css" href="./web_bluetooth.css" />
|
|
||||||
<script>
|
|
||||||
|
|
||||||
const poweredUP = new PoweredUP.PoweredUP();
|
|
||||||
|
|
||||||
let vernie = null;
|
|
||||||
let controller = null;
|
|
||||||
|
|
||||||
poweredUP.on("discover", async (hub) => { // Wait to discover Vernie
|
|
||||||
|
|
||||||
if (hub instanceof PoweredUP.BoostMoveHub) {
|
|
||||||
vernie = hub;
|
|
||||||
await vernie.connect();
|
|
||||||
vernie.setLEDColor(PoweredUP.Consts.Color.BLUE);
|
|
||||||
document.getElementById("color").style.backgroundColor = PoweredUP.Consts.ColorNames[PoweredUP.Consts.Color.BLUE];
|
|
||||||
console.log(`Connected to Vernie (${vernie.name})!`);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (vernie && controller) {
|
|
||||||
console.log("You're now ready to go!");
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
const scan = function () {
|
|
||||||
if (PoweredUP.isWebBluetooth) {
|
|
||||||
poweredUP.scan(); // Start scanning for hubs
|
|
||||||
} else {
|
|
||||||
alert("Your browser does not support the Web Bluetooth specification.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
window.addEventListener("gamepadconnected", (event) => {
|
|
||||||
|
|
||||||
controller = event.gamepad;
|
|
||||||
if (!(controller.id.indexOf("54c") >= 0 && controller.id.indexOf("5c4") >= 0)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("Connected to PlayStation DualShock 4!");
|
|
||||||
|
|
||||||
let start = null;
|
|
||||||
let previousLeft = 0;
|
|
||||||
let previousRight = 0;
|
|
||||||
|
|
||||||
const inputLoop = async () => {
|
|
||||||
controller = navigator.getGamepads()[0];
|
|
||||||
if (vernie) {
|
|
||||||
|
|
||||||
if (controller.buttons[14].pressed) { // Move the head left when left is pressed
|
|
||||||
await vernie.setMotorAngle("D", 35, -20);
|
|
||||||
} else if (controller.buttons[15].pressed) { // Move the head right when right is pressed
|
|
||||||
await vernie.setMotorAngle("D", 35, 20);
|
|
||||||
} else if (controller.buttons[17].pressed) { // Fire when the touchpad is pressed down
|
|
||||||
await vernie.setMotorAngle("D", 80, 20);
|
|
||||||
await vernie.setMotorAngle("D", 80, -20);
|
|
||||||
}
|
|
||||||
|
|
||||||
const left = Math.floor(50 * (controller.axes[1] * -1));
|
|
||||||
const right = Math.floor(50 * (controller.axes[3] * -1));
|
|
||||||
if (left !== previousLeft || right !== previousRight) {
|
|
||||||
vernie.setMotorSpeed("AB", [left, right]); // Move tracks based on analog stick input
|
|
||||||
previousLeft = left;
|
|
||||||
previousRight = right;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
start = requestAnimationFrame(inputLoop);
|
|
||||||
}
|
|
||||||
|
|
||||||
inputLoop();
|
|
||||||
|
|
||||||
if (vernie && controller) {
|
|
||||||
console.log("You're now ready to go!");
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
</script>
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
<div><h1>Vernie / PlayStation DualShock 4 Remote Control</h1></div>
|
|
||||||
<div>
|
|
||||||
<a class="button" href="#" onclick="scan();">Scan for Vernie</a>
|
|
||||||
</div>
|
|
||||||
<div id="current_color">
|
|
||||||
<span>Current Color: </span><div id="color"> </div>
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
|
|
||||||
</html>
|
|
@ -1,60 +0,0 @@
|
|||||||
/*
|
|
||||||
*
|
|
||||||
* This runs the train under our Christmas tree. It uses the 10254 Winter Holiday Train retrofitted with a Powered UP hub and train motor.
|
|
||||||
* It also uses a WeDo 2.0 hub with Powered UP distance sensor to detect the train approaching the station and slow it down.
|
|
||||||
*
|
|
||||||
* Note that if you want to use this yourself you don't need to use a WeDo 2.0 hub, you can use any hub that can accept a distance or color/distance sensor.
|
|
||||||
*
|
|
||||||
* The maximum speed of the train is set to a constant 50. A further improvement can be made by scaling the speed up according to the current battery voltage,
|
|
||||||
* so the speed doesn't slow as the battery starts dying.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
const PoweredUP = require("..");
|
|
||||||
|
|
||||||
const poweredUP = new PoweredUP.PoweredUP();
|
|
||||||
poweredUP.scan(); // Start scanning for hubs
|
|
||||||
|
|
||||||
console.log("Looking for Hubs...");
|
|
||||||
|
|
||||||
let train = null;
|
|
||||||
let sensor = null;
|
|
||||||
|
|
||||||
let ramping = false;
|
|
||||||
|
|
||||||
poweredUP.on("discover", async (hub) => { // Wait to discover hubs
|
|
||||||
|
|
||||||
if (hub.name === "NK_Winter_Train") {
|
|
||||||
await hub.connect(); // Connect to hub
|
|
||||||
console.log(`Connected to train!`);
|
|
||||||
train = hub;
|
|
||||||
} else if (hub.name === "NK_Winter_Sensor") {
|
|
||||||
await hub.connect(); // Connect to hub
|
|
||||||
console.log(`Connected to sensor!`);
|
|
||||||
sensor = hub;
|
|
||||||
|
|
||||||
sensor.on("distance", (_, distance) => {
|
|
||||||
if (distance < 5 && !ramping) {
|
|
||||||
await stopTrain();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (train && sensor) {
|
|
||||||
console.log("Train and sensor connected, starting!");
|
|
||||||
await startTrain();
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
const startTrain = async () => {
|
|
||||||
ramping = true;
|
|
||||||
await train.rampMotorSpeed("A", 0, 50, 2000);
|
|
||||||
ramping = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const stopTrain = async () => {
|
|
||||||
ramping = true;
|
|
||||||
await train.rampMotorSpeed("A", 50, 0, 2000);
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user