Added train reverse example

This commit is contained in:
Nathan Kunicki 2018-07-26 09:09:59 +01:00
parent af3109ac2c
commit 9cd657af81
2 changed files with 34 additions and 83 deletions

34
examples/train_reverse.js Normal file
View File

@ -0,0 +1,34 @@
const LPF2 = require("..").LPF2;
const lpf2 = new LPF2();
lpf2.scan(); // Start scanning for hubs
console.log("Looking for Hubs...");
lpf2.on("discover", async (hub) => { // Wait to discover hubs
await hub.connect(); // Connect to hub
console.log("Connected to Hub!");
hub.on("color", (port, color) => {
if (color === LPF2.Consts.Colors.YELLOW) {
hub.setMotorSpeed("A", 0);
await hub.wait(2000);
hub.setMotorSpeed("A", -40);
} else if (color === LPF2.Consts.Colors.RED) {
hub.setMotorSpeed("A", 0);
await hub.wait(2000);
hub.setMotorSpeed("A", 40);
}
});
await hub.wait(2000);
hub.setMotorSpeed("A", 40);
});

View File

@ -1,83 +0,0 @@
const LPF2 = require("..").LPF2;
const lpf2 = new LPF2();
lpf2.scan(); // Start scanning for Vernie
console.log("Looking for Vernie (Please turn on the Hub now)...");
lpf2.on("discover", async (vernie) => { // Wait to discover Vernie
let running = false;
lpf2.stop(); // Stop scanning for Vernie (We've just found him!)
await vernie.connect(); // Connect to Vernie
console.log("Connected to Vernie!");
// Waiting for a hand wave
vernie.on("distance", async (port, distance) => {
if (running || distance > 100) {
return;
}
running = true;
await vernie.sleep(500);
// Shake head
await vernie.setMotorAngle("D", 40, 20);
await vernie.setMotorAngle("D", 80, -20);
await vernie.setMotorAngle("D", 40, 20);
await vernie.sleep(500);
// Turn right
await vernie.wait([
vernie.setMotorAngle("A", 50, 10),
vernie.setMotorAngle("B", 50, -10)
]);
// Shake head
await vernie.setMotorAngle("D", 40, 20);
await vernie.sleep(500);
// Turn left
await vernie.wait([
vernie.setMotorAngle("A", 100, -10),
vernie.setMotorAngle("B", 100, 10)
]);
// Shake head
await vernie.setMotorAngle("D", 80, -20);
await vernie.sleep(500);
// Turn right
await vernie.wait([
vernie.setMotorAngle("A", 50, 10),
vernie.setMotorAngle("B", 50, -10)
]);
// Shake head
await vernie.setMotorAngle("D", 80, 20);
await vernie.setMotorAngle("D", 40, -20);
await vernie.sleep(500);
// Move forward
await vernie.setMotorAngle("AB", 200, 10);
// FIRE!
await vernie.setMotorAngle("D", 80, 20);
await vernie.setMotorAngle("D", 80, -20);
// Move back
await vernie.setMotorAngle("AB", 200, -10);
running = false;
});
});