Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Main(string angle)
- {
- /* Twinkie Industries™ Automation Presents
- * Rotor(s) Controller v1.5
- This script will allow you to add as many rotors as you like and then control their angles
- in one easy step.
- Add the rotor(s) you want to control to the script and pass the desired angle using the run argument.
- When you run the script the rotors will move to the desired angle. If you leave the angle BLANK it will
- stop the rotors.
- */
- // PLEASE EDIT THIS SECTION ONLY------------------------------------------------------------------------------------------------------------------------------
- //Set the speed you want the rotor(s) to move at.
- double desiredVelocity = 1.5;
- // Add your rotor(s) here
- // Each rotor will need its name added to the list. Make sure to check the quotation marks and commas.
- string[] names =
- {
- "Rotor",
- "Rotor 2",
- "Rotor 3"
- };
- // PLEASE DO NOT EDIT BELOW-----------------------------------------------------------------------------------------------------------------------------------
- #region Trust Issues
- //Stop on no angle (also don't trust user input)
- float setAngle;
- if (string.IsNullOrEmpty(angle) || string.IsNullOrWhiteSpace(angle) || desiredVelocity == 0 || !float.TryParse(angle, out setAngle))
- {
- Stop();
- return;
- }
- // Check ranges
- desiredVelocity = Math.Abs(desiredVelocity);
- setAngle = Clamp(setAngle, -361f, 361f);
- 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 rotors connected together and one of them doesn't load it could go badly. It's safer to exit.
- }
- listRotors.Add(rotor);
- }
- #endregion Trust Issues
- //Loop through each rotor on the list
- for (int i = 0; i < listRotors.Count; i++)
- {
- float setVelocity = 0f;
- if (listRotors[i].RotorLock)
- listRotors[i].RotorLock = false;
- float curAngle = RadiansToDegrees(listRotors[i].Angle);
- if (curAngle == setAngle)
- return;
- setVelocity = (curAngle < setAngle) ? (float)desiredVelocity : -(float)desiredVelocity;
- //Set the appropriate limit
- if (setVelocity > 0)
- listRotors[i].UpperLimitRad = DegreesToRadians(setAngle);
- else
- listRotors[i].LowerLimitRad = DegreesToRadians(setAngle);
- //Set the velocity
- listRotors[i].SetValue<float>("Velocity", setVelocity);
- }
- }
- void Stop()
- {
- //Loop through each rotor on the list
- for (int i = 0; i < listRotors.Count; i++)
- {
- //Setting the angles to more than 360 sets them to unlimited
- listRotors[i].SetValue<float>("LowerLimit", -361f);
- listRotors[i].SetValue<float>("UpperLimit", 361f);
- //Set velocity to 0
- listRotors[i].SetValue<float>("Velocity", 0f);
- //Lock
- listRotors[i].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 rotors</summary>
- List<IMyMotorStator> listRotors = new List<IMyMotorStator>();
Add Comment
Please, Sign In to add comment