Exemplo n.º 1
0
        void doModeScans()
        {
            switch (iMode)
            {
            case 0:
                current_state = 400;
                break;

            case 400:
            {         // init camera scan for asteroids
                ResetMotion();
                turnEjectorsOn();
                sleepAllSensors();
                scaningElapsedMs = 0;

                // initialize cameras
                scanfrontScanner  = new QuadrantCameraScanner(this, cameraForwardList);
                scanbackScanner   = new QuadrantCameraScanner(this, cameraBackwardList);
                scanleftScanner   = new QuadrantCameraScanner(this, cameraLeftList);
                scanrightScanner  = new QuadrantCameraScanner(this, cameraRightList);
                scantopScanner    = new QuadrantCameraScanner(this, cameraUpList);
                scanbottomScanner = new QuadrantCameraScanner(this, cameraDownList);

                current_state = 410;
                break;
            }

            case 410:
            {
                StatusLog("Long Range Scan", textPanelReport);
                if (scanfrontScanner == null)         // in case we reload/compile in this state..
                {
                    current_state = 400;
                }
                bWantMedium       = true;
                scaningElapsedMs += Runtime.TimeSinceLastRun.TotalMilliseconds;
                // use for timeout...

                // do camera scans
                if (scanfrontScanner.DoScans())
                {
                    if (scanfrontScanner.lastDetectedInfo.Type == MyDetectedEntityType.Asteroid)
                    {
                        MinerProcessScan(scanfrontScanner.lastDetectedInfo);
                    }
                }
                else if (scanbackScanner.DoScans())
                {
                    MinerProcessScan(scanbackScanner.lastDetectedInfo);
                }
                else if (scanleftScanner.DoScans())
                {
                    MinerProcessScan(scanleftScanner.lastDetectedInfo);
                }
                else if (scanrightScanner.DoScans())
                {
                    MinerProcessScan(scanrightScanner.lastDetectedInfo);
                }
                else if (scantopScanner.DoScans())
                {
                    MinerProcessScan(scantopScanner.lastDetectedInfo);
                }
                else if (scanbottomScanner.DoScans())
                {
                    MinerProcessScan(scanbottomScanner.lastDetectedInfo);
                }
                // take the first one found.
                // TODO: do all search and then choose 'best' (closest?)
                // TODO: Aim at the hit position and not 'CENTER' for more randomized start on asteroid
                // TODO: once we find asteroid(s) choose how to find ore intelligently and not just randomly
                if (bValidAsteroid)
                {
                    current_state = 120;
                }

                string s = "";
                s += "Front: ";
                if (scanfrontScanner.DoneScanning())
                {
                    s += "DONE!";
                }
                else
                {
                    s += scanfrontScanner.SCAN_DISTANCE.ToString("0") + " meters";
                }
                s += "\n";

                s += "Back: ";
                if (scanbackScanner.DoneScanning())
                {
                    s += "DONE!";
                }
                else
                {
                    s += scanbackScanner.SCAN_DISTANCE.ToString("0") + " meters";
                }
                s += "\n";

                s += "Left: ";
                if (scanleftScanner.DoneScanning())
                {
                    s += "DONE!";
                }
                else
                {
                    s += scanleftScanner.SCAN_DISTANCE.ToString("0") + " meters";
                }
                s += "\n";

                s += "Right: ";
                if (scanrightScanner.DoneScanning())
                {
                    s += "DONE!";
                }
                else
                {
                    s += scanrightScanner.SCAN_DISTANCE.ToString("0") + " meters";
                }
                s += "\n";

                s += "Top: ";
                if (scantopScanner.DoneScanning())
                {
                    s += "DONE!";
                }
                else
                {
                    s += scantopScanner.SCAN_DISTANCE.ToString("0") + " meters";
                }
                s += "\n";

                s += "Bottom: ";
                if (scanbottomScanner.DoneScanning())
                {
                    s += "DONE!";
                }
                else
                {
                    s += scanbottomScanner.SCAN_DISTANCE.ToString("0") + " meters";
                }
                s += "\n";

                StatusLog(s, textPanelReport);

                if (
                    scanfrontScanner.DoneScanning() &&
                    scanbackScanner.DoneScanning() &&
                    scanleftScanner.DoneScanning() &&
                    scanrightScanner.DoneScanning() &&
                    scantopScanner.DoneScanning() &&
                    scanbottomScanner.DoneScanning()
                    )
                {
                    // all scans have run and didn't find asteroid..
                    //
                    setMode(MODE_ATTENTION);
                }
                break;
            }
            }
        }
Exemplo n.º 2
0
        void doModeScans()
        {
            StatusLog("clear", textPanelReport);
            StatusLog(moduleName + ":SCAN!", textPanelReport);
            Echo("Scan:current_state=" + current_state.ToString());

            switch (current_state)
            {
            case 0:
            {         // init camera scan for asteroids/objects
                ResetMotion();
                scanElapsedMs = 0;

                // initialize cameras
                scanfrontScanner  = new QuadrantCameraScanner(this, cameraForwardList, 5000);
                scanbackScanner   = new QuadrantCameraScanner(this, cameraBackwardList, 5000);
                scanleftScanner   = new QuadrantCameraScanner(this, cameraLeftList, 5000);
                scanrightScanner  = new QuadrantCameraScanner(this, cameraRightList, 5000);
                scantopScanner    = new QuadrantCameraScanner(this, cameraUpList, 5000);
                scanbottomScanner = new QuadrantCameraScanner(this, cameraDownList, 5000);

                current_state = 410;
                bWantFast     = true;
                break;
            }

            case 410:
            {
                StatusLog("Long Range Scan", textPanelReport);
                if (scanfrontScanner == null)         // in case we reload/compile in this state..
                {
                    bWantFast     = true;
                    current_state = 0;
                    return;
                }
                bWantMedium    = true;
                scanElapsedMs += Runtime.TimeSinceLastRun.TotalMilliseconds;
                // use for timeout...

                // do camera scans

                if (scanfrontScanner.DoScans())
                {
                    AsteroidProcessLDEI(scanfrontScanner.myLDEI);
                }
                if (scanbackScanner.DoScans())
                {
                    AsteroidProcessLDEI(scanbackScanner.myLDEI);
                }
                if (scanleftScanner.DoScans())
                {
                    AsteroidProcessLDEI(scanleftScanner.myLDEI);
                }
                if (scanrightScanner.DoScans())
                {
                    AsteroidProcessLDEI(scanrightScanner.myLDEI);
                }
                if (scantopScanner.DoScans())
                {
                    AsteroidProcessLDEI(scantopScanner.myLDEI);
                }
                if (scanbottomScanner.DoScans())
                {
                    AsteroidProcessLDEI(scanbottomScanner.myLDEI);
                }

                // TODO: if missing cameras on a side(s), rotate ship to point cameras at that side
                // TODO: Flags for other options (TBD)
                // TODO: scan range
                // TODO: stop on first hit (by type?)
                // TODO: all sides or specific sides?

                string s = "";
                s += "Front: ";
                if (scanfrontScanner.DoneScanning())
                {
                    s += "DONE!";
                }
                else
                {
                    s += scanfrontScanner.SCAN_DISTANCE.ToString("0") + " meters";
                }
                s += " " + scanfrontScanner.myLDEI.Count + " objects";
                s += "\n";

                s += "Back: ";
                if (scanbackScanner.DoneScanning())
                {
                    s += "DONE!";
                }
                else
                {
                    s += scanbackScanner.SCAN_DISTANCE.ToString("0") + " meters";
                }
                s += " " + scanbackScanner.myLDEI.Count + " objects";
                s += "\n";

                s += "Left: ";
                if (scanleftScanner.DoneScanning())
                {
                    s += "DONE!";
                }
                else
                {
                    s += scanleftScanner.SCAN_DISTANCE.ToString("0") + " meters";
                }
                s += " " + scanleftScanner.myLDEI.Count + " objects";
                s += "\n";

                s += "Right: ";
                if (scanrightScanner.DoneScanning())
                {
                    s += "DONE!";
                }
                else
                {
                    s += scanrightScanner.SCAN_DISTANCE.ToString("0") + " meters";
                }
                s += " " + scanrightScanner.myLDEI.Count + " objects";
                s += "\n";

                s += "Top: ";
                if (scantopScanner.DoneScanning())
                {
                    s += "DONE!";
                }
                else
                {
                    s += scantopScanner.SCAN_DISTANCE.ToString("0") + " meters";
                }
                s += " " + scantopScanner.myLDEI.Count + " objects";
                s += "\n";

                s += "Bottom: ";
                if (scanbottomScanner.DoneScanning())
                {
                    s += "DONE!";
                }
                else
                {
                    s += scanbottomScanner.SCAN_DISTANCE.ToString("0") + " meters";
                }
                s += " " + scanbottomScanner.myLDEI.Count + " objects";
                s += "\n";

                if (AsteroidFindNearest() < 0)
                {
                    s += "No Known Asteroid";
                }
                else
                {
                    s += "FOUND at least one asteroid!";
                }

                StatusLog(s, textPanelReport);
                Echo(s);

                if (
                    scanfrontScanner.DoneScanning() &&
                    scanbackScanner.DoneScanning() &&
                    scanleftScanner.DoneScanning() &&
                    scanrightScanner.DoneScanning() &&
                    scantopScanner.DoneScanning() &&
                    scanbottomScanner.DoneScanning()
                    )
                {
                    setMode(ScansDoneMode);
                    current_state = ScansDoneState;

                    // reset to default for possible next run
                    ScansDoneMode  = MODE_SCANCOMPLETED;
                    ScansDoneState = 0;
                }
                break;
            }
            }
        }
Exemplo n.º 3
0
            void doModeScans()
            {
                int iMode  = _wicoControl.IMode;
                int iState = _wicoControl.IState;

//                StatusLog("clear", textPanelReport);
//                StatusLog(moduleName + ":SCAN!", textPanelReport);
                _program.Echo("Scan:iState=" + iState.ToString());

                switch (iState)
                {
                case 0:
                {         // init camera scan for asteroids/objects
                    _program.ResetMotion();
                    scanElapsedMs = 0;

                    // initialize cameras
                    scanfrontScanner  = new QuadrantCameraScanner(_program, _cameras.GetForwardCameras(), 5000);
                    scanbackScanner   = new QuadrantCameraScanner(_program, _cameras.GetBackwardCameras(), 5000);
                    scanleftScanner   = new QuadrantCameraScanner(_program, _cameras.GetLeftCameras(), 5000);
                    scanrightScanner  = new QuadrantCameraScanner(_program, _cameras.GetRightCameras(), 5000);
                    scantopScanner    = new QuadrantCameraScanner(_program, _cameras.GetUpCameras(), 5000);
                    scanbottomScanner = new QuadrantCameraScanner(_program, _cameras.GetDownwardCameras(), 5000);

                    _wicoControl.SetState(410);        // iState = 410;
                    _wicoControl.WantFast();
                    break;
                }

                case 410:
                {
//                            StatusLog("Long Range Scan", textPanelReport);
                    if (scanfrontScanner == null)         // in case we reload/compile in this state..
                    {
                        _wicoControl.WantFast();
                        iState = 0;
                        return;
                    }
                    _wicoControl.WantMedium();
                    scanElapsedMs += _program.Runtime.TimeSinceLastRun.TotalMilliseconds;
                    // use for timeout...

                    // do camera scans

                    if (scanfrontScanner.DoScans())
                    {
                        _asteroids.AsteroidProcessLDEI(scanfrontScanner.myLDEI);
                    }
                    if (scanbackScanner.DoScans())
                    {
                        _asteroids.AsteroidProcessLDEI(scanbackScanner.myLDEI);
                    }
                    if (scanleftScanner.DoScans())
                    {
                        _asteroids.AsteroidProcessLDEI(scanleftScanner.myLDEI);
                    }
                    if (scanrightScanner.DoScans())
                    {
                        _asteroids.AsteroidProcessLDEI(scanrightScanner.myLDEI);
                    }
                    if (scantopScanner.DoScans())
                    {
                        _asteroids.AsteroidProcessLDEI(scantopScanner.myLDEI);
                    }
                    if (scanbottomScanner.DoScans())
                    {
                        _asteroids.AsteroidProcessLDEI(scanbottomScanner.myLDEI);
                    }

                    // TODO: if missing cameras on a side(s), rotate ship to point cameras at that side
                    // TODO: Flags for other options (TBD)
                    // TODO: scan range
                    // TODO: stop on first hit (by type?)
                    // TODO: all sides or specific sides?

                    string s = "";
                    s += "Front: ";
                    if (scanfrontScanner.DoneScanning())
                    {
                        s += "DONE!";
                    }
                    else
                    {
                        s += scanfrontScanner.SCAN_DISTANCE.ToString("0") + " meters";
                    }
                    s += " " + scanfrontScanner.myLDEI.Count + " objects";
                    s += "\n";

                    s += "Back: ";
                    if (scanbackScanner.DoneScanning())
                    {
                        s += "DONE!";
                    }
                    else
                    {
                        s += scanbackScanner.SCAN_DISTANCE.ToString("0") + " meters";
                    }
                    s += " " + scanbackScanner.myLDEI.Count + " objects";
                    s += "\n";

                    s += "Left: ";
                    if (scanleftScanner.DoneScanning())
                    {
                        s += "DONE!";
                    }
                    else
                    {
                        s += scanleftScanner.SCAN_DISTANCE.ToString("0") + " meters";
                    }
                    s += " " + scanleftScanner.myLDEI.Count + " objects";
                    s += "\n";

                    s += "Right: ";
                    if (scanrightScanner.DoneScanning())
                    {
                        s += "DONE!";
                    }
                    else
                    {
                        s += scanrightScanner.SCAN_DISTANCE.ToString("0") + " meters";
                    }
                    s += " " + scanrightScanner.myLDEI.Count + " objects";
                    s += "\n";

                    s += "Top: ";
                    if (scantopScanner.DoneScanning())
                    {
                        s += "DONE!";
                    }
                    else
                    {
                        s += scantopScanner.SCAN_DISTANCE.ToString("0") + " meters";
                    }
                    s += " " + scantopScanner.myLDEI.Count + " objects";
                    s += "\n";

                    s += "Bottom: ";
                    if (scanbottomScanner.DoneScanning())
                    {
                        s += "DONE!";
                    }
                    else
                    {
                        s += scanbottomScanner.SCAN_DISTANCE.ToString("0") + " meters";
                    }
                    s += " " + scanbottomScanner.myLDEI.Count + " objects";
                    s += "\n";

                    if (_asteroids.AsteroidFindNearest() < 0)
                    {
                        s += "No Known Asteroid";
                    }
                    else
                    {
                        s += "FOUND at least one asteroid!";
                    }

//                            StatusLog(s, textPanelReport);
                    _program.Echo(s);

                    if (
                        scanfrontScanner.DoneScanning() &&
                        scanbackScanner.DoneScanning() &&
                        scanleftScanner.DoneScanning() &&
                        scanrightScanner.DoneScanning() &&
                        scantopScanner.DoneScanning() &&
                        scanbottomScanner.DoneScanning()
                        )
                    {
                        _wicoControl.SetMode(ScansDoneMode, ScansDoneState);

                        // reset to default for possible next run
                        ScansDoneMode  = WicoControl.MODE_NAVNEXTTARGET;
                        ScansDoneState = 0;
                    }
                    break;
                }
                }
            }
