Example #1
0
        IEnumerable <string> CaptureTarget()
        {
            Runtime.UpdateFrequency = UpdateFrequency.Update1;
            foreach (IMySensorBlock sensor in Sensors)
            {
                sensor.Enabled = true;
            }
            yield return(null);

            MyDetectedEntityInfo entity = new MyDetectedEntityInfo();

            foreach (IMySensorBlock sensor in Sensors)
            {
                sensor.DetectedEntities(Entities);
                foreach (MyDetectedEntityInfo ent in Entities)
                {
                    if (ent.EntityId == TargetEntityID)
                    {
                        entity = ent;
                        break;
                    }
                }
            }
            if (entity.IsEmpty())
            {
                Message($"Failed to detect target with ID={TargetEntityID}");
                yield return("ReturnHome");
            }
            Timeout = ActionTimeout;
            Message("Capturing target...");
            Pilot.Tasks.Clear();
            var      goal     = new Waypoint(entity);
            Vector3D approach = entity.Position - Clamp.GetPosition();
            var      strategy = new DockingStrategy(goal, Clamp, approach);

            strategy.AutoLockDistance = 2 * entity.BoundingBox.HalfExtents.Max();
            Pilot.Tasks.Add(strategy);
            while (!Pilot.Update(Runtime.TimeSinceLastRun.TotalSeconds))
            {
                if (ActionTimeout > 0)
                {
                    Timeout -= Runtime.TimeSinceLastRun.TotalSeconds;
                }
                if (!goal.UpdateEntity(Sensors))
                {
                    Message("Aborting: target left sensor range.");
                    yield return("ReturnHome");
                }
                else if (HasToAbort())
                {
                    yield return("ReturnHome");
                }
                else
                {
                    yield return(null);
                }
            }
            Message("Target captured.");
            yield return("ReturnHome");
        }
Example #2
0
        private void PerformDocking()
        {
            if (Pilot.Tasks.Count > 0)
            {
                QuickHalt();
            }
            Vector3D dock, approach, orientation;

            if (!Front.EnableRaycast)
            {
                Front.EnableRaycast = true;
            }
            if (!Connector.IsFunctional || !GetApproachFromScan(Front, out dock, out approach, out orientation))
            {
                Echo("Can't dock.");
            }
            else
            {
                var task = new DockingStrategy(dock, Connector, approach, orientation);
                task.PositionEpsilon = 0.25;
                Pilot.Tasks.Add(task);
            }
        }
Example #3
0
        void Configure(string data, bool runtime)
        {
            MyIniParseResult result;

            ini.Clear();
            if (!ini.TryParse(data, out result))
            {
                throw new FormatException($"Failed to parse configuration:\n{result.Error}\nLine: {result.LineNo}");
            }
            AbortDistance    = ini.Get("General", "AbortDistance").ToDouble(3000.0);
            ScanningDistance = ini.Get("General", "ScanningDistance").ToDouble(40.0);
            MaxSpeed         = ini.Get("General", "MaxSpeed").ToDouble(100.0);
            ActionTimeout    = ini.Get("General", "ActionTimeout").ToDouble(120.0);
            TransmitTag      = ini.Get("General", "TransmitTag").ToString("");
            DisableAntenna   = ini.Get("General", "DisableAntenna").ToBoolean(false);
            if (!string.IsNullOrEmpty(TransmitTag))
            {
                IGC.UnicastListener.SetMessageCallback("process_message");
            }
            if (ini.ContainsKey("General", "Dock") && ini.ContainsKey("General", "DockApproach"))
            {
                if (!Vector3D.TryParse(ini.Get("General", "Dock").ToString(), out DockLocation))
                {
                    throw new FormatException("Dock location information is not correct.");
                }
                if (!Vector3D.TryParse(ini.Get("General", "DockApproach").ToString(), out DockApproachVector))
                {
                    throw new FormatException("Dock approach information is not correct.");
                }
            }
            else if (Connector.Status == MyShipConnectorStatus.Connected)
            {
                DockingStrategy.CalculateApproach(Connector.OtherConnector, out DockLocation, out DockApproachVector);
            }
            if (Vector3D.IsZero(DockLocation) && Vector3D.IsZero(DockApproachVector))
            {
                throw new Exception("Dock is not configured");
            }
            GrinderGroup = ini.Get("General", "GrinderGroup").ToString();
            if (!Vector3D.TryParse(ini.Get("General", "GrinderLocation").ToString(), out GrinderLocation) ||
                !Vector3D.TryParse(ini.Get("General", "GrinderApproach").ToString(), out GrinderApproach) ||
                Vector3D.IsZero(GrinderApproach))
            {
                IMyBlockGroup grinders = GridTerminalSystem.GetBlockGroupWithName(GrinderGroup);
                if (grinders != null)
                {
                    AnalyzeGrinders(grinders);
                }
                else
                {
                    GrinderLocation = Vector3D.Zero;
                    GrinderApproach = Vector3D.Zero;
                }
            }
            else
            {
                GrinderApproach.Normalize();
            }
            #region Parse approach vectors
            List <MyIniKey> keys  = new List <MyIniKey>();
            List <string>   lines = new List <string>();
            string          _;
            Vector3D        vec;
            Approaches.Clear();
            ini.GetKeys("Approach", keys);
            foreach (MyIniKey key in keys)
            {
                List <Vector3D> approach = new List <Vector3D>();
                ini.Get(key).GetLines(lines);
                foreach (string line in lines)
                {
                    if (Waypoint.TryParseGPS(line, out vec, out _))
                    {
                        approach.Add(vec);
                    }
                    else
                    {
                        throw new FormatException($"Approach {key.Name} waypoint not in GPS format:\n{line}");
                    }
                }
                Approaches.Add(key.Name, approach);
            }
            if (Approaches.Count == 0)
            {
                throw new Exception("No approaches configured!");
            }
            Timeout = ActionTimeout;
            #endregion
            #region Runtime items - restoring a saved state
            if (runtime)
            {
                string state = ini.Get("Runtime", "CurrentState").ToString("");
                if (string.IsNullOrEmpty(state))
                {
                    TargetLocation          = Vector3D.Zero;
                    ChosenApproach          = "";
                    ApproachIndex           = -1;
                    TargetEntityID          = 0;
                    State_Machine           = new StateMachine(StateMaker, "");
                    Runtime.UpdateFrequency = UpdateFrequency.None;
                }
                else
                {
                    Vector3D loc;
                    if (!Vector3D.TryParse(ini.Get("Runtime", "TargetLocation").ToString(""), out loc))
                    {
                        throw new Exception("Failed to restore target location");
                    }
                    else
                    {
                        TargetLocation = new Waypoint(loc, Vector3D.Zero, "Target");
                    }
                    ChosenApproach          = ini.Get("Runtime", "ChosenApproach").ToString("");
                    ApproachIndex           = ini.Get("Runtime", "ApproachIndex").ToInt32(-1);
                    TargetEntityID          = ini.Get("Runtime", "TargetEntityID").ToInt64(0);
                    State_Machine           = new StateMachine(StateMaker, state);
                    Runtime.UpdateFrequency = UpdateFrequency.Update10;
                }
            }
            else
            {
                TargetLocation          = Vector3D.Zero;
                ChosenApproach          = "";
                ApproachIndex           = -1;
                TargetEntityID          = 0;
                State_Machine           = new StateMachine(StateMaker, "");
                Runtime.UpdateFrequency = UpdateFrequency.None;
            }
            #endregion
        }