|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools |
Rating:
|
Display Modes |
|
|
|
#1
|
|||
|
|||
|
Gyro PID Programming
Hello,
I am attempting to use a PID loop to force the robot into the 0 degrees starting position at a click of a button. [+] At first, I created a PIDController and initialized it with the Gyro object as the PIDSource, and my own object with a custom PIDWrite routine which consists of: Code:
void PIDWrite(float Output)
{
Drive->TankDrive(Output, -Output); //This makes the robot turn around
}
[+] I then decided to write my own PIDGet() too. All the routine does is get the raw angle, scale it to a value from -1.0 to 1.0 and return it. (I am under the impression that the Output and Source variables should be scaled to the same range, please tell me if this is wrong.) After scaling both the Output and Source vars, setting the setpoint to 0.0 (the starting position at which the gyro was reset) and enabling the PID Controller, nothing happens Please tell me if you can find anything wrong with what I did, and thanks in advance. |
|
#2
|
||||
|
||||
|
Re: Gyro PID Programming
Quote:
|
|
#3
|
|||
|
|||
|
Re: Gyro PID Programming
Alright, I don't currently have the code with me so this is an approximation:
Code:
#define GYRO_SCALE(x) ((x) / 360)
/*
* This fixes the Angle overflow problem, so the angle is always in the range
* -360 to 360
*/
float Gyro_FixAngle(float Angle)
{
int Laps = Angle / 360;
return (Angle - (Laps * 360));
}
double PIDGet()
{
return GYRO_SCALE(Gyro_FixAngle(Gyro1->GetAngle()));
}
|
|
#4
|
||||
|
||||
|
Re: Gyro PID Programming
Quote:
You want your robot to always take the shortest angle to get back to the zero position. For example, if your gyro angle is reading 359 degrees, you don't want to rotate the robot back 359 degrees to get to zero. You want to rotate it forward 1 degree to get back to zero. So you want the corrected gyro angle in the range -180 to +180, with zero being your starting position. |
|
#5
|
||||
|
||||
|
Re: Gyro PID Programming
Quote:
Code:
double corrected_angle(double Angle){
return Angle + 360*floor(0.5-Angle/360);
}
|
|
#6
|
||||||
|
||||||
|
Re: Gyro PID Programming
What are your PID gains?
|
|
#7
|
|||
|
|||
|
Re: Gyro PID Programming
Make sure you set continuous to true on your controller to accomplish this.
|
|
#8
|
||||
|
||||
|
Re: Gyro PID Programming
Quote:
|
|
#9
|
|||
|
|||
|
Re: Gyro PID Programming
True, but if the setpoint is ever anything but zero, it may not take the shortest route.
|
|
#10
|
||||
|
||||
|
Re: Gyro PID Programming
Quote:
But if it were, just correct the angle_error instead and use setpoint = angle_error; process_variable = 0; or setpoint = 0; process_variable = - angle_error; |
|
#11
|
|||
|
|||
|
Re: Gyro PID Programming
Thank you very much for your comments!
[+] About the PID Gains, I'm totally clueless as to what to set them to, so I just set the P variable to 0.1 and zeroed out the others. (Perhaps this was causing the unexpected results when I directly used the Gyro as input, because the output was always @ 100% all the time); Could I set them to something more appropriate? [+] Should my current method work when I fix the Angle correction routine? or should I use the 2-PIDControllers method? It sounds odd to me that two PIDControllers could 'collaborate' together, when running independently. Last edited by UriF : 01-02-2013 at 03:37. |
|
#12
|
|||
|
|||
|
Re: Gyro PID Programming
Quote:
|
|
#13
|
|||
|
|||
|
Re: Gyro PID Programming
OK. I would still like to know about tuning the PID Gains.
|
|
#14
|
|||
|
|||
|
Re: Gyro PID Programming
Quote:
As Ether suggested, -180 to 180 range, that may be a little high. It would only take an error of 10 degrees to create full motor output. So, get the input corrected to match the suggested range. This isn't just scaling or constraining, btw. You will need to add or subtract the extra turns if they are in there (fmod). Make sure your setpoint is in the same terms as the input, degrees. And then start tuning your p gain. Don't worry about the others for now. |
|
#15
|
|||
|
|||
|
Re: Gyro PID Programming
Thank you Jefferson, Ether. You have helped me understand this greatly.
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|