Skip to content

Commit

Permalink
Merge pull request #74 from ZiwKerman/develop
Browse files Browse the repository at this point in the history
Last pre-overhaul update
  • Loading branch information
ZiwKerman committed Mar 6, 2016
2 parents d3a37e7 + bc0a693 commit 0e46b04
Show file tree
Hide file tree
Showing 4 changed files with 381 additions and 251 deletions.
12 changes: 12 additions & 0 deletions InfernalRobotics/InfernalRobotics/API/IRWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ private class IRControlGroup : IControlGroup
private readonly object actualControlGroup;

private PropertyInfo nameProperty;
private PropertyInfo vesselProperty;
private PropertyInfo forwardKeyProperty;
private PropertyInfo expandedProperty;
private PropertyInfo speedProperty;
Expand All @@ -230,6 +231,7 @@ public IRControlGroup(object cg)
private void FindProperties()
{
nameProperty = IRControlGroupType.GetProperty("Name");
vesselProperty = IRControlGroupType.GetProperty("Vessel");
forwardKeyProperty = IRControlGroupType.GetProperty("ForwardKey");
reverseKeyProperty = IRControlGroupType.GetProperty("ReverseKey");
speedProperty = IRControlGroupType.GetProperty("Speed");
Expand All @@ -255,6 +257,11 @@ public string Name
set { nameProperty.SetValue(actualControlGroup, value, null); }
}

public Vessel Vessel
{
get { return vesselProperty != null ? (Vessel)vesselProperty.GetValue(actualControlGroup, null) : null; }
}

