Skip to content

Commit

Permalink
UI stuff, added manual control
Browse files Browse the repository at this point in the history
  • Loading branch information
rakeshpai committed Sep 21, 2017
1 parent a3a2aaa commit 72eec6f
Show file tree
Hide file tree
Showing 16 changed files with 294 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ AlphaBot

***Work in progress.***

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

More documentation is [on the wiki](https://github.com/rakeshpai/alphabot/wiki).
29 changes: 29 additions & 0 deletions brains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { bus } = require('./utils');
const { obj: map } = require('through2-map');
const { notify } = require('./ui-server');
const powerOff = require('power-off');
const manualControl = require('./manual-control');

let mode = 'manual';
notify('mode', mode);

bus.on('setMode', value => {
mode = value;
notify('mode', value);
});

let shutdownInitiated = false;
const shutdown = () => {
if(shutdownInitiated) return;

powerOff(() => {});
shutdownInitiated = true;
}

module.exports = map(sensors => {
if(sensors.raw.batteryVoltage < 6) shutdown();

if(mode === 'manual') return manualControl(sensors);

return { speed: 0, steering: 0 };
});
6 changes: 3 additions & 3 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ module.exports = {
// Getting this right is kinda important, so it's worth fiddling with
// these values. Parameters are easy to measure using just a ruler.
// All dimensions in mm.
wheelbase: 130, // distance between the centres of the wheels
wheelbase: 140, // distance between the centres of the wheels

wheels: {
left: { diameter: 66 },
right: { diameter: 66 },
left: { diameter: 67 },
right: { diameter: 67 },
},

// Slew rate is the maximum allowable change in the mtor PWM
Expand Down
8 changes: 2 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
const hardware = require('./hardware');
const { obj: map } = require('through2-map');
const interpretSensors = require('./sensors');
const brains = require('./brains');
const setMotorSpeeds = require('./motor-command');
require('./ui-server');

const brains = map(sensors => {
return { speed: 0, steering: 0 };
});
require('./ui-server'); // Not strictly needed, as it's automatically init'd.

hardware
.pipe(interpretSensors)
Expand Down
41 changes: 41 additions & 0 deletions manual-control.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { bus } = require('./utils');
const config = require('./config');
const lirc = require('./utils/lirc_receiver');

const remoteKeysToInput = {
'KEY_CHANNEL': 'forward',
'KEY_PREVIOUS': 'left',
'KEY_PLAYPAUSE': 'right',
'KEY_NEXT': 'reverse'
}

let input = null;
let inputTimer = null;

const setInput = newInput => {
if(inputTimer) clearTimeout(inputTimer);

input = newInput;
inputTimer = setTimeout(() => input = null, 150);
}

lirc.on('keypress', ({ key }) => {
if(key in remoteKeysToInput) setInput(remoteKeysToInput[key]);
});

bus.on('motorCommand', setInput);

module.exports = sensors => {
let command = { speed: 0, steering: 0 };

if(input) {
switch(input) {
case 'forward': command.speed = config.topSpeed; break;
case 'left': command.steering = 15; break;
case 'right': command.steering = -15; break;
case 'reverse': command.speed = -1 * config.topSpeed; break;
}
}

return command;
}
4 changes: 2 additions & 2 deletions motor-command.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { obj: map } = require('through2-map');
const config = require('./config');
const { log } = require('./ui-server');
const { notify } = require('./ui-server');

const previous = { left: 0, right: 0 };

Expand All @@ -23,7 +23,7 @@ module.exports = map(({speed, steering, ts}) => {
previous.left = Math.round(slewedSpeed(previous.left, leftSpeed));
previous.right = Math.round(slewedSpeed(previous.right, rightSpeed));

log('motors', previous);
notify('motors', previous);

return { wheels: previous, ts };
});
81 changes: 81 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@
"start": "concurrently \"nodemon index.js\" \"PORT=80 react-scripts start\""
},
"nodemonConfig": {
"ignore": [ "public", "build", "src" ]
"ignore": [
"public",
"build",
"src"
]
},
"author": "rakeshpai",
"license": "MIT",
"dependencies": {
"alphabot-hal": "^1.0.1",
"concurrently": "^3.5.0",
"glamor": "^2.20.40",
"mousetrap": "^1.6.1",
"nodemon": "^1.12.0",
"power-off": "^1.1.2",
"react-chartist": "^0.13.0",
"react-scripts": "^1.0.13",
"reconnecting-websocket": "^3.2.1",
"split2": "^2.1.1",
"through2-map": "^3.0.0",
"ws": "^3.1.0"
}
Expand Down
4 changes: 2 additions & 2 deletions sensors/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { obj: map } = require('through2-map');
const odometry = require('./odometry');
const { log } = require('../ui-server');
const { notify } = require('../ui-server');

module.exports = map(sensors => {
const processed = {
Expand All @@ -9,7 +9,7 @@ module.exports = map(sensors => {
ts: sensors.ts
};

log('sensed', processed);
notify('sensed', processed);

return processed;
});
10 changes: 0 additions & 10 deletions src/App.js

This file was deleted.

7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React from 'react';
import { render } from 'react-dom';
import App from './App';
import App from './components/App';
import store from './store';
import './keyboard-shortcuts';

// import registerServiceWorker from './registerServiceWorker';
// registerServiceWorker();

const renderApp = () => render(<App />, document.getElementById('root'));

renderApp();
store.onChange = renderApp;

// registerServiceWorker();
34 changes: 34 additions & 0 deletions src/keyboard-shortcuts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import store from './store';
import mousetrap from 'mousetrap';

const debounce = (time, fn) => {
let lastCallTime;

return () => {
if(!lastCallTime || Date.now() - lastCallTime > time) {
lastCallTime = Date.now();
fn();
}
}
}

// Bind keyboard shortcuts
const directionKeys = {
w: 'forward',
a: 'left',
s: 'reverse',
d: 'right',
up: 'forward',
down: 'reverse',
left: 'left',
right: 'right'
};

Object.keys(directionKeys).forEach((key, i, obj) => {
mousetrap.bind(key, debounce( 50, () => {
store.motorCommand(directionKeys[key]);
}));
});

mousetrap.bind('r', debounce(500, store.resetAll));
mousetrap.bind('m', debounce(500, () => store.setMode(store.mode==='manual'?'auto':'manual')));
Loading

0 comments on commit 72eec6f

Please sign in to comment.