Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Engineer Chip as Sensor #17

Open
andrey-zakharov opened this issue Jan 14, 2015 · 0 comments
Open

Engineer Chip as Sensor #17

andrey-zakharov opened this issue Jan 14, 2015 · 0 comments

Comments

@andrey-zakharov
Copy link

Hi.
Just playing around with kOS and KerbalEngineer and faced to absence of sensors to detect ground.
And what if KerbalEngineer module becomes sensor and then we can probe it from kOS.

Something like this

    public class FlightEngineerModule : PartModule
    {
        [KSPField(guiActive=true)]
        public float slope = float.NaN;

        [KSPField( guiActive = true )]
        public float heading = float.NaN;

        //[KSPField( guiActive = true )]
        //SlopeReadoutField slopeReadoutField;

        /// <summary>
        /// to avoid onUpdate
        /// </summary>
        [KSPEvent( active=true, guiActive = true, guiName = "Measure Surface data" )]
        public void measureSurfaceData() {
            ///get all surface and update probes. 
            ///right now just slope @ head

            ReadoutModule slopeReadout = Readouts.ReadoutLibrary.GetReadout( "Surface.Slope" );
            // tbd ReadoutModule as PartModule.
            // now design breach!
            // BAD BAD BAD BAD BAD BAD
            Slope sl = (Slope)slopeReadout;
            sl._Update();
            this.slope = (float)sl.slope;
            this.heading = (float)sl.heading;


            //this.slopeReadoutField.slope = slope;
            //this.slopeReadoutField.heading = heading;
        }

        public override void OnStart(StartState state) {
            //if ( slopeReadoutField == null ) {
            ////    slopeReadoutField = new SlopeReadoutField();
            //}
        }

    }

in slope readout I have to break routine into two different:

    public class Slope : ReadoutModule
    {
        public double slope = float.NaN;
        public double heading = float.NaN;

        #region Constructors

        public Slope() {
            this.Name = "Slope";
            this.Category = ReadoutCategory.GetCategory( "Surface" );
            this.HelpString = "Shows the slope of the terrain below your vessel.";
            this.IsDefault = true;
        }

        #endregion

        #region Methods: public

        public override void Draw(SectionModule section) {
            this._Update();
            this.DrawLine( this.GetSlopeAngleAndHeading(), section.IsHud );
        }

        //public override void Update() {
        // base.Update();
        public void _Update() {
            var mainBody = FlightGlobals.ActiveVessel.mainBody;
            var rad = ( FlightGlobals.ActiveVessel.CoM - mainBody.position ).normalized;
            RaycastHit hit;
            if ( Physics.Raycast( FlightGlobals.ActiveVessel.CoM, -rad, out hit, Mathf.Infinity, 1 << 15 ) ) { // Just "Local Scenery" please {
                var norm = hit.normal;
                norm = norm.normalized;
                var raddotnorm = Vector3d.Dot( rad, norm );
                if ( raddotnorm > 1.0 ) {
                    raddotnorm = 1.0;
                } else if ( raddotnorm < 0.0 ) {
                    raddotnorm = 0.0;
                }
                this.slope = Math.Acos( raddotnorm ) * 180 / Math.PI;

                if ( slope < 0.05 ) {
                    this.heading = float.NaN;
                } else {
                    var side = Vector3d.Cross( rad, norm ).normalized;
                    var east = Vector3d.Cross( rad, Vector3d.up ).normalized;
                    var north = Vector3d.Cross( rad, east ).normalized;
                    var sidedoteast = Vector3d.Dot( side, east );
                    this.heading = Math.Acos( sidedoteast ) * 180 / Math.PI;

                    var sidedotnorth = Vector3d.Dot( side, north );
                    if ( sidedotnorth < 0 ) {
                        heading = 360 - heading;
                    }
                }
            }
        }

        #endregion

        #region Methods: private

        private string GetSlopeAngleAndHeading() {
            try {
                string result = double.IsNaN( slope ) ? "--°" : Units.ToAngle( slope, 1 );
                result += " @ " + ( double.IsNaN( heading ) ? "---°" : Units.ToAngle( heading, 1 ) );
                return result;
            }
            catch ( Exception ex ) {
                Logger.Exception( ex, "Surface->Slope->GetSlopeAngleAndHeading" );
                return "--° @ ---°";
            }
        }

        #endregion
    }

and voila KerbalEngineer as sensor

later i will provide patches if you want.
But it looks like it leads to some high redesign of Readouts to layout it into PartModules or IConfigNode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant