mission calibrate_sparton_compass {
  """
  This mission yoyos in a spiral while calibrating the Sparton AHRS-M2
  Micro AHRS/IMU with AdaptCal.
  """

  # Mission-wide settings

  arguments {
    MissionTimeout = 60 minute
      """
      Maximum duration of mission
      """

    NeedCommsTime = 60 minute
      """
      Elapsed time after previous surface communications when vehicle will
      begin to ascend for additional surface communications
      """

    # You probably need to change these.

    # You probably do not need to change these.

    YoYoMinDepth = 5 meter
      """
      Minimum depth while performing the YoYo behavior.
      """

    YoYoMaxDepth = 40 meter
      """
      Maximum depth while performing the YoYo behavior.
      """

    YoYoMinAltitude = 7 meter
      """
      Minimum altitude while performing the YoYo behavior (for
      bottom-terminated YoYos).
      """

    YoYoPitch = 20 degree
      """
      Pitch (plus and minus) for yo-yo behavior.
      """

    RudderAngle = 13 degree
      """
      Rudder angle to use while performing the spiral cast.
      """

    Speed = 1 meter_per_second
      """
      Vehicle speed.
      """

    CalibrationInitTimeout = 2 minute
      """
      Elapsed time allowed for calibration to initialize
      """

    # You are even less likely to need to change these.

    MinAltitude = 5 meter
      """
      Minimum height above the sea floor for the entire mission.
      """

    MaxDepth = 50 meter
      """
      Maximum depth for the entire mission.
      """

    MinOffshore = 2 kilometer
      """
      Minimum offshore distance for the entire mission.
      """
  }

  # Missions should almost always start with a timeout

  timeout duration=MissionTimeout

  insert Insert/Science.xml {
    """
    Most missions will run the science sensors. If you don't place this
    aggregate above NeedComms, science instruments get turned off on the
    last upcast and while floating on the surface.
    """
  }

  # Most missions should use the NeedComms aggregate.

  insert id="NeedComms" Insert/NeedComms.xml

  assign in sequence NeedComms:DiveInterval = NeedCommsTime
  # Missions should almost always start with standard safety envelopes; most missions should not expose the parameters of these envelopes.

  insert Insert/StandardEnvelopes.xml

  assign in sequence StandardEnvelopes:MinAltitude = MinAltitude
  assign in sequence StandardEnvelopes:MaxDepth = MaxDepth
  assign in sequence StandardEnvelopes:MinOffshore = MinOffshore
  # Check for additional instructions first.

  call id="StartingMission" refId="NeedComms"

  # Many missions will keep mass position and buoyancy volume fixed at defaults.

  behavior Guidance:Pitch {
    run in parallel

    set massPosition = Control:VerticalControl.massDefault
  }

  behavior Guidance:Buoyancy {
    run in parallel

    set position = Control:VerticalControl.buoyancyNeutral
  }

  behavior Guidance:DepthEnvelope {
    """
    Another depth envelope for the YoYo behavior. This envelope should
    fall within the limits of the standard safety envelopes in
    Insert/StandardEnvelopes.xml in order to avoid commanding high pitch
    angles for depth-terminated YoYos.
    """

    run in parallel

    set minDepth = YoYoMinDepth

    set maxDepth = YoYoMaxDepth

    set pitch = YoYoPitch
  }

  behavior Guidance:AltitudeEnvelope {
    """
    Another altitude envelope for the YoYo behavior. This envelope
    should fall within the limits of the standard safety envelopes in
    Insert/StandardEnvelopes.xml in order to avoid commanding high pitch
    angles for bottom-terminated YoYos.
    """

    run in parallel

    set minAltitude = YoYoMinAltitude

    set pitch = YoYoPitch
  }

  behavior Guidance:YoYo {
    run in parallel

    set pitch = YoYoPitch
  }

  aggregate AdaptCal {
    run in sequence

    readDatum id="WaitForCalibrationInit" {
      timeout duration=CalibrationInitTimeout {
        syslog important "Failed to initialize calibration within timeout. Stopping mission."

        behavior Guidance:Execute {
          run in sequence

          set command = "stop"
        }
      }

      Sensor:AHRS_M2.number_of_calbration_points
    }

    aggregate AcquireCalibrationPoints {
      run in sequence

      behavior Guidance:Point {
        run in parallel

        set rudderAngle = RudderAngle
      }

      behavior Guidance:SetSpeed {
        run in parallel

        set speed = Speed
      }
    }

    behavior Sensor:CalibrateAHRS_M2 {
      run in parallel
    }
  }
}
