Difference between revisions of "Thread:User talk:Cbrowne/Is this a sensible way to do it?/reply (5)"

From Robowiki
Jump to navigation Jump to search
 
(add <pre>)
 
Line 4: Line 4:
  
 
I've implemented a function, "turnGunTo" which (in theory) turns the gun to a given (absolute) heading.  I'm struggling to get it to work, though, as the gun just appears to spin continuously.  Here's my function:
 
I've implemented a function, "turnGunTo" which (in theory) turns the gun to a given (absolute) heading.  I'm struggling to get it to work, though, as the gun just appears to spin continuously.  Here's my function:
 +
<pre>
 
public void turnGunTo(double newHeading) {
 
public void turnGunTo(double newHeading) {
 
double oldHeading = getGunHeadingRadians();  
 
double oldHeading = getGunHeadingRadians();  
Line 9: Line 10:
 
setTurnGunLeft(Utils.normalRelativeAngle(calculateTurnAmount(newHeading,oldHeading)));
 
setTurnGunLeft(Utils.normalRelativeAngle(calculateTurnAmount(newHeading,oldHeading)));
 
}
 
}
 +
</pre>
 
And, for reference, the calculateTurnAmount() function:
 
And, for reference, the calculateTurnAmount() function:
 +
<pre>
 
public double calculateTurnAmount(double newHeading,double oldHeading) {
 
public double calculateTurnAmount(double newHeading,double oldHeading) {
 
// turn amount is a segment of a full circle, calculated by (circle - leftAngle) + rightAngle;
 
// turn amount is a segment of a full circle, calculated by (circle - leftAngle) + rightAngle;
 
return (FULL_CIRCLE - Math.max(newHeading, oldHeading)) + Math.min(newHeading, oldHeading);
 
return (FULL_CIRCLE - Math.max(newHeading, oldHeading)) + Math.min(newHeading, oldHeading);
 
}
 
}
 +
</pre>
 
What am I doing wrong?  Also, is the method abstraction superfluous?  It seems so, but in some ways it makes it a little bit more semantically-obvious what I'm trying to achieve.
 
What am I doing wrong?  Also, is the method abstraction superfluous?  It seems so, but in some ways it makes it a little bit more semantically-obvious what I'm trying to achieve.

Latest revision as of 17:57, 11 October 2011

Wow, that one is excellent. I'm not quite grokking it yet, but experiments have shown that it performs much smoother than my previous implementation.

Now to get the gun to point in the right direction.

I've implemented a function, "turnGunTo" which (in theory) turns the gun to a given (absolute) heading. I'm struggling to get it to work, though, as the gun just appears to spin continuously. Here's my function:

	public void turnGunTo(double newHeading) {
		double oldHeading = getGunHeadingRadians(); 
		// use of normalRelativeAngle means we don't have to decide left or right
		setTurnGunLeft(Utils.normalRelativeAngle(calculateTurnAmount(newHeading,oldHeading)));
	}

And, for reference, the calculateTurnAmount() function:

	public double calculateTurnAmount(double newHeading,double oldHeading) {
		// turn amount is a segment of a full circle, calculated by (circle - leftAngle) + rightAngle;
		return (FULL_CIRCLE - Math.max(newHeading, oldHeading)) + Math.min(newHeading, oldHeading);
	}

What am I doing wrong? Also, is the method abstraction superfluous? It seems so, but in some ways it makes it a little bit more semantically-obvious what I'm trying to achieve.