Advertisement
NickNDS

Rotating Piston Drill Controller

Nov 26th, 2019 (edited)
937
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 20.26 KB | None | 0 0
  1. public string pistonKeyword = "drill";
  2.  
  3.  
  4. public float rotationVelocity = 0.5f,
  5.              angleVariance = 0.25f,
  6.              pistonExtensionSpeed = 0.5f, rotorAngleStepDistance = 0.5f,
  7.              nextRotorAngle = 0f, currentDistance = 0f, pistonExtensionDistance = 0.5f,
  8.              cargoCapacityLimit = 0.9f, autoRange = 0f,
  9.              maxPistonDistance = 0f, distanceVariance = 0.12f,
  10.              returnSpeed = 0.5f, pistonMaximum = 10f, currentAngle = 0f;
  11.  
  12. List<IMyPistonBase>
  13.     downPistons = new List<IMyPistonBase>(),
  14.     upPistons = new List<IMyPistonBase>();
  15.  
  16. List<IMyShipDrill> drills = new List<IMyShipDrill>();
  17.  
  18. List<IMyCargoContainer> containers = new List<IMyCargoContainer>();
  19.  
  20. public bool drilling = false, retracting = true, stepping = false,
  21. centeredRotor = true, drillingRotor = false, auto = false,
  22. scanningRange = false, outCapacity = false;
  23.  
  24. public SavedRotor savedRotor;
  25.  
  26. public IMyCameraBlock cameraBlock;
  27.  
  28. public IMyShipConnector connector;
  29.  
  30. public string settingBackup = "";
  31.  
  32. public TimeSpan lockableSpan = new TimeSpan(0, 0, 0);
  33.  
  34. public IMyTextSurface mySurface;
  35.  
  36. public int outTicks = 10;
  37.  
  38. public Program()
  39. {
  40.     Runtime.UpdateFrequency = UpdateFrequency.Update1;
  41.     if (Me.CustomData == "") SaveData();
  42.     else LoadData();
  43.     Scan();
  44.     try
  45.     {
  46.         IMyTextSurfaceProvider provider = (IMyTextSurfaceProvider)Me;
  47.         if (provider.SurfaceCount > 0)
  48.         {
  49.             mySurface = provider.GetSurface(0);
  50.             mySurface.ContentType = ContentType.TEXT_AND_IMAGE;
  51.         }
  52.     }
  53.     catch { }
  54.     ResetPistons();
  55. }
  56.  
  57. public void Main(string argument, UpdateType updateSource)
  58. {
  59.     outCapacity = false;
  60.     if (Me.CustomData != settingBackup) LoadData();
  61.     if (argument != "") try { Commands(argument.ToLower()); } catch { }
  62.     if (scanningRange) AutoRange();
  63.     if (drilling)
  64.     {
  65.         Echo("Drilling");
  66.         if (autoRange != 0f) Echo("Auto Range: " + autoRange.ToString("N1"));
  67.         DrillScript();
  68.     }
  69.     else if (retracting)
  70.     {
  71.         Echo("Inactive");
  72.         Echo("Drills: " + drills.Count);
  73.         Echo("Pistons: " + (downPistons.Count + upPistons.Count));
  74.         Echo("Camera: " + (cameraBlock != null).ToString());
  75.         Echo("Connector: " + (connector != null).ToString());
  76.         Echo($"Extension: {ExtensionRemaining():N1}m");
  77.         SetDrills(false);
  78.         SetRotor(0f);
  79.         ResetPistons();
  80.         currentDistance = 0f;
  81.         if (auto && FillLevel() < cargoCapacityLimit)
  82.         {
  83.             retracting = false;
  84.             drilling = true;
  85.             AutoRange();
  86.         }
  87.         if (!drilling && connector != null)
  88.         {
  89.             connector.Enabled = true;
  90.             if (connector.Status == MyShipConnectorStatus.Connectable)
  91.             {
  92.                 if (lockableSpan.TotalSeconds >= 5.0) connector.Connect();
  93.                 else lockableSpan += Runtime.TimeSinceLastRun;
  94.             }
  95.             else lockableSpan = new TimeSpan(0, 0, 0);
  96.         }
  97.     }
  98.     CheckRotors();
  99.     OutStatus();
  100. }
  101.  
  102. public void Scan()
  103. {
  104.     //Variables
  105.     List<IMyPistonBase> pistonList = new List<IMyPistonBase>();
  106.     //Get base piston
  107.     GridTerminalSystem.GetBlocksOfType<IMyPistonBase>(pistonList, b => b.CubeGrid == Me.CubeGrid && b.CustomName.ToLower().Contains(pistonKeyword));
  108.     if (pistonList.Count == 0) return;
  109.     //Reset variables
  110.     maxPistonDistance = 0f;
  111.     upPistons.Clear();
  112.     downPistons.Clear();
  113.     drills.Clear();
  114.     containers.Clear();
  115.     cameraBlock = null;
  116.     connector = null;
  117.     savedRotor = null;
  118.     //Set base piston
  119.     upPistons.Add(pistonList[0]);
  120.     maxPistonDistance += upPistons[0].HighestPosition - (upPistons[0].HighestPosition - upPistons[0].MaxLimit);
  121.     //Set pistons by up/down
  122.     IMyPistonBase currentPiston = upPistons[0];
  123.     pistonList.Clear();
  124.     GridTerminalSystem.GetBlocksOfType<IMyPistonBase>(pistonList, b => b.CubeGrid == currentPiston.TopGrid);
  125.     while (pistonList.Count > 0)
  126.     {
  127.         if (SameDirection(upPistons[0], pistonList[0]))
  128.         {
  129.             upPistons.Add(pistonList[0]);
  130.             maxPistonDistance += upPistons[upPistons.Count - 1].HighestPosition - (upPistons[upPistons.Count - 1].HighestPosition - upPistons[upPistons.Count - 1].MaxLimit);
  131.         }
  132.         else if (OppositeDirection(upPistons[0], pistonList[0]))
  133.         {
  134.             downPistons.Add(pistonList[0]);
  135.             maxPistonDistance += downPistons[downPistons.Count - 1].HighestPosition - downPistons[downPistons.Count - 1].MinLimit;
  136.         }
  137.         currentPiston = pistonList[0];
  138.  
  139.         pistonList.Clear();
  140.         GridTerminalSystem.GetBlocksOfType<IMyPistonBase>(pistonList, b => b.CubeGrid == currentPiston.TopGrid);
  141.     }
  142.     //Get rotor
  143.     List<IMyMotorAdvancedStator> rotors = new List<IMyMotorAdvancedStator>();
  144.     GridTerminalSystem.GetBlocksOfType<IMyMotorAdvancedStator>(rotors, b => b.CubeGrid == currentPiston.TopGrid);
  145.     if (rotors.Count == 0) return;
  146.     //Set rotor
  147.     savedRotor = new SavedRotor();
  148.     savedRotor.rotor = rotors[0];
  149.     savedRotor.angleVariance = angleVariance;
  150.     savedRotor.desiredAngle = savedRotor.rotor.Angle * 180f / (float)Math.PI;
  151.     nextRotorAngle = savedRotor.desiredAngle;
  152.     savedRotor.rotationVelocity = rotationVelocity;
  153.     savedRotor.rotor.TargetVelocityRPM = 0f;
  154.     //Try to get connector
  155.     List<IMyShipConnector> connectorList = new List<IMyShipConnector>();
  156.     if (connector == null)
  157.     {
  158.         GridTerminalSystem.GetBlocksOfType<IMyShipConnector>(connectorList, b => b.CubeGrid == savedRotor.rotor.TopGrid || b.CubeGrid == currentPiston.TopGrid);
  159.         if (connectorList.Count > 0) connector = connectorList[0];
  160.     }
  161.     //Get drills
  162.     GridTerminalSystem.GetBlocksOfType<IMyShipDrill>(drills, b => b.CubeGrid == savedRotor.rotor.TopGrid);
  163.     //Get camera
  164.     List<IMyCameraBlock> cameraList = new List<IMyCameraBlock>();
  165.     GridTerminalSystem.GetBlocksOfType<IMyCameraBlock>(cameraList, b => b.CubeGrid == savedRotor.rotor.TopGrid);
  166.     if (cameraList.Count > 0) cameraBlock = cameraList[0];
  167.     //Get containers
  168.     GridTerminalSystem.GetBlocksOfType<IMyCargoContainer>(containers, b => b.CubeGrid == Me.CubeGrid);
  169. }
  170.  
  171. public void ResetPistons()
  172. {
  173.     SetPistonVelocity(downPistons, -returnSpeed);
  174.     SetPistonMax(downPistons, 0f);
  175.     SetPistonVelocity(upPistons, returnSpeed);
  176.     SetPistonMin(upPistons, -1f);
  177. }
  178.  
  179. public bool SameDirection(IMyTerminalBlock originBlock, IMyTerminalBlock block)
  180. {
  181.     if (Vector3D.Distance(originBlock.WorldMatrix.Up, block.WorldMatrix.Up) <= distanceVariance) return true;
  182.     return false;
  183. }
  184.  
  185. public bool OppositeDirection(IMyTerminalBlock originBlock, IMyTerminalBlock block)
  186. {
  187.     if (Vector3D.Distance(originBlock.WorldMatrix.Up, block.WorldMatrix.Down) <= distanceVariance) return true;
  188.     return false;
  189. }
  190.  
  191. public void OutStatus()
  192. {
  193.     if (mySurface != null)
  194.     {
  195.         outTicks++;
  196.         if (outTicks >= 10)
  197.         {
  198.             outTicks = 0;
  199.  
  200.             StringBuilder builder = new StringBuilder();
  201.             if (drilling) builder.Append("Drilling");
  202.             if (retracting) builder.AppendLine($"{(drilling ? "Drilling" : retracting ? "Retracting" : "Idle")}{(auto ? " : Auto" : "")}");
  203.             if (drilling)
  204.             {
  205.                 builder.Append(($"Progress: {Math.Round(Progress() * 100.0, 2)}%").PadRight(20));
  206.                 builder.AppendLine(($"Current Angle: {(Math.Round(currentAngle, 2)).ToString().PadLeft(6)}").PadLeft(20));
  207.                 builder.Append(($"Depth: {Math.Round(currentDistance, 1)}m").PadRight(20));
  208.                 builder.AppendLine(($"Next Angle: {(Math.Round(nextRotorAngle, 2)).ToString().PadLeft(6)}").PadLeft(20));
  209.                 builder.AppendLine($"Extension: {ExtensionRemaining():N1}m");
  210.             }
  211.             if (drilling || auto)
  212.             {
  213.                 builder.AppendLine($"Fill Level: {Math.Round(FillLevel() * 100f, 2)}%");
  214.             }
  215.             builder.AppendLine($"Extending Pistons: {downPistons.Count}");
  216.             builder.AppendLine($"Retracting Pistons: {upPistons.Count}");
  217.             builder.AppendLine($"Drills: {drills.Count}");
  218.             builder.AppendLine($"Rotor: {savedRotor != null && savedRotor.rotor != null}");
  219.             builder.AppendLine($"Camera: {cameraBlock != null}");
  220.             builder.AppendLine($"Connector: {connector != null}");
  221.  
  222.             mySurface.WriteText(builder.ToString());
  223.         }
  224.     }
  225. }
  226.  
  227. public void Commands(string arg)
  228. {
  229.     if (arg.StartsWith("set "))
  230.     {
  231.         try
  232.         {
  233.             float distance = float.Parse(arg.Replace(" ", "").Substring(3));
  234.             if (distance > 0f)
  235.             {
  236.                 StepDistance(distance - currentDistance);
  237.             }
  238.         }
  239.         catch { }
  240.     }
  241.     else switch (arg)
  242.         {
  243.             case "drill":
  244.                 drilling = !drilling;
  245.                 retracting = !drilling;
  246.                 if (drilling)
  247.                 {
  248.                     SetPistonVelocity(downPistons, pistonExtensionSpeed);
  249.                     if (connector != null) connector.Enabled = false;
  250.                 }
  251.                 else if (connector != null) connector.Enabled = true;
  252.                 break;
  253.             case "scan":
  254.                 Scan();
  255.                 break;
  256.             case "auto":
  257.                 auto = !auto;
  258.                 SaveData();
  259.                 if (auto && cameraBlock != null) AutoRange();
  260.                 else
  261.                 {
  262.                     drilling = false;
  263.                     retracting = true;
  264.                     if (connector != null) connector.Enabled = true;
  265.                 }
  266.                 break;
  267.             case "measure":
  268.                 AutoRange();
  269.                 break;
  270.         }
  271. }
  272.  
  273. static string[] SplitLines(string data)
  274. {
  275.     return data.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
  276. }
  277.  
  278. public void SaveData()
  279. {
  280.     StringBuilder builder = new StringBuilder();
  281.     builder.AppendLine("pistonExtensionSpeed=" + pistonExtensionSpeed);
  282.     builder.AppendLine("pistonExtensionDistance=" + pistonExtensionDistance);
  283.     builder.AppendLine("pistonKeyword=" + pistonKeyword);
  284.     builder.AppendLine("rotorAngleStepDistance=" + rotorAngleStepDistance);
  285.     builder.AppendLine("rotationVelocity=" + rotationVelocity);
  286.     builder.AppendLine("angleVariance=" + angleVariance);
  287.     builder.Append("auto=" + auto);
  288.     Me.CustomData = builder.ToString();
  289.     settingBackup = Me.CustomData;
  290. }
  291.  
  292. public void LoadData()
  293. {
  294.     string[] settingArray = SplitLines(Me.CustomData);
  295.     for (int i = 0; i < settingArray.Length; i++)
  296.         if (settingArray[i].Contains("=")) ProcessSetting(settingArray[i].TrimEnd(';'));
  297.     settingBackup = Me.CustomData;
  298. }
  299.  
  300. public void ProcessSetting(string setting)
  301. {
  302.     bool boolSetting = false;
  303.     float floatSetting = 0f;
  304.     int settingIndex = setting.IndexOf("=");
  305.     string stringSetting = setting.Substring(settingIndex + 1), settingKey = setting.Substring(0, settingIndex);
  306.     boolSetting = stringSetting.ToLower().Contains("true");
  307.     try { floatSetting = float.Parse(stringSetting); } catch { }
  308.     switch (settingKey)
  309.     {
  310.         case "pistonExtensionSpeed":
  311.             pistonExtensionSpeed = floatSetting;
  312.             break;
  313.         case "pistonExtensionDistance":
  314.             pistonExtensionDistance = floatSetting;
  315.             break;
  316.         case "pistonKeyword":
  317.             pistonKeyword = stringSetting;
  318.             break;
  319.         case "rotorAngleStepDistance":
  320.             rotorAngleStepDistance = floatSetting;
  321.             break;
  322.         case "rotationVelocity":
  323.             rotationVelocity = floatSetting;
  324.             break;
  325.         case "angleVariance":
  326.             angleVariance = floatSetting;
  327.             break;
  328.         case "auto":
  329.             auto = boolSetting;
  330.             break;
  331.     }
  332. }
  333.  
  334. public void AutoRange()
  335. {
  336.     if (connector != null) connector.Enabled = false;
  337.     double scanDistance = maxPistonDistance;
  338.     currentDistance = 0f;
  339.     if (!scanningRange)
  340.     {
  341.         cameraBlock.EnableRaycast = true;
  342.         scanningRange = true;
  343.     }
  344.     if (cameraBlock.CanScan(scanDistance))
  345.     {
  346.         MyDetectedEntityInfo info = cameraBlock.Raycast(scanDistance, 0f, 0f);
  347.         if (info.HitPosition.HasValue)
  348.         {
  349.             Progress();
  350.             float hitDistance = (float)Vector3D.Distance(cameraBlock.GetPosition(), info.HitPosition.Value) - 7.25f + currentDistance;
  351.             if (info.Type == MyDetectedEntityType.Planet || info.Type == MyDetectedEntityType.Asteroid)
  352.             {
  353.                 scanningRange = false;
  354.                 SetPistonVelocity(downPistons, pistonExtensionSpeed);
  355.                 SetPistonVelocity(upPistons, -pistonExtensionSpeed);
  356.                 SetPistonMax(downPistons, 0f);
  357.                 SetPistonMin(upPistons, -1f);
  358.                 StepDistance(hitDistance);
  359.                 drilling = true;
  360.                 retracting = false;
  361.             }
  362.         }
  363.     }
  364. }
  365.  
  366. public void DrillScript()
  367. {
  368.     if (FillLevel() < cargoCapacityLimit)
  369.     {
  370.         SetDrills(true);
  371.         if (RotorPlaced() && ExtensionRemaining() < 1.5) StepRotor();
  372.         SetRotor(nextRotorAngle);
  373.         bool isCentered = IsCentered();
  374.         if (centeredRotor && !isCentered)
  375.         {
  376.             centeredRotor = false;
  377.             drillingRotor = true;
  378.         }
  379.         else if (!centeredRotor && isCentered) centeredRotor = true;
  380.         if (drillingRotor && centeredRotor)
  381.         {
  382.             drillingRotor = false;
  383.             if (!FullyExtended(downPistons) || !FullyRetracted(upPistons))
  384.                 StepDistance(pistonExtensionDistance);
  385.             else
  386.             {
  387.                 drilling = false;
  388.                 retracting = true;
  389.             }
  390.         }
  391.         Echo($"Progress: {Math.Round(Progress() * 100.0, 2)}% Done");
  392.     }
  393.     else SetDrills(false);
  394. }
  395.  
  396. public float FillLevel()
  397. {
  398.     float currLevel = 0f, maxLevel = 0f;
  399.     for (int i = 0; i < containers.Count; i++)
  400.     {
  401.         IMyInventory inv = containers[i].GetInventory(0);
  402.         currLevel += (float)inv.CurrentVolume;
  403.         maxLevel += (float)inv.MaxVolume;
  404.     }
  405.     return currLevel / maxLevel;
  406. }
  407.  
  408. public bool IsCentered()
  409. {
  410.     float angle = savedRotor.CurrentAngle();
  411.     return 360f - angle <= angleVariance || angle <= angleVariance;
  412. }
  413.  
  414. public void SetDrills(bool enabled)
  415. {
  416.     for (int i = 0; i < drills.Count; i++)
  417.         ((IMyFunctionalBlock)drills[i]).Enabled = enabled;
  418. }
  419.  
  420. public bool RotorPlaced()
  421. {
  422.     currentAngle = savedRotor.CurrentAngle();
  423.     Echo("Current Angle: " + currentAngle);
  424.     Echo("Next Angle: " + nextRotorAngle);
  425.     Echo($"Current Depth: {currentDistance}m");
  426.     return Math.Abs((savedRotor.desiredAngle % 360f) - (currentAngle % 360f)) <= angleVariance;
  427. }
  428.  
  429. public void StepDistance(float distance)
  430. {
  431.     SetPistonVelocity(downPistons, pistonExtensionSpeed);
  432.     SetPistonVelocity(upPistons, -pistonExtensionSpeed);
  433.  
  434.     float remainingDistance = distance;
  435.  
  436.     for (int i = 0; i < downPistons.Count && remainingDistance > 0f; i++)
  437.     {
  438.         IMyPistonBase piston = (IMyPistonBase)downPistons[i];
  439.         if (piston.MaxLimit < piston.HighestPosition)
  440.         {
  441.             if (piston.MaxLimit + remainingDistance <= piston.HighestPosition)
  442.             {
  443.                 piston.MaxLimit = piston.MaxLimit + remainingDistance;
  444.                 remainingDistance = 0f;
  445.             }
  446.             else
  447.             {
  448.                 remainingDistance -= piston.HighestPosition - piston.MaxLimit;
  449.                 piston.MaxLimit = piston.HighestPosition;
  450.             }
  451.         }
  452.     }
  453.  
  454.     for (int i = 0; i < upPistons.Count && remainingDistance > 0f; i++)
  455.     {
  456.         IMyPistonBase piston = (IMyPistonBase)upPistons[i];
  457.         if (piston.MinLimit > 0f)
  458.         {
  459.             if (piston.MinLimit - remainingDistance >= 0f)
  460.             {
  461.                 piston.MinLimit = piston.MinLimit - remainingDistance;
  462.                 remainingDistance = 0f;
  463.             }
  464.             else
  465.             {
  466.                 remainingDistance -= piston.MinLimit;
  467.                 piston.MinLimit = 0f;
  468.             }
  469.         }
  470.     }
  471. }
  472.  
  473. public void StepRotor()
  474. {
  475.     nextRotorAngle += rotorAngleStepDistance;
  476.     if (nextRotorAngle >= 360f) nextRotorAngle -= 360f;
  477. }
  478.  
  479. public double ExtensionRemaining()
  480. {
  481.     double remainder = 0;
  482.  
  483.     foreach (IMyPistonBase piston in upPistons)
  484.         remainder += Math.Max(0, piston.CurrentPosition - piston.MinLimit);
  485.     foreach (IMyPistonBase piston in downPistons)
  486.         remainder += Math.Max(0, piston.MaxLimit - piston.CurrentPosition);
  487.  
  488.     return remainder;
  489. }
  490.  
  491. public double Progress()
  492. {
  493.     double max = maxPistonDistance, curr = 0;
  494.     for (int i = 0; i < upPistons.Count; i++)
  495.         curr += upPistons[i].HighestPosition - (upPistons[i].CurrentPosition + (upPistons[i].HighestPosition - upPistons[i].MaxLimit));
  496.     for (int i = 0; i < downPistons.Count; i++)
  497.         curr += downPistons[i].CurrentPosition - downPistons[i].MinLimit;
  498.     currentDistance = (float)curr;
  499.     return curr / max;
  500. }
  501.  
  502. public void SetPistonMax(List<IMyPistonBase> pistons, float distance)
  503. {
  504.     for (int i = 0; i < pistons.Count; i++)
  505.         pistons[i].MaxLimit = distance == -1f || distance > pistons[i].MaxLimit ? pistons[i].HighestPosition : distance;
  506. }
  507.  
  508. public void SetPistonMin(List<IMyPistonBase> pistons, float distance)
  509. {
  510.     for (int i = 0; i < pistons.Count; i++)
  511.         pistons[i].MinLimit = distance == -1f || distance > pistons[i].HighestPosition ? pistons[i].HighestPosition : distance;
  512. }
  513.  
  514. public void SetPistonVelocity(List<IMyPistonBase> pistons, float velocity)
  515. {
  516.     for (int i = 0; i < pistons.Count; i++)
  517.         pistons[i].Velocity = velocity;
  518. }
  519.  
  520. public bool FullyRetracted(List<IMyPistonBase> pistons)
  521. {
  522.     for (int i = 0; i < pistons.Count; i++)
  523.     {
  524.         IMyPistonBase pistonBase = pistons[i];
  525.         if (pistonBase.CurrentPosition > 0f)
  526.             return false;
  527.     }
  528.     return true;
  529. }
  530.  
  531. public bool FullyExtended(List<IMyPistonBase> pistons)
  532. {
  533.     for (int i = 0; i < pistons.Count; i++)
  534.     {
  535.         IMyPistonBase pistonBase = pistons[i];
  536.         if (pistonBase.CurrentPosition < pistonBase.HighestPosition)
  537.             return false;
  538.     }
  539.     return true;
  540. }
  541.  
  542. public bool Positive(float angleA, float angleB)
  543. {
  544.     if (angleA < angleB)
  545.     {
  546.         if (Math.Abs(angleA - angleB) < 180f)
  547.             return true;
  548.         else return false;
  549.     }
  550.     else
  551.     {
  552.         if (Math.Abs(angleA - angleB) < 180f)
  553.             return false;
  554.         else return true;
  555.     }
  556. }
  557.  
  558. public void CheckRotors()
  559. {
  560.     savedRotor.CheckRotor();
  561. }
  562.  
  563. public void SetRotor(float angle)
  564. {
  565.     savedRotor.desiredAngle = angle;
  566.     savedRotor.rotationVelocity = rotationVelocity;
  567.     savedRotor.angleVariance = angleVariance;
  568. }
  569.  
  570. public class SavedRotor
  571. {
  572.     public IMyMotorAdvancedStator rotor;
  573.     public float desiredAngle = 0f, angleVariance = 1f, rotationVelocity = 0f;
  574.  
  575.     public void IncrementRotor(float increment)
  576.     {
  577.         if (desiredAngle + increment > 360) desiredAngle = desiredAngle + increment - 360f;
  578.         else if (desiredAngle + increment < 0) desiredAngle = 360 + desiredAngle + increment;
  579.         else desiredAngle += increment;
  580.     }
  581.  
  582.     public float CurrentAngle()
  583.     {
  584.         return (rotor.Angle * 180f / (float)Math.PI) % 360f;
  585.     }
  586.  
  587.     public void CheckRotor()
  588.     {
  589.         float currentAngle = rotor.Angle * 180f / (float)Math.PI;
  590.         if (Math.Abs(currentAngle - desiredAngle) > angleVariance)
  591.         {
  592.             float positiveDistance = 0f, negativeDistace = 0f;
  593.             if (desiredAngle > currentAngle)
  594.             {
  595.                 positiveDistance = desiredAngle - currentAngle;
  596.                 negativeDistace = currentAngle + 360f - desiredAngle;
  597.             }
  598.             else
  599.             {
  600.                 negativeDistace = currentAngle - desiredAngle;
  601.                 positiveDistance = desiredAngle + 360f - currentAngle;
  602.             }
  603.             if (positiveDistance < negativeDistace || positiveDistance == negativeDistace) rotor.TargetVelocityRPM = rotationVelocity;
  604.             else rotor.TargetVelocityRPM = -rotationVelocity;
  605.         }
  606.         else rotor.TargetVelocityRPM = 0f;
  607.     }
  608. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement