Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
d453fe5
Basic implementation of BasicMotor types
Dec 6, 2019
4af1d3d
Added autosubscribe when adding eventlisteners
Dec 7, 2019
8ffc60b
attachedDevices now a map
Dec 7, 2019
93a1369
Basic code for passing messages through to the device
Dec 9, 2019
d382c3d
Basic ColorDistanceSensor and TachoMotor handling
Dec 9, 2019
a02249f
Implemented basic lights, device busy-ness
Dec 9, 2019
f9cea9a
Yay, port object is no longer a thing
Dec 9, 2019
7fdc754
Start of hub refactor
Dec 9, 2019
5c03e5c
More refactoring
Dec 9, 2019
1793eb0
Removing commented code
Dec 9, 2019
6beeda5
Implemented methods for retrieving hubs and devices
Dec 11, 2019
6a7c489
Added more motor types
nathankellenicki Dec 15, 2019
198d637
Renamed devices
nathankellenicki Dec 16, 2019
81cbdac
Merge branch 'feature/devices' of github.com:nathankellenicki/node-po…
Dec 16, 2019
31ac932
Motion and tilt sensor
Dec 16, 2019
2a67242
Removed some commented code from hubs
Dec 16, 2019
023b141
Better handling of device initialisation, implemented more modes
Dec 17, 2019
37be7f8
Rename
Dec 17, 2019
406e9e9
Rename again
Dec 17, 2019
225f331
Better directory structure
Dec 17, 2019
df70d8a
Renamed devices
nathankellenicki Dec 17, 2019
04b79ad
Renamed devices
nathankellenicki Dec 17, 2019
388bce1
Mapping of events to modes
Dec 17, 2019
36c34a9
Exported device ModeMap
Dec 17, 2019
ffd3cce
Exported hub PortMap
Dec 17, 2019
364089b
Power to speed when rotating by angle
Dec 18, 2019
de4feb0
Added functions to wait for port attachments
Dec 18, 2019
c1b8697
VoltageSensor
Dec 18, 2019
2e06a17
CurrentSensor
Dec 18, 2019
6d23179
PUPRemoteButton
Dec 18, 2019
195c0d5
HubLED and MoveHubTiltSensor
Dec 18, 2019
6e77d69
Control+ (Technic Medium Hub) sensors
Dec 19, 2019
a5a9b2e
Control+ (Technic Medium Hub) devices, renamed hubs to be more inline…
Dec 19, 2019
705fbd3
writeDirect function on device
Dec 19, 2019
bc644c8
Duplo motors and sensors
Dec 19, 2019
0350176
Bug fixes, hubs now wait for hub properties before being connected
Dec 19, 2019
2dabedd
More WeDo 2.0 devices
Dec 20, 2019
579f823
Device events now emit on hub too
nathankellenicki Dec 23, 2019
9113d22
Named internal ports on hubs
nathankellenicki Dec 23, 2019
c5146bd
Renamed button to remoteButton on RC
nathankellenicki Dec 24, 2019
290f469
Device events emit value objects
nathankellenicki Dec 27, 2019
077a737
WeDo 2.0 unsubscribing
Jan 6, 2020
9f087e3
Merge branch 'feature/devices' of github.com:nathankellenicki/node-po…
nathankellenicki Jan 7, 2020
673f5ff
Start of port combining
Jan 8, 2020
afa7c74
Merge branch 'feature/devices' of github.com:nathankellenicki/node-po…
Jan 8, 2020
875293a
Fixed linting errors and removed namespaces
Jan 8, 2020
bf721ed
Virtual port attachment and detachment
Jan 8, 2020
b43b59e
Virtual ports tested
Jan 9, 2020
f7e86d3
Added setSpeed, virtual port multiparam options
Jan 9, 2020
74be8a7
Initial absolute motor support
Jan 10, 2020
56eaa7c
Fixed parts of absolute angle
nathankellenicki Jan 10, 2020
63f1c10
Servo calibration and reset
Jan 10, 2020
5fde49c
Adding power and brightness ramping back in
Jan 13, 2020
ea20b1b
Can not retrieve human readable device type from devices. Updated web…
Jan 13, 2020
dfd22b1
Can not retrieve human readable device type from devices. Updated web…
Jan 13, 2020
0c3ff8b
Devices now cache notified values for easy retrieval without events
Jan 13, 2020
225b60f
Devices now cache notified values for easy retrieval without events
Jan 13, 2020
d329ce6
Added support for Spike Prime motors
Jan 13, 2020
ef31567
Added support for Technic Color Sensor and Technic Distance Sensor (S…
Jan 15, 2020
407acce
Added Technic Force Sensor (Spike Prime)
Jan 15, 2020
c6f8015
Added example of slow speed train control with a Medium Linear Motor …
Jan 29, 2020
e34eea9
Removed examples with old API
Jan 29, 2020
c0009f8
Updated vernie sample, added braking style
Feb 7, 2020
e721e0f
Updated docs, readme, version
nathankellenicki Feb 10, 2020
a34c422
Added announement to readme
nathankellenicki Feb 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions examples/new_device_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
*
* 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}`)

if (device instanceof PoweredUP.ControlPlusLargeMotor) {
const motor = device;

motor.on("rotate", (angle) => {
console.log(`Rotate ${angle}`);
});

await motor.rotateByAngle(9000, 50);
await motor.rotateByAngle(9000, -50);
await motor.rotateByAngle(9000, 50);
await motor.rotateByAngle(9000, -50);
motor.power(100);
}

if (device instanceof PoweredUP.ColorDistanceSensor) {
const sensor = device;
sensor.on("distance", (distance) => { // Adding an event handler for distance automatically subscribes to distance notifications
console.log(`Distance ${distance}`);
});
sensor.on("color", (color) => {
console.log(`Color ${color}`);
});
}

device.on("detach", () => {
console.log(`Detached device ${device.type} from ${device.port}`)
})

});

hub.on("disconnect", () => {
console.log("Hub disconnected");
})

});
60 changes: 60 additions & 0 deletions examples/winter_village_train.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
*
* 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();
}
});
Copy link
Contributor

@aileo aileo Dec 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This listen a device (sensor) event on the hub, does it mean we support device events at the hub level ?

If it does not call the same method on the device, it does not subscribe to the correct mode.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I shouldn’t have committed that! :) It’s for my house - designed to run on the old code (master). But I haven’t though more about supporting that as of now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes a nice example !

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the new waitForDeviceByType should allow you to migrate this to the new paradigm

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - it was an idea that only occurred to me last night while thinking about other stuff, but it solves a problem that many people have, ie. not knowing when to call getDeviceByType without watching for an attach event, by which point it's provided to you anyway. It just makes it easier to maintain a more sequential coding pattern, which is easier for a lot of people.

Copy link
Owner Author

@nathankellenicki nathankellenicki Dec 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also how I was thinking this could support internal motors/functions on a hub rather on device. ie. If a boost internal motor has a method setSpeed(speed), I can create a lightweight wrapper on the hub called setSpeed(port, speed), which simply passes through the values to the method on the device after using waitForDeviceByPort.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome


}

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);
}
41 changes: 41 additions & 0 deletions src/basicmotor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Device } from "./device";
import { Hub } from "./hub";

import * as Consts from "./consts";

import { mapSpeed } from "./utils";

export class BasicMotor extends Device {


constructor (hub: Hub, portId: number, type: number = Consts.DeviceType.UNKNOWN) {
super(hub, portId, type);
}


/**
* Set the motor speed.
* @method BasicMotor#power
* @param {number} power For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
* @returns {Promise} Resolved upon successful completion of command.
*/
public power (power: number) {
return new Promise((resolve) => {
const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, mapSpeed(power)]);
this.send(data);
return resolve();
});
}


/**
* Fully (hard) stop the motor.
* @method BasicMotor#brake
* @returns {Promise} Resolved upon successful completion of command.
*/
public brake () {
return this.power(127);
}


}
Loading