Advertisement
Whiplash141

Seff and Whip's Drone Code v2

Jul 3rd, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.61 KB | None | 0 0
  1. //Code by Seff - Edited by Whiplash141 7/3/16 - v2
  2. const double maxRangeFromOrigin = 20000; //in meters
  3. const double evasionRange = 200; //in meters
  4. const double standoffRange = 400; //in meters
  5. const double updatesPerSecond = 10;
  6. const double maxSpeed = 50;
  7.  
  8. //   NO TOUCH BELOW DIS LINE
  9. //-----------------------------
  10. bool originSet = false;
  11.  
  12. List<IMyTerminalBlock> remoteList = new List<IMyTerminalBlock>();
  13. List<IMyTerminalBlock> weaponList = new List<IMyTerminalBlock>();
  14. List<IMyTerminalBlock> gyroList = new List<IMyTerminalBlock>();
  15. List<IMyTerminalBlock> thrusterList = new List<IMyTerminalBlock>();
  16.  
  17. Vector3D origin = new Vector3D(0, 0, 0);
  18.  
  19. const double timeMaxCycle = 1 / updatesPerSecond;
  20. double timeCurrentCycle;
  21.  
  22. void Main(string argument)
  23. {
  24.     if (!originSet)
  25.     {
  26.         if (argument == null || argument == "")
  27.         {
  28.             origin = Me.GetPosition();
  29.             originSet = true;
  30.             //Me.TryRun( origin.ToString() );
  31.             Echo("Self as origin");
  32.         }
  33.         else
  34.         {
  35.             originSet = Vector3D.TryParse(argument, out origin);
  36.             Echo("Arg as origin");
  37.         }
  38.     }
  39.    
  40.     timeCurrentCycle += Runtime.TimeSinceLastRun.TotalSeconds;
  41.    
  42.     if(timeCurrentCycle >= timeMaxCycle)
  43.     {
  44.         //To do: define welders
  45.  
  46.         GridTerminalSystem.GetBlocksOfType<IMyRemoteControl>(remoteList, (x => x.CubeGrid == Me.CubeGrid));
  47.         GridTerminalSystem.GetBlocksOfType<IMyGyro>(gyroList, (x => x.CubeGrid == Me.CubeGrid));
  48.         if (remoteList.Count > 0)
  49.         {
  50.             IMyRemoteControl rcmain = null;
  51.             rcmain = remoteList[0] as IMyRemoteControl;
  52.            
  53.             GetGyroOrientation(rcmain, gyroList); //get relative gyro orietation
  54.             GetThrustOrientation(rcmain);
  55.            
  56.             Vector3D target = new Vector3D(0, 0, 0);
  57.             bool success = rcmain.GetNearestPlayer(out target);
  58.  
  59.             if (success)
  60.             {
  61.                 bool gotoOrigin = false;
  62.                 GridTerminalSystem.GetBlocksOfType<IMyLargeTurretBase>(weaponList);
  63.                 if (weaponList.Count == 0)
  64.                 {
  65.                     gotoOrigin = true;
  66.                 }
  67.                 else
  68.                 {
  69.                     bool hasUsableGun = false;
  70.                     for (int i = 0; i < weaponList.Count; ++i)
  71.                     {
  72.                         var weapon = weaponList[i] as IMyLargeTurretBase;
  73.                         weapon.ApplyAction("OnOff_On");
  74.                         if (!weapon.IsFunctional)
  75.                             continue;
  76.                         else if (weapon.HasInventory() && !weapon.GetInventory(0).IsItemAt(0))
  77.                             continue;
  78.                         else
  79.                             hasUsableGun = true;
  80.                     }
  81.  
  82.                     if (!hasUsableGun)
  83.                     {
  84.                         gotoOrigin = true;
  85.                     }
  86.                 }
  87.  
  88.                 if (Vector3D.Distance(target, origin) > maxRangeFromOrigin)
  89.                 {
  90.                     gotoOrigin = true;
  91.                 }
  92.  
  93.                
  94.                 Vector3D destination;
  95.                 Vector3D self = rcmain.GetPosition();
  96.                 Vector3D frontVector = rcmain.WorldMatrix.Forward;
  97.                 Vector3D leftVector = rcmain.WorldMatrix.Left;
  98.                 Vector3D upVector = rcmain.WorldMatrix.Up;
  99.  
  100.                 /// Piloting Parameters ///
  101.                 if (gotoOrigin)
  102.                 {
  103.                     destination = origin - self;
  104.                     ThrustOverrideOn(forwardThrust);  
  105.                     ThrustOverrideOff(backwardsThrust);
  106.                 }
  107.                 else
  108.                 {
  109.                     var directHeadingVec = target - self;
  110.                     var range = directHeadingVec.Length();
  111.    
  112.                     if (range < evasionRange)
  113.                     {
  114.                         destination = directHeadingVec.Cross(leftVector);
  115.                         ThrustOverrideOn(forwardThrust);  
  116.                         ThrustOverrideOff(backwardsThrust);
  117.                     }
  118.                     else if (range < standoffRange) //reverse
  119.                     {
  120.                         destination = directHeadingVec;
  121.                         ThrustOverrideOn(backwardsThrust);  
  122.                         ThrustOverrideOff(forwardThrust);
  123.                     }
  124.                     else //forward
  125.                     {
  126.                         destination = directHeadingVec;
  127.                         ThrustOverrideOn(forwardThrust);  
  128.                         ThrustOverrideOff(backwardsThrust);
  129.                     }
  130.                    
  131.                     //To do: Add Welders on
  132.                 }
  133.                
  134.                 double speed = rcmain.GetShipSpeed();
  135.                 if (speed > maxSpeed)
  136.                 {
  137.                     ThrustOverrideOff(backwardsThrust);
  138.                     ThrustOverrideOff(forwardThrust);
  139.                 }
  140.                
  141.                 double yawAngle = 0; double pitchAngle = 0;
  142.                 GetRotationAngles(destination, frontVector, leftVector, upVector, out yawAngle, out pitchAngle);
  143.                
  144.                 ApplyGyroOverride(pitchAngle, yawAngle, 0, gyroList);
  145.             }
  146.         }
  147.         else
  148.         {
  149.             Echo("Error: No remote controls found!");
  150.         }
  151.        
  152.         timeCurrentCycle = 0;
  153.     }
  154. }
  155.  
  156. List<IMyTerminalBlock> backwardsThrust = new List<IMyTerminalBlock>();
  157. List<IMyTerminalBlock> forwardThrust = new List<IMyTerminalBlock>();
  158. void GetThrustOrientation(IMyTerminalBlock ref_block)
  159. {
  160.     List<IMyTerminalBlock> allThrust = new List<IMyTerminalBlock>();
  161.     GridTerminalSystem.GetBlocksOfType<IMyThrust>(allThrust);
  162.     backwardsThrust.Clear();
  163.     forwardThrust.Clear();
  164.     var forwardDirection = ref_block.WorldMatrix.Forward;
  165.     foreach (IMyThrust thisThrust in allThrust)
  166.     {
  167.         var thrustDirection = thisThrust.WorldMatrix.Backward;
  168.         if (thrustDirection == forwardDirection)
  169.         {
  170.             forwardThrust.Add(thisThrust);
  171.         }
  172.         else if (thrustDirection == -forwardDirection)
  173.         {
  174.             backwardsThrust.Add(thisThrust);
  175.         }
  176.     }
  177. }
  178.  
  179. void ThrustOverrideOn(List<IMyTerminalBlock> thrust_list)
  180. {
  181.     foreach( IMyThrust thisThrust in thrust_list )
  182.     {
  183.         thisThrust.SetValue<float>("Override", float.MaxValue);
  184.     }
  185. }
  186.  
  187. void ThrustOverrideOff(List<IMyTerminalBlock> thrust_list)
  188. {
  189.     foreach( IMyThrust thisThrust in thrust_list )
  190.     {
  191.         thisThrust.SetValue<float>("Override", float.MinValue);
  192.     }
  193. }
  194.  
  195. //Whip's Get Rotation Angles Method
  196. void GetRotationAngles(Vector3D v_target, Vector3D v_front, Vector3D v_left, Vector3D v_up, out double yaw, out double pitch)
  197. {
  198.     //Dependencies: VectorProjection() | VectorCompareDirection()
  199.     var projTargetFront = VectorProjection(v_target, v_front);
  200.     var projTargetLeft = VectorProjection(v_target, v_left);
  201.     var projTargetUp = VectorProjection(v_target, v_up);
  202.     var projTargetUpPlane = projTargetFront + projTargetLeft;
  203.  
  204.     //---Get Yaw and Pitch Angles    
  205.     yaw = Math.Asin(projTargetLeft.Length() / projTargetUpPlane.Length());
  206.     pitch = Math.Atan(projTargetUp.Length() / projTargetUpPlane.Length());
  207.  
  208.     //---Check if yaw angle is left or right    
  209.     //yaw is backwards b/c keen uses some weird axis notation
  210.     yaw = -1 * VectorCompareDirection(v_left, projTargetLeft) * yaw;
  211.  
  212.     //---Check if pitch angle is up or down    
  213.     pitch = VectorCompareDirection(v_up, projTargetUp) * pitch;
  214. }
  215.  
  216. //Whip's Gyro Orientation Method
  217. string[] gyroRelativeYaw;
  218. string[] gyroRelativePitch;
  219. string[] gyroRelativeRoll;
  220. int[] gyroYawSign;
  221. int[] gyroPitchSign;
  222. int[] gyroRollSign;
  223.  
  224. void GetGyroOrientation(IMyTerminalBlock reference_block, List<IMyTerminalBlock> gyro_list)
  225. {
  226.     gyroRelativeYaw = new string[gyro_list.Count];
  227.     gyroRelativePitch = new string[gyro_list.Count];
  228.     gyroRelativeRoll = new string[gyro_list.Count];
  229.  
  230.     gyroYawSign = new int[gyro_list.Count];
  231.     gyroPitchSign = new int[gyro_list.Count];
  232.     gyroRollSign = new int[gyro_list.Count];
  233.  
  234.     var reference_up = reference_block.WorldMatrix.Up; //assuming rot right
  235.     var reference_right = reference_block.WorldMatrix.Right; //assuming rot up
  236.     var reference_forward = reference_block.WorldMatrix.Forward; //assuming rot up
  237.  
  238.     for (int i = 0; i < gyro_list.Count; i++)
  239.     {
  240.         var gyro_forward = gyro_list[i].WorldMatrix.Forward;
  241.         var gyro_backward = gyro_list[i].WorldMatrix.Backward;
  242.         var gyro_up = gyro_list[i].WorldMatrix.Up;
  243.         var gyro_down = gyro_list[i].WorldMatrix.Down;
  244.         var gyro_left = gyro_list[i].WorldMatrix.Left;
  245.         var gyro_right = gyro_list[i].WorldMatrix.Right;
  246.  
  247.         /// Pitch Fields ///    
  248.         if (reference_right == gyro_forward)
  249.         {
  250.             gyroRelativePitch[i] = "Roll";
  251.             gyroPitchSign[i] = 1;
  252.         }
  253.         else if (reference_right == gyro_backward)
  254.         {
  255.             gyroRelativePitch[i] = "Roll";
  256.             gyroPitchSign[i] = -1;
  257.         }
  258.         else if (reference_right == gyro_right)
  259.         {
  260.             gyroRelativePitch[i] = "Pitch";
  261.             gyroPitchSign[i] = 1;
  262.         }
  263.         else if (reference_right == gyro_left)
  264.         {
  265.             gyroRelativePitch[i] = "Pitch";
  266.             gyroPitchSign[i] = -1;
  267.         }
  268.         else if (reference_right == gyro_up)
  269.         {
  270.             gyroRelativePitch[i] = "Yaw";
  271.             gyroPitchSign[i] = -1;
  272.         }
  273.         else if (reference_right == gyro_down)
  274.         {
  275.             gyroRelativePitch[i] = "Yaw";
  276.             gyroPitchSign[i] = 1;
  277.         }
  278.  
  279.         /// Yaw Fields ///
  280.         if (reference_up == gyro_forward)
  281.         {
  282.             gyroRelativeYaw[i] = "Roll";
  283.             gyroYawSign[i] = -1;
  284.         }
  285.         else if (reference_up == gyro_backward)
  286.         {
  287.             gyroRelativeYaw[i] = "Roll";
  288.             gyroYawSign[i] = 1;
  289.         }
  290.         else if (reference_up == gyro_right)
  291.         {
  292.             gyroRelativeYaw[i] = "Pitch";
  293.             gyroYawSign[i] = -1;
  294.         }
  295.         else if (reference_up == gyro_left)
  296.         {
  297.             gyroRelativeYaw[i] = "Pitch";
  298.             gyroYawSign[i] = 1;
  299.         }
  300.         else if (reference_up == gyro_up)
  301.         {
  302.             gyroRelativeYaw[i] = "Yaw";
  303.             gyroYawSign[i] = 1;
  304.         }
  305.         else if (reference_up == gyro_down)
  306.         {
  307.             gyroRelativeYaw[i] = "Yaw";
  308.             gyroYawSign[i] = -1;
  309.         }
  310.  
  311.         /// Roll Fields ///
  312.         if (reference_forward == gyro_forward)
  313.         {
  314.             gyroRelativeRoll[i] = "Roll";
  315.             gyroRollSign[i] = 1;
  316.         }
  317.         else if (reference_forward == gyro_backward)
  318.         {
  319.             gyroRelativeRoll[i] = "Roll";
  320.             gyroRollSign[i] = -1;
  321.         }
  322.         else if (reference_forward == gyro_right)
  323.         {
  324.             gyroRelativeRoll[i] = "Pitch";
  325.             gyroRollSign[i] = 1;
  326.         }
  327.         else if (reference_forward == gyro_left)
  328.         {
  329.             gyroRelativeRoll[i] = "Pitch";
  330.             gyroRollSign[i] = -1;
  331.         }
  332.         else if (reference_forward == gyro_up)
  333.         {
  334.             gyroRelativeRoll[i] = "Yaw";
  335.             gyroRollSign[i] = -1;
  336.         }
  337.         else if (reference_forward == gyro_down)
  338.         {
  339.             gyroRelativeRoll[i] = "Yaw";
  340.             gyroRollSign[i] = 1;
  341.         }
  342.     }
  343. }
  344.  
  345. void ApplyGyroOverride(double pitch_speed, double yaw_speed, double roll_speed, List<IMyTerminalBlock> gyro_list)
  346. {
  347.     for (int i = 0; i < gyro_list.Count; i++)
  348.     {
  349.         var thisGyro = gyro_list[i] as IMyGyro;
  350.         if (thisGyro != null)
  351.         {
  352.             thisGyro.SetValue<float>(gyroRelativeYaw[i], (float)yaw_speed * gyroYawSign[i]);
  353.             thisGyro.SetValue<float>(gyroRelativePitch[i], (float)pitch_speed * gyroPitchSign[i]);
  354.             thisGyro.SetValue<float>(gyroRelativeRoll[i], (float)roll_speed * gyroRollSign[i]);
  355.             thisGyro.SetValue("Override", true);
  356.         }
  357.     }
  358. }
  359.  
  360. int VectorCompareDirection(Vector3D a, Vector3D b)
  361. {
  362.     double check = a.Dot(b);
  363.     if (check < 0)
  364.         return -1;
  365.     else
  366.         return 1;
  367. }
  368.  
  369. Vector3D VectorProjection(Vector3D a, Vector3D b) //proj a on b    
  370. {
  371.     Vector3D projection = a.Dot(b) / b.Length() / b.Length() * b;
  372.     return projection;
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement