Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rakeshpai committed Sep 12, 2017
0 parents commit d894417
Show file tree
Hide file tree
Showing 7 changed files with 1,373 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
AlphaBot
===

***Work in progress.***

This app controls the [AplhaBot](http://www.waveshare.com/wiki/AlphaBot).

To install the app:
* `git clone` or download and unzip this repo.
* `cd` to the checkout, and type in `npm install`.

To run the app:

```
sudo npm start
```
7 changes: 7 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
// Duration between actions in the bot. Sensor polling interval, if you like.
// Too low, and you don't have enough sensor data to act
// Too high, and you react too sluggishly
// 50ms is a good number.
clock: 50, // milliseconds
}
48 changes: 48 additions & 0 deletions hardware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// This is a duplex stream that pipes out data from the sensors
// every config.clock milliseconds.
// Motor commands can be piped into this stream, which then get sent
// to the motors.

const { Duplex } = require('stream');
const hal = require('alphabot-hal')();
const config = require('./config');

const hardware = new Duplex({
readableObjectMode: true,
writableObjectMode: true,
write(chunk, encoding, done) {
hal.wheels.left((chunk.wheels && chunk.wheels.left) || 0);
hal.wheels.right((chunk.wheels && chunk.wheels.right) || 0);

done();
},
read() {}
});

// Buffer up tick counts
var ticks = { left: 0, right: 0 };
hal.on('leftTick', () => ticks.left++);
hal.on('rightTick', () => ticks.right++);

// Write to the stream every config.clock milliseconds
setInterval(() => {
hardware.push({
ticks,
obstacleSensors: {
left: hal.obstacleSensors.left(),
right: hal.obstacleSensors.right()
},
batteryVoltage: hal.batteryVoltage(),
ts: Date.now()
});

ticks.left = ticks.right = 0;
}, config.clock);

process.on('exit', () => {
// Stop motors when exiting
hal.wheels.left(0);
hal.wheels.right(0);
});

module.exports = hardware;
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const hardware = require('./hardware');
const { obj: map } = require('through2-map');

// Give the robot a placeholder brain.
// Act braindead, ignore sensors, don't move
const brains = map(sensors => ({
wheels: { left: 0, right: 0 }
}))

hardware.pipe(brains).pipe(hardware);
Loading

0 comments on commit d894417

Please sign in to comment.