Autopilot No rating results yet

posted in: Control, Navigation, Operations, Scripts | 1

Used in conjunction with my Navigation Script, this script will pilot your ship to the target coordinates.

It rotates your ship to the proper heading, then uses thruster override to get you there. Once you come within 500 meters of your target, thrusters are shut down.

// Autopilot script
   
void Main()    
{    
    // Define all the block lists we are going to need 
    List navigation = new List();    
    List gyros = new List();     
    List thrusters = new List();     

	// Populate the navigation list with the "NAV:" block
    GridTerminalSystem.SearchBlocksOfName("NAV:" , navigation);   
   
    // Populate the gyros list with all blocks named "Control Gyro" 
    GridTerminalSystem.SearchBlocksOfName("Control Gyro" , gyros);     
      
    // Populate the thrusters list with all blocks that have 
	// "(Forward)" in the name
    GridTerminalSystem.SearchBlocksOfName("(Forward)" , thrusters);     
      
	// Figure out which direction we need to rotate the ship
	String navigationBlockName = navigation[0].CustomName;
	String[] navigationArray = navigationBlockName.Split((char)13);
	
	int pitchAdjustment = 0;
	int yawAdjustment = 0;
	String thrustAdjustment = "";
    for (int i=0; i < navigationArray.Length; i++) 
	{	
		String[] parts;
		
		if (navigationArray[i].Contains("NAV:"))
		{
			parts = navigationArray[i].Split(':');
			thrustAdjustment = parts[1];
		}
		
		if (navigationArray[i].Contains("Pitch"))
		{
			parts = navigationArray[i].Split(' ');
			if (parts[1].Equals("Down")) 
			{
				pitchAdjustment = Convert.ToInt32(parts[2]) * -1;
			}
			else
			{
				pitchAdjustment = Convert.ToInt32(parts[2]);
			}
		}
		if (navigationArray[i].Contains("Yaw"))
		{
			parts = navigationArray[i].Split(' ');
			if (parts[1].Equals("Left")) 
			{
				yawAdjustment = Convert.ToInt32(parts[2]) * -1;
			}
			else
			{
				yawAdjustment = Convert.ToInt32(parts[2]);
			}
		}
    }   
   
    // Loop through the blocks in the navigation list
    for (int i=0; i < gyros.Count; i++)    
    {    
        IMyGyro g = gyros[i] as IMyGyro;	
		g.SetValueFloat("Yaw",yawAdjustment/10f);
		g.SetValueFloat("Pitch",pitchAdjustment/10f);
		if ((yawAdjustment == 0) && (pitchAdjustment == 0)) 
		{
		    if (g.GyroOverride) 
			{
				g.GetActionWithName("Override").Apply(g);
			}
		}
		else 
		{
		    if (!g.GyroOverride) 
			{
				g.GetActionWithName("Override").Apply(g);
			}
		}
	}	
	
	String unit = "m";
	float multiplier = 1f;
	if (thrustAdjustment.Contains("km")) 
	{
	   multiplier = 1000f;
	}
	float distance = Convert.ToInt32(thrustAdjustment.Replace("km","").Replace("m","")) * multiplier;
	float thrust = 12000f;
	if (distance < 500)
	{
		thrust = 0f;
	}
	
    for (int i=0; i < thrusters.Count; i++)    
    {    
		if (thrusters[i] is IMyThrust)
		{
			IMyThrust t = thrusters[i] as IMyThrust;
			t.SetValueFloat("Override", thrust);
		}
	}
}   
 

Please rate this post

One Response

  1. Sean L Palmer

    this is nice! However it does not handle the gyros being mounted in differing orientations – they’ll fight each other. Another improvement may be to automatically detect which thrusters are aimed which way and fire the appropriate ones. These are things I’ve already gotten working as part of my hovercraft/elevator stabilizer control script thing I’ve been working on. Contact me for more details.

Leave a Reply