Advertisement
Guest User

NavComputer

a guest
Jul 1st, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. void Main(string argument)
  2. {
  3.  
  4. List<IMyTerminalBlock> panels = new List<IMyTerminalBlock>(); //Place to put panels
  5. GridTerminalSystem.GetBlocksOfType<IMyTextPanel>(panels); //Get ALL THE PANELS!
  6. IMyTextPanel panel = panels[0] as IMyTextPanel; //Get specific panel
  7.  
  8. List<IMyTerminalBlock> cameras = new List<IMyTerminalBlock>(); //Place to put cameras
  9. GridTerminalSystem.GetBlocksOfType<IMyCameraBlock>(cameras); //Get ALL THE CAMERAS!
  10. IMyCameraBlock camera = cameras[0] as IMyCameraBlock; //Get specific Camera
  11.  
  12. List<IMyTerminalBlock> gyros = new List<IMyTerminalBlock>(); //Place to put gyros
  13. GridTerminalSystem.GetBlocksOfType<IMyGyro>(gyros); //Get ALL THE Gyros!
  14. IMyGyro gyro = gyros[0] as IMyGyro; //Get specific Gyro
  15.  
  16. panel.WritePublicText("");
  17.  
  18. stablizeAtVector(80, 80, 80, gyro, camera, panel);
  19.  
  20. }
  21.  
  22. void stablizeAtVector(float yaw, float pitch, float roll, IMyGyro gyro, IMyTerminalBlock frontObject, IMyTextPanel panel) //Yaw, Pitch, Roll are the desired points in degrees, gyro is gyro, and frontObject is an object in the front of the ship
  23. {
  24. List<IMyTerminalBlock> allBlocks = new List<IMyTerminalBlock>();
  25. GridTerminalSystem.GetBlocks(allBlocks);
  26. FixedVector3D rotation = VectorAngle(GetMeanCenter(allBlocks), GetBlockPosFixed(frontObject));
  27.  
  28. AddToPanel("XRot: " + rotation.X.ToString(), panel);
  29. AddToPanel("YRot: " + rotation.Y.ToString(), panel);
  30. AddToPanel("ZRot: " + rotation.Z.ToString(), panel);
  31.  
  32. /*
  33. float relYaw = yaw - (float)rotation.Y;
  34. AddToPanel("RelYaw: " + relYaw, panel);
  35. if (Math.Abs((int)rotation.Y) > 8 && Math.Abs((int)rotation.Y) < 352) //Prevent getting stuck
  36. {
  37. if ((int)Math.Abs(relYaw) >= 180) //More Efficient
  38. {
  39. relYaw = Math.Sign(relYaw) * (360 - Math.Abs(relYaw));
  40. }
  41. }
  42. */
  43.  
  44. float relPitch = pitch - (float)rotation.X;
  45. if (Math.Abs((int)rotation.X) > 2 && Math.Abs((int)rotation.X) < 358 && Math.Abs((int)relPitch) > 2 && Math.Abs((int)relPitch) < 358) //Prevent getting stuck
  46. {
  47. if ((int)Math.Abs(relPitch) <= 180) //More Efficient
  48. {
  49. relPitch = Math.Sign(relPitch) * (360 - Math.Abs(relPitch));
  50. }
  51. setGyroPower(0, (relPitch / 360) * -6, 0, gyro);
  52. }
  53.  
  54. //AddToPanel("RelYaw: " + relYaw, panel);
  55. AddToPanel("RelPitch: " + relPitch, panel);
  56.  
  57. AddToPanel("XOver: " + gyro.Pitch, panel);
  58. AddToPanel("YOver: " + gyro.Yaw, panel);
  59. AddToPanel("ZOver: " + gyro.Roll, panel);
  60. }
  61.  
  62.  
  63. void setGyroPower(float yaw, float pitch, float roll, IMyGyro gyro) //-6 to 6 and everything in between
  64. {
  65. if (gyro.Yaw > yaw)
  66. {
  67. gyro.ApplyAction("DecreaseYaw");
  68. } else
  69. {
  70. gyro.ApplyAction("IncreaseYaw");
  71. }
  72.  
  73. if (gyro.Pitch > pitch)
  74. {
  75. gyro.ApplyAction("DecreasePitch");
  76. }
  77. else
  78. {
  79. gyro.ApplyAction("IncreasePitch");
  80. }
  81.  
  82. if (gyro.Roll > roll)
  83. {
  84. gyro.ApplyAction("DecreaseRoll");
  85. }
  86. else
  87. {
  88. gyro.ApplyAction("IncreaseRoll");
  89. }
  90. }
  91.  
  92.  
  93. FixedVector3D VectorAngle(FixedVector3D pointA, FixedVector3D pointB)
  94. {
  95. FixedVector3D extPoint = new FixedVector3D(pointB.X - pointA.X, pointB.Y - pointA.Y, pointB.Z - pointA.Z);
  96. FixedVector3D result = new FixedVector3D(0,0,0);
  97. result.X = RadianToDegree(Math.Atan2(extPoint.Y, extPoint.Z));
  98. result.Y = RadianToDegree(Math.Atan2(extPoint.X, extPoint.Z));
  99. result.Z = RadianToDegree(Math.Atan2(extPoint.Y, extPoint.X));
  100. return result;
  101. }
  102.  
  103. double RadianToDegree(double angle)
  104. {
  105. return (angle * (180.0 / Math.PI)) + 180;
  106. }
  107.  
  108. FixedVector3D GetBlockPosRelativeCenter(IMyTerminalBlock block) //Get the block's position relative to the mean center
  109. {
  110. List<IMyTerminalBlock> allBlocks = new List<IMyTerminalBlock>();
  111. GridTerminalSystem.GetBlocks(allBlocks);
  112. FixedVector3D center = GetMeanCenter(allBlocks);
  113. FixedVector3D blockPosWorld = GetBlockPosFixed(block);
  114. return new FixedVector3D(center.X - blockPosWorld.X, center.Y - blockPosWorld.Y, center.Z - blockPosWorld.Z);
  115. }
  116.  
  117. string GetBlockType(IMyTerminalBlock block) //Self-Explainatory
  118. {
  119. string[] getType = block.GetType().ToString().Split('.');
  120. return getType[getType.Length - 1];
  121. }
  122.  
  123. FixedVector3D GetMeanCenter(List<IMyTerminalBlock> blocks) //Get average center of all the blocks on-board
  124. {
  125. FixedVector3D result = new FixedVector3D(0,0,0);
  126. for (int i = 0; i < blocks.Count; i++)
  127. {
  128. IMyTerminalBlock block = blocks[i];
  129. FixedVector3D blockPos = GetBlockPosFixed(block);
  130. result.X += blockPos.X;
  131. result.Y += blockPos.Y;
  132. result.Z += blockPos.Z;
  133. }
  134. result.X = result.X / blocks.Count;
  135. result.Y = result.Y / blocks.Count;
  136. result.Z = result.Z / blocks.Count;
  137. return result;
  138. }
  139.  
  140. void AddToPanel(string append, IMyTextPanel panel) //Sorta like Debug.Log() or Serial.Println()
  141. {
  142. panel.ShowPublicTextOnScreen();
  143. panel.WritePublicText(append + "\n", true);
  144. }
  145.  
  146. FixedVector3D GetBlockPosFixed(IMyTerminalBlock block) //Get block position (Without Vector3D Bugs)
  147. {
  148. VRageMath.Vector3 blockPosition = block.GetPosition();
  149. string[] strSeperators = { "{X:", " Y:", " Z:", "}" };
  150. string[] hackPos = blockPosition.ToString().Split(strSeperators, StringSplitOptions.RemoveEmptyEntries);
  151. FixedVector3D hackPosition = new FixedVector3D(Convert.ToDouble(hackPos[0]), Convert.ToDouble(hackPos[1]), Convert.ToDouble(hackPos[2]));
  152. return hackPosition;
  153. }
  154.  
  155. struct FixedVector3D { //Because Keen's Vector3/Vector3D Does not behave, here is a replacement
  156. public double X; public double Y; public double Z;
  157. public FixedVector3D(double s_X, double s_Y, double s_Z)
  158. {
  159. X = s_X; Y = s_Y; Z = s_Z;
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement