Skip to content

Commit

Permalink
v1.3.1
Browse files Browse the repository at this point in the history
Add robot.close command.
Change how create.prompt functions, updated examples.
  • Loading branch information
Pecacheu committed Jun 15, 2019
1 parent 9971e3c commit cf5d078
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 46 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ Check it out [on YouTube](http://youtu.be/lE6Q39KX6Ag)!

- **create.ports(callback)**

Lists all ports available on the computer. The only parameter to the callback function is an array of ports. If no ports were found, the array will be empty.
Lists all ports available on the computer. The only parameter to *callback* is an array of ports. If no ports were found, the array will be empty.

- **create.prompt(callback)**

Shows a dialogue listing available ports and asks the user to select one. While in the dialogue, the user can quit the application by typing `quit` or `exit`.
Shows a dialogue listing available ports and asks the user to select one, before automatically calling **create.open**. While in the dialogue, the user can quit the application by typing `quit` or `exit`.

- **robot = create.open(port, callback=null)**
- **create.open(port, callback=null)**

Opens the port to a robot object. The optional callback will be called when the robot is ready to receive commands. The callback's parameter will be the robot object (which is also returned by the function).
Opens the port to a robot object. An optional *callback* will be called when the robot is ready to receive commands. The callback's parameter will be the new *Robot* object.

Optionally, the *port* argument can be Duplex node stream instead of a port, allowing you to use a custom library to communicate with the robot. If you need the Roomba's default serial settings, use the macro **create.serial**, which is initialized to `{baudRate:115200, dataBits:8, parity:'none', stopBits:1, flowControl:0}`

- **create.debug** and **create.inputMode**

Expand All @@ -34,7 +36,8 @@ For all functions with parameters, setting any parameter to `null` will use it's
- **robot.stop()** Stops communication with the robot. You'll have to send a `start` again to resume communication.
- **robot.safe()** Sets the mode to *SAFE*. You have complete control of the robot unless a safety sensor is triggered.
- **robot.full()** Sets the mode to *FULL*. You have complete control of the robot even if safety sensors are triggered.
- **robot.power()** This command fully powers down Roomba. Communication is no longer possible.
- **robot.power()** This command powers down the Roomba, however it does not close the serial connection.
- **robot.close()** Close the serial connection to the robot. It is recommended to put the robot back in *PASSIVE* mode first.

**Note:** *SAFE* mode is identical to *FULL* mode, with the exception that if a safety sensor (such as a cliff sensor) is triggered, the robot reverts back to *PASSIVE* mode.

Expand Down
2 changes: 1 addition & 1 deletion examples/dock.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const create = require('create2');
let robot, run=1, drRun=0, drAngle=0, angle=0;

function start() {
create.prompt((p) => {create.open(p,main)});
create.prompt(main);
}

//Main Program:
Expand Down
2 changes: 1 addition & 1 deletion examples/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let robot, input = 0;
function start() {
create.debug = true; //Data will be logged to terminal.
create.inputMode = 0; //Only relevant when debug is on.
create.prompt((p) => {create.open(p,main)});
create.prompt(main);
}

//todo dock.js: replace 'var' with 'let'; use '=>'
Expand Down
65 changes: 27 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

"use strict";

const chalk = require('chalk'), Serial = require('serialport');
const chalk = require('chalk'), Serial = require('serialport'), stream = require('stream');
exports.serial = {baudRate:115200, dataBits:8, parity:'none', stopBits:1, flowControl:0};

//----------- Helpful Functions -----------

Expand Down Expand Up @@ -53,19 +54,17 @@ exports.prompt = (cb) => {
console.log(chalk.yellow("-----------------------------------\n"));
console.log(chalk.cyan("Please enter the port you want to use:"));
//Wait for user input:
function onPortSelectInput(newPort) {
newPort = newPort.replace(/\n/g, ""); newPort = newPort.replace(/\r/g, "");
let portExists=0;
for(let i=0; i < ports.length; i++) if(newPort == ports[i].comName) { portExists=1; break; }
if(!portExists && Number(newPort) && ports[Number(newPort)-1]) {
newPort = ports[Number(newPort)-1].comName; portExists=1;
function onPortSelectInput(port) {
port=port.replace(/\n/g, "").replace(/\r/g, ""); let portExists=0;
for(let i=0; i < ports.length; i++) if(port == ports[i].comName) { portExists=1; break; }
if(!portExists && Number(port) && ports[Number(port)-1]) {
port = ports[Number(port)-1].comName; portExists=1;
}
if(portExists) {
console.log(chalk.bgGreen.black("Listening on port \""+newPort+"\""));
process.stdin.removeListener('data', onPortSelectInput);
console.log(); cb(newPort);
console.log(chalk.bgGreen.black("Listening on port \""+port+"\"")+"\n");
process.stdin.removeListener('data', onPortSelectInput); exports.open(port,cb);
} else {
console.log(chalk.bgRed.black("Port \""+newPort+"\" does not exist!"));
console.log(chalk.bgRed.black("Port \""+port+"\" does not exist!"));
}
}
process.stdin.resume();
Expand All @@ -82,36 +81,18 @@ exports.prompt = (cb) => {
});
}

//Init iRobot Serial:
function initSerial(r, port, cb) {
r.port = new Serial(port, {
baudRate:115200, dataBits:8, parity:'none', stopBits:1, flowControl:0
});
r.port.once('open', () => {
//Send Start Command:
r.write(128); setSensorRead(1,r);
//Listen to Incoming Data:
r.port.on('data', (data) => {
//Combine Previous Data:
if(r.inBuf) r.inBuf = Buffer.concat([r.inBuf,data]); else r.inBuf = data;
//Data Parsing Timer:
if(r.inTmr) clearTimeout(r.inTmr); r.inTmr = setTimeout(() => {
if(r.dataParser && r.inBuf && r.inBuf.length >= 80) { r.dataParser(r.inBuf); r.inBuf = r.inTmr = null; }
if(exports.debug && r.inBuf) console.log(chalk.bold.green("Packet Miss ["+r.inBuf.length+"]"));
},8);
});
if(cb) cb(r);
});
}

//Open iRobot Serial Port:
exports.open = (port, cb) => {
let r = new Robot(); initSerial(r,port,cb); return r;
let r = new Robot();
if(typeof port == 'string') r.port = new Serial(port, exports.serial);
else if(port instanceof stream.Duplex) r.port = port; else { cb(); return; }
r.port.once('open', () => { r.start(); r.port.on('data', r.read); if(cb) cb(r); });
r.port.on('error', () => {});
}

function Robot() {
this.data = {}; this.delta = {}; this.on = {};
}
function Robot() { this.data = {}, this.delta = {}, this.on = {}; }
Object.defineProperty(Robot.prototype,'readEnable',{get(){return r._rEn},set(e){setSensorRead(e,this)}});
Robot.prototype.close = function() { setSensorRead(0,this); this.port.close(); }

Robot.prototype.write = function() {
for(let i=0,l=arguments.length,a; i<l; i++) {
Expand All @@ -124,7 +105,15 @@ Robot.prototype.write = function() {
}
}

Object.defineProperty(Robot.prototype,'readEnable',{get(){return r._rEn},set(e){setSensorRead(e,this)}});
Robot.prototype.read = function(data) {
//Combine Previous Data:
let r=this; if(r.inBuf) r.inBuf = Buffer.concat([r.inBuf,data]); else r.inBuf = data;
//Data Parsing Timer:
if(r.inTmr) clearTimeout(r.inTmr); r.inTmr = setTimeout(() => {
if(r.dataParser && r.inBuf && r.inBuf.length >= 80) { r.dataParser(r.inBuf); r.inBuf = r.inTmr = null; }
if(exports.debug && r.inBuf) console.log(chalk.bold.green("Packet Miss ["+r.inBuf.length+"]"));
},8);
}

//----------- Create 2 Status Control -----------

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create2",
"version": "1.3.0",
"version": "1.3.1",
"description": "iRobot Create 2 Open Interface for Node.js",
"main": "index.js",
"repository": "git://github.com/pecacheu/create2.git",
Expand Down

0 comments on commit cf5d078

Please sign in to comment.