Skip to content

Instantly share code, notes, and snippets.

@BrianAdams
Last active December 17, 2015 00:09
Show Gist options
  • Save BrianAdams/5518276 to your computer and use it in GitHub Desktop.
Save BrianAdams/5518276 to your computer and use it in GitHub Desktop.
From: src/static/js/keypad.js Line 35-53 are the power settings in percent power based on the key pressed. If you change line 37 from .1 to .05 that would mean the 1-key would adjust the power down to 5%. At the low power settings, the calibration of the ESC become more important. We may also need to add code to actually add more power at first …
var KEYS = {
32: { // space (all-stop)
command: 'stop'
},
38: { // up (forward)
command: 'command',
position: 'throttle',
value: 1
},
40: { // down (aft)
command: 'command',
position: 'throttle',
value: -1
},
37: { // left (turn left)
command: 'command',
position: 'yaw',
value: -1
},
39: { // right (turn right)
command: 'command',
position: 'yaw',
value: 1
},
16: { // shift (lift up)
command: 'command',
position: 'lift',
value: 1
},
17: { //ctrl (lift down)
command: 'command',
position: 'lift',
value: -1
},
49: { //1 (power-1)
command: 'power',
value: .1
},
50: { //2 (power-2)
command: 'power',
value: .25
},
51: { //3 (power-3)
command: 'power',
value: .5
},
52: { //4 (power-4)
command: 'power',
value: .75
},
53: { //5 (power-5)
command: 'power',
value: 1
},
55: { //7 (vtrim)
command: 'vtrim',
value: 1
},
56: { //8 (vttrim)
command: 'vtrim',
value: -1
},
57: { //9 (ttrim -)
command: 'ttrim',
value: -1
},
48: { //0 (ttrim +)
command: 'ttrim',
value: 1
},
81: { //Q (tilt up)
command: 'tilt',
value: 1
},
65: { //A (tilt fwd)
command: 'tilt',
value: 0
},
90: { //Z (tilt down)
command: 'tilt',
value: -1
},
80: { //p (brightness up)
command: 'light',
value: 1
},
79: { //o (brightness down)
command: 'light',
value: -1
}
}
var KeyPad = function() {
var power = .5; //default to mid power
var vtrim = 0; //default to no trim
var ttrim = 0;
var kp = {};
var servoTiltHandler = function(value){};
var brightnessHandler = function(value){};
kp.bindServoTilt = function(callback){
servoTiltHandler=callback;
};
kp.bindBrightness = function(callback){
brightnessHandler=callback;
};
var vtrimHandler = function(value){
vtrim+=value;
positions.lift = (1/1000)*vtrim;
};
var ttrimHandler = function(value){
ttrim+=value;
positions.throttle = (1/1000)*ttrim;
};
var stopHandler = function(){
vtrim = 0;
ttrim = 0;
positions.throttle = 0;
positions.yaw = 0;
positions.lift = 0;
};
var positions = {
throttle: 0,
yaw: 0,
lift: 0
};
$(window).keydown(function(evt) {
var info = KEYS[evt.keyCode];
if (!info) return;
evt.preventDefault();
if(info.command=='command')
positions[info.position] = info.value*power;
else if(info.command=='tilt')
servoTiltHandler(info.value);
else if(info.command=='light')
brightnessHandler(info.value);
else if(info.command=='power')
power=info.value;
else if(info.command=='vtrim')
vtrimHandler(info.value);
else if (info.command=='ttrim')
ttrimHandler(info.value);
else if(info.command=='stop')
stopHandler();
});
$(window).keyup(function(evt) {
var info = KEYS[evt.keyCode];
if (!info) return;
evt.preventDefault();
if(info.command=='command'){
positions[info.position] = 0;
if (info.position == 'throttle')
positions.throttle = (1/1000)*ttrim;
if (info.position == 'lift')
positions.lift = (1/1000)*vtrim;
}
else if(info.command=='tilt')
servoTiltHandler(info.value);
});
kp.getPositions = function() {
return positions;
}
kp.isAvailable = function() {
return true;
}
return kp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment