Advertisement
Whiplash141

Whip's Angle Increment Code

Mar 17th, 2017
14,141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.74 KB | None | 0 0
  1. /// Whip's Rotor Angle Increment Code v3 /// revision: 3/17/17
  2. /*
  3. Setup:
  4. 1) Place this code in a program block
  5. 2) Make a timer with actions:
  6.     - Run this program with NO ARGUMENT
  7.     - Start itself
  8.     - Trigger Now itself
  9. 3) Name the rotors that you wish to increment with the name tag "Increment" (you can custmize this below)
  10.  
  11. Arguments:
  12.  
  13. <angle> : typing a number will INCREMENT the rotor angle by that number
  14.     Ex: "+10" will increment by 10 degrees
  15.     Ex: "-30" will increment by -30 degrees
  16.  
  17. set <angle> : typing the word "set" in front of the angle will make the code EXPLICITLY set the rotor angle to the
  18.               input number.
  19.     Ex: "set 130" will set the rotor angle to 130 degrees
  20.  
  21. */
  22.  
  23. const string rotorName = "Increment";
  24. const float rotationVelocity = 3f;
  25. bool lockRotorsOnLimit = true;
  26.  
  27. ////NO TOUCHEY BELOW HERE///
  28. const double updatesPerSecond = 10;
  29. const double maxCycleTime = 1 / updatesPerSecond;
  30. double currentCycleTime = 0;
  31.  
  32. void Main(string arg)
  33. {
  34.     if (arg != "")
  35.     {
  36.         ParseArguments(arg);
  37.     }
  38.    
  39.     currentCycleTime += Runtime.TimeSinceLastRun.TotalSeconds;
  40.    
  41.     if (currentCycleTime < maxCycleTime)
  42.         return;
  43.    
  44.     if (lockRotorsOnLimit)
  45.     {
  46.        CheckRotorLock();
  47.     }
  48.    
  49.     currentCycleTime = 0;
  50. }
  51.  
  52. void ParseArguments(string argument)
  53. {
  54.     string command = "";
  55.     bool setRotorAngle = false;
  56.     if (argument.ToLower().Contains("set "))
  57.     {
  58.         command = argument.ToLower().Replace("set ", "");
  59.         setRotorAngle = true;
  60.     }
  61.     else if (argument.Contains("+"))
  62.     {
  63.         command = argument.Replace("+", "");
  64.     }
  65.     else
  66.     {
  67.         command = argument;
  68.     }
  69.    
  70.     float value;
  71.     bool isNumeric = Single.TryParse(command, out value);
  72.     if (isNumeric)
  73.     {
  74.         if (setRotorAngle)
  75.         {
  76.             Echo("Argument parsed as: " + value.ToString());
  77.             Echo("Setting rotor angle...");
  78.             SetRotorAngles(value);
  79.         }
  80.         else
  81.         {
  82.             Echo("Argument parsed as: " + value.ToString());
  83.             Echo("Incrementing rotors...");
  84.             IncrementRotor(value);
  85.         }
  86.        
  87.     }
  88.     else if (command.ToLower() == "round")
  89.     {
  90.         Echo("Argument parsed as: " + command);
  91.         Echo("Rounding off rotor angles...");
  92.         RoundRotorAngles();
  93.     }
  94.     else
  95.     {
  96.         Echo("Error: Argument '" + argument + "' could not be parsed");
  97.     }
  98. }
  99.  
  100. void CheckRotorLock()
  101. {
  102.     var rotors = new List<IMyTerminalBlock>();
  103.     GridTerminalSystem.SearchBlocksOfName(rotorName, rotors, IsRotor);
  104.     foreach (IMyMotorStator thisRotor in rotors)
  105.     {
  106.         if (Math.Abs(thisRotor.Angle - thisRotor.UpperLimit) < 0.01f || Math.Abs(thisRotor.Angle - thisRotor.LowerLimit) < 0.01f)
  107.         {
  108.             thisRotor.SetValue("Force weld", true);
  109.         }
  110.         else
  111.         {
  112.             thisRotor.SetValue("Force weld", false);
  113.         }
  114.     }
  115. }
  116.  
  117. void IncrementRotor(float increment)
  118. {
  119.     var rotors = new List<IMyTerminalBlock>();
  120.     GridTerminalSystem.SearchBlocksOfName(rotorName, rotors, IsRotor);
  121.     foreach (IMyMotorStator thisRotor in rotors)
  122.     {
  123.         float currentAngle = thisRotor.Angle / (float)Math.PI * 180f;
  124.         float newAngle = currentAngle + increment;
  125.        
  126.         Echo("Current Angle: " + currentAngle.ToString());
  127.         Echo("New Angle: " + newAngle.ToString());
  128.  
  129.         if (newAngle > currentAngle)
  130.         {
  131.             thisRotor.SetValue<float>("UpperLimit", newAngle);
  132.             thisRotor.SetValue<float>("LowerLimit", -361f);
  133.             thisRotor.SetValue<float>("Velocity", rotationVelocity);
  134.         }
  135.         else
  136.         {
  137.             thisRotor.SetValue<float>("LowerLimit", newAngle);
  138.             thisRotor.SetValue<float>("UpperLimit", 361f);
  139.             thisRotor.SetValue<float>("Velocity", -rotationVelocity);
  140.         }
  141.     }
  142. }
  143.  
  144. void RoundRotorAngles()
  145. {
  146.     var rotors = new List<IMyTerminalBlock>();
  147.     GridTerminalSystem.SearchBlocksOfName(rotorName, rotors, IsRotor);
  148.     foreach (IMyMotorStator thisRotor in rotors)
  149.     {
  150.         float currentAngle = thisRotor.Angle / (float)Math.PI * 180f;
  151.         float newAngle = (float)Math.Round(currentAngle);
  152.        
  153.         Echo("Current Angle: " + currentAngle.ToString());
  154.         Echo("New Angle: " + newAngle.ToString());
  155.  
  156.         if (newAngle > currentAngle)
  157.         {
  158.             thisRotor.SetValue<float>("UpperLimit", newAngle);
  159.             thisRotor.SetValue<float>("LowerLimit", -361f);
  160.             thisRotor.SetValue<float>("Velocity", rotationVelocity);
  161.         }
  162.         else
  163.         {
  164.             thisRotor.SetValue<float>("LowerLimit", newAngle);
  165.             thisRotor.SetValue<float>("UpperLimit", 361f);
  166.             thisRotor.SetValue<float>("Velocity", -rotationVelocity);
  167.         }
  168.     }
  169. }
  170.  
  171. void SetRotorAngles(float angle)
  172. {
  173.     var rotors = new List<IMyTerminalBlock>();
  174.     GridTerminalSystem.SearchBlocksOfName(rotorName, rotors, IsRotor);
  175.     foreach (IMyMotorStator thisRotor in rotors)
  176.     {
  177.         float currentAngle = thisRotor.Angle / (float)Math.PI * 180f;
  178.        
  179.         Echo("Current Angle: " + currentAngle.ToString());
  180.         Echo("New Angle: " + angle.ToString());
  181.  
  182.         if (angle > currentAngle)
  183.         {
  184.             thisRotor.SetValue<float>("UpperLimit", angle);
  185.             thisRotor.SetValue<float>("LowerLimit", -361f);
  186.             thisRotor.SetValue<float>("Velocity", rotationVelocity);
  187.         }
  188.         else
  189.         {
  190.             thisRotor.SetValue<float>("LowerLimit", angle);
  191.             thisRotor.SetValue<float>("UpperLimit", 361f);
  192.             thisRotor.SetValue<float>("Velocity", -rotationVelocity);
  193.         }
  194.     }
  195. }
  196.  
  197. bool IsRotor(IMyTerminalBlock block)
  198. {
  199.     var cast = block as IMyMotorStator;
  200.     return cast != null;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement