Whiplash141

Whip's GASP Missile Script v37.4c

Mar 6th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 23.88 KB | None | 0 0
  1.    
  2. /*  
  3. Whip's GASP Missile System v37.4c - revised: 2/27/16
  4. /// PERSONAL VERSION ///
  5. /// Stable ///
  6.     Missile 1
  7. ___________________________________________________________________  
  8. Description:    
  9.     This script allows the user to manually guide a missile with the
  10.     heading of his/her ship.
  11.    
  12. To-do:
  13.     -account for landing gear rotors and connectors
  14.     -account for reactors as power source
  15.  
  16. Changes:
  17.     -changed dT from ideal to measured
  18.     -added PD to roll
  19.     -added CoM offset
  20.     -alysius' drift concept
  21.    
  22. Code by Whiplash141 :)    
  23. */    
  24.  
  25. /*
  26. ___________________________________________________________________  
  27.    
  28. ========== You can edit these variables to your liking ============  
  29. ___________________________________________________________________  
  30. */
  31.  
  32. //---Missile name  
  33.     const string missileNumber = "1";
  34.     const string strMissileTag = "Missile " + missileNumber; //(MANDATORY) unique id of the missile
  35.     const string strMainThrustTag = "Main"; //(MANDATORY) tag on main forward thrusters
  36.     const string strSideThrustTag = "Side"; //(Optional) tag on side thrusters  
  37.     const string strDetachThrustTag = "Detach"; //(Optional) tag on detach thrust
  38.  
  39. //---Reference name
  40.     const string strShooterReferenceName = "Shooter Reference"; //name of the remote on the shooter vessel
  41.  
  42. //---Runtime vars  
  43.     const int updates_per_second = 10; //self explanatory :P
  44.     const double battery_discharge_duration = 1;
  45.     const double guidance_delay = 1; //time (in seconds) that the missile will delay guidance activation by
  46.     const double detach_duration = 1; //time that the missile will execute detaching function
  47.     const double main_ignition_delay =  1; //time (in seconds) that the missile will delay main engine activation
  48.    
  49. //---Control system
  50.     const double proportionalConstant = 100;
  51.     const double derivativeConstant = 50;
  52.  
  53. //---Drift compensation
  54.     static bool driftCompensation = true;
  55.     const double compensationConstant = 2;
  56.     const double centerOfMassOffset = -1.5 * .5; //in meters measured from the control gyro
  57. /*  
  58. ___________________________________________________________________  
  59.    
  60. ============= Don't touch anything below this :) ==================  
  61. ___________________________________________________________________  
  62. */
  63. //---So many lists...
  64.     List<IMyTerminalBlock> missileBlocks = new List<IMyTerminalBlock>();
  65.     List<IMyTerminalBlock> mainThrusters = new List<IMyTerminalBlock>();
  66.     List<IMyTerminalBlock> sideThrusters = new List<IMyTerminalBlock>();
  67.     List<IMyTerminalBlock> detachThrusters = new List<IMyTerminalBlock>();
  68.     List<IMyTerminalBlock> artMasses = new List<IMyTerminalBlock>();
  69.     List<IMyTerminalBlock> mergeBlocks = new List<IMyTerminalBlock>();
  70.     List<IMyTerminalBlock> batteries = new List<IMyTerminalBlock>();
  71.     List<IMyTerminalBlock> remotes = new List<IMyTerminalBlock>();
  72.     List<IMyTerminalBlock> shooterRefrenceList = new List<IMyTerminalBlock>();
  73.     List<IMyTerminalBlock> gyros = new List<IMyTerminalBlock>();
  74.     List<IMyTerminalBlock> timers = new List<IMyTerminalBlock>();
  75.     List<IMyTerminalBlock> programs = new List<IMyTerminalBlock>();
  76.     List<IMyTerminalBlock> importantBlocks = new List<IMyTerminalBlock>();
  77.  
  78.     Vector3D shooterFrontNorm;
  79.     Vector3D shooterLeftNorm;
  80.     Vector3D shooterUpNorm;
  81.     Vector3D originPos;
  82.     Vector3D missilePos;
  83.     Vector3D lastMissilePos;
  84.     Vector3D headingVec;
  85.     Vector3D destinationVec;
  86.     Vector3D gravVec;
  87.  
  88.     IMyRemoteControl shooterRefrence;
  89.     IMyGyro missileRefrence;
  90.    
  91.     bool isSetup = false;
  92.     bool firstRun = true;
  93.     bool shouldKill = false;
  94.     bool firstGuidance = true;
  95.     bool hasPassed = false;
  96.     bool killAllowed = false;
  97.     bool inGravity = false;
  98.     bool controlGyros = true;
  99.    
  100.     bool missileStage1 = false;
  101.     bool missileStage2 = false;
  102.     bool missileStage3 = false;
  103.     bool missileStage4 = false;
  104.  
  105.    
  106.     double distanceFromShooter;  
  107.     double max_kill_time = 3;
  108.     double kill_time = 0;  
  109.     double timeElapsed = 0; //time elapsed over current iterations
  110.     double timeTotal = 0; //total time program has been running
  111.     double lastYawAngle = 0;
  112.     double lastPitchAngle = 0;
  113.     double lastRollAngle = 0;
  114.    
  115.     static double max_distance = 10000; //static b/c we change it on kill command
  116.     const double degToRad = Math.PI / 180;
  117.     const double radToDeg = 180 / Math.PI;
  118.     const double max_time_to_guide = 300; //in seconds
  119.     const double timeLimit =  1 / (double)updates_per_second;
  120.     double compensationBounds = Math.Acos(1 / compensationConstant) * radToDeg;
  121.  
  122. void Main( string arg )
  123. {
  124.     if( arg.ToLower() == "run" )
  125.     {
  126.         if (!isSetup)
  127.         {
  128.             GrabBlocks();
  129.         }
  130.         else //will not run or release missile until has run setup succesfully  
  131.         {  
  132.  
  133.             if( firstRun )
  134.             {
  135.                 timeElapsed = 0;
  136.                 timeTotal = 0;
  137.                 firstRun = false;
  138.             }else{
  139.                 timeElapsed += ElapsedTime.TotalSeconds;
  140.                 timeTotal += ElapsedTime.TotalSeconds;
  141.             }
  142.  
  143.             LaunchMissile();  
  144.             StatusCheck();
  145.             if( timeTotal > guidance_delay + battery_discharge_duration && timeElapsed >= timeLimit)  
  146.             {
  147.                 Echo("Guidance Active"); Echo("Run Time: " + Math.Round(timeTotal).ToString());
  148.                 GuideMissile();
  149.                 timeElapsed = 0;
  150.             }
  151.         }
  152.     }
  153.     else if(arg.ToLower() == "setup")
  154.     {
  155.         GrabBlocks();
  156.     }
  157.     else if(arg.ToLower() == "kill" && killAllowed)
  158.     {
  159.         shouldKill = true;
  160.         max_distance = double.PositiveInfinity;
  161.     }
  162.    
  163.     if(timeTotal > max_time_to_guide)
  164.     {
  165.         shouldKill = true;
  166.         max_distance = double.PositiveInfinity;
  167.     }
  168. }        
  169.    
  170. void GrabBlocks()    
  171. {    
  172.     GridTerminalSystem.SearchBlocksOfName( strMissileTag, missileBlocks );  
  173.     GridTerminalSystem.SearchBlocksOfName( strShooterReferenceName, shooterRefrenceList );  
  174.    
  175.     for( int i = 0; i < shooterRefrenceList.Count; i++ )  
  176.     {  
  177.         importantBlocks.Add( shooterRefrenceList[i] );  
  178.     }  
  179.  
  180.     for( int i = 0; i < missileBlocks.Count; i++ )  
  181.     {
  182.         var thisBlock = missileBlocks[i] as IMyTerminalBlock;  
  183.  
  184.         if( thisBlock is IMyThrust )  
  185.         {
  186.             if( thisBlock.CustomName.Contains( strSideThrustTag ) &&
  187.                 thisBlock.CustomName.Contains( strDetachThrustTag ) )  
  188.             {
  189.                 sideThrusters.Add( thisBlock );
  190.                 detachThrusters.Add( thisBlock );
  191.             }
  192.             else if( thisBlock.CustomName.Contains( strMainThrustTag ) &&
  193.                 thisBlock.CustomName.Contains( strDetachThrustTag ) )
  194.             {
  195.                 mainThrusters.Add( thisBlock );
  196.                 detachThrusters.Add( thisBlock );
  197.             }
  198.             else if( thisBlock.CustomName.Contains( strSideThrustTag ) )  
  199.             {
  200.                 sideThrusters.Add( thisBlock );  
  201.             }
  202.             else if( thisBlock.CustomName.Contains( strMainThrustTag ) )  
  203.             {
  204.                 mainThrusters.Add( thisBlock );  
  205.             }
  206.             else if( thisBlock.CustomName.Contains ( strDetachThrustTag ) )
  207.             {
  208.                 detachThrusters.Add( thisBlock );
  209.             }
  210.         }
  211.         else if( thisBlock is IMyVirtualMass )  
  212.         {
  213.             artMasses.Add( thisBlock );  
  214.         }
  215.         else if( thisBlock is IMyBatteryBlock )  
  216.         {
  217.             batteries.Add( thisBlock );  
  218.         }
  219.         else if( thisBlock is IMyGyro )  
  220.         {
  221.             gyros.Add( thisBlock );  
  222.             importantBlocks.Add( thisBlock );  
  223.         }
  224.         else if( thisBlock is IMyShipMergeBlock )  
  225.         {
  226.             mergeBlocks.Add( thisBlock );  
  227.         }
  228.         else if( thisBlock is IMyTimerBlock )  
  229.         {
  230.             timers.Add( thisBlock );  
  231.             importantBlocks.Add( thisBlock );  
  232.         }
  233.         else if( thisBlock is IMyProgrammableBlock )  
  234.         {
  235.             programs.Add( thisBlock );  
  236.             importantBlocks.Add( thisBlock );  
  237.         }
  238.         else if( thisBlock is IMyRemoteControl )
  239.         {
  240.             remotes.Add( thisBlock );
  241.         }
  242.     }
  243.  
  244.     if( sideThrusters.Count == 0 )
  245.     {
  246.         Echo("[OPTIONAL] No side thrusters found");
  247.     }
  248.    
  249.     if( detachThrusters.Count == 0 )
  250.     {
  251.         Echo("[OPTIONAL] No detach thrusters found");
  252.     }
  253.    
  254.     if( artMasses.Count == 0 )
  255.     {
  256.         Echo("[OPTIONAL] No artificial masses found");
  257.     }
  258.    
  259.     if( shooterRefrenceList.Count == 0 )    
  260.     {    
  261.         Echo("[FAILED] No shooter refrence block found");    
  262.         isSetup = false;    
  263.         return;    
  264.     }      
  265.     else if( gyros.Count == 0 )    
  266.     {    
  267.         Echo("[FAILED] No control gyros found");    
  268.         isSetup = false;    
  269.         return;    
  270.     }    
  271.     else if( mainThrusters.Count == 0)  
  272.     {  
  273.         Echo("[FAILED] No main thrusters found");  
  274.         isSetup = false;  
  275.         return;  
  276.     }  
  277.     else if( batteries.Count == 0)  
  278.     {  
  279.         Echo("[FAILED] No batteries found");  
  280.         isSetup = false;  
  281.         return;  
  282.     }  
  283.     else if( mergeBlocks.Count == 0)  
  284.     {  
  285.         Echo("[FAILED] No merge blocks found");  
  286.         isSetup = false;  
  287.         return;  
  288.     }  
  289.     else    
  290.     {      
  291.         Echo("[SUCCESS] Ready to run");    
  292.         shooterRefrence = shooterRefrenceList[0] as IMyRemoteControl;    
  293.         missileRefrence = gyros[0] as IMyGyro;    
  294.         isSetup = true;    
  295.     }    
  296. }    
  297.    
  298. void StatusCheck()  
  299. {  
  300.     for (int k = 0; k < importantBlocks.Count; k++)    
  301.     {    
  302.         IMyTerminalBlock block = importantBlocks[k];    
  303.         IMySlimBlock slim = block.CubeGrid.GetCubeBlock(block.Position);  
  304.         if( slim.CurrentDamage > 0 )  
  305.         {  
  306.             Echo("Damage");  
  307.             kill_time = max_kill_time;  
  308.             KillGuidance( 0, 0 );  
  309.             return;  
  310.         }  
  311.     }  
  312. }  
  313.  
  314. void LaunchMissile()
  315. {
  316. //stage 1
  317.     if ( !missileStage1 ) //set battery to discharge
  318.     {
  319.         for(int i = 0 ; i < batteries.Count ; i++)
  320.         {
  321.             var thisBattery = batteries[i] as IMyBatteryBlock;
  322.             if( thisBattery != null )
  323.             {
  324.                 thisBattery.ApplyAction( "OnOff_On" ); //make sure our battery is on
  325.                 thisBattery.SetValue( "Recharge", false );
  326.             }
  327.         }
  328.         missileStage1 = true;
  329.     }
  330. //stage 2
  331.     else if( timeTotal >= battery_discharge_duration && !missileStage2 ) //detach missile
  332.     {
  333.         for( int i = 0 ; i < artMasses.Count ; i++ )
  334.         {
  335.             var thisMass = artMasses[i] as IMyVirtualMass;
  336.             if( thisMass != null ) thisMass.ApplyAction( "OnOff_On" );
  337.         }
  338.        
  339.         for( int i = 0; i < gyros.Count; i++ )
  340.         {
  341.             var thisGyro = gyros[i] as IMyGyro;
  342.             if( thisGyro != null ) thisGyro.ApplyAction("OnOff_On");
  343.         }
  344.  
  345.         for( int i = 0 ; i < mergeBlocks.Count ; i++ )
  346.         {
  347.             var thisMerge = mergeBlocks[i] as IMyShipMergeBlock;      
  348.             if( thisMerge != null ) thisMerge.ApplyAction( "OnOff_Off" );      
  349.         }
  350.  
  351.         for( int i = 0; i < detachThrusters.Count; i++ )
  352.         {
  353.             var thisThrust = detachThrusters[i] as IMyThrust;
  354.             if( thisThrust != null )
  355.             {
  356.                 thisThrust.ApplyAction( "OnOff_On" );
  357.                 thisThrust.SetValue<float>( "Override", float.MaxValue );
  358.             }
  359.         }
  360.  
  361.         ManeuveringThrust( false );
  362.         killAllowed = true;
  363.         missileStage2 = true;
  364.        
  365.     }
  366. //stage 3
  367.     else if( timeTotal >= battery_discharge_duration + detach_duration && !missileStage3 )
  368.     {
  369.         ManeuveringThrust( true );
  370.        
  371.         for( int i = 0; i < detachThrusters.Count; i++ )
  372.         {
  373.             var thisThrust = detachThrusters[i] as IMyThrust;
  374.             if( thisThrust != null ) thisThrust.SetValue<float>( "Override", float.MinValue );
  375.         }
  376.         missileStage3 = true;
  377.     }
  378. //stage 4
  379.     else if( timeTotal >= battery_discharge_duration + main_ignition_delay + detach_duration && !missileStage4) //fire missile    
  380.     {
  381.         for( int i = 0 ; i < artMasses.Count ; i++ )  
  382.         {
  383.             var thisMass = artMasses[i] as IMyVirtualMass;  
  384.             if( thisMass != null ) thisMass.ApplyAction( "OnOff_Off" );  
  385.         }
  386.        
  387.         for( int i = 0 ; i < mergeBlocks.Count ; i++ ) //for safety
  388.         {
  389.             var thisMerge = mergeBlocks[i] as IMyShipMergeBlock;      
  390.             if( thisMerge != null && thisMerge.GetValue<bool>("OnOff") ) thisMerge.ApplyAction( "OnOff_Off" );      
  391.         }
  392.        
  393.         MainThrustOverride();            
  394.         missileStage4 = true;  
  395.     }
  396. }
  397.  
  398. void GuideMissile()
  399. {
  400. //---Get positions of our blocks with relation to world center    
  401.     if( !shouldKill )
  402.         originPos = shooterRefrence.GetPosition();    
  403.  
  404.     missilePos = missileRefrence.GetPosition();  
  405.  
  406. //---Find current distance from shooter to missile    
  407.     distanceFromShooter = Vector3D.Distance( originPos, missilePos );    
  408.    
  409. //---Check if we are in range
  410.     if( distanceFromShooter > max_distance )  
  411.     {
  412.         Echo( "Out of range" );
  413.         shouldKill = true;
  414.     }
  415.  
  416. //---Get orientation vectors from our shooter vessel    
  417.     if( !shouldKill )  
  418.     {  
  419.         MatrixD shoterOrientation = shooterRefrence.WorldMatrix;
  420.         Vector3D shooterForwardVec = shoterOrientation.Forward;
  421.         Vector3D shooterLeftVec = shoterOrientation.Left;
  422.         Vector3D shooterUpVec = shoterOrientation.Up;
  423.  
  424.         shooterFrontNorm = Vector3D.Normalize( shooterForwardVec );
  425.         shooterLeftNorm = Vector3D.Normalize( shooterLeftVec );
  426.         shooterUpNorm = Vector3D.Normalize( shooterUpVec );
  427.     }  
  428.  
  429. //---Find vector from shooter to missile    
  430.     var shooterToMissile = missilePos - originPos;
  431.  
  432. //---Determine where missile is in relation to shooter
  433.     bool isLeft = CheckDotPositive( shooterToMissile, shooterLeftNorm );
  434.     bool isUp = CheckDotPositive( shooterToMissile, shooterUpNorm );
  435.     int signLeft; int signUp;
  436.     if( isLeft )
  437.         signLeft = 1;
  438.     else
  439.         signLeft = -1;
  440.      
  441.     if( isUp )
  442.         signUp = 1;
  443.     else
  444.         signUp = -1;
  445.  
  446. //---Calculate angle between shooter vector and missile vector    
  447.     double rawDevAngle = Math.Acos( shooterFrontNorm.Dot( Vector3D.Normalize( shooterToMissile ) ) ) * radToDeg;
  448.  
  449. //---Calculate perpendicular distance from shooter vector    
  450.     var projectionVec = VectorProjection( shooterToMissile, shooterFrontNorm );  
  451.     double deviationDistance = Vector3D.Distance( projectionVec, shooterToMissile );    
  452.  
  453. //---Find front left and top vectors of our missile
  454.     MatrixD missileOrientation = missileRefrence.WorldMatrix;
  455.     Vector3D missileFrontVec = missileOrientation.Forward;
  456.     Vector3D missileLeftVec = missileOrientation.Left;
  457.     Vector3D missileUpVec = missileOrientation.Up;
  458.    
  459. //---Check if we have gravity
  460.     double rollAngle = 0; double rollSpeed = 0;
  461.  
  462.     if( remotes.Count != 0 )
  463.     {
  464.         var remote = remotes[0] as IMyRemoteControl;
  465.         inGravity = false;
  466.         if( remote != null )
  467.         {
  468.             gravVec = remote.GetNaturalGravity();
  469.             double gravMag = gravVec.Length();
  470.             if( !double.IsNaN( gravMag ) && gravMag != 0 )
  471.             {
  472.                 if( gravVec.Dot( missileUpVec ) < 0 )
  473.                 {
  474.                     rollAngle = Math.PI / 2 - Math.Acos( gravVec.Dot( missileLeftVec ) / gravVec.Length() / missileLeftVec.Length() );
  475.                 }else{
  476.                     rollAngle = Math.PI + Math.Acos( gravVec.Dot( missileLeftVec ) / gravVec.Length() / missileLeftVec.Length() );
  477.                 }
  478.                
  479.                 if(firstGuidance) lastRollAngle = rollAngle;
  480.                
  481.                 rollSpeed = proportionalConstant * rollAngle + derivativeConstant * (rollAngle - lastRollAngle) / timeElapsed;
  482.  
  483.                 inGravity = true;
  484.             }
  485.         }
  486.     }
  487.  
  488. //---Determine scaling factor  
  489.     double scalingFactor;    
  490.     if( rawDevAngle < 90 )
  491.     {
  492.         scalingFactor = projectionVec.Length() + 200; //travel approx. 250m from current position in direction of target vector
  493.         destinationVec = shooterRefrence.GetPosition() + scalingFactor * shooterFrontNorm;
  494.         if( !hasPassed )
  495.             hasPassed = true;
  496.     }
  497.     else if( hasPassed )
  498.     {
  499.         scalingFactor = -projectionVec.Length() + 200;
  500.         destinationVec = shooterRefrence.GetPosition() + scalingFactor * shooterFrontNorm + signLeft * 50 * shooterLeftNorm + signUp * 50 * shooterUpNorm;
  501.     }
  502.     else
  503.     {
  504.         scalingFactor = -projectionVec.Length() + 200;
  505.         destinationVec = shooterRefrence.GetPosition() + scalingFactor * shooterFrontNorm;
  506.     }
  507.  
  508. //---Find vector from missile to destinationVec  
  509.     var missileToTargetVec = Vector3D.Subtract( destinationVec, missilePos );  
  510.  
  511. //---Check if we have any initial values
  512.     if(firstGuidance)
  513.     {
  514.         lastMissilePos = missilePos;
  515.     }
  516.    
  517.    
  518. //---Offset the missile position for the Comparer
  519.     missilePos += centerOfMassOffset * missileFrontVec;
  520.  
  521.  
  522. //---Get travel vector
  523.     var travelVec = missilePos - lastMissilePos;
  524.  
  525. //---Calc our new heading based upon our travel vector  
  526.     //headingVec = missileToTargetVec;
  527.     if(missileStage4)  
  528.     {
  529.         headingVec = CalculateHeadingVector(missileToTargetVec, travelVec, driftCompensation);
  530.     }else{
  531.         headingVec = CalculateHeadingVector(missileToTargetVec, travelVec, false);
  532.     }
  533.  
  534. //---Project target vector onto our top left and up vectors  
  535.     var projTargetFront = VectorProjection( headingVec, missileFrontVec);  
  536.     var projTargetLeft = VectorProjection( headingVec, missileLeftVec);    
  537.     var projTargetUp = VectorProjection( headingVec, missileUpVec);  
  538.     var projTargetFrontLeftPlane = projTargetFront + projTargetLeft;  
  539.  
  540. //---Get Yaw and Pitch Angles    
  541.     double yawAngle = Math.Atan( projTargetLeft.Length() / projTargetFront.Length() );    
  542.     double pitchAngle = Math.Atan( projTargetUp.Length() / projTargetFrontLeftPlane.Length() );
  543.    
  544.     if(firstGuidance)
  545.     {
  546.         lastPitchAngle = pitchAngle;
  547.         lastYawAngle = yawAngle;
  548.         firstGuidance = false;
  549.     }
  550.  
  551. //---Check if x is positive or negative    
  552.     bool isPositiveFront = CheckDotPositive( missileFrontVec, projTargetFront );    
  553.     if( !isPositiveFront )  
  554.         yawAngle += Math.PI/2; //we only change one value so it doesnt spaz                  
  555.  
  556. //---Check if yaw angle is left or right    
  557.     bool isPositiveLeft = CheckDotPositive( missileLeftVec, projTargetLeft );  
  558.     if( isPositiveLeft ) //yaw is backwards for what ever reason    
  559.         yawAngle = -yawAngle;    
  560.  
  561. //---Check if pitch angle is up or down    
  562.     bool isPositiveUp = CheckDotPositive( missileUpVec, projTargetUp );  
  563.     if( !isPositiveUp )    
  564.         pitchAngle = -pitchAngle;    
  565.  
  566. //---Angle controller  
  567.     double yawSpeed = proportionalConstant * yawAngle + derivativeConstant * (yawAngle - lastYawAngle) / timeElapsed;
  568.     double pitchSpeed = proportionalConstant * pitchAngle + derivativeConstant * (pitchAngle - lastPitchAngle) / timeElapsed;
  569.  
  570. //---Set appropriate gyro override
  571.     if( controlGyros )
  572.     {
  573.         for( int i = 0; i < gyros.Count; i++ )
  574.         {
  575.             var thisGyro = gyros[i] as IMyGyro;
  576.             if(thisGyro != null)
  577.             {
  578.                 thisGyro.SetValue<float>( "Yaw", (float)yawSpeed );
  579.                 thisGyro.SetValue<float>( "Pitch", (float)pitchSpeed );
  580.                 if( inGravity )
  581.                 {
  582.                     thisGyro.SetValue<float>( "Roll", (float)rollSpeed );
  583.                 }
  584.                 thisGyro.SetValue( "Override", true );    
  585.             }
  586.         }
  587.     }    
  588.  
  589. //---Store previous values
  590.     lastYawAngle = yawAngle;
  591.     lastPitchAngle = pitchAngle;
  592.     lastRollAngle = rollAngle;
  593.     lastMissilePos = missilePos;
  594.    
  595.     if( shouldKill )
  596.     {
  597.         KillGuidance( rawDevAngle, deviationDistance );  
  598.     }
  599. }
  600.  
  601. Vector3D CalculateHeadingVector( Vector3D target, Vector3D travel, bool driftComp )
  602. {
  603.     var targetNorm = Vector3D.Normalize(target);
  604.     if(!driftComp)
  605.     {
  606.         return targetNorm;
  607.     }
  608.     var travelNorm = Vector3D.Normalize(travel);
  609.     //var projTemp = VectorProjection( travelNorm, targetNorm );
  610.     //var rejTemp =  projTemp - travelNorm;
  611.     if(travel.Length() / timeLimit < 10)
  612.     {
  613.         return targetNorm;
  614.     }else{
  615.         return  targetNorm - (travelNorm / compensationConstant);
  616.     }
  617. }  
  618.    
  619. void ManeuveringThrust( bool turnOn )          
  620. {              
  621.     for( int i = 0 ; i < sideThrusters.Count ; i++ )          
  622.     {          
  623.         IMyThrust thisThrust = sideThrusters[i] as IMyThrust;  
  624.         if(thisThrust != null)
  625.         {
  626.             if( turnOn ) { thisThrust.ApplyAction( "OnOff_On" ); }
  627.             else { thisThrust.ApplyAction( "OnOff_Off" ); }
  628.         }          
  629.     }          
  630. }          
  631.  
  632. void MainThrustOverride()            
  633. {                        
  634.     for( int i = 0; i < mainThrusters.Count; i++ )                
  635.     {                  
  636.         IMyThrust thisThrust = mainThrusters[i] as IMyThrust;
  637.         if(thisThrust != null)
  638.         {
  639.             thisThrust.ApplyAction( "OnOff_On" );  
  640.             thisThrust.SetValue<float>( "Override", float.MaxValue );
  641.         }
  642.     }            
  643. }  
  644.    
  645. void KillGuidance( double angleOfDeviation, double distanceOfDeviation )  
  646. {  
  647.     if( angleOfDeviation < 5 && distanceOfDeviation < 1 )  
  648.     {  
  649.         kill_time += timeElapsed;  
  650.     }else{  
  651.         kill_time = 0;  
  652.     }  
  653.  
  654.     if( kill_time >= max_kill_time || timeTotal >= max_time_to_guide )  
  655.     {  
  656.         for( int i = 0; i < gyros.Count; i++ )  
  657.         {    
  658.             var thisGyro = gyros[i] as IMyGyro;
  659.             if(thisGyro != null)
  660.             {
  661.                 thisGyro.SetValue<float>( "Yaw", 0f );    
  662.                 thisGyro.SetValue<float>( "Pitch", 0f );  
  663.                 thisGyro.SetValue<float>( "Roll", 0f );                
  664.                 thisGyro.SetValue( "Override", true );  
  665.             }
  666.         }  
  667.  
  668.         if( !inGravity )
  669.         {
  670.             for( int i = 0; i < mainThrusters.Count; i++ )
  671.             {
  672.                 var thisThruster = mainThrusters[i] as IMyThrust;
  673.                 if(thisThruster != null) thisThruster.ApplyAction( "OnOff_Off" );
  674.             }
  675.  
  676.             for( int i = 0; i < sideThrusters.Count; i++ )
  677.             {
  678.                 var thisThruster = sideThrusters[i] as IMyThrust;
  679.                 if(thisThruster != null) thisThruster.ApplyAction( "OnOff_Off" );
  680.             }
  681.         }
  682.            
  683.         for( int i = 0; i < importantBlocks.Count; i++ )
  684.         {
  685.             var thisBlock = importantBlocks[i] as IMyTerminalBlock;
  686.             if(thisBlock != null)
  687.             {
  688.                 if( !( thisBlock is IMyRemoteControl ) || !( thisBlock is IMyProgrammableBlock ) )
  689.                     thisBlock.ApplyAction( "OnOff_Off" );
  690.             }
  691.         }
  692.     }
  693. }
  694.  
  695. Vector3D VectorProjection( Vector3D a, Vector3D b ) //proj a on b  
  696. {
  697.     Vector3D projection = a.Dot( b ) / b.Length() / b.Length() * b;
  698.     return projection;
  699. }
  700.  
  701. bool CheckDotPositive( Vector3D a, Vector3D b )
  702. {
  703.     double check =  a.Dot( b );
  704.     if( check > 0 )
  705.     {
  706.         return true;
  707.     }else{
  708.         return false;
  709.     }
  710. }
Advertisement
Add Comment
Please, Sign In to add comment