/*
    a CompassPilot should not be used -- acc. to API,
    "Compass should be added to a PoseProvider"

    http://www.lejos.org/nxt/nxj/tutorial/WheeledVehicles/WheeledVehicles.htm
    *   Navigator uses a DifferentialPilot and an OdometryPoseProvider
    *   "The OdometryPoseProvider keeps track of the robot position 
        and heading."
	 *   "To do this, it needs to know about every move made by 
            the DifferentialPilot."
	     *   "So the pose provider needs to register as a listener 
            with the pilot by calling the addListener method."
        *   "After the pilot adds pose provider as a listener, it 
            will automatically call the moveStarted method on the 
            pose provider every time a movement starts, and moveStopped 
            when the move is complete."

    BUT -- for now, we are using a CompassPilot, and this SHOULD calibrate
    its compass sensor... we hope!

*/

import lejos.nxt.addon.*;
import lejos.nxt.*;
import lejos.robotics.navigation.*;

/**
    calibrate compass sensor

    Since CompassPilot calibrate method does not terminate,
    call CompassHTSensor's startCalibration and stopCalibration
    instead, rotating the robot pilot as specified in the API for
    method startCalibration

    KUDOS to Tobe Wood for showing this works, and to Dakota Ware
    for also suggesting this as a possibility!

    @author Bagnall, course text, p. 324
    @author adapted by Sharon Tuttle, Tobe Wood
    @version 2015-04-09
*/

public class CalibrateCompass
{
    /**
        calibrate compass sensor

        p. 324 - "Once this code is executed, the calibration
        settings are stored in non-volatile memory on the
        sensor, so you won't have to rerun the calibration when
        you turn in NXT brick off." ...allegedly...

        @param args not used
    */

    public static void main(String[] args)
    {
        LCD.drawString("starting", 0, 0);

        CompassHTSensor myCompassSens = 
            new CompassHTSensor(SensorPort.S1);

        CompassPilot myCompPilot =
            new CompassPilot(myCompassSens, 5.6F, 15.5F, Motor.B,
                             Motor.C, true);

        LCD.drawString("try startCalib", 0, 1);
        myCompPilot.setRotateSpeed(15);

        myCompassSens.startCalibration();
        myCompPilot.rotate(720, true);
        Button.waitForAnyPress();

        LCD.drawString("after rotate", 0, 2);
        myCompassSens.stopCalibration();

        LCD.drawString("click to quit", 0, 3);
        Button.waitForAnyPress();
    }
}