KSP-KOS / KOS

Fully programmable autopilot mod for KSP. Originally By Nivekk
Other
687 stars 229 forks source link

[Feature Possibility?] InfernalRobotics compatibility. #238

Closed frencrs closed 9 years ago

frencrs commented 9 years ago

OK so I've got InfernalRobotics parts working in kOS using this class under kOS/Suffixed/Part/

This gets the rotation/translation value of the current part, plus the speed settings and movement limits. I'm working on expanding the suffix list now.

EXAMPLE:

LIST IROBOTICS in T.
SET IREXT to T[0].
PRINT IREXT:ROTATION.
0.77773445343
SET IREXT:MOVENEG to 1. //This command will start the part moving in the negative direction.

IRoboticsValue.cs

using System;
using System.Collections.Generic;
using InfernalRobotics;
using MuMech;
using KSPAPIExtensions;

namespace kOS.Suffixed.Part
{
    public class IRoboticsValue: PartValue
    {
        private readonly MuMech.MuMechToggle irMod;
        public float targetDegree;
        public float lastDegree;
        public float deltaDegree;

        public IRoboticsValue(global::Part part, MuMech.MuMechToggle module, SharedObjects sharedObj) : base(module.part, sharedObj)
        {
            this.irMod = module;
            this.targetDegree = 0;
            this.lastDegree = 0;
            this.deltaDegree = 0;
        }

        public override object GetSuffix(string suffixName)
        {
            switch (suffixName)
            {
                case "DEGREE":
                    return GetCurrentDegree();
                case "TARGETDEGREE":
                    return targetDegree;
                case "LASTDEGREE":
                    return lastDegree;
                case "DELTADEGREE":
                    return deltaDegree;
                case "TRANSLATION":
                    return irMod.translation;
                case "ROTATION":
                    return irMod.rotation;
                case "SPEED":
                    if (irMod.translateJoint) {
                        return irMod.keyTranslateSpeed;
                    } else {
                        return irMod.keyRotateSpeed;
                    }
                case "TSPEEDFINE":
                    return irMod.speedTweakFine;
                case "TSPEEDCOURSE":
                    return irMod.speedTweak;
                case "MINRANGE":
                    return irMod.minTweak;
                case "MAXRANGE":
                    return irMod.maxTweak;
                case "LOCKED":
                    return irMod.isMotionLock;
                case "SERVONAME":
                    return irMod.servoName;
                case "JOINTSPRING":
                    return irMod.jointSpring;
                case "JOINTDAMPING":
                    return irMod.jointDamping;
            }
            return base.GetSuffix(suffixName);
        }

        public override bool SetSuffix (string suffixName, object value)
        {
            switch (suffixName) {
            case "SPEED":
                var nSpeed = (float)Convert.ToDouble (value);
                if (irMod.translateJoint) {
                    irMod.keyTranslateSpeed = nSpeed;
                } else {
                    irMod.keyRotateSpeed = nSpeed;
                }
                return true;
            case "TSPEEDFINE":
                var tfSpeed = (float)Convert.ToDouble (value);
                if (tfSpeed == 0) {
                    return false;
                }
                irMod.speedTweakFine = tfSpeed;
                return true;
            case "TSPEEDCOURSE":
                var tcSpeed = (float)Convert.ToDouble (value);
                if (tcSpeed == 0) {
                    return false;
                }
                irMod.speedTweak = tcSpeed;
                return true;
            case "MINRANGE":
                var nminRange = (float)Convert.ToDouble (value);
                irMod.minTweak = nminRange;
                return true;
            case "MAXRANGE":
                var nmaxRange = (float)Convert.ToDouble (value);
                irMod.maxTweak = nmaxRange;
                return true;
            case "MOVEPOS":
                bool tp = Convert.ToBoolean(value);
                if (tp) {
                    irMod.moveFlags |= irMod.invertAxis ? 0x100 : 0x200;
                    lastDegree = GetCurrentDegree();
                } else {
                    irMod.moveFlags &= irMod.invertAxis ? ~0x100 : ~0x200;
                    deltaDegree = GetCurrentDegree() - lastDegree;
                }
                return true;
            case "MOVENEG":
                bool tn = Convert.ToBoolean(value);
                if (tn) {
                    irMod.moveFlags |= irMod.invertAxis ? 0x200 : 0x100;
                    lastDegree = GetCurrentDegree();
                } else {
                    irMod.moveFlags &= irMod.invertAxis ? ~0x200 : ~0x100;
                    deltaDegree = GetCurrentDegree() - lastDegree;
                }
                return true;
            case "MOVEORG":
                bool tr = Convert.ToBoolean(value);
                if (tr) {
                    irMod.moveFlags |= 0x400;
                    lastDegree = GetCurrentDegree();
                } else {
                    irMod.moveFlags &= ~0x400;
                    deltaDegree = GetCurrentDegree() - lastDegree;
                }
                return true;
            case "STOPMOVE":
                bool sm = Convert.ToBoolean(value);
                if (sm) {
                    irMod.moveFlags = 0x000;
                    deltaDegree = GetCurrentDegree() - lastDegree;
                }
                return true;
            case "ACTIVATE":
                bool av = Convert.ToBoolean(value);
                if (av) {
                    irMod.Activate ();
                } else {
                    irMod.Deactivate ();
                }
                return true;
            case "TARGETDEGREE":
                var td = (float)Convert.ToDouble (value);
                targetDegree = td;
                return true;                
            case "LOCK":
                bool lc = Convert.ToBoolean(value);
                irMod.SetLock(lc);
                return true;
            case "SERVONAME":
                string sn = Convert.ToString(value);
                irMod.servoName = sn;
                return true;
            case "JOINTSPRING":
                float js = (float)Convert.ToDouble(value);
                irMod.jointSpring = js;
                return true;
            case "JOINTDAMPING":
                float jd = (float)Convert.ToDouble(value);
                irMod.jointDamping = jd;
                return true;
            }

            return base.SetSuffix(suffixName, value);
        }

        private float GetCurrentDegree ()
        {
            if (irMod.translateJoint) {
                return irMod.translation;
            } else {
                return irMod.rotation;
            }
        }

        public new static ListValue PartsToList(IEnumerable<global::Part> parts, SharedObjects sharedObj)
        {
            var toReturn = new ListValue();
            foreach (var part in parts)
            {
                foreach (PartModule module in part.Modules)
                {
                    UnityEngine.Debug.Log("Module Found: "+ module);
                    var IRm = module as MuMech.MuMechToggle;
                    if (IRm != null)
                    {
                        toReturn.Add(new IRoboticsValue(part, IRm, sharedObj));
                    }
                }
            }
            return toReturn;
        }
    }
}

Plus of course adding to PrintList.cs:

case "irobotics":
list = GetIRoboticsList(shared);
break;

and:

private kList GetIRoboticsList(SharedObjects shared)
{
    var list = new kList();
    list.AddColumn("Name", 30, ColumnAlignment.Left);
    list.AddColumn("UID", 10, ColumnAlignment.Left);
    foreach (Part part in shared.Vessel.Parts)
        {
            foreach (PartModule module in part.Modules)
            {
                var IRm = module as MuMech.MuMechToggle;
                 if (IRm != null)
                 {
                      list.AddItem(part.partInfo.title, part.uid);
                 }
            }
       }

     return list;
}
Dunbaratu commented 9 years ago

This is going to be dealt with a bit differently, by #275