public string ForwardKey
{
get { return (string)forwardKeyProperty.GetValue(actualControlGroup, null); }
Expand Down Expand Up @@ -598,6 +605,9 @@ public interface IControlGroup : IEquatable<IControlGroup>
{
string Name { get; set; }

//can only be used in Flight, null checking is mandatory
Vessel Vessel { get; }

string ForwardKey { get; set; }

string ReverseKey { get; set; }
Expand Down Expand Up @@ -625,6 +635,8 @@ public interface IServo : IEquatable<IServo>
{
string Name { get; set; }

uint UID { get; }

bool Highlight { set; }

float Position { get; }
Expand Down
17 changes: 9 additions & 8 deletions InfernalRobotics/InfernalRobotics/Command/Interpolator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,6 @@ public void Update(float deltaT)
return;
}

if ((Math.Abs(Velocity) < maxDeltaVel) && // end conditions
(Math.Abs(targetPos - Position) < (2f * maxDeltaVel * deltaT)))
{ // (generous to avoid oscillations)
Logger.Log(string.Format("pos={0} targetPos={1}, 2f*maxDeltaVel*dalteT={2}", Position, targetPos, (2f * maxDeltaVel * deltaT)), Logger.Level.SuperVerbose);
Position = targetPos;
return;
}

float newVel = Math.Min(CmdVelocity, MaxVelocity);
if (!isSpeedMode)
{
Expand All @@ -148,6 +140,15 @@ public void Update(float deltaT)
newVel = Math.Min(newVel, Velocity + maxDeltaVel); // acceleration limit
newVel = Math.Max(newVel, Velocity - maxDeltaVel);


if ((Math.Abs(Velocity) < maxDeltaVel) && // end conditions
(Math.Abs(targetPos - Position) < (2f * newVel * deltaT)))
{ // (generous to avoid oscillations)
Logger.Log(string.Format("pos={0} targetPos={1}, 2f*maxDeltaVel*dalteT={2}", Position, targetPos, (2f * maxDeltaVel * deltaT)), Logger.Level.SuperVerbose);
Position = targetPos;
return;
}

Velocity = newVel;
Position += Velocity * deltaT;

Expand Down
177 changes: 123 additions & 54 deletions InfernalRobotics/InfernalRobotics/Command/ServoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,28 @@

namespace InfernalRobotics.Command
{
[KSPAddon(KSPAddon.Startup.EveryScene, false)]
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class ServoControllerFlight : ServoController
{
public override string AddonName { get { return this.name; } }
}

[KSPAddon(KSPAddon.Startup.EditorAny, false)]
public class ServoControllerEditor : ServoController
{
public override string AddonName { get { return this.name; } }
}

public class ServoController : MonoBehaviour
{
public virtual String AddonName { get; set; }

protected static bool UseElectricCharge = true;
protected static ServoController ControllerInstance;

public List<ControlGroup> ServoGroups;
private int partCounter;
private int loadedVesselCounter = 0;

public static ServoController Instance { get { return ControllerInstance; } }

Expand Down Expand Up @@ -104,40 +118,6 @@ public static void RemoveServo(IServo servo)
Logger.Log("[ServoController] AddServo finished successfully", Logger.Level.Debug);
}

private void OnVesselChange(Vessel v)
{
Logger.Log(string.Format("[ServoController] vessel {0}", v.name));
ServoGroups = null;

var groups = new List<ControlGroup>();
var groupMap = new Dictionary<string, int>();

foreach (var servo in v.ToServos())
{
if (!groupMap.ContainsKey(servo.Group.Name))
{
groups.Add(new ControlGroup(servo));
groupMap[servo.Group.Name] = groups.Count - 1;
}
else
{
ControlGroup g = groups[groupMap[servo.Group.Name]];
g.AddControl(servo);
}
}

Logger.Log(string.Format("[ServoController] {0} groups", groups.Count));

if (groups.Count > 0)
ServoGroups = groups;

foreach (var servo in v.ToServos())
{
servo.RawServo.SetupJoints();
}
Logger.Log("[ServoController] OnVesselChange finished successfully", Logger.Level.Debug);
}

private void OnPartAttach(GameEvents.HostTargetAction<Part, Part> hostTarget)
{
Part part = hostTarget.host;
Expand Down Expand Up @@ -224,16 +204,98 @@ private void OnEditorShipModified(ShipConstruct ship)
Logger.Log("[ServoController] OnEditorShipModified finished successfully", Logger.Level.Debug);
}

private void OnEditorRestart()
{
ServoGroups = null;
Logger.Log ("OnEditorRestart called", Logger.Level.Debug);
}

private void OnEditorLoad(ShipConstruct s, CraftBrowser.LoadType t)
{
OnEditorShipModified (s);
Logger.Log ("OnEditorLoad called", Logger.Level.Debug);
}
/// <summary>
/// Rebuilds the servo groups. Only works in flight.
/// </summary>
private void RebuildServoGroups()
{
ServoGroups = new List<ControlGroup>();

for(int i=0; i<FlightGlobals.Vessels.Count; i++)
{
var vessel = FlightGlobals.Vessels [i];

if (!vessel.loaded)
continue;

var groups = new List<ControlGroup>();
var groupMap = new Dictionary<string, int>();

foreach(var servo in vessel.ToServos())
{
if (!groupMap.ContainsKey(servo.Group.Name))
{
groups.Add(new ControlGroup(servo, vessel));
groupMap[servo.Group.Name] = groups.Count - 1;
}
else
{
ControlGroup g = groups[groupMap[servo.Group.Name]];
g.AddControl(servo);
}
}

ServoGroups.AddRange (groups);
}

if (ServoGroups.Count == 0)
ServoGroups = null;
}

private void OnVesselChange(Vessel v)
{
Logger.Log(string.Format("[ServoController] vessel {0}", v.name));

RebuildServoGroups ();

foreach (var servo in v.ToServos())
{
servo.RawServo.SetupJoints();
}
Logger.Log("[ServoController] OnVesselChange finished successfully", Logger.Level.Debug);
}

private void OnVesselWasModified(Vessel v)
{
RebuildServoGroups ();
}

private void OnVesselLoaded (Vessel v)
{
Logger.Log("[ServoController] OnVesselLoaded, v=" + v.GetName());
RebuildServoGroups ();
}

private void OnVesselUnloaded (Vessel v)
{
Logger.Log("[ServoController] OnVesselUnloaded, v=" + v.GetName());
RebuildServoGroups ();
}

private void Awake()
{
Logger.Log("[ServoController] awake");
Logger.Log("[ServoController] awake, AddonName = " + this.AddonName);

GameScenes scene = HighLogic.LoadedScene;

if (scene == GameScenes.FLIGHT)
{
GameEvents.onVesselChange.Add(OnVesselChange);
GameEvents.onVesselWasModified.Add(OnVesselWasModified);
GameEvents.onVesselLoaded.Add (OnVesselLoaded);
GameEvents.onVesselDestroy.Add (OnVesselUnloaded);
GameEvents.onVesselGoOnRails.Add (OnVesselUnloaded);
ControllerInstance = this;
}
else if (scene == GameScenes.EDITOR)
Expand All @@ -250,28 +312,19 @@ private void Awake()
ControllerInstance = null;
}

Logger.Log("[ServoController] awake finished successfully", Logger.Level.Debug);
}

private void OnEditorRestart()
{
ServoGroups = null;
Logger.Log ("OnEditorRestart called", Logger.Level.Debug);
}

private void OnEditorLoad(ShipConstruct s, CraftBrowser.LoadType t)
{
OnEditorShipModified (s);
Logger.Log ("OnEditorLoad called", Logger.Level.Debug);
Logger.Log("[ServoController] awake finished successfully, AddonName = " + this.AddonName, Logger.Level.Debug);
}

private void OnVesselWasModified(Vessel v)
private void FixedUpdate()
{
if (v == FlightGlobals.ActiveVessel)
//because OnVesselDestroy and OnVesselGoOnRails seem to only work for active vessel I had to build this stupid workaround
if(HighLogic.LoadedSceneIsFlight)
{
ServoGroups = null;

OnVesselChange(v);
if(FlightGlobals.Vessels.Count(v => v.loaded) != loadedVesselCounter)
{
RebuildServoGroups ();
loadedVesselCounter = FlightGlobals.Vessels.Count(v => v.loaded);
}
}
}

Expand All @@ -286,6 +339,10 @@ private void OnDestroy()
GameEvents.onEditorShipModified.Remove(OnEditorShipModified);
GameEvents.onEditorLoad.Remove(OnEditorLoad);
GameEvents.onEditorRestart.Remove(OnEditorRestart);

GameEvents.onVesselLoaded.Remove (OnVesselLoaded);
GameEvents.onVesselDestroy.Remove (OnVesselUnloaded);
GameEvents.onVesselGoOnRails.Remove (OnVesselUnloaded);
Logger.Log("[ServoController] OnDestroy finished successfully", Logger.Level.Debug);
}

Expand All @@ -297,6 +354,13 @@ public class ControlGroup
private string forwardKey;
private string reverseKey;
private readonly List<IServo> servos;
private readonly Vessel vessel;

public ControlGroup(IServo servo, Vessel v)
: this(servo)
{
vessel = v;
}

public ControlGroup(IServo servo)
: this()
Expand Down Expand Up @@ -337,6 +401,11 @@ public IList<IServo> Servos
get { return servos; }
}

public Vessel Vessel
{
get { return vessel; }
}

public string ForwardKey
{
get { return forwardKey; }
Expand Down
Loading

0 comments on commit 0e46b04

Please sign in to comment.