Advertisement
LtTwinkie

LtTwinkie's Multiple Hinge Control v1.3

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