Exemplo n.º 4
0
        void doModeFindOre()
        {
            List <IMySensorBlock> aSensors = null;
            IMySensorBlock        sb;
            IMySensorBlock        sb2;

            StatusLog("clear", textPanelReport);
            StatusLog(moduleName + ":FindOre", textPanelReport);
            Echo("FIND ORE:current_state=" + current_state.ToString());
            double maxThrust = calculateMaxThrust(thrustForwardList);
//            Echo("maxThrust=" + maxThrust.ToString("N0"));

            MyShipMass myMass;

            myMass = ((IMyShipController)shipOrientationBlock).CalculateShipMass();
            double effectiveMass = myMass.PhysicalMass;
//            Echo("effectiveMass=" + effectiveMass.ToString("N0"));

            double maxDeltaV = (maxThrust) / effectiveMass;

//            Echo("maxDeltaV=" + maxDeltaV.ToString("0.00"));

//            Echo("Cargo=" + cargopcent.ToString() + "%");

//            Echo("velocity=" + velocityShip.ToString("0.00"));
//            Echo("miningElapsedMs=" + miningElapsedMs.ToString("0.00"));

            //           IMyTextPanel txtPanel = getTextBlock("Sensor Report");
            //           StatusLog("clear", txtPanel);

            if (bValidAsteroid)
            {
                debugGPSOutput("Pre-Valid Ast", vTargetAsteroid);
            }
//            if (miningAsteroidID > 0)
            Echo("Our Asteroid=" + miningAsteroidID.ToString());

            //            if(vExpectedExit.AbsMax()>.5)
            {
//                Vector3D vT = shipOrientationBlock.GetPosition() + vExpectedExit * 150;
//                debugGPSOutput("ExpectedExit", vT);
            }
            switch (current_state)
            {
            case 0:
                if (fMiningMinThrust < fTargetMiningMps * 1.1f)
                {
                    fMiningMinThrust = fTargetMiningMps * 1.1f;
                }
                bValidAsteroid = false;     /// really?  shouuldn't we be keepint this?
                bValidExit     = false;
                bWaitingCargo  = false;

                ResetMotion();
                //                    turnDrillsOff();
                turnEjectorsOff();

                current_state = 10;
                bWantFast     = true;

                if (!HasDrills())
                {
                    current_state = 400;
                    return;
                }

                if (sensorsList.Count < 2)
                {
                    StatusLog(OurName + ":" + moduleName + " Find Ore: Not Enough Sensors!", textLongStatus, true);
                    setMode(MODE_ATTENTION);
                }
                sensorsList[0].DetectAsteroids = true;
                sensorsList[1].DetectAsteroids = true;

                if (miningAsteroidID > 0)     // return to a known asteroid
                {
                    bValidAsteroid        = true;
                    vTargetAsteroid       = AsteroidGetPosition(miningAsteroidID);
                    vExpectedAsteroidExit = vTargetAsteroid - shipOrientationBlock.GetPosition();
                    vExpectedAsteroidExit.Normalize();

                    current_state = 120;
                }

                break;

            case 10:
                //sb = sensorsList[0];
                sleepAllSensors();
                setSensorShip(sensorsList[0], 2, 2, 2, 2, 2, 2);
                //                    setSensorShip(sensorsList[1], 50, 50, 50, 50, 50, 0);
                miningElapsedMs = 0;
                current_state   = 11;
                break;

            case 11:
            {
                miningElapsedMs += Runtime.TimeSinceLastRun.TotalMilliseconds;
                if (miningElapsedMs < dSensorSettleWaitMS)
                {
                    return;
                }

                aSensors = activeSensors();
                bool bFoundAsteroid = false;
                for (int i = 0; i < aSensors.Count; i++)
                {
                    IMySensorBlock s = aSensors[i] as IMySensorBlock;
//                            StatusLog(aSensors[i].CustomName + " ACTIVE!", txtPanel);
                    Echo(aSensors[i].CustomName + " ACTIVE!");

                    List <MyDetectedEntityInfo> lmyDEI = new List <MyDetectedEntityInfo>();
                    s.DetectedEntities(lmyDEI);
                    if (AsteroidProcessLDEI(lmyDEI))
                    {
                        bFoundAsteroid = true;
                    }

/*
 *                          StatusLog("#DetectedEntities=" + lmyDEI.Count, txtPanel);
 *
 *                          strbMining.Clear();
 *                          strbMining.Append("Sensor Position:" + s.GetPosition().ToString());
 *                          strbMining.AppendLine();
 *
 *
 *                          for (int j = 0; j < lmyDEI.Count; j++)
 *                          {
 *
 *                              strbMining.Append("Name: " + lmyDEI[j].Name);
 *                              strbMining.AppendLine();
 *                              strbMining.Append("Type: " + lmyDEI[j].Type);
 *                              strbMining.AppendLine();
 *                              if (lmyDEI[j].Type == MyDetectedEntityType.Asteroid)
 *                              {
 *                                  addDetectedEntity(lmyDEI[j]);
 *                                  StatusLog("Found Asteroid!", txtPanel);
 *                                  bFoundAsteroid = true;
 *                                  currentAst.EntityId = lmyDEI[j].EntityId;
 *                                  currentAst.BoundingBox = lmyDEI[j].BoundingBox;
 *                                  bValidAsteroid = true;
 *                                  vTargetAsteroid = lmyDEI[j].Position;
 *                              }
 *                          }
 *                          StatusLog(strbMining.ToString(), txtPanel);
 */
                }
                if (bFoundAsteroid)
                {
                    current_state = 100;
                }
                else
                {
                    // no asteroid in sensor range.  Try cameras
                    current_state = 400;
                }
            }
            break;

            case 20:
            {
                // started find ore while 'inside' asteroid.
                // point towards exit

                /*
                 * vExpectedExit = shipOrientationBlock.GetPosition() - currentAst.Position;
                 * vExpectedExit.Normalize();
                 * bValidExit = true;
                 */
                current_state = 31;
            }
            break;

            case 31:
                sb  = sensorsList[0];
                sb2 = sensorsList[1];
                sleepAllSensors();
                setSensorShip(sb, 0, 0, 0, 0, 45, 0);
                setSensorShip(sb2, 5, 5, 5, 5, 0, 15);
                current_state   = 32;
                miningElapsedMs = 0;
                break;

            case 32:
                miningElapsedMs += Runtime.TimeSinceLastRun.TotalMilliseconds;
                if (miningElapsedMs < dSensorSettleWaitMS)
                {
                    return;                                            // delay for sensor settling
                }
                current_state = 35;
                break;

            case 35:
            {         // active mining
                      // TODO: check just the front sensor and we are 'exiting' if no asteroid active.
                      //
                sb  = sensorsList[0];
                sb2 = sensorsList[1];
                bool bLocalAsteroid     = false;
                bool bForwardAsteroid   = false;
                bool bSourroundAsteroid = false;
                bool bLarge             = false;
                bool bSmall             = false;
                SensorActive(sb, ref bForwardAsteroid, ref bLarge, ref bSmall);
                SensorActive(sb2, ref bSourroundAsteroid, ref bLarge, ref bSmall);

                aSensors = activeSensors();
                Echo(aSensors.Count + " Active Sensors");
                for (int i = 0; i < aSensors.Count; i++)
                {
                    IMySensorBlock s = aSensors[i] as IMySensorBlock;
                    List <MyDetectedEntityInfo> lmyDEI = new List <MyDetectedEntityInfo>();
                    s.DetectedEntities(lmyDEI);
                    if (AsteroidProcessLDEI(lmyDEI))
                    {
                        bLocalAsteroid = true;
                    }
                }
                if (!bLocalAsteroid)
                {         // we have exited the asteroid.
                    Echo("No Local Asteroid found");
                    ResetMotion();
                    if (cargopcent > cargopctlowwater || maxDeltaV < (fMiningMinThrust))
                    {
                        // we need to dump our contents
                        turnEjectorsOn();
                    }
                    current_state = 300;
                    bWantFast     = true;
                }
                else if (bSourroundAsteroid)
                {         // we are inside asteroid
                    turnEjectorsOn();
                    //                            blockApplyAction(ejectorList, "OnOff_On");
                    if (maxDeltaV < fMiningMinThrust || cargopcent > cargopcthighwater && !bWaitingCargo)         //
                    {
                        ResetMotion();
                        turnEjectorsOn();
                        bWaitingCargo = true;
                    }
                    if (bWaitingCargo)
                    {         // continue to wait
                        ResetMotion();
                        // need to check how much stone we have.. if zero(ish), then we're full.. go exit.
                        OreDoCargoCheck();
                        if (currentStoneAmount() < 15)
                        {
                            // we are full and not much stone ore in us...
                            ResetMotion();
                            turnEjectorsOff();
                            setMode(MODE_EXITINGASTEROID);
                        }
                        // TODO: Needs time-out
                        Echo("Cargo above low water: Waiting");
                        if (maxDeltaV > fMiningMinThrust && cargopcent < cargopctlowwater)
                        {
                            bWaitingCargo = false;         // can now move.
                        }
                    }
                    else
                    {
                        GyroMain("forward", vExpectedAsteroidExit, shipOrientationBlock);
                        //                                GyroMain("forward", vExpectedExit, shipOrientationBlock);
                        turnDrillsOn();
                        mineMoveForward(fTargetMiningMps, fMiningAbortMps);
                        //                                bWantFast = true;
                        bWantMedium = true;
                    }
                }
                else
                {
                    // we have nothing in front, but are still close
                    turnDrillsOff();
                    mineMoveForward(fAsteroidApproachMps, fAsteroidApproachAbortMps);
                    bWantMedium = true;
                }
            }
            break;

            case 100:
                turnDrillsOff();
                sleepAllSensors();
                sb = sensorsList[0];
                setSensorShip(sb, 0, 0, 0, 0, 50, 0);
                current_state   = 101;
                miningElapsedMs = 0;
                break;

            case 101:
                miningElapsedMs += Runtime.TimeSinceLastRun.TotalMilliseconds;
                if (miningElapsedMs < dSensorSettleWaitMS)
                {
                    return;                                            // delay for sensor settling
                }
                current_state++;
                break;

            case 102:
            {
                aSensors       = activeSensors();
                bValidAsteroid = false;
                for (int i = 0; i < aSensors.Count; i++)
                {
                    IMySensorBlock s = aSensors[i] as IMySensorBlock;
//                            StatusLog(aSensors[i].CustomName + " ACTIVE!", txtPanel);
                    Echo(aSensors[i].CustomName + " ACTIVE!");

                    List <MyDetectedEntityInfo> lmyDEI = new List <MyDetectedEntityInfo>();
                    s.DetectedEntities(lmyDEI);
                    AsteroidProcessLDEI(lmyDEI);
                }
                //                        if (!bValidAsteroid)
                {
                    // not in sensor range. start checking cameras
                    double scandist = 500;

                    /*
                     *                          if (bValidAsteroid)
                     *                          {
                     *                              scandist = (shipOrientationBlock.GetPosition() - vTargetAsteroid).Length();
                     *                          }
                     */
                    if (doCameraScan(cameraForwardList, scandist))
                    {         // we scanned
                        if (!lastDetectedInfo.IsEmpty())
                        {     // we hit something
                            if (lastDetectedInfo.Type == MyDetectedEntityType.Asteroid)
                            {
                                MinerProcessScan(lastDetectedInfo);

                                /*
                                 * addDetectedEntity(lastDetectedInfo);
                                 * vTargetAsteroid = (Vector3D)lastDetectedInfo.HitPosition;
                                 * bValidAsteroid = true;
                                 * vExpectedExit = vTargetAsteroid - shipOrientationBlock.GetPosition();
                                 * vExpectedExit.Normalize();
                                 * bValidExit = true;
                                 */
                            }
                        }
                        else
                        {
                            // no asteroid detected.  Check surroundings for one.
                            current_state = 110;
                            bValidExit    = false;
                        }
                    }
                }
                if (bValidExit)
                {
                    current_state = 120;                     //found asteroid ahead
                }
            }
            break;

            case 110:
            {         // asteroid NOT in front. big sensor search for asteroids in area
                sleepAllSensors();
                setSensorShip(sensorsList[0], 50, 50, 50, 50, 50, 50);
                miningElapsedMs = 0;
                current_state   = 111;
            }
            break;

            case 111:
                miningElapsedMs += Runtime.TimeSinceLastRun.TotalMilliseconds;
                if (miningElapsedMs < dSensorSettleWaitMS)
                {
                    return;                                            // delay for sensor settling
                }
                current_state++;
                break;

            case 112:
            {         // asteroid not in front. Check sensors
                aSensors = activeSensors();
                //                        bValidAsteroid = false;
                for (int i = 0; i < aSensors.Count; i++)
                {
                    IMySensorBlock s = aSensors[i] as IMySensorBlock;
//                            StatusLog(aSensors[i].CustomName + " ACTIVE!", txtPanel);
                    Echo(aSensors[i].CustomName + " ACTIVE!");

                    List <MyDetectedEntityInfo> lmyDEI = new List <MyDetectedEntityInfo>();
                    s.DetectedEntities(lmyDEI);
                    AsteroidProcessLDEI(lmyDEI);
                }
                if (bValidAsteroid)
                {
                    bWantFast = true;
                    if (GyroMain("forward", vTargetAsteroid - shipOrientationBlock.GetPosition(), shipOrientationBlock))
                    {
                        // we are aimed
                        current_state = 100;
                    }
                }
            }
            break;

            case 120:
                sleepAllSensors();
                setSensorShip(sensorsList[0], 5, 5, 5, 5, 25, 0);
                //                    setSensorShip(sensorsList[1], 50, 50, 50, 50, 50, 0);
                miningElapsedMs = 0;
                current_state   = 121;

                break;

            case 121:
            {         // approach
                if (!bValidAsteroid)
                {
                    current_state = 10;
                    return;
                }
                bWantFast = true;

                // we should have asteroid in front.
                bool bAimed = true;

                Echo(bValidExit.ToString() + " " + Vector3DToString(vExpectedAsteroidExit));
                bAimed = GyroMain("forward", vExpectedAsteroidExit, shipOrientationBlock);

                miningElapsedMs += Runtime.TimeSinceLastRun.TotalMilliseconds;
                if (miningElapsedMs < dSensorSettleWaitMS)
                {
                    return;
                }

                if (bAimed)
                {
                    double scandist;
                    double distanceSQ = 0;
                    if (bValidAsteroid)
                    {
                        distanceSQ = (vTargetAsteroid - shipOrientationBlock.GetPosition()).LengthSquared();
                        scandist   = distanceSQ;
                        Echo("distanceSQ=" + distanceSQ.ToString("0.00"));

                        // TODO: we need to keep the radius/BB of the target asteroid..
                        //                                if (lastDetectedInfo.BoundingBox.Contains(vTargetLocation) != ContainmentType.Disjoint)
                        if (distanceSQ > 512 * 512)
                        {
                            current_state = 130;
                            bWantFast     = true;
                            return;
                        }
                    }
                    else
                    {
                        scandist = 500;          // unknown scan distance
                    }
                    bWantFast   = false;
                    bWantMedium = true;
                    if (doCameraScan(cameraForwardList, scandist))
                    {
                        if (!lastDetectedInfo.IsEmpty())
                        {
                            // we have a target
                            //                                   if (lmyDEI[j].Type == MyDetectedEntityType.Asteroid)
                            {
                                Vector3D vTarget  = (Vector3D)lastDetectedInfo.HitPosition;
                                double   distance = (vTarget - lastCamera.GetPosition()).Length();
                                Echo("Distance=" + distance.ToString());
                                if (distance < 15)
                                {
                                    vLastAsteroidContact = shipOrientationBlock.GetPosition();
                                    if (!bValidInitialAsteroidContact)
                                    {
                                        vInitialAsteroidContact      = vLastAsteroidContact;
                                        bValidInitialAsteroidContact = true;
                                    }
                                    current_state = 31;
                                    ResetMotion();
                                }
                                else if (distance > 100)
                                {
                                    vInitialAsteroidContact = (Vector3D)lastDetectedInfo.HitPosition;
                                    current_state           = 130;// do faster travel
                                }
                                else
                                {
                                    mineMoveForward(fAsteroidApproachMps, fAsteroidApproachAbortMps);
                                }
                            }
                            //                                      bValidExit = true;
                            //                                        vExpectedExit = vTarget - shipOrientationBlock.GetPosition();
                        }
                        else
                        {
                            /*
                             * // we scanned, but didn't hit anything.  it's likely a donut
                             * double distance = (vTargetAsteroid - shipOrientationBlock.GetPosition()).Length();
                             * Echo("Distance=" + distance.ToString());
                             * mineMoveForward(fTargetMiningmps,fAbortmps);
                             * if (distance < 5)
                             * {
                             *  vExpectedExit = vTargetAsteroid - shipOrientationBlock.GetPosition();
                             *  current_state = 31;
                             *  ResetMotion();
                             * }
                             */
                            //                                    setMode(MODE_ATTENTION);
                            // or we made a whole there..
                        }
                    }
                    // ELSE no scan available.. just wait
                    else
                    {
                        Echo("No Camera SCAN!");
                    }

                    // check sensors for one in front of us
                    aSensors = activeSensors();
                    bool bFoundAsteroid = false;
                    for (int i = 0; i < aSensors.Count; i++)
                    {
                        IMySensorBlock s = aSensors[i] as IMySensorBlock;
//                                StatusLog(aSensors[i].CustomName + " ACTIVE!", txtPanel);
//                                Echo(aSensors[i].CustomName + " ACTIVE!");

                        List <MyDetectedEntityInfo> lmyDEI = new List <MyDetectedEntityInfo>();
                        s.DetectedEntities(lmyDEI);
                        if (AsteroidProcessLDEI(lmyDEI))
                        {
                            bFoundAsteroid = true;
                        }
                    }
                    if (bFoundAsteroid)
                    {
                        current_state = 31;
                    }
                }
                else
                {
                    Echo("Aiming");
                }
                // BUG: Gets stuck in state if nothing hit.. (ie, donut)
            }
            break;

            case 130:
            {         // far travel to asteroid. use doTravelMovement
                Echo("Far travel to asteroid");

                doTravelMovement(vTargetAsteroid, 45, 135, 140, true);
                break;
            }

            case 135:
            {        // we have 'arrived' from far travel
                // wait for motion to slow
                if (velocityShip < fAsteroidApproachMps)
                {
                    current_state = 120;
                }
                ResetMotion();
                break;
            }

            case 140:
            {
                ResetTravelMovement();
                ResetMotion();
                calcCollisionAvoid(vInitialAsteroidContact);
                current_state = 150;
                break;
            }

            case 150:
            {
                doTravelMovement(vAvoid, 5.0f, 130, 160);
                break;
            }

            case 160:
            {
                ResetTravelMovement();
                ResetMotion();
                setMode(MODE_ATTENTION);
                break;
            }

            case 300:
            {
                // we have exitted the asteroid.  Prepare for another run or to go dock
                Echo("Exitted!");
                ResetMotion();
                sleepAllSensors();

                if (maxDeltaV < fMiningMinThrust || cargopcent > cargopctlowwater)
                {
                    setMode(MODE_DOCKING);
                }
                else
                {
                    vLastAsteroidExit = shipOrientationBlock.GetPosition();
                    //                           dist = (vLastExit - vInitialExit).Length();
                    if (!bValidInitialAsteroidExit)
                    {
                        vInitialAsteroidExit      = vLastAsteroidExit;
                        bValidInitialAsteroidExit = true;
                    }
                    setMode(MODE_SEARCHORIENT);
                    // prepare for another run.
                }
                break;
            }

            case 400:
            {         // init camera scan for asteroids
                ResetMotion();
                turnEjectorsOn();
                sleepAllSensors();
                miningElapsedMs = 0;

                // initialize cameras
                miningfrontScanner  = new QuadrantCameraScanner(this, cameraForwardList, 5000);
                miningbackScanner   = new QuadrantCameraScanner(this, cameraBackwardList, 5000);
                miningleftScanner   = new QuadrantCameraScanner(this, cameraLeftList, 5000);
                miningrightScanner  = new QuadrantCameraScanner(this, cameraRightList, 5000);
                miningtopScanner    = new QuadrantCameraScanner(this, cameraUpList, 5000);
                miningbottomScanner = new QuadrantCameraScanner(this, cameraDownList, 5000);

                current_state = 410;
                break;
            }

            case 410:
            {
                StatusLog("Long Range Scan", textPanelReport);
                if (miningfrontScanner == null)         // in case we reload/compile in this state..
                {
                    current_state = 400;
                }
                bWantMedium      = true;
                miningElapsedMs += Runtime.TimeSinceLastRun.TotalMilliseconds;
                // use for timeout...

                // do camera scans

                if (miningfrontScanner.DoScans())
                {
                    AsteroidProcessLDEI(miningfrontScanner.myLDEI);
                }
                if (miningbackScanner.DoScans())
                {
                    AsteroidProcessLDEI(miningbackScanner.myLDEI);
                }
                if (miningleftScanner.DoScans())
                {
                    AsteroidProcessLDEI(miningleftScanner.myLDEI);
                }
                if (miningrightScanner.DoScans())
                {
                    AsteroidProcessLDEI(miningrightScanner.myLDEI);
                }
                if (miningtopScanner.DoScans())
                {
                    AsteroidProcessLDEI(miningtopScanner.myLDEI);
                }
                if (miningbottomScanner.DoScans())
                {
                    AsteroidProcessLDEI(miningbottomScanner.myLDEI);
                }

                // take the first one found.
                // TODO: do all search and then choose 'best' (closest?)
                // TODO: Aim at the hit position and not 'CENTER' for more randomized start on asteroid
                // TODO: once we find asteroid(s) choose how to find ore intelligently and not just randomly

                /*
                 * if (bValidAsteroid)
                 *  current_state = 120;
                 */
                string s = "";
                s += "Front: ";
                if (miningfrontScanner.DoneScanning())
                {
                    s += "DONE!";
                }
                else
                {
                    s += miningfrontScanner.SCAN_DISTANCE.ToString("0") + " meters";
                }
                s += " " + miningfrontScanner.myLDEI.Count + " asteroids";
                s += "\n";

                s += "Back: ";
                if (miningbackScanner.DoneScanning())
                {
                    s += "DONE!";
                }
                else
                {
                    s += miningbackScanner.SCAN_DISTANCE.ToString("0") + " meters";
                }
                s += " " + miningbackScanner.myLDEI.Count + " asteroids";
                s += "\n";

                s += "Left: ";
                if (miningleftScanner.DoneScanning())
                {
                    s += "DONE!";
                }
                else
                {
                    s += miningleftScanner.SCAN_DISTANCE.ToString("0") + " meters";
                }
                s += " " + miningleftScanner.myLDEI.Count + " asteroids";
                s += "\n";

                s += "Right: ";
                if (miningrightScanner.DoneScanning())
                {
                    s += "DONE!";
                }
                else
                {
                    s += miningrightScanner.SCAN_DISTANCE.ToString("0") + " meters";
                }
                s += " " + miningrightScanner.myLDEI.Count + " asteroids";
                s += "\n";

                s += "Top: ";
                if (miningtopScanner.DoneScanning())
                {
                    s += "DONE!";
                }
                else
                {
                    s += miningtopScanner.SCAN_DISTANCE.ToString("0") + " meters";
                }
                s += " " + miningtopScanner.myLDEI.Count + " asteroids";
                s += "\n";

                s += "Bottom: ";
                if (miningbottomScanner.DoneScanning())
                {
                    s += "DONE!";
                }
                else
                {
                    s += miningbottomScanner.SCAN_DISTANCE.ToString("0") + " meters";
                }
                s += " " + miningbottomScanner.myLDEI.Count + " asteroids";
                s += "\n";

                if (AsteroidFindNearest() < 0)
                {
                    s += "No Known Asteroid";
                }
                else
                {
                    s += "FOUND at least one asteroid!";
                }

                StatusLog(s, textPanelReport);
                Echo(s);


                if (
                    miningfrontScanner.DoneScanning() &&
                    miningbackScanner.DoneScanning() &&
                    miningleftScanner.DoneScanning() &&
                    miningrightScanner.DoneScanning() &&
                    miningtopScanner.DoneScanning() &&
                    miningbottomScanner.DoneScanning()
                    )
                {
                    //                            long asteroidID = -1;
                    if (HasDrills())
                    {
                        miningAsteroidID = AsteroidFindNearest();
                        if (miningAsteroidID < 0)
                        {
                            // all scans have run and didn't find asteroid..
                            setMode(MODE_ATTENTION);
                        }
                        else
                        {
                            bValidAsteroid        = true;
                            vTargetAsteroid       = AsteroidGetPosition(miningAsteroidID);
                            vExpectedAsteroidExit = vTargetAsteroid - shipOrientationBlock.GetPosition();
                            vExpectedAsteroidExit.Normalize();

                            current_state = 120;
                        }
                    }
                    else
                    {         // if no drills, we are done.
                        setMode(MODE_IDLE);
                    }
                }
                break;
            }

            case 425:
            {
                // aim at asteroid location
                if (!bValidExit)
                {
                    setMode(MODE_ATTENTION);
                }
                {
                    bWantFast = true;
                    if (GyroMain("forward", vExpectedAsteroidExit - shipOrientationBlock.GetPosition(), shipOrientationBlock))
                    {
                        // we are aimed
                        current_state = 120;
                    }
                }
                break;
            }
            }
        }