Skip to content

Commit

Permalink
Merge pull request #308 from Peppie23/rover_computer
Browse files Browse the repository at this point in the history
Rover computer
  • Loading branch information
Peppie84 committed Jan 18, 2015
2 parents b30a846 + e16660b commit b0637f6
Show file tree
Hide file tree
Showing 10 changed files with 1,103 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/RemoteTech/FlightComputer/Commands/AbstractCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public static ICommand LoadCommand(ConfigNode n, FlightComputer fc)
case "CancelCommand": { command = new CancelCommand(); break; }
case "TargetCommand": { command = new TargetCommand(); break; }
case "EventCommand": { command = new EventCommand(); break; }
case "DriveCommand": { command = new DriveCommand(); break; }
}

if (command != null)
Expand Down
183 changes: 183 additions & 0 deletions src/RemoteTech/FlightComputer/Commands/DriveCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
using System;
using System.Text;
using UnityEngine;

namespace RemoteTech.FlightComputer.Commands
{
public class DriveCommand : AbstractCommand
{
public enum DriveMode
{
Off,
Turn,
Distance,
DistanceHeading,
Coord
}

[Persistent] public float steering;
[Persistent] public float target;
[Persistent] public float target2;
[Persistent] public float speed;
[Persistent] public DriveMode mode;

private bool mAbort;
private RoverComputer mRoverComputer;

public override void Abort() { mAbort = true; }

public override bool Pop(FlightComputer f)
{
mRoverComputer = f.mRoverComputer;
mRoverComputer.InitMode(this);
return true;
}

public override bool Execute(FlightComputer f, FlightCtrlState fcs)
{
if (mAbort) {
fcs.wheelThrottle = 0.0f;
f.Vessel.ActionGroups.SetGroup(KSPActionGroup.Brakes, true);
return true;
}

return f.mRoverComputer.Drive(this, fcs);
}

public static DriveCommand Off()
{
return new DriveCommand() {
mode = DriveMode.Off,
TimeStamp = RTUtil.GameTime
};
}

public static DriveCommand Turn(float steering, float degrees, float speed)
{
return new DriveCommand() {
mode = DriveMode.Turn,
steering = steering,
target = degrees,
speed = speed,
TimeStamp = RTUtil.GameTime
};
}

public static DriveCommand Distance(float distance, float steerClamp, float speed)
{
return new DriveCommand() {
mode = DriveMode.Distance,
steering = steerClamp,
target = distance,
speed = speed,
TimeStamp = RTUtil.GameTime
};
}

public static DriveCommand DistanceHeading(float distance, float heading, float steerClamp, float speed)
{
return new DriveCommand() {
mode = DriveMode.DistanceHeading,
steering = steerClamp,
target = distance,
target2 = heading,
speed = speed,
TimeStamp = RTUtil.GameTime
};
}

public static DriveCommand Coord(float steerClamp, float latitude, float longitude, float speed)
{
return new DriveCommand() {
mode = DriveMode.Coord,
steering = steerClamp,
target = latitude,
target2 = longitude,
speed = speed,
TimeStamp = RTUtil.GameTime
};
}

public override String Description
{
get
{
StringBuilder s = new StringBuilder();
switch (mode) {
case DriveMode.Coord:
s.Append("Drive to: ");
s.Append(new Vector2(target, target2).ToString("0.000"));
s.Append(" @ ");
s.Append(RTUtil.FormatSI(Math.Abs(speed), "m/s"));
if (mRoverComputer != null) {
s.Append(" (");
s.Append(RTUtil.FormatSI(mRoverComputer.Delta, "m"));
s.Append(" ");
s.Append(RTUtil.FormatDuration(mRoverComputer.DeltaT, false));
s.Append(")"); ;
}
break;
case DriveMode.Distance:
s.Append("Drive: ");
s.Append(RTUtil.FormatSI(target, "m"));
if (speed > 0)
s.Append(" forwards @");
else
s.Append(" backwards @");
s.Append(RTUtil.FormatSI(Math.Abs(speed), "m/s"));
if (mRoverComputer != null) {
s.Append(" (");
s.Append(RTUtil.FormatSI(mRoverComputer.Delta, "m"));
s.Append(" ");
s.Append(RTUtil.FormatDuration(mRoverComputer.DeltaT, false));
s.Append(")"); ;
}
break;
case DriveMode.Turn:
s.Append("Turn: ");
s.Append(target.ToString("0.0"));
if (steering < 0)
s.Append("° right @");
else
s.Append("° left @");
s.Append(Math.Abs(steering).ToString("P"));
s.Append(" Steering");
if (mRoverComputer != null) {
s.Append(" (");
s.Append(mRoverComputer.Delta.ToString("F2"));
s.Append("° ");
s.Append(RTUtil.FormatDuration(mRoverComputer.DeltaT, false));
s.Append(")"); ;
}
break;
case DriveMode.DistanceHeading:
s.Append("Drive: ");
s.Append(RTUtil.FormatSI(target, "m"));
s.Append(", Hdg: ");
s.Append(target2.ToString("0"));
s.Append("° @ ");
s.Append(RTUtil.FormatSI(Math.Abs(speed), "m/s"));
if (mRoverComputer != null) {
s.Append(" (");
s.Append(RTUtil.FormatSI(mRoverComputer.Delta, "m"));
s.Append(" ");
s.Append(RTUtil.FormatDuration(mRoverComputer.DeltaT, false));
s.Append(")");
}
break;
case DriveMode.Off:
s.Append("Turn rover computer off");
break;
}

return s.ToString() + Environment.NewLine + base.Description;
}
}

public override string ShortName
{
get { return "Drive"; }
}

}
}
46 changes: 29 additions & 17 deletions src/RemoteTech/FlightComputer/FlightComputer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ public State Status
private readonly SortedDictionary<int, ICommand> mActiveCommands = new SortedDictionary<int, ICommand>();
private readonly List<ICommand> mCommandQueue = new List<ICommand>();
private readonly PriorityQueue<DelayedFlightCtrlState> mFlightCtrlQueue = new PriorityQueue<DelayedFlightCtrlState>();

private FlightComputerWindow mWindow;
public FlightComputerWindow Window { get { if (mWindow != null) mWindow.Hide(); return mWindow = new FlightComputerWindow(this); } }

public RoverComputer mRoverComputer { get; private set; }

public FlightComputer(ISignalProcessor s)
{
SignalProcessor = s;
Expand All @@ -103,8 +105,11 @@ public FlightComputer(ISignalProcessor s)
mActiveCommands[attitude.Priority] = attitude;

GameEvents.onVesselChange.Add(OnVesselChange);

mRoverComputer = new RoverComputer();
mRoverComputer.SetVessel(Vessel);
}

/// <summary>
/// After switching the vessel close the current flightcomputer.
/// </summary>
Expand Down Expand Up @@ -170,7 +175,10 @@ public void OnUpdate()
public void OnFixedUpdate()
{
if (Vessel == null)
{
Vessel = SignalProcessor.Vessel;
mRoverComputer.SetVessel(Vessel);
}

// only handle onFixedUpdate if the ship is unpacked
if (Vessel.packed)
Expand All @@ -191,6 +199,7 @@ public void OnFixedUpdate()
{
SanctionedPilots.Clear();
Vessel = SignalProcessor.Vessel;
mRoverComputer.SetVessel(Vessel);
}
Vessel.OnFlyByWire = OnFlyByWirePre + Vessel.OnFlyByWire + OnFlyByWirePost;

Expand Down Expand Up @@ -250,10 +259,10 @@ private void PopCommand()
if (dc.ExtraDelay > 0)
{
dc.ExtraDelay -= TimeWarp.deltaTime;
}
else
} else
{
if (SignalProcessor.Powered) {
if (SignalProcessor.Powered)
{
// Note: depending on implementation, dc.Pop() may execute the event
if (dc.Pop(this)) {
mActiveCommands[dc.Priority] = dc;
Expand Down Expand Up @@ -322,20 +331,21 @@ public void initPIDParameters()
// Probably because the ship is only half-initialized...
public void updatePIDParameters()
{
if (Vessel != null) {
Vector3d torque = SteeringHelper.GetTorque (Vessel,
if (Vessel != null)
{
Vector3d torque = SteeringHelper.GetTorque(Vessel,
Vessel.ctrlState != null ? Vessel.ctrlState.mainThrottle : 0.0f);
var CoM = Vessel.findWorldCenterOfMass ();
var MoI = Vessel.findLocalMOI (CoM);
var CoM = Vessel.findWorldCenterOfMass();
var MoI = Vessel.findLocalMOI(CoM);

Vector3d ratio = new Vector3d (
Vector3d ratio = new Vector3d(
torque.x != 0 ? MoI.x / torque.x : 0,
torque.y != 0 ? MoI.y / torque.y : 0,
torque.z != 0 ? MoI.z / torque.z : 0
);

Tf = Mathf.Clamp ((float)ratio.magnitude / 20f, 2 * TimeWarp.fixedDeltaTime, 1f);
Tf = Mathf.Clamp ((float)Tf, (float)TfMin, (float)TfMax);
Tf = Mathf.Clamp((float)ratio.magnitude / 20f, 2 * TimeWarp.fixedDeltaTime, 1f);
Tf = Mathf.Clamp((float)Tf, (float)TfMin, (float)TfMax);
}
initPIDParameters();
}
Expand All @@ -354,7 +364,7 @@ public void orderCommandList()
mCommandQueue.Clear();

// add the sorted queue
foreach(var command in backupList)
foreach (var command in backupList)
{
mCommandQueue.Add(command);
}
Expand All @@ -381,7 +391,10 @@ public void load(ConfigNode n)

// Load the current vessel from signalprocessor if we've no on the flightcomputer
if (Vessel == null)
{
Vessel = SignalProcessor.Vessel;
mRoverComputer.SetVessel(Vessel);
}

// Read Flightcomputer informations
ConfigNode FlightNode = n.GetNode("FlightComputer");
Expand All @@ -397,7 +410,7 @@ public void load(ConfigNode n)
foreach (ConfigNode cmdNode in ActiveCommands.nodes)
{
ICommand cmd = AbstractCommand.LoadCommand(cmdNode, this);

if (cmd != null)
{
mActiveCommands[cmd.Priority] = cmd;
Expand Down Expand Up @@ -446,8 +459,7 @@ public void load(ConfigNode n)
// TODO: Need better text
RTUtil.ScreenMessage("You missed the burn command!");
continue;
}
else
} else
{
// change the extra delay to x/100
cmd.ExtraDelay = (qCounter) / 100;
Expand All @@ -472,7 +484,7 @@ public void Save(ConfigNode n)

ConfigNode ActiveCommands = new ConfigNode("ActiveCommands");
ConfigNode Commands = new ConfigNode("Commands");

foreach (KeyValuePair<int, ICommand> cmd in mActiveCommands)
{
cmd.Value.Save(ActiveCommands, this);
Expand Down
Loading

0 comments on commit b0637f6

Please sign in to comment.