Created
July 29, 2014 20:57
-
-
Save wjessop/f1034977d7c833178b17 to your computer and use it in GitHub Desktop.
tankdrive code from http://www.goodrobot.com/en/2009/09/tank-drive-via-joystick-control/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tankdrive function, that converts the x and y values | |
// to store as left/right | |
function tankdrive(x, y) { | |
// first Compute the angle in deg | |
// First hypotenuse | |
var z = Math.sqrt(x*x + y*y); | |
// angle in radians | |
rad = Math.acos(Math.abs(x)/z); | |
// and in degrees | |
angle = rad*180/Math.PI; | |
// Now angle indicates the measure of turn | |
// Along a straight line, with an angle o, the turn co-efficient is same | |
// this applies for angles between 0-90, with angle 0 the co-eff is -1 | |
// with angle 45, the co-efficient is 0 and with angle 90, it is 1 | |
var tcoeff = -1 + (angle/90)*2; | |
var turn = tcoeff * Math.abs(Math.abs(y) - Math.abs(x)); | |
turn = Math.round(turn*100)/100; | |
// And max of y or x is the movement | |
var move = Math.max(Math.abs(y),Math.abs(x)); | |
// First and third quadrant | |
if( (x >= 0 && y >= 0) || (x < 0 && y < 0) ) { | |
left = move; | |
right = turn; | |
} else { | |
right = move; | |
left = turn; | |
} | |
// Reverse polarity | |
if(y < 0) { | |
left = 0 - left; | |
right = 0 - right; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Tankdrive code converted to Ruby | |
def motor_speeds(x, y) | |
return 0,0 if x == 0 && y == 0 | |
# first Compute the angle in deg | |
# First hypotenuse | |
z = Math.sqrt(x*x + y*y) | |
# angle in radians | |
rad = Math.acos(x.abs / z) | |
# and in degrees | |
angle = rad * 180 / Math::PI | |
# Now angle indicates the measure of turn | |
# Along a straight line, with an angle o, the turn co-efficient is same | |
# this applies for angles between 0-90, with angle 0 the co-eff is -1 | |
# with angle 45, the co-efficient is 0 and with angle 90, it is 1 | |
tcoeff = -1 + (angle / 90) * 2 | |
turn = tcoeff * (y.abs - x.abs).abs | |
turn = (turn * 100).round / 100 | |
# And max of y or x is the movement | |
move = y.abs > x.abs ? y.abs : x.abs | |
# First and third quadrant | |
if ( (x >= 0 && y >= 0) || (x < 0 && y < 0) ) | |
left = move | |
right = turn | |
else | |
right = move | |
left = turn | |
end | |
# Reverse polarity | |
if (y < 0) | |
left = 0 - left | |
right = 0 - right | |
end | |
return left, right | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment