Skip to content

Commit

Permalink
Merge branch 'refs/heads/ZiwKerman-v2.0-overhaul' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ZiwKerman committed Mar 30, 2016
2 parents 0e46b04 + fe3bdeb commit a7ecc76
Show file tree
Hide file tree
Showing 83 changed files with 2,635 additions and 2,886 deletions.
73 changes: 45 additions & 28 deletions InfernalRobotics/InfernalRobotics/API/IRWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class IRWrapper
protected internal static Type IRServoType { get; set; }
protected internal static Type IRServoPartType { get; set; }
protected internal static Type IRServoMechanismType { get; set; }
protected internal static Type IRServoMotorType { get; set; }
protected internal static object ActualServoController { get; set; }

internal static IRAPI IRController { get; set; }
Expand Down Expand Up @@ -53,6 +54,17 @@ internal static bool InitWrapper()
return false;
}

IRServoMotorType = AssemblyLoader.loadedAssemblies
.Select(a => a.assembly.GetExportedTypes())
.SelectMany(t => t)
.FirstOrDefault(t => t.FullName == "InfernalRobotics.Control.IServoMotor");

if (IRServoMotorType == null)
{
LogFormatted("[IR Wrapper] Failed to grab ServoMotor Type");
return false;
}

IRServoType = AssemblyLoader.loadedAssemblies
.Select(a => a.assembly.GetExportedTypes())
.SelectMany(t => t)
Expand Down Expand Up @@ -358,6 +370,7 @@ public bool Equals(IControlGroup other)
public class IRServo : IServo
{
private object actualServoMechanism;
private object actualServoMotor;

private PropertyInfo maxConfigPositionProperty;
private PropertyInfo minPositionProperty;
Expand Down Expand Up @@ -402,32 +415,36 @@ private void FindProperties()
var mechanismProperty = IRServoType.GetProperty("Mechanism");
actualServoMechanism = mechanismProperty.GetValue(actualServo, null);

var motorProperty = IRServoType.GetProperty("Motor");
actualServoMotor = motorProperty.GetValue(actualServo, null);

positionProperty = IRServoMechanismType.GetProperty("Position");
minPositionProperty = IRServoMechanismType.GetProperty("MinPositionLimit");
maxPositionProperty = IRServoMechanismType.GetProperty("MaxPositionLimit");

minConfigPositionProperty = IRServoMechanismType.GetProperty("MinPosition");
maxConfigPositionProperty = IRServoMechanismType.GetProperty("MaxPosition");

speedProperty = IRServoMechanismType.GetProperty("SpeedLimit");
configSpeedProperty = IRServoMechanismType.GetProperty("DefaultSpeed");
currentSpeedProperty = IRServoMechanismType.GetProperty("CurrentSpeed");
accelerationProperty = IRServoMechanismType.GetProperty("AccelerationLimit");
isMovingProperty = IRServoMechanismType.GetProperty("IsMoving");
isFreeMovingProperty = IRServoMechanismType.GetProperty("IsFreeMoving");
isLockedProperty = IRServoMechanismType.GetProperty("IsLocked");
isAxisInvertedProperty = IRServoMechanismType.GetProperty("IsAxisInverted");

speedProperty = IRServoMotorType.GetProperty("SpeedLimit");
configSpeedProperty = IRServoMotorType.GetProperty("DefaultSpeed");
currentSpeedProperty = IRServoMotorType.GetProperty("CurrentSpeed");
accelerationProperty = IRServoMotorType.GetProperty("AccelerationLimit");
isAxisInvertedProperty = IRServoMotorType.GetProperty("IsAxisInverted");
}

private void FindMethods()
{
moveRightMethod = IRServoMechanismType.GetMethod("MoveRight", BindingFlags.Public | BindingFlags.Instance);
moveLeftMethod = IRServoMechanismType.GetMethod("MoveLeft", BindingFlags.Public | BindingFlags.Instance);
moveCenterMethod = IRServoMechanismType.GetMethod("MoveCenter", BindingFlags.Public | BindingFlags.Instance);
moveNextPresetMethod = IRServoMechanismType.GetMethod("MoveNextPreset", BindingFlags.Public | BindingFlags.Instance);
movePrevPresetMethod = IRServoMechanismType.GetMethod("MovePrevPreset", BindingFlags.Public | BindingFlags.Instance);
stopMethod = IRServoMechanismType.GetMethod("Stop", BindingFlags.Public | BindingFlags.Instance);
moveToMethod = IRServoMechanismType.GetMethod("MoveTo", new[] { typeof(float), typeof(float) });
moveRightMethod = IRServoMotorType.GetMethod("MoveRight", BindingFlags.Public | BindingFlags.Instance);
moveLeftMethod = IRServoMotorType.GetMethod("MoveLeft", BindingFlags.Public | BindingFlags.Instance);
moveCenterMethod = IRServoMotorType.GetMethod("MoveCenter", BindingFlags.Public | BindingFlags.Instance);
moveNextPresetMethod = IRServoMotorType.GetMethod("MoveNextPreset", BindingFlags.Public | BindingFlags.Instance);
movePrevPresetMethod = IRServoMotorType.GetMethod("MovePrevPreset", BindingFlags.Public | BindingFlags.Instance);
stopMethod = IRServoMotorType.GetMethod("Stop", BindingFlags.Public | BindingFlags.Instance);
moveToMethod = IRServoMotorType.GetMethod("MoveTo", new[] { typeof(float), typeof(float) });
}

private readonly object actualServo;
Expand Down Expand Up @@ -479,25 +496,25 @@ public float MaxPosition

public float ConfigSpeed
{
get { return (float)configSpeedProperty.GetValue(actualServoMechanism, null); }
get { return (float)configSpeedProperty.GetValue(actualServoMotor, null); }
}

public float Speed
{
get { return (float)speedProperty.GetValue(actualServoMechanism, null); }
set { speedProperty.SetValue(actualServoMechanism, value, null); }
get { return (float)speedProperty.GetValue(actualServoMotor, null); }
set { speedProperty.SetValue(actualServoMotor, value, null); }
}

public float CurrentSpeed
{
get { return (float)currentSpeedProperty.GetValue(actualServoMechanism, null); }
set { currentSpeedProperty.SetValue(actualServoMechanism, value, null); }
get { return (float)currentSpeedProperty.GetValue(actualServoMotor, null); }
set { currentSpeedProperty.SetValue(actualServoMotor, value, null); }
}

public float Acceleration
{
get { return (float)accelerationProperty.GetValue(actualServoMechanism, null); }
set { accelerationProperty.SetValue(actualServoMechanism, value, null); }
get { return (float)accelerationProperty.GetValue(actualServoMotor, null); }
set { accelerationProperty.SetValue(actualServoMotor, value, null); }
}

public bool IsMoving
Expand All @@ -518,43 +535,43 @@ public bool IsLocked

public bool IsAxisInverted
{
get { return (bool)isAxisInvertedProperty.GetValue(actualServoMechanism, null); }
set { isAxisInvertedProperty.SetValue(actualServoMechanism, value, null); }
get { return (bool)isAxisInvertedProperty.GetValue(actualServoMotor, null); }
set { isAxisInvertedProperty.SetValue(actualServoMotor, value, null); }
}

public void MoveRight()
{
moveRightMethod.Invoke(actualServoMechanism, new object[] { });
moveRightMethod.Invoke(actualServoMotor, new object[] { });
}

public void MoveLeft()
{
moveLeftMethod.Invoke(actualServoMechanism, new object[] { });
moveLeftMethod.Invoke(actualServoMotor, new object[] { });
}

public void MoveCenter()
{
moveCenterMethod.Invoke(actualServoMechanism, new object[] { });
moveCenterMethod.Invoke(actualServoMotor, new object[] { });
}

public void MoveNextPreset()
{
moveNextPresetMethod.Invoke(actualServoMechanism, new object[] { });
moveNextPresetMethod.Invoke(actualServoMotor, new object[] { });
}

public void MovePrevPreset()
{
movePrevPresetMethod.Invoke(actualServoMechanism, new object[] { });
movePrevPresetMethod.Invoke(actualServoMotor, new object[] { });
}

public void MoveTo(float position, float speed)
{
moveToMethod.Invoke(actualServoMechanism, new object[] { position, speed });
moveToMethod.Invoke(actualServoMotor, new object[] { position, speed });
}

public void Stop()
{
stopMethod.Invoke(actualServoMechanism, new object[] { });
stopMethod.Invoke(actualServoMotor, new object[] { });
}

public bool Equals(IServo other)
Expand Down
6 changes: 4 additions & 2 deletions InfernalRobotics/InfernalRobotics/Command/Interpolator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public Interpolator()

public bool Initialised { get; set; }

private const float precisionDelta = 0.001f;

#endregion config

public float GetPosition()
Expand Down Expand Up @@ -122,7 +124,7 @@ public void Update(float deltaT)
Logger.Log(string.Format("Update: targetPos={0}, cmdPos={1},min/maxpos={2},{3}", targetPos, CmdPosition, MinPosition, MaxPosition), Logger.Level.SuperVerbose);

if ((Math.Abs(Velocity) < maxDeltaVel) &&
((Position == targetPos) || (CmdVelocity == 0f)))
(targetPos == Position) || (CmdVelocity == 0f))
{
Active = false;
Velocity = 0;
Expand All @@ -148,7 +150,7 @@ public void Update(float deltaT)
Position = targetPos;
return;
}

Velocity = newVel;
Position += Velocity * deltaT;

Expand Down
14 changes: 8 additions & 6 deletions InfernalRobotics/InfernalRobotics/Command/ServoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private void OnEditorRestart()
Logger.Log ("OnEditorRestart called", Logger.Level.Debug);
}

private void OnEditorLoad(ShipConstruct s, CraftBrowser.LoadType t)
private void OnEditorLoad(ShipConstruct s, KSP.UI.Screens.CraftBrowserDialog.LoadType t)
{
OnEditorShipModified (s);
Logger.Log ("OnEditorLoad called", Logger.Level.Debug);
Expand Down Expand Up @@ -346,6 +346,8 @@ private void OnDestroy()
Logger.Log("[ServoController] OnDestroy finished successfully", Logger.Level.Debug);
}

//TODO: move this to a separate file and extend if necessary
//this will require changes in API
public class ControlGroup
{
private bool stale;
Expand Down Expand Up @@ -466,7 +468,7 @@ public void MoveRight()
{
foreach (var servo in Servos)
{
servo.Mechanism.MoveRight();
servo.Motor.MoveRight();
}
}
}
Expand All @@ -477,7 +479,7 @@ public void MoveLeft()
{
foreach (var servo in Servos)
{
servo.Mechanism.MoveLeft();
servo.Motor.MoveLeft();
}
}
}
Expand All @@ -488,7 +490,7 @@ public void MoveCenter()
{
foreach (var servo in Servos)
{
servo.Mechanism.MoveCenter();
servo.Motor.MoveCenter();
}
}
}
Expand Down Expand Up @@ -524,7 +526,7 @@ public void Stop()
{
foreach (var servo in Servos)
{
servo.Mechanism.Stop();
servo.Motor.Stop();
}
}
}
Expand All @@ -535,7 +537,7 @@ private void Freshen()

