1. This forum is obsolete and read-only. Feel free to contact us at support.keenswh.com

[Method] Apply Gyro Override

Discussion in 'Programming Released Codes' started by Whiplash141, Jan 15, 2018.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Whiplash141

    Whiplash141 Junior Engineer

    Messages:
    964
    Howdy!
    I figured I'd dump all of my helpful methods on the forums to help those who are looking to learn. This is my standard method for applying a gyro override using a specified block as a rotation reference.

    Code:
    Code:
    //Whip's ApplyGyroOverride Method v9 - 8/19/17
    void ApplyGyroOverride(double pitch_speed, double yaw_speed, double roll_speed, List<IMyGyro> gyro_list, IMyTerminalBlock reference)
    {
    	var rotationVec = new Vector3D(-pitch_speed, yaw_speed, roll_speed); //because keen does some weird stuff with signs
    	var shipMatrix = reference.WorldMatrix;
    	var relativeRotationVec = Vector3D.TransformNormal(rotationVec, shipMatrix);
    	foreach (var thisGyro in gyro_list)
    	{
    		var gyroMatrix = thisGyro.WorldMatrix;
    		var transformedRotationVec = Vector3D.TransformNormal(relativeRotationVec, Matrix.Transpose(gyroMatrix));
    		thisGyro.Pitch = (float)transformedRotationVec.X;
    		thisGyro.Yaw = (float)transformedRotationVec.Y;
    		thisGyro.Roll = (float)transformedRotationVec.Z;
    		thisGyro.GyroOverride = true;
    	}
    }


    Example Use:
    Code:
    //Speeds are in radians per second
    double desiredPitchSpeed = 1;
    double desiredYawSpeed = 2;
    double desiredRollSpeed = 3;
    
    List<IMyGyro> gyros = new List<IMyGyro>();
    
    void Main()
    {
    	var referenceBlock = GridTerminalSystem.GetBlockWithName("Reference"); //grab a block with which to base rotations off off
    	
    	GridTerminalSystem.GetBlocksOfType(gyros); //grab list of gyros
    
    	bool isBroke = false; //error handling
    	if (referenceBlock == null)
    	{
    		isBroke = true;
    		Echo("Error: No reference block found");
    	}
    
    	if (gyros.Count == 0)
    	{
    		 isBroke = true;
    		Echo("Error: No gyros found");
    	}
    
    	if (isBroke)
    		return;
    
    	ApplyGyroOverride(desiredPitchSpeed, desiredYawSpeed, desiredRollSpeed, gyros, referenceBlock); //apply gyro override
    }
    


    Hope y'all find this helpful :)
     
    Last edited: Jun 11, 2019
    • Like Like x 2
  2. BumbleGrum

    BumbleGrum Trainee Engineer

    Messages:
    11
    That is helpful. Thank you. I just hope I can remember where I found this code when I need it! :)
     
Thread Status:
This last post in this thread was made more than 31 days old.