Skip to content

Instantly share code, notes, and snippets.

@bitmorse
Created January 25, 2016 14:23
Show Gist options
  • Save bitmorse/fab766fed89d3df2d35d to your computer and use it in GitHub Desktop.
Save bitmorse/fab766fed89d3df2d35d to your computer and use it in GitHub Desktop.
octanis1_rover_ds3_controller
"use strict";
var GamePad = require( 'node-gamepad' );
var Cylon = require('cylon');
var controller = new GamePad( 'ps3/dualshock3' );
var rover_state = {
control: {
current_controlled_strut: 1
}
};
var controller_command_map = [
{'event':'dpadUp:press', 'commands':['motf']},
{'event':'dpadDown:press', 'commands':['motb']},
{'event':'dpadLeft:press', 'commands':['motl']},
{'event':'dpadRight:press', 'commands':['motr']},
{'event':'dpadUp:release', 'commands':['motx']},
{'event':'dpadDown:release', 'commands':['motx']},
{'event':'dpadLeft:release', 'commands':['motx']},
{'event':'dpadRight:release', 'commands':['motx']},
{'event':'select:press', 'commands':['motx','moth1','moth2','moth3','moth4']},
{'event':'start:press', 'commands':['motn']},
{'event':'r1:press', 'commands':[ function(){ RoverSelectStrut(1); } ]},
{'event':'r2:press', 'commands':[ function(){ RoverSelectStrut(2); } ]},
{'event':'l1:press', 'commands':[ function(){ RoverSelectStrut(3); } ]},
{'event':'l2:press', 'commands':[ function(){ RoverSelectStrut(4); } ]},
{'event':'triangle:press', 'commands':[ function(my){ RoverWriteToConsole(my, 'motu'+RoverGetStrut()) }] },
{'event':'x:press', 'commands':[ function(my){ RoverWriteToConsole(my, 'motd'+RoverGetStrut()) }] },
{'event':'circle:press', 'commands':[ function(my){ RoverWriteToConsole(my, 'moth'+RoverGetStrut()) }] },
]
//connect the dualshock3 controller
controller.connect();
//start robot handler
Cylon.robot({
connections: {
bluetooth: { adaptor: 'ble', uuid: 'cbef787e50934150ace41ce6385efabf' }
},
devices: {
octanis1_rover_ble: {
driver: "ble-characteristic",
serviceId: "ffe0", characteristicId: "ffe1",
connection: "bluetooth"
}
},
work: function(my) {
RoverMapCommands(my, controller_command_map);
RoverHandleBLENotifications(my.octanis1_rover_ble);
}
}).start();
//rover specifics
function RoverHandleBLENotifications(ble_device){
ble_device.notifyCharacteristic(function(err, data) {
if (err) { return console.error("NotifyError: ", err); }
console.log("NotifyData: ", data);
});
}
function RoverMapCommands(my, controller_command_map){
controller_command_map.forEach(function(map){
controller.on(map.event, function(){
map.commands.forEach(function(cmd){
if(typeof cmd === "function"){
cmd(my);
}else{
RoverWriteToConsole(my, cmd);
}
});
});
});
}
function RoverWriteToConsole(my, command_string){
console.log("Sent: "+command_string);
my.octanis1_rover_ble.writeCharacteristic(command_string, function(err, data) {
if (err) { return console.error("Error: ", err); }
console.log("Data: ", data);
});
}
function RoverSelectStrut(strutNum){
console.log("selected strut "+strutNum+" for control");
rover_state.control.current_controlled_strut = strutNum;
}
function RoverGetStrut(){
return rover_state.control.current_controlled_strut;
}
controller.on('battery:change', function (value) {
console.log("DS3 Controller Battery: " + value);
});
@bitmorse
Copy link
Author

make sure to have a packages.json filled with
{
"name":"sandbox",
"dependencies": {
"cylon": "",
"node-gamepad": "0.1.14",
"cylon-ble": "
"
}}

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