Whiplash141

Whip's Skid Steer Script

Aug 26th, 2017
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.56 KB | None | 0 0
  1. //Whip's Tank Skid Steering System v9 - 1/8/18
  2.  
  3. const string controlSeatName = "Driver";
  4.  
  5. const float driveFriction = 50f;
  6. const float turnFriction = 10f;
  7. const float rotationSpeed = .5f;
  8. bool invertSteerWhenReversing = true;
  9.  
  10. //------------------------------------------------------
  11. // ============== NO TOUCHEY BELOW HERE ================
  12. //------------------------------------------------------
  13.  
  14. const double updatesPerSecond = 10;
  15. const double updateTime = 1 / updatesPerSecond;
  16. double currentTime = 0;
  17.  
  18. const double refreshInterval = 10;
  19. double timeSinceRefresh = 141;
  20. bool isSetup = false;
  21.  
  22. Program()
  23. {
  24.     Runtime.UpdateFrequency = UpdateFrequency.Update1;
  25. }
  26.  
  27. void Main(string argument, UpdateType updateType)
  28. {
  29.     try
  30.     {
  31.         if ((updateType & UpdateType.Update1) == 0)
  32.             return;
  33.         //implied else
  34.        
  35.         currentTime += 1.0/60.0;
  36.         timeSinceRefresh += 1.0/60.0;
  37.  
  38.         if (!isSetup || timeSinceRefresh >= refreshInterval)
  39.         {
  40.             isSetup = GrabBlocks();
  41.             timeSinceRefresh = 0;
  42.         }
  43.        
  44.         if (!isSetup)
  45.             return;
  46.        
  47.         if (currentTime < updateTime)
  48.             return;
  49.         else
  50.             currentTime = 0;
  51.        
  52.         Echo("WMI Skid Steering System Online..." + RunningSymbol());
  53.         Echo($"\nTime until next block refresh:\n{Math.Round(Math.Max(0, refreshInterval - timeSinceRefresh))} seconds\n");
  54.  
  55.         var inputVec = controller.MoveIndicator;
  56.    
  57.         if (inputVec.Z <= 0 || !invertSteerWhenReversing) //W
  58.         {
  59.             if (inputVec.X < 0) //A
  60.             {
  61.                 TurnRight(leftWheels, rightWheels, gyros, inputVec);
  62.             }
  63.             else if (inputVec.X > 0) //D
  64.             {
  65.                 TurnLeft(leftWheels, rightWheels, gyros, inputVec);
  66.             }
  67.             else
  68.             {
  69.                 NoTurn(leftWheels, rightWheels, gyros);
  70.             }
  71.         }
  72.         else
  73.         {
  74.             if (inputVec.X < 0) //A
  75.             {
  76.                 TurnLeft(leftWheels, rightWheels, gyros, inputVec);
  77.             }
  78.             else if (inputVec.X > 0) //D
  79.             {
  80.                 TurnRight(leftWheels, rightWheels, gyros, inputVec);
  81.             }
  82.             else
  83.             {
  84.                 NoTurn(leftWheels, rightWheels, gyros);
  85.             }
  86.         }
  87.     }
  88.     catch
  89.     {
  90.         isSetup = false;
  91.     }
  92. }
  93.  
  94. List<IMyMotorSuspension> wheels = new List<IMyMotorSuspension>();
  95. List<IMyShipController> controllers = new List<IMyShipController>();
  96. List<IMyGyro> gyros = new List<IMyGyro>();
  97. List<IMyMotorSuspension> leftWheels = new List<IMyMotorSuspension>();
  98. List<IMyMotorSuspension> rightWheels = new List<IMyMotorSuspension>();
  99. IMyShipController controller = null;
  100.  
  101. bool GrabBlocks()
  102. {
  103.     GridTerminalSystem.GetBlocksOfType(controllers, block => block.CustomName.Contains(controlSeatName));
  104.     if (controllers.Count == 0)
  105.     {
  106.         Echo($"Error: No ship controller named '{controlSeatName}' found");
  107.         return false;
  108.     }
  109.     controller = GetControlledShipController(controllers);
  110.    
  111.     GridTerminalSystem.GetBlocksOfType(wheels, block => block.CubeGrid == controller.CubeGrid);
  112.     if (wheels.Count == 0)
  113.     {
  114.         Echo("Error: No wheels found on same grid as controller");
  115.         return false;
  116.     }
  117.    
  118.     GridTerminalSystem.GetBlocksOfType(gyros, block => block.CubeGrid == Me.CubeGrid);
  119.     if (gyros.Count == 0)
  120.     {
  121.         Echo("Optional: No gyros found on same grid as controller");
  122.     }
  123.     else
  124.     {
  125.         GetGyroOrientation(controller, gyros);
  126.     }
  127.    
  128.     leftWheels = new List<IMyMotorSuspension>();
  129.     rightWheels = new List<IMyMotorSuspension>();
  130.     GetWheelSide(controller, wheels, out leftWheels, out rightWheels);
  131.    
  132.     return true;
  133. }
  134.  
  135. void NoTurn(List<IMyMotorSuspension> leftWheels, List<IMyMotorSuspension> rightWheels, List<IMyGyro> gyros)
  136. {
  137.     InvertWheelPropulsion(leftWheels, false);
  138.     InvertWheelPropulsion(rightWheels, false);
  139.     InvertSteering(leftWheels, false);
  140.     InvertSteering(rightWheels, false);
  141.     SetFriction(leftWheels, driveFriction);
  142.     SetFriction(rightWheels, driveFriction);
  143.     ApplyGyroOverride(0, 0, 0, gyros);
  144.     SetGyroPower(gyros, .1f);
  145. }
  146.  
  147. void TurnRight(List<IMyMotorSuspension> leftWheels, List<IMyMotorSuspension> rightWheels,  List<IMyGyro> gyros, Vector3D inputVec)
  148. {
  149.     InvertWheelPropulsion(leftWheels, true);
  150.     InvertWheelPropulsion(rightWheels, false);
  151.     InvertSteering(leftWheels, true);
  152.     InvertSteering(rightWheels, false);
  153.     SetFriction(leftWheels, turnFriction);
  154.     SetFriction(rightWheels, turnFriction);
  155.     SetGyroPower(gyros, 1f);
  156.     if (inputVec.Z <= 0)
  157.         ApplyGyroOverride(0, -rotationSpeed, 0, gyros);
  158.     else
  159.         ApplyGyroOverride(0, rotationSpeed, 0, gyros);
  160. }
  161.  
  162. void TurnLeft(List<IMyMotorSuspension> leftWheels, List<IMyMotorSuspension> rightWheels,  List<IMyGyro> gyros, Vector3D inputVec)
  163. {
  164.     InvertWheelPropulsion(leftWheels, false);
  165.     InvertWheelPropulsion(rightWheels, true);
  166.     InvertSteering(leftWheels, false);
  167.     InvertSteering(rightWheels, true);
  168.     SetFriction(leftWheels, turnFriction);
  169.     SetFriction(rightWheels, turnFriction);
  170.     SetGyroPower(gyros, 1f);
  171.     if (inputVec.Z <= 0)
  172.         ApplyGyroOverride(0, rotationSpeed, 0, gyros);
  173.     else
  174.         ApplyGyroOverride(0, -rotationSpeed, 0, gyros);
  175. }
  176.  
  177. Vector3D VectorProjection( Vector3D a, Vector3D b )
  178. {
  179.     return a.Dot( b ) / b.LengthSquared() * b;  
  180. }
  181.  
  182. void InvertWheelPropulsion(List<IMyMotorSuspension> wheels, bool invert)
  183. {
  184.     foreach (var wheel in wheels)
  185.         wheel.SetValue("InvertPropulsion", invert);
  186. }
  187.  
  188. void InvertSteering(List<IMyMotorSuspension> wheels, bool invert)
  189. {
  190.     foreach (var wheel in wheels)
  191.         wheel.SetValue("InvertSteering", invert);
  192. }
  193.  
  194. void SetFriction(List<IMyMotorSuspension> wheels, float friction)
  195. {
  196.     foreach (var wheel in wheels)
  197.         wheel.SetValue("Friction", friction);
  198. }
  199.  
  200. void SetGyroPower(List<IMyGyro> gyros, float power)
  201. {
  202.     foreach (var gyro in gyros)
  203.         gyro.GyroPower = power;
  204. }
  205.  
  206. void GetWheelSide(IMyTerminalBlock reference, List<IMyMotorSuspension> wheels, out List<IMyMotorSuspension> leftWheels, out List<IMyMotorSuspension> rightWheels)
  207. {
  208.     leftWheels = new List<IMyMotorSuspension>();
  209.     rightWheels = new List<IMyMotorSuspension>();
  210.  
  211.     foreach (IMyMotorSuspension thisWheel in wheels)
  212.     {
  213.         if (reference.WorldMatrix.Left.Dot(thisWheel.WorldMatrix.Up) > 0.9) //left wheel
  214.             leftWheels.Add(thisWheel);
  215.         else if (-(reference.WorldMatrix.Left.Dot(thisWheel.WorldMatrix.Up)) > 0.9)
  216.             rightWheels.Add(thisWheel);
  217.     }
  218. }
  219.  
  220. /*void GetWheelSide(IMyTerminalBlock reference, List<IMyMotorSuspension> wheels, out List<IMyMotorSuspension> leftWheels, out List<IMyMotorSuspension> rightWheels)
  221. {
  222.     leftWheels = new List<IMyMotorSuspension>();
  223.     rightWheels = new List<IMyMotorSuspension>();
  224.  
  225.     foreach (IMyMotorSuspension thisWheel in wheels)
  226.     {
  227.         if (reference.WorldMatrix.Left.Dot(thisWheel.GetPosition() - reference.GetPosition()) > 0) //left wheel
  228.             leftWheels.Add(thisWheel);
  229.         else
  230.             rightWheels.Add(thisWheel);
  231.     }
  232. }*/
  233.  
  234. IMyShipController GetControlledShipController(List<IMyShipController> SCs)
  235. {
  236.     foreach (IMyShipController thisController in SCs)
  237.     {
  238.         if (thisController.IsUnderControl && thisController.CanControlShip)
  239.             return thisController;
  240.     }
  241.  
  242.     return SCs[0];
  243. }
  244.  
  245. //Whip's Gyro Orientation Method
  246.  
  247.     string[] gyroRelativeYaw;
  248.     string[] gyroRelativePitch;
  249.     string[] gyroRelativeRoll;
  250.     int[] gyroYawSign;
  251.     int[] gyroPitchSign;
  252.     int[] gyroRollSign;
  253.  
  254. void GetGyroOrientation(IMyTerminalBlock reference_block, List<IMyGyro> gyro_list)
  255. {
  256.     gyroRelativeYaw = new string[gyro_list.Count];
  257.     gyroRelativePitch = new string[gyro_list.Count];
  258.     gyroRelativeRoll = new string[gyro_list.Count];
  259.    
  260.     gyroYawSign = new int[gyro_list.Count];
  261.     gyroPitchSign = new int[gyro_list.Count];
  262.     gyroRollSign = new int[gyro_list.Count];
  263.  
  264.     var reference_up = reference_block.WorldMatrix.Up; //assuming rot right
  265.     var reference_right = reference_block.WorldMatrix.Right; //assuming rot up
  266.     var reference_forward = reference_block.WorldMatrix.Forward; //assuming rot up
  267.  
  268.     for (int i = 0; i < gyro_list.Count; i++)
  269.     {
  270.         var gyro_forward = gyro_list[i].WorldMatrix.Forward;
  271.         var gyro_backward = gyro_list[i].WorldMatrix.Backward;
  272.         var gyro_up = gyro_list[i].WorldMatrix.Up;
  273.         var gyro_down = gyro_list[i].WorldMatrix.Down;
  274.         var gyro_left = gyro_list[i].WorldMatrix.Left;
  275.         var gyro_right = gyro_list[i].WorldMatrix.Right;
  276.  
  277.         /// Pitch Fields ///  
  278.         if (reference_right == gyro_forward)
  279.         {
  280.             gyroRelativePitch[i] = "Roll";
  281.             gyroPitchSign[i] = 1;
  282.         }
  283.         else if (reference_right == gyro_backward)
  284.         {
  285.             gyroRelativePitch[i] = "Roll";
  286.             gyroPitchSign[i] = -1;
  287.         }
  288.         else if (reference_right == gyro_right)
  289.         {
  290.             gyroRelativePitch[i] = "Pitch";
  291.             gyroPitchSign[i] = 1;
  292.         }
  293.         else if (reference_right == gyro_left)
  294.         {
  295.             gyroRelativePitch[i] = "Pitch";
  296.             gyroPitchSign[i] = -1;
  297.         }
  298.         else if (reference_right == gyro_up)
  299.         {
  300.             gyroRelativePitch[i] = "Yaw";
  301.             gyroPitchSign[i] = -1;
  302.         }
  303.         else if (reference_right == gyro_down)
  304.         {
  305.             gyroRelativePitch[i] = "Yaw";
  306.             gyroPitchSign[i] = 1;
  307.         }
  308.  
  309.         /// Yaw Fields ///
  310.         if (reference_up == gyro_forward)
  311.         {
  312.             gyroRelativeYaw[i] = "Roll";
  313.             gyroYawSign[i] = -1;
  314.         }
  315.         else if (reference_up == gyro_backward)
  316.         {
  317.             gyroRelativeYaw[i] = "Roll";
  318.             gyroYawSign[i] = 1;
  319.         }
  320.         else if (reference_up == gyro_right)
  321.         {
  322.             gyroRelativeYaw[i] = "Pitch";
  323.             gyroYawSign[i] = -1;
  324.         }
  325.         else if (reference_up == gyro_left)
  326.         {
  327.             gyroRelativeYaw[i] = "Pitch";
  328.             gyroYawSign[i] = 1;
  329.         }
  330.         else if (reference_up == gyro_up)
  331.         {
  332.             gyroRelativeYaw[i] = "Yaw";
  333.             gyroYawSign[i] = 1;
  334.         }
  335.         else if (reference_up == gyro_down)
  336.         {
  337.             gyroRelativeYaw[i] = "Yaw";
  338.             gyroYawSign[i] = -1;
  339.         }
  340.        
  341.         /// Roll Fields ///
  342.         if (reference_forward == gyro_forward)
  343.         {
  344.             gyroRelativeRoll[i] = "Roll";
  345.             gyroRollSign[i] = 1;
  346.         }
  347.         else if (reference_forward == gyro_backward)
  348.         {
  349.             gyroRelativeRoll[i] = "Roll";
  350.             gyroRollSign[i] = -1;
  351.         }
  352.         else if (reference_forward == gyro_right)
  353.         {
  354.             gyroRelativeRoll[i] = "Pitch";
  355.             gyroRollSign[i] = 1;
  356.         }
  357.         else if (reference_forward == gyro_left)
  358.         {
  359.             gyroRelativeRoll[i] = "Pitch";
  360.             gyroRollSign[i] = -1;
  361.         }
  362.         else if (reference_forward == gyro_up)
  363.         {
  364.             gyroRelativeRoll[i] = "Yaw";
  365.             gyroRollSign[i] = -1;
  366.         }
  367.         else if (reference_forward == gyro_down)
  368.         {
  369.             gyroRelativeRoll[i] = "Yaw";
  370.             gyroRollSign[i] = 1;
  371.         }
  372.     }
  373. }
  374.  
  375. void ApplyGyroOverride(double pitch_speed, double yaw_speed, double roll_speed, List<IMyGyro> gyro_list)
  376. {
  377.     for (int i = 0; i < gyro_list.Count; i++)
  378.     {
  379.         var thisGyro = gyro_list[i] as IMyGyro;
  380.         if (thisGyro != null)
  381.         {
  382.             thisGyro.SetValue<float>(gyroRelativeYaw[i], (float)yaw_speed * gyroYawSign[i]);
  383.             thisGyro.SetValue<float>(gyroRelativePitch[i], (float)pitch_speed * gyroPitchSign[i]);
  384.             thisGyro.SetValue<float>(gyroRelativeRoll[i], (float)roll_speed * gyroRollSign[i]);
  385.             thisGyro.SetValue( "Override", true );
  386.         }
  387.     }
  388. }
  389.  
  390. //Whip's Running Symbol Method v6
  391. int runningSymbolVariant = 0;
  392. string RunningSymbol()
  393. {
  394.     runningSymbolVariant++;
  395.     string strRunningSymbol = "";
  396.  
  397.     if (runningSymbolVariant == 0)
  398.         strRunningSymbol = "|";
  399.     else if (runningSymbolVariant == 1)
  400.         strRunningSymbol = "/";
  401.     else if (runningSymbolVariant == 2)
  402.         strRunningSymbol = "--";
  403.     else if (runningSymbolVariant == 3)
  404.     {
  405.         strRunningSymbol = "\\";
  406.         runningSymbolVariant = -1;
  407.     }
  408.  
  409.     return strRunningSymbol;
  410. }
Advertisement
Add Comment
Please, Sign In to add comment