Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Main(string angle)
- {
- /* Twinkie Industries™ Automation Presents
- * Multiple Hinge Control v1.3
- This script will allow you to add as many hinges as you like and then control their angles
- in one easy step.
- Add one programming block for each setting that you want and paste in the script.
- Add all of your hinges to the script and set the desired angle.
- When you run the script the hinges will move to the indicated angle.
- */
- // PLEASE EDIT THIS SECTION ONLY------------------------------------------------------------------------------------------------------------------------------
- // Set the speed you want the hinge(s) to move at.
- double desiredVelocity = 1.0;
- // Add your hinges here
- // Each hinge will need its name added to the list. Make sure to check the quotation marks and commas.
- string[] names =
- {
- "Hinge",
- "Hinge 2",
- "Hinge 3"
- };
- // PLEASE DO NOT EDIT BELOW-----------------------------------------------------------------------------------------------------------------------------------
- #region Trust Issues
- // Check for stop conditions
- if (desiredVelocity == 0 || string.IsNullOrWhiteSpace(angle))
- {
- Stop();
- return;
- }
- // Parse the angle
- float setAngle;
- if (!float.TryParse(angle, out setAngle))
- return;
- // Check ranges
- desiredVelocity = Math.Abs(desiredVelocity);
- setAngle = Clamp(setAngle, -90, 90);
- foreach (string name in names)
- {
- var rotor = GridTerminalSystem.GetBlockWithName(name) as IMyMotorStator;
- if (rotor == null)
- {
- Echo($"No rotor found with name: {name}");
- return; // If you have your hinges connected together and one of them doesn't load it could go badly. It's safer to exit.
- }
- listHinges.Add(rotor);
- }
- #endregion Trust Issues
- //Loop through each hinge on the list
- for (int i = 0; i < listHinges.Count; i++)
- {
- if (listHinges[i].RotorLock)
- listHinges[i].RotorLock = false;
- float setVelocity = 0;
- //Find the rotation direction needed
- float curAngle = RadiansToDegrees(listHinges[i].Angle);
- if (curAngle != setAngle)
- setVelocity = (float)((curAngle < setAngle) ? desiredVelocity: -desiredVelocity);
- //Set the appropriate limit
- if (setVelocity > 0)
- listHinges[i].UpperLimitRad = DegreesToRadians(setAngle);
- else
- listHinges[i].LowerLimitRad = DegreesToRadians(setAngle);
- //Set the velocity
- listHinges[i].SetValue<float>("Velocity", setVelocity);
- }
- }
- private void Stop()
- {
- foreach (IMyMotorStator hinge in listHinges)
- {
- hinge.TargetVelocityRPM = 0f;
- hinge.UpperLimitDeg = 90f;
- hinge.LowerLimitDeg = -90f;
- hinge.RotorLock = true;
- }
- }
- // Returns "value" limited to the range of "min" to "max".
- public static T Clamp<T>(T value, T min, T max) where T : IComparable<T>
- {
- if (value.CompareTo(min) < 0) return min;
- if (value.CompareTo(max) > 0) return max;
- return value;
- }
- private float RadiansToDegrees(float angle) => angle * (180.0f / (float)Math.PI);
- private float DegreesToRadians(float angle) => angle * ((float)Math.PI / 180.0f);
- /// <summary>List of hinges</summary>
- List<IMyMotorStator> listHinges = new List<IMyMotorStator>();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement