Skip to content

Instantly share code, notes, and snippets.

@suhajdab
Created July 21, 2015 13:30
Show Gist options
  • Save suhajdab/1682e6e1e4f3b4080bf6 to your computer and use it in GitHub Desktop.
Save suhajdab/1682e6e1e4f3b4080bf6 to your computer and use it in GitHub Desktop.
var api = require('./API.js');
var route = ['r','','l','f','f','f','f','','','','f','f','f','f','f'],
move = '',
env = [],
lastEnv = [];
function next() {
move = route.shift();
}
function followWall(decision) {
if (lastEnv[1] == 1 && env[1] > 1) decision = 'r';
else if (lastEnv[3] == 1 && env[3] > 1) decision = 'l';
console.log('followWall: ' + decision);
return decision;
}
function avoidObstacle(decision) {
if (env[0] < 2) {
// block ahead
if (env[1] > 1) decision = 'r';
else if (env[3] > 1) decision = 'l';
else decision = 'b';
}
console.log('avoid: ' + decision);
return decision;
}
next();
exports.update = function() {
if (api.identifyTarget()) {
api.fireCannon();
} else {
env = [api.lidarFront(),api.lidarRight(),api.lidarBack(),api.lidarLeft()];
var decision = move;
decision = avoidObstacle(decision);
if (move == 'f') decision = followWall(decision);
switch (decision) {
case 'r':
api.turnRight();
next();
break;
case 'l':
api.turnLeft();
next();
break;
case 'b':
api.moveBackward();
route.unshift('b');
break;
default:
api.moveForward();
}
lastEnv = env.slice();
}
};
@suhajdab
Copy link
Author

rev 1: 1251

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment