Whiplash141

Whip's Tank Skid Steering System [Revision: 9-1-15]

Sep 1st, 2015
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.15 KB | None | 0 0
  1. /*    
  2. Whip's Tank Skid Steering System - Revision 8 - 9/1/2015    
  3. /// Adapted for Dwarf-Lord Pangolin's Tanks ///    
  4. ______________________________________________________________________  
  5.    
  6. Instructions:  
  7. Run the script with the desired arguments to execute specific movements  
  8. ______________________________________________________________________  
  9.    
  10. Arguments:  
  11. "advance" - Moves forward  
  12. "reverse" - Moves Backward  
  13. "left"    - Turns left  
  14. "right"   - Turns right  
  15. "stop"    - Full stop  
  16.    
  17. Arguments should be entered without quotes; letter case is unimportant  
  18. ______________________________________________________________________  
  19.    
  20. To-do List:  
  21. * Add precise rotor speed settings
  22.    
  23. Feel free to shoot me any questions!  
  24.    
  25. - Whiplash141 - http://steamcommunity.com/id/Whiplash141/  
  26. */    
  27.    
  28. string AxleLeftName = "Rotor - Left Axle";  
  29. string AxleRightName = "Rotor - Right Axle";  
  30.    
  31. float VelocityLeft;  
  32. float VelocityRight;  
  33. float VelocityAverage;  
  34.    
  35. List<IMyTerminalBlock> AxleLeft = new List<IMyTerminalBlock>();    
  36. List<IMyTerminalBlock> AxleRight = new List<IMyTerminalBlock>();    
  37. List<IMyTerminalBlock> AllRotors = new List<IMyTerminalBlock>();    
  38.    
  39. void Main (string argument)    
  40. {    
  41.     GridTerminalSystem.GetBlocksOfType<IMyMotorStator>(AllRotors);    
  42.     GridTerminalSystem.SearchBlocksOfName(AxleLeftName,AxleLeft);    
  43.     GridTerminalSystem.SearchBlocksOfName(AxleRightName,AxleRight);    
  44.        
  45.     for(int i = 0 ; i < AllRotors.Count ; i++)    
  46.     {    
  47.         if(AllRotors[i].CustomName.Contains(AxleLeftName))    
  48.         {    
  49.             AxleLeft.Add(AllRotors[i] as IMyMotorStator);    
  50.         }    
  51.         else if(AllRotors[i].CustomName.Contains(AxleRightName))    
  52.         {    
  53.             AxleRight.Add(AllRotors[i] as IMyMotorStator);    
  54.         }      
  55.     }    
  56.        
  57.     switch(argument.ToLower())    
  58.     {    
  59.         case "right":    
  60.             Echo("Turning Right");    
  61.             TurnRight();    
  62.             break;    
  63.                
  64.         case "left":    
  65.             Echo("Turning Left");    
  66.             TurnLeft();    
  67.             break;    
  68.                
  69.         case "advance":    
  70.             Echo("Advancing");    
  71.             Advance();    
  72.             break;    
  73.                
  74.         case "reverse":    
  75.             Echo("Reversing");    
  76.             Reverse();    
  77.             break;    
  78.         case "stop":    
  79.             Echo("Full Stop");    
  80.             FullStop();    
  81.             break;    
  82.                
  83.         case "default":    
  84.             Echo("Idle");  
  85.             break;    
  86.            
  87.     }    
  88. }    
  89.    
  90. void TurnRight()      
  91. {      
  92.     for(int i = 0 ; i < AxleRight.Count ; i++)    
  93.     {    
  94.         var CurrentRight = AxleRight[i] as IMyMotorStator;    
  95.         CurrentRight.ApplyAction("DecreaseVelocity");    
  96.     }    
  97.    
  98.     for(int i = 0 ; i < AxleLeft.Count ; i++)    
  99.     {    
  100.         var CurrentLeft = AxleLeft[i] as IMyMotorStator;    
  101.         CurrentLeft.ApplyAction("DecreaseVelocity");    
  102.     }    
  103. }    
  104.    
  105. void TurnLeft()      
  106. {      
  107.     for(int i = 0 ; i < AxleRight.Count ; i++)    
  108.     {    
  109.         var CurrentRight = AxleRight[i] as IMyMotorStator;  
  110.         CurrentRight.ApplyAction("IncreaseVelocity");    
  111.     }    
  112.    
  113.     for(int i = 0 ; i < AxleLeft.Count ; i++)    
  114.     {    
  115.         var CurrentLeft = AxleLeft[i] as IMyMotorStator;    
  116.         CurrentLeft.ApplyAction("IncreaseVelocity");    
  117.     }    
  118. }    
  119.    
  120. void Advance()      
  121. {      
  122.    
  123.     for(int i = 0 ; i < AxleRight.Count ; i++)    
  124.     {    
  125.         var CurrentRight = AxleRight[i] as IMyMotorStator;  
  126.         CurrentRight.ApplyAction("IncreaseVelocity");    
  127.     }    
  128.    
  129.     for(int i = 0 ; i < AxleLeft.Count ; i++)    
  130.     {    
  131.         var CurrentLeft = AxleLeft[i] as IMyMotorStator;    
  132.         CurrentLeft.ApplyAction("DecreaseVelocity");    
  133.     }    
  134.        
  135.     if(AxleRight[0] != null && AxleLeft[0] != null)  
  136.     {  
  137.         var ThisLeft = AxleLeft[0] as IMyMotorStator;
  138.         var ThisRight = AxleRight[0] as IMyMotorStator;
  139.         VelocityLeft = Math.Abs(ThisLeft.Velocity); Echo("Left: " + VelocityLeft);  
  140.         VelocityRight = Math.Abs(ThisRight.Velocity); Echo("Right: " + VelocityRight);  
  141.            
  142.         if(VelocityLeft != VelocityRight)  
  143.         {  
  144.             VelocityAverage = Convert.ToInt32(VelocityLeft / 2 + VelocityRight / 2);
  145.             VelocityAverage = VelocityAverage - VelocityAverage % 12; Echo("Avg: " + VelocityAverage);  
  146.                
  147.             for(int i = 0 ; i < AxleRight.Count ; i++)    
  148.             {    
  149.                 var CurrentRight = AxleRight[i] as IMyMotorStator;  
  150.                 CurrentRight.SetValue<float>("Velocity",VelocityAverage);    
  151.             }  
  152.                
  153.             for(int i = 0 ; i < AxleLeft.Count ; i++)    
  154.             {    
  155.                 var CurrentLeft = AxleLeft[i] as IMyMotorStator;  
  156.                 CurrentLeft.SetValue<float>("Velocity",-1 * VelocityAverage);    
  157.             }  
  158.         }    
  159.     }  
  160. }    
  161.    
  162. void Reverse()      
  163. {      
  164.     for(int i = 0 ; i < AxleRight.Count ; i++)    
  165.     {    
  166.         var CurrentRight = AxleRight[i] as IMyMotorStator;    
  167.         CurrentRight.ApplyAction("DecreaseVelocity");    
  168.     }    
  169.    
  170.     for(int i = 0 ; i < AxleLeft.Count ; i++)    
  171.     {    
  172.         var CurrentLeft = AxleLeft[i] as IMyMotorStator;    
  173.         CurrentLeft.ApplyAction("IncreaseVelocity");    
  174.     }
  175.  
  176.     if(AxleRight[0] != null && AxleLeft[0] != null)  
  177.     {  
  178.         var ThisLeft = AxleLeft[0] as IMyMotorStator;
  179.         var ThisRight = AxleRight[0] as IMyMotorStator;
  180.         VelocityLeft = Math.Abs(ThisLeft.Velocity); Echo("Left: " + VelocityLeft);  
  181.         VelocityRight = Math.Abs(ThisRight.Velocity); Echo("Right: " + VelocityRight);  
  182.            
  183.         if(VelocityLeft != VelocityRight)  
  184.         {  
  185.             VelocityAverage = Convert.ToInt32(VelocityLeft / 2 + VelocityRight / 2);
  186.             VelocityAverage = VelocityAverage - VelocityAverage % 12; Echo("Avg: " + VelocityAverage);  
  187.                
  188.             for(int i = 0 ; i < AxleRight.Count ; i++)    
  189.             {    
  190.                 var CurrentRight = AxleRight[i] as IMyMotorStator;  
  191.                 CurrentRight.SetValue<float>("Velocity",-1 * VelocityAverage);    
  192.             }  
  193.                
  194.             for(int i = 0 ; i < AxleLeft.Count ; i++)    
  195.             {    
  196.                 var CurrentLeft = AxleLeft[i] as IMyMotorStator;  
  197.                 CurrentLeft.SetValue<float>("Velocity",VelocityAverage);    
  198.             }  
  199.         }    
  200.     }
  201. }    
  202.    
  203. void FullStop()    
  204. {    
  205.     for(int i = 0 ; i < AxleRight.Count ; i++)    
  206.     {    
  207.         var CurrentRight = AxleRight[i] as IMyMotorStator;    
  208.         CurrentRight.ApplyAction("ResetVelocity");    
  209.     }    
  210.    
  211.     for(int i = 0 ; i < AxleLeft.Count ; i++)    
  212.     {    
  213.         var CurrentLeft = AxleLeft[i] as IMyMotorStator;    
  214.         CurrentLeft.ApplyAction("ResetVelocity");    
  215.     }    
  216. }
Advertisement
Add Comment
Please, Sign In to add comment