Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.42 KB | None | 0 0
  1. enum State {
  2.   Initial,
  3.   ZRetracted,
  4.   YRetracted,
  5.   XRetracted,
  6.   DrillDown,
  7.   Retract,
  8.   Advance,
  9.   DrillMove,
  10.   Shutdown,
  11.   Done
  12. };
  13.  
  14. State state = State.Initial;
  15. const float delta = 2.0f;
  16. const float drillSpeed = 0.3f;
  17. float x = 0.0f, y = 0.0f;
  18.  
  19. public Program() {
  20.   Runtime.UpdateFrequency = UpdateFrequency.Update10;
  21. }
  22.  
  23. public IMyPistonBase Piston(String name) {
  24.   return GridTerminalSystem.GetBlockWithName("Piston " + name) as IMyPistonBase;
  25. }
  26.  
  27. public void Main() {
  28.   var x1 = Piston("X1"); var x2 = Piston("X2");
  29.   var y1 = Piston("Y1"); var y2 = Piston("Y2");
  30.   var z1 = Piston("Z1"); var z2 = Piston("Z2");
  31.   var drill = GridTerminalSystem.GetBlockWithName("Drill") as IMyShipDrill;
  32.   switch (state) {
  33.     case State.Initial: {
  34.       drill.Enabled = false;
  35.       int progress = (int) Math.Round(
  36.           100 * (z1.CurrentPosition + z2.CurrentPosition) / 20.0f);
  37.       Echo("Waiting for Z to retract (" + progress.ToString() + "%)");
  38.       z1.MinLimit = z2.MinLimit = 0.0f;
  39.       z1.MaxLimit = z2.MaxLimit = 10.0f;
  40.       z1.Velocity = z2.Velocity = -1.0f;
  41.       if (z1.CurrentPosition == 0.0f && z2.CurrentPosition == 0.0f)
  42.         state = State.ZRetracted;
  43.       break;
  44.     }
  45.     case State.ZRetracted: {
  46.       int progress = (int) Math.Round(
  47.           100 * (y1.CurrentPosition + y2.CurrentPosition) / 20.0f);
  48.       Echo("Waiting for Y to retract (" + progress.ToString() + "%)");
  49.       y1.MinLimit = y2.MinLimit = 0.0f;
  50.       y1.MaxLimit = y2.MaxLimit = 10.0f;
  51.       y1.Velocity = y2.Velocity = -0.5f;
  52.       if (y1.CurrentPosition == 0.0f && y2.CurrentPosition == 0.0f)
  53.         state = State.YRetracted;
  54.       break;
  55.     }
  56.     case State.YRetracted: {
  57.       int progress = (int) Math.Round(
  58.           100 * (x1.CurrentPosition + x2.CurrentPosition) / 20.0f);
  59.       Echo("Waiting for X to retract (" + progress.ToString() + "%)");
  60.       x1.MinLimit = x2.MinLimit = 0.0f;
  61.       x1.MaxLimit = x2.MaxLimit = 10.0f;
  62.       x1.Velocity = x2.Velocity = -1.0f;
  63.       if (x1.CurrentPosition == 0.0f && x2.CurrentPosition == 0.0f)
  64.         state = State.XRetracted;
  65.       break;
  66.     }
  67.     case State.XRetracted: {
  68.       Echo("Initialized. Starting drill...");
  69.       x1.Velocity = x2.Velocity = 0.0f;
  70.       y1.Velocity = y2.Velocity = 0.0f;
  71.       z1.Velocity = z2.Velocity = 0.0f;
  72.       x = 0.0f;
  73.       y = 0.0f;
  74.       state = State.DrillDown;
  75.       break;
  76.     }
  77.     case State.DrillDown: {
  78.       int progress = (int) Math.Round(
  79.           100 * (z1.CurrentPosition + z2.CurrentPosition) / 20.0f);
  80.       Echo("Drilling (" + progress.ToString() + "%)");
  81.       drill.Enabled = true;
  82.       z1.Velocity = z2.Velocity = 0.5f * drillSpeed;
  83.       if (z1.CurrentPosition == 10.0f && z2.CurrentPosition == 10.0f)
  84.         state = State.Retract;
  85.       break;
  86.     }
  87.     case State.Retract: {
  88.       int progress = (int) Math.Round(
  89.           100 * (y1.CurrentPosition + y2.CurrentPosition) / 20.0f);
  90.       Echo("Retracting (" + progress.ToString() + "%)");
  91.       drill.Enabled = false;
  92.       z1.Velocity = z2.Velocity = -0.5f;
  93.       if (z1.CurrentPosition == 0.0f && z2.CurrentPosition == 0.0f)
  94.         state = State.Advance;
  95.       break;
  96.     }
  97.     case State.Advance: {
  98.       Echo("Advancing drill head...");
  99.       if (x == 20.0f && y == 20.0f) {
  100.         state = State.Shutdown;
  101.         break;
  102.       } else if (x == 20.0f) {
  103.         x = 0.0f;
  104.         y = Math.Min(y + delta, 20.0f);
  105.       } else {
  106.         x = Math.Min(x + delta, 20.0f);
  107.       }
  108.       state = State.DrillMove;
  109.       break;
  110.     }
  111.     case State.DrillMove: {
  112.       Echo("Moving drill head...");
  113.       x1.Velocity = x2.Velocity = 0.5f;
  114.       y1.Velocity = y2.Velocity = 0.5f;
  115.       x1.MinLimit = x2.MinLimit = 0.5f * x;
  116.       x1.MaxLimit = x2.MaxLimit = 0.5f * x;
  117.       y1.MinLimit = y2.MinLimit = 0.5f * y;
  118.       y1.MaxLimit = y2.MaxLimit = 0.5f * y;
  119.       float currentX = x1.CurrentPosition + x2.CurrentPosition;
  120.       float currentY = y1.CurrentPosition + y2.CurrentPosition;
  121.       if (Math.Abs(x - currentX) < 0.01f && Math.Abs(y - currentY) < 0.01f)
  122.         state = State.DrillDown;
  123.       break;
  124.     }
  125.     case State.Shutdown: {
  126.       Echo("Shutting down...");
  127.       x1.Velocity = x2.Velocity = 0.0f;
  128.       y1.Velocity = y2.Velocity = 0.0f;
  129.       z1.Velocity = z2.Velocity = 0.0f;
  130.       drill.Enabled = false;
  131.       state = State.Done;
  132.       break;
  133.     }
  134.     case State.Done: {
  135.       Echo("Done");
  136.       break;
  137.     }
  138.   }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement