GoTo

From RoboWiki
Jump to: navigation, search
This article is a stub. You can help RoboWiki by expanding it.

Some robots use the coordinate system to move. These robots are called GoTo robots and usually use a type of GoTo code. Such as a GoTo Surfer. By using a GoTo method, a robot can go directly to a set of desired coordinates.

Standard GoTo

This is your standard GoTo method. It is fairly accurate on a single call.

/**
 * This method is very verbose to explain how things work.
 * Do not obfuscate/optimize this sample.
 */
private void goTo(double x, double y) {
	/* Transform our coordinates into a vector */
	x -= getX();
	y -= getY();
 
	/* Calculate the angle to the target position */
	double angleToTarget = Math.atan2(x, y);
 
	/* Calculate the turn required get there */
	double targetAngle = Utils.normalRelativeAngle(angleToTarget - getHeadingRadians());
 
	/* 
	 * The Java Hypot method is a quick way of getting the length
	 * of a vector. Which in this case is also the distance between
	 * our robot and the target location.
	 */
	double distance = Math.hypot(x, y);
 
	/* This is a simple method of performing set front as back */
	double turnAngle = Math.atan(Math.tan(targetAngle));
	setTurnRightRadians(turnAngle);
	if(targetAngle == turnAngle) {
		setAhead(distance);
	} else {
		setBack(distance);
	}
}

Smallest Codesize GoTo

This GoTo method has been optimized for codesize. It will not start moving full speed until generally pointing in the correct direction, and depending on how far away the go-to point is. This method should be called every round.

private void goTo(int x, int y) {
    double a;
    setTurnRightRadians(Math.tan(
        a = Math.atan2(x -= (int) getX(), y -= (int) getY()) 
              - getHeadingRadians()));
    setAhead(Math.hypot(x, y) * Math.cos(a));
}
Personal tools