Now Gyro

Hello, my last post was about encoders but now its about Gyro. Do people usually people use Gyro with Encoders?

That’s it. Thanks!

Yes… of course.

@meng then whats the point of Gyro? Is it just a more precise way to turn?

Most of the time we used gyro for turning. And if used correctly, the gyro will give you a more precise turning.

As for encoder, we normally used it to track how far the robot should move.

@meng Thank you.

They gyro can also ensure that the robot drives straight, even when you are using encoders.

Puffffffff…

My mind is blown from all the PID and Gyro and Encoders’ Programming…

#pragma config(Sensor, in1,    Gyro,           sensorGyro)
#pragma config(Sensor, dgtl1,  RightDriveEncoder, sensorQuadEncoder)
#pragma config(Sensor, dgtl11, LeftDriveEncoder, sensorQuadEncoder)
#pragma config(Motor,  port1,           RightDrive1,   tmotorVex393HighSpeed_HBridge, PIDControl, reversed, encoderPort, dgtl1)
#pragma config(Motor,  port2,           RightDrive2,   tmotorVex393HighSpeed_MC29, PIDControl, reversed, encoderPort, dgtl1)
#pragma config(Motor,  port9,           LeftDrive2,    tmotorVex393HighSpeed_MC29, PIDControl, encoderPort, dgtl11)
#pragma config(Motor,  port10,          LeftDrive1,    tmotorVex393HighSpeed_HBridge, PIDControl, encoderPort, dgtl11)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

int GyroValue = SensorValue(Gyro);
int GyroMotorTarget;
void GyroReset()
{
	SensorType[in1] = sensorNone;
	wait1Msec(1000);
	SensorType[in1] = sensorGyro;
	wait1Msec(1150);//Might have to raise to 2000 if Gyro Drifts
}	
task GyroTask()
{
	GyroReset();
	int RightGyro90;
	int LeftGyro90;
	int Straight;
	int Backwards;
	while (true)
		if(RightGyro90 == 1 && SensorValue(Gyro) < 900)
		{
			GyroMotorTarget = 900;
		}
		else if(RightGyro90 == 1 && SensorValue(Gyro) > 900)
		{
			GyroMotorTarget = -900;
		}
		
		else if(LeftGyro90 == 1 && SensorValue(Gyro) > 900)
		{
			GyroMotorTarget = -900;
		}
		else if(Straight == 1 && SensorValue(Gyro) != 0)
		{
			GyroMotorTarget = 0;
		}
		else if(Backwards == 1 && SensorValue(Gyro) != 1800)
		{
			GyroMotorTarget = 1800;
		}
}


/*
1. Set Kp, Ki and Kd to 0. This will disable them for now.
2. Start increasing Kp until there is some oscillation in the error. Not too much
oscillation, just a noticeable amount. Save this value for Kp as “Ku” - the value for
the ultimate or critical gain.
3. You now need to measure the period of the oscillation. Save the value for the period
as “Pu” - the period at the ultimate or critical gain.

PID 0.60Ku 2Kp/Pu KpPu/8
*/

	static float RightDriveTarget;
	static float RightDriveSpeed;
	static float LeftDriveSpeed;
	static float LeftDriveTarget;
	static float DriveKp = 0;//TUNING NEEDED
	static float DriveKi = 0;//TUNING NEEDED
	static float DriveKd = 0;//TUNING NEEDED
task DriveBasePID()
{
//Right Variables Declartion

	float RightDriveSensor;
	float RightDriveError;
	float RightDriveIntegral;
	float RightDriveDerivative;
	float RightDrivePreviousError;
	float RightDriveMaximumError = 0;//TUNING NEEDED
	
//Left Variables Declaration
	float LeftDriveSensor;
	float LeftDriveError;
	float LeftDriveIntegral;
	float LeftDriveDerivative;
	float LeftDrivePreviousError;
	float LeftDriveMaximumError;//TUNING NEEDED
	
	while(true)
	{
		
		//Right P
		RightDriveSensor = SensorValue(RightDriveEncoder);
		//Right I
		RightDriveError = RightDriveTarget - RightDriveSensor;
		RightDriveIntegral = RightDriveIntegral + RightDriveError;
		if(RightDriveError == 0)
		{
			RightDriveIntegral = 0;
		}
		if(abs(RightDriveError) > RightDriveMaximumError)
		{
			RightDriveIntegral = 0;
		}
		//Right D
		RightDriveDerivative = RightDriveError - RightDrivePreviousError;
		RightDrivePreviousError = RightDriveError;
		//Set Speed 
		RightDriveSpeed = DriveKp*RightDriveError + DriveKi*RightDriveIntegral + DriveKd*RightDriveDerivative;
		
		
		//Left P
		LeftDriveSensor = SensorValue(LeftDriveEncoder);
		//Left I
		LeftDriveError = LeftDriveTarget - LeftDriveSensor;
		LeftDriveIntegral = LeftDriveIntegral + LeftDriveError;
		if(LeftDriveError == 0)
		{
			LeftDriveIntegral = 0;
		}
		if(abs(LeftDriveError) >LeftDriveMaximumError)
		{
			LeftDriveIntegral = 0;
		}
		//Left D
		LeftDriveDerivative = LeftDriveError - LeftDrivePreviousError;
		LeftDrivePreviousError = LeftDriveError;	
		//Set Speed
		LeftDriveSpeed = DriveKp*LeftDriveError + DriveKi*LeftDriveIntegral + DriveKd*LeftDriveDerivative;
	}
}
void LeftDriveManual (int LeftDriveManual)
{
	if(abs(LeftDriveManual) >= 10)
	{
		motor[LeftDrive1] = LeftDriveManual;
		motor[LeftDrive2] = LeftDriveManual;
	}
	else
	{
		motor[LeftDrive1] = 0;
		motor[LeftDrive2] = 0;
	}
}
void RightDriveManual (int RightDriveManual)
{
	if(abs(RightDriveManual) >= 10)
	{
		motor[RightDrive1] = RightDriveManual;
		motor[RightDrive2] = RightDriveManual;
	}
	else
	{
		motor[RightDrive1] = 0;
		motor[RightDrive2] = 0;
	}
}

void straight(int Speed)//Positive is Straight
{
	motor[RightDrive1] = Speed;
	motor[RightDrive2] = Speed;
	motor[LeftDrive1] = Speed;
	motor[LeftDrive1] = Speed;
}
void turn(int Speed)//Positive is Clockwise
{
	motor[RightDrive1] = Speed;
	motor[RightDrive2] = Speed;
	motor[LeftDrive1] = -Speed;
	motor[LeftDrive1] = -Speed;
}

int DriveState;
int DriveDirectionSelection;
int Speed = GyroMotorTarget;
task DriveControl()
{
	switch(DriveDirectionSelection)
	{
		case 0:
		DriveState = 1;
		//Manual Control
		RightDriveManual(1);
		LeftDriveManual(1);
		
		case 1:
		DriveState = 2;
		// Forwards / Backwards
		startTask (DriveBasePID);
		startTask (GyroTask);
		straight(Speed);
		
		case 2:
		DriveState = 3;
		//Right / Left
		startTask(DriveBasePID);
		startTask(GyroTask);
		turn(Speed);
		
	}
}

Would someone please check if what I’m doing with Gyro and Encoders make sense or not? Sorry. I don’t have access to our robot…