LtTwinkie

Twinkie's Rotor(s) Controller v1.5

Jun 25th, 2020
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.71 KB | None | 0 0
  1. void Main(string angle)
  2. {
  3.     /* Twinkie Industries™ Automation Presents  
  4.      * Rotor(s) Controller v1.5
  5.  
  6.     This script will allow you to add as many rotors as you like and then control their angles    
  7.     in one easy step.  
  8.  
  9.     Add the rotor(s) you want to control to the script and pass the desired angle using the run argument.    
  10.     When you run the script the rotors will move to the desired angle. If you leave the angle BLANK it will
  11.     stop the rotors.
  12.     */
  13.  
  14.     // PLEASE EDIT THIS SECTION ONLY------------------------------------------------------------------------------------------------------------------------------
  15.  
  16.     //Set the speed you want the rotor(s) to move at.
  17.     double desiredVelocity = 1.5;
  18.  
  19.     // Add your rotor(s) here  
  20.     // Each rotor will need its name added to the list. Make sure to check the quotation marks and commas.
  21.     string[] names =
  22.     {
  23.         "Rotor",
  24.         "Rotor 2",
  25.         "Rotor 3"
  26.     };
  27.  
  28.     // PLEASE DO NOT EDIT BELOW-----------------------------------------------------------------------------------------------------------------------------------
  29.  
  30.     #region Trust Issues
  31.  
  32.     //Stop on no angle (also don't trust user input)
  33.     float setAngle;
  34.     if (string.IsNullOrEmpty(angle) || string.IsNullOrWhiteSpace(angle) || desiredVelocity == 0 || !float.TryParse(angle, out setAngle))
  35.     {
  36.         Stop();
  37.         return;
  38.     }
  39.  
  40.     // Check ranges
  41.     desiredVelocity = Math.Abs(desiredVelocity);
  42.     setAngle        = Clamp(setAngle, -361f, 361f);
  43.  
  44.     foreach (string name in names)
  45.     {
  46.         var rotor = GridTerminalSystem.GetBlockWithName(name) as IMyMotorStator;
  47.  
  48.         if (rotor == null)
  49.         {
  50.             Echo($"No rotor found with name: {name}");
  51.             return;  // If you have your rotors connected together and one of them doesn't load it could go badly. It's safer to exit.
  52.         }
  53.  
  54.         listRotors.Add(rotor);
  55.     }
  56.  
  57.     #endregion Trust Issues
  58.  
  59.     //Loop through each rotor on the list  
  60.     for (int i = 0; i < listRotors.Count; i++)
  61.     {
  62.         float setVelocity = 0f;
  63.  
  64.         if (listRotors[i].RotorLock)
  65.             listRotors[i].RotorLock = false;
  66.  
  67.         float curAngle = RadiansToDegrees(listRotors[i].Angle);
  68.         if (curAngle == setAngle)
  69.             return;
  70.  
  71.         setVelocity = (curAngle < setAngle) ? (float)desiredVelocity : -(float)desiredVelocity;
  72.  
  73.         //Set the appropriate limit  
  74.         if (setVelocity > 0)
  75.             listRotors[i].UpperLimitRad = DegreesToRadians(setAngle);
  76.         else
  77.             listRotors[i].LowerLimitRad = DegreesToRadians(setAngle);
  78.  
  79.         //Set the velocity  
  80.         listRotors[i].SetValue<float>("Velocity", setVelocity);  
  81.     }    
  82. }    
  83.  
  84. void Stop()
  85. {
  86.     //Loop through each rotor on the list
  87.     for (int i = 0; i < listRotors.Count; i++)
  88.     {
  89.         //Setting the angles to more than 360 sets them to unlimited
  90.         listRotors[i].SetValue<float>("LowerLimit", -361f);
  91.         listRotors[i].SetValue<float>("UpperLimit", 361f);
  92.  
  93.         //Set velocity to 0
  94.         listRotors[i].SetValue<float>("Velocity", 0f);
  95.  
  96.         //Lock
  97.         listRotors[i].RotorLock = true;
  98.     }
  99. }
  100.  
  101. // Returns "value" limited to the range of "min" to "max".
  102. public static T Clamp<T>(T value, T min, T max) where T : IComparable<T>
  103. {
  104.     if (value.CompareTo(min) < 0) return min;
  105.     if (value.CompareTo(max) > 0) return max;
  106.     return value;
  107. }
  108.  
  109. private float RadiansToDegrees(float angle) => angle * (180.0f / (float)Math.PI);
  110.  
  111. private float DegreesToRadians(float angle) => angle * ((float)Math.PI / 180.0f);
  112.  
  113. /// <summary>List of rotors</summary>  
  114. List<IMyMotorStator> listRotors = new List<IMyMotorStator>();
Add Comment
Please, Sign In to add comment