Advertisement
LtTwinkie

LtTwinkie's Multiple Hinge Control v1.1

Jul 27th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1.     void Main(string arg)
  2.     {
  3.         /* Twinkie Industries™ Automation Presents
  4.          * Multiple Hinge Control v1.1
  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 set of hinges that you want to control.
  10.         Add all of your hinges to the script and set the desired velocity.  
  11.         Set the programming block argument to the angle you want the hinges to move to and run it. All of the hinges will move to that angle.
  12.         You can create cockpit toolbar buttons from the programming block to make preset angles.
  13.          */
  14.  
  15.         //PLEASE EDIT THIS SECTION ONLY------------------------------------------------------------------------------------------------------------------------------  
  16.        
  17.         /*Add your hinges here  
  18.         Each hinge will need its own line with a unique name between the quotation marks.  */
  19.         listHinges.Add(GridTerminalSystem.GetBlockWithName("Conveyor Hinge") as IMyMotorStator);
  20.         listHinges.Add(GridTerminalSystem.GetBlockWithName("Conveyor Hinge 2") as IMyMotorStator);  
  21.  
  22.         //Movement velocity
  23.         float velocity = 30f;
  24.        
  25.         //-----------------------------------------------------------------------------------------------------------------------------------------------------------  
  26.  
  27.         setAngle = Single.Parse(arg);
  28.  
  29.         if (setAngle <= 90 && setAngle >= -90)//Check angle limits
  30.         {
  31.             //Loop through each hinge on the list
  32.             for (int i = 0; i < listHinges.Count; i++)
  33.             {
  34.                 IMyMotorStator currentHinge = listHinges[i];
  35.                 if (currentHinge == null)
  36.                     continue;
  37.  
  38.                 //Find the rotation direction needed
  39.                 float curAngle = getAngle(currentHinge);
  40.  
  41.                 if (debug)
  42.                 {
  43.                     Echo("current angle " + curAngle);
  44.                     Echo("set angle " + setAngle);
  45.                 }
  46.  
  47.                 if (curAngle == setAngle) setVelocity = 0f;
  48.                 else setVelocity = (curAngle < setAngle) ? velocity : -velocity;
  49.  
  50.                 if (debug) Echo("setVelocity " + setVelocity);
  51.  
  52.                 //Set the appropriate limit
  53.                 if (setVelocity > 0) currentHinge.SetValue<float>("UpperLimit", (float)setAngle);
  54.                 else currentHinge.SetValue<float>("LowerLimit", (float)setAngle);
  55.  
  56.                 //Set the velocity
  57.                 currentHinge.SetValue<float>("Velocity", setVelocity);
  58.             }
  59.         }
  60.     }
  61.  
  62.     /// <summary>Returns the current angle of a hinge</summary>
  63.     float getAngle(IMyMotorStator hinge)
  64.     {
  65.         string data = hinge.DetailedInfo;
  66.         string[] dataSplit = data.Split(':');
  67.         return float.Parse(dataSplit[1].Substring(0, dataSplit[1].Length - 1));
  68.     }
  69.  
  70.     /// <summary>Velocity to be set</summary>
  71.     float setVelocity;
  72.  
  73.     /// <summary>The angle to be set</summary>
  74.     double setAngle;
  75.  
  76.     /// <summary>List of hinges</summary>
  77.     List<IMyMotorStator> listHinges = new List<IMyMotorStator>();
  78.  
  79.     /// <summary>Enable debugging</summary>
  80.     bool debug = false;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement