Guest User

Rotor Slaving V001503062233

a guest
Mar 6th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. /*
  2. * Cats makes the best programmers!
  3. * Controls the velocity of a list of slave rotors to maintain equivalent rotation relative to a pair of master rotors
  4. * The difference in angle between RM1 and all RS1 and RS1i rotors is converted to a velocity and applied to the RS1 and RS1i rotors
  5. * The same is done for RM2 and RS2 and RS2i rotors.
  6. * The result should be that all the rotors maintain the same angle allowing them to be used in custom turrets
  7. * This script is very basic and unlikely to be directly useful. It is indented to be a starting point for further development.
  8. * http://steamcommunity.com/sharedfiles/filedetails/?id=392439615
  9. */
  10.  
  11. const string SHIP_PREFIX = null; // here for prefix filtering. if set to a string, the script will lokk for blocks whos name starts with that string, ignoring all others.
  12.  
  13. const string ROTOR_MASTER_ONE = "[RM1]"; // Base (hour angle) rotor master (Only one used for this script)
  14. const string ROTOR_MASTER_TWO = "[RM2]"; // Second (Declination) rotor master (Only one used for this script)
  15.  
  16. const string ROTOR_SLAVE_ONE = "[RS1]"; // Base (hour angle) rotor slave (use as many as you like!)
  17. const string ROTOR_SLAVE_TWO = "[RS2]"; // Second (Declination) rotor slave (use as many as you like!)
  18. const string ROTOR_SLAVE_ONE_INV = "[RS1i]"; // Base (hour angle) rotor slave Inverted (use as many as you like!)
  19. const string ROTOR_SLAVE_TWO_INV = "[RS2i]"; // Second (Declination) rotor slave Inverted (use as many as you like!)
  20.  
  21. const float SLOW = 5; // how much to slow down the turret rotation speed by (needed value depends on small or large ship)
  22.  
  23. bool set = false; // determines whether or not setup has been run.
  24. IMyMotorStator[] Masters = null; // stores the 2 Master rotor items
  25. List<IMyMotorStator> SlavesOne = null; // stores all slave one rotors
  26. List<IMyMotorStator> SlavesOneInv = null; // stores all slave one inverse rotors
  27. List<IMyMotorStator> SlavesTwo = null; // stores all slave two rotors
  28. List<IMyMotorStator> SlavesTwoInv = null; // stores all slave two inverse rotors
  29.  
  30.  
  31. // used once to find all the target rotors
  32. public void Setup() {
  33. Masters = new IMyMotorStator[2];
  34. SlavesOne = new List<IMyMotorStator>();
  35. SlavesOneInv = new List<IMyMotorStator>();
  36. SlavesTwo = new List<IMyMotorStator>();
  37. SlavesTwoInv = new List<IMyMotorStator>();
  38.  
  39. var blocks = new List<IMyTerminalBlock>();
  40. GridTerminalSystem.GetBlocksOfType<IMyMotorStator>( blocks );
  41. for( int e = 0; e < blocks.Count; e++ ) {
  42. blocks[e].SetValueFloat( "Velocity", 0 );
  43. if( SHIP_PREFIX == null || blocks[e].CustomName.StartsWith( SHIP_PREFIX ) ) {
  44. if( blocks[e].CustomName.Contains( ROTOR_SLAVE_ONE ) ) {
  45. SlavesOne.Add( (IMyMotorStator)blocks[e] );
  46. } else if ( blocks[e].CustomName.Contains( ROTOR_SLAVE_TWO ) ) {
  47. SlavesTwo.Add( (IMyMotorStator)blocks[e] );
  48. } else if( blocks[e].CustomName.Contains( ROTOR_SLAVE_ONE_INV ) ) {
  49. SlavesOneInv.Add( (IMyMotorStator)blocks[e] );
  50. } else if ( blocks[e].CustomName.Contains( ROTOR_SLAVE_TWO_INV ) ) {
  51. SlavesTwoInv.Add( (IMyMotorStator)blocks[e] );
  52. } else if ( blocks[e].CustomName.Contains( ROTOR_MASTER_ONE ) ) {
  53. Masters[0] = (IMyMotorStator)blocks[e];
  54. } else if ( blocks[e].CustomName.Contains( ROTOR_MASTER_TWO ) ) {
  55. Masters[1] = (IMyMotorStator)blocks[e];
  56. }
  57. }
  58. }
  59.  
  60. if( Masters[0] == null || Masters[1] == null ) {
  61. throw new System.ArgumentException("Setup error", "Masters Not Found");
  62. }
  63. }
  64.  
  65. // Update all slave rotor velocities to move them toward the Master rotor angle
  66. public void Update() {
  67. float tarDeg = getAngle( Masters[0] );
  68. float curDeg = 0;
  69. for( int e = 0; e < SlavesOne.Count; e++ ) {
  70. curDeg = getAngle( SlavesOne[e] );
  71. SlavesOne[e].SetValueFloat( "Velocity", getNeededVel( tarDeg, curDeg ) / SLOW );
  72. }
  73.  
  74. for( int e = 0; e < SlavesOneInv.Count; e++ ) {
  75. curDeg = getAngle( SlavesOneInv[e] );
  76. SlavesOneInv[e].SetValueFloat( "Velocity", getNeededVel( ((float)360)-tarDeg, curDeg ) / SLOW );
  77. }
  78.  
  79. tarDeg = getAngle( Masters[1] );
  80. for( int e = 0; e < SlavesTwo.Count; e++ ) {
  81. curDeg = getAngle( SlavesTwo[e] );
  82. SlavesTwo[e].SetValueFloat( "Velocity", getNeededVel( tarDeg, curDeg ) / SLOW );
  83. }
  84.  
  85. for( int e = 0; e < SlavesTwoInv.Count; e++ ) {
  86. curDeg = getAngle( SlavesTwoInv[e] );
  87. SlavesTwoInv[e].SetValueFloat( "Velocity", getNeededVel( ((float)360)-tarDeg, curDeg ) / SLOW );
  88. }
  89. }
  90.  
  91. // just because it got repeated so often
  92. public float getAngle( IMyMotorStator motor ) {
  93. string data = motor.DetailedInfo;
  94. string[] dataSplit = data.Split( ':' );
  95. return float.Parse( dataSplit[1].Substring( 0, dataSplit[1].Length-1 ) );
  96. }
  97.  
  98. // There is probably a better way to do this, but this appears to work.
  99. // I feel like there should be an elegant mathematical solution for this rather than if else
  100. public float getNeededVel( float tDeg, float cDeg ) {
  101. float nVel = tDeg - cDeg;
  102. if( nVel > 180 ) {
  103. nVel = nVel - ((float)360);
  104. } else if( nVel < -180 ) {
  105. nVel = ((float)360) + nVel;
  106. }
  107. return nVel;
  108. }
  109.  
  110.  
  111. void Main() {
  112. if( set == false ) {
  113. Setup();
  114. set = true;
  115. } else {
  116. Update();
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment