< Summary

Class:PnController
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Controller/PnController.cs
Covered lines:24
Uncovered lines:7
Coverable lines:31
Total lines:61
Line coverage:77.4% (24 of 31)
Covered branches:0
Total branches:0
Covered methods:2
Total methods:2
Method coverage:100% (2 of 2)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PnController(...)0%110100%
PlanImpl(...)0%4.344072.41%

File(s)

/github/workspace/Assets/Scripts/Controller/PnController.cs

#LineLine coverage
 1using System;
 2using UnityEngine;
 3
 4// The proportional navigation controller applies proportional navigation to steer the agent towards
 5// its target.
 6public class PnController : IController {
 7  // Negative closing velocity turn factor.
 8  protected const float negativeClosingVelocityTurnFactor = 100f;
 9
 10  // Minimum line-of-sight rate.
 11  protected const float minimumLosRate = 0.2f;
 12
 13  // Proportional navigation gain.
 14  protected float _navigationGain;
 15
 10016  public PnController(Agent agent, float navigationGain) : base(agent) {
 5017    _navigationGain = navigationGain;
 5018  }
 19
 5020  protected override Vector3 PlanImpl(in Transformation relativeTransformation) {
 21    // Cache the transform and velocity.
 5022    Transform agentTransform = _agent.transform;
 5023    Vector3 right = agentTransform.right;
 5024    Vector3 up = agentTransform.up;
 5025    Vector3 forward = agentTransform.forward;
 5026    Vector3 position = agentTransform.position;
 27
 5028    Vector3 velocity = _agent.GetVelocity();
 5029    float speed = velocity.magnitude;
 30
 31    // Extract the bearing and closing velocity from the relative transformation.
 5032    float losAz = relativeTransformation.position.azimuth;
 5033    float losEl = relativeTransformation.position.elevation;
 5034    float losRateAz = relativeTransformation.velocity.azimuth;
 5035    float losRateEl = relativeTransformation.velocity.elevation;
 36    // The closing velocity is negative because the closing velocity is opposite to the range rate.
 5037    float closingVelocity = -relativeTransformation.velocity.range;
 38
 39    // Set the turn factor, which is equal to the closing velocity by default.
 5040    float turnFactor = closingVelocity;
 41    // Handle a negative closing velocity. In this case, since the target is moving away from the
 42    // agent, apply a stronger turn.
 5043    if (closingVelocity < 0) {
 044      turnFactor = Mathf.Max(1f, Mathf.Abs(closingVelocity) * negativeClosingVelocityTurnFactor);
 045    }
 46
 47    // Handle the spiral behavior if the target is at a bearing of 90 degrees +- 10 degrees.
 5048    if (Mathf.Abs(Mathf.Abs(losAz) - 90f * Mathf.Deg2Rad) < 10f * Mathf.Deg2Rad ||
 049        Mathf.Abs(Mathf.Abs(losEl) - 90f * Mathf.Deg2Rad) < 10f * Mathf.Deg2Rad) {
 50      // Check that the agent is not moving in a spiral by clamping the LOS rate.
 051      losRateAz = Mathf.Sign(losRateAz) * Mathf.Max(Mathf.Abs(losRateAz), minimumLosRate);
 052      losRateEl = Mathf.Sign(losRateEl) * Mathf.Max(Mathf.Abs(losRateEl), minimumLosRate);
 053      turnFactor = Mathf.Abs(closingVelocity) * negativeClosingVelocityTurnFactor;
 054    }
 55
 5056    float accelerationAz = _navigationGain * turnFactor * losRateAz;
 5057    float accelerationEl = _navigationGain * turnFactor * losRateEl;
 5058    Vector3 accelerationInput = right * accelerationAz + up * accelerationEl;
 5059    return accelerationInput;
 5060  }
 61}