if (UseElectricCharge)
{
float chargeRequired = Servos.Where(s => s.Mechanism.IsFreeMoving == false).Select(s => s.ElectricChargeRequired).Sum();
float chargeRequired = Servos.Where (s => s.Mechanism.IsFreeMoving == false).Sum (s => s.ElectricChargeRequired);
foreach (var servo in Servos)
{
servo.Group.ElectricChargeRequired = chargeRequired;
Expand Down
8 changes: 4 additions & 4 deletions InfernalRobotics/InfernalRobotics/Command/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public float GetSpeedUnit()
if (servo == null)
return 0f;

return servo.Mechanism.DefaultSpeed;
return servo.Motor.DefaultSpeed;
}

// external interface
Expand All @@ -59,7 +59,7 @@ public void MoveIncremental(float posDelta, float speed)
if (!interpolator.Active)
servo.Mechanism.Reconfigure();

float axisCorrection = servo.Mechanism.IsAxisInverted ? -1 : 1;
float axisCorrection = servo.Motor.IsAxisInverted ? -1 : 1;
interpolator.SetIncrementalCommand(posDelta*axisCorrection, speed * GetSpeedUnit());
}

Expand All @@ -75,14 +75,14 @@ public bool IsMoving()

public float ToInternalPos(float externalPos)
{
if (servo.Mechanism.IsAxisInverted)
if (servo.Motor.IsAxisInverted)
return servo.Mechanism.MinPositionLimit + servo.Mechanism.MaxPositionLimit - externalPos;
return externalPos;
}

public float ToExternalPos(float internalPos)
{
if (servo.Mechanism.IsAxisInverted)
if (servo.Motor.IsAxisInverted)
return servo.Mechanism.MinPositionLimit + servo.Mechanism.MaxPositionLimit - internalPos;
return internalPos;
}
Expand Down
64 changes: 11 additions & 53 deletions InfernalRobotics/InfernalRobotics/Control/IMechanism.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ namespace InfernalRobotics.Control
{
public interface IMechanism
{
/// <summary>
/// Gets the current position.
/// </summary>
/// <value>The position.</value>
float Position { get; }

/// <summary>
Expand Down Expand Up @@ -43,68 +47,22 @@ public interface IMechanism
/// Returns/set locked state of the servo. Locked servos do not move until unlocked.
/// </summary>
bool IsLocked { get; set; }
/// <summary>
/// Returns/sets servo's Acceleration multiplier
/// </summary>
float AccelerationLimit { get; set; }

/// <summary>
/// Returns/sets servo's inverted status
/// </summary>
bool IsAxisInverted { get; set; }

/// <summary>
/// the speed from part.cfg is used as the default unit of speed
/// Gets or sets the spring power.
/// </summary>
float DefaultSpeed { get; }
/// <value>The spring power.</value>
float SpringPower { get; set; }

/// <summary>
/// The current rate of travel, like right now
/// Gets or sets the damping power for spring. Usen in conjuction with SpringPower to create suspension effect.
/// </summary>
float CurrentSpeed { get; }
/// <value>The damping power.</value>
float DampingPower { get; set; }

/// <summary>
/// The maximum speed that the servo can travel
/// </summary>
float MaxSpeed { get; }

/// <summary>
/// User setting to limit the speed of the servo
/// </summary>
float SpeedLimit { get; set; }

/// <summary>
/// Commands the servo to move in the direction that decreases its Position
/// </summary>
void MoveLeft();

/// <summary>
/// Coomands the servo to move towards its DefaultPosition
/// </summary>
void MoveCenter();

/// <summary>
/// Commands the servo to move in the direction that increases its Position
/// </summary>
void MoveRight();

/// <summary>
/// Commands the servo to stop
/// </summary>
void Stop();

/// <summary>
/// Commands the servo to move to specified position at current speed
/// </summary>
/// <param name="position"></param>
void MoveTo(float position);

/// <summary>
/// Commands the servo to move to specified position at specified speed multiplier
/// Reinitialize the Mechanism, update joint parameters.
/// </summary>
/// <param name="position"></param>
/// <param name="speed"></param>
void MoveTo(float position, float speed);
void Reconfigure();

/// <summary>
Expand Down
Loading

0 comments on commit a7ecc76

Please sign in to comment.