Go to Post WE might have a different plan than being overweight, but thats just my guess - pakrat [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rating: Thread Rating: 22 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 31-01-2013, 14:39
UriF UriF is offline
Registered User
FRC #2213
 
Join Date: Jan 2012
Location: Israel
Posts: 27
UriF is an unknown quantity at this point
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 was getting unexpected results, and It turned out that the Gyro's PIDGet() was actually returning the raw angle and not a value scaled from -1.0 to 1.0

[+] 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.
Reply With Quote
  #2   Spotlight this post!  
Unread 31-01-2013, 14:44
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,098
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Gyro PID Programming

Quote:
Originally Posted by UriF View Post
All the routine does is get the raw angle, scale it to a value from -1.0 to 1.0 and return it.
Show us how you did this.


Reply With Quote
  #3   Spotlight this post!  
Unread 31-01-2013, 14:52
UriF UriF is offline
Registered User
FRC #2213
 
Join Date: Jan 2012
Location: Israel
Posts: 27
UriF is an unknown quantity at this point
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()));
}
Reply With Quote
  #4   Spotlight this post!  
Unread 31-01-2013, 17:34
Happy Birthday! Jefferson Jefferson is offline
Registered User
AKA: Jeff Clements
FRC #0016 (Bomb Squad)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Mountain Home, AR
Posts: 258
Jefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond repute
Re: Gyro PID Programming

Quote:
Originally Posted by UriF View Post
Hello,
(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.)
This is unnecessary. You can use the unscaled input (0-360). The right control variables will be different if you use the scaled input. But I can't think of a good reason to scale like you did.

Also, you could probably use the unmodified version of the PIDContoller by just setting up two controllers. They would both use the gyro as an input, but the output would be different (the two drive motors). The gains for one side will be the inverse of the other side in order to invert the output. I don't think you can do this with Robot Builder, but you could create them by hand in the RobotMap class.
Reply With Quote
  #5   Spotlight this post!  
Unread 31-01-2013, 18:01
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,098
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Gyro PID Programming

Quote:
Originally Posted by UriF View Post
Code:
/*
 * 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()));
}
You don't want the (corrected) gyro angle in the range -360 to +360.

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.



Reply With Quote
  #6   Spotlight this post!  
Unread 31-01-2013, 18:12
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,098
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Gyro PID Programming

Quote:
Originally Posted by Ether View Post
You don't want the (corrected) gyro angle in the range -360 to +360 ... you want the corrected gyro angle in the range -180 to +180, with zero being your starting position.
This should do the trick:

Code:
double corrected_angle(double Angle){
return Angle + 360*floor(0.5-Angle/360);
}
Then use setpoint=0 and process_variable = corrected_angle as inputs to a PID controller.


Reply With Quote
  #7   Spotlight this post!  
Unread 31-01-2013, 19:00
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,576
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: Gyro PID Programming

What are your PID gains?
Reply With Quote
  #8   Spotlight this post!  
Unread 31-01-2013, 19:27
Happy Birthday! Jefferson Jefferson is offline
Registered User
AKA: Jeff Clements
FRC #0016 (Bomb Squad)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Mountain Home, AR
Posts: 258
Jefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond repute
Re: Gyro PID Programming

Quote:
Originally Posted by Ether View Post

You want your robot to always take the shortest angle to get back to the zero position.
Make sure you set continuous to true on your controller to accomplish this.
Reply With Quote
  #9   Spotlight this post!  
Unread 31-01-2013, 20:35
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,098
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Gyro PID Programming

Quote:
Originally Posted by Jefferson View Post
Make sure you set continuous to true on your controller to accomplish this.
Not required if angle is -180..+180 and setpoint is 0.


Reply With Quote
  #10   Spotlight this post!  
Unread 31-01-2013, 21:12
Happy Birthday! Jefferson Jefferson is offline
Registered User
AKA: Jeff Clements
FRC #0016 (Bomb Squad)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Mountain Home, AR
Posts: 258
Jefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond repute
Re: Gyro PID Programming

True, but if the setpoint is ever anything but zero, it may not take the shortest route.
Reply With Quote
  #11   Spotlight this post!  
Unread 31-01-2013, 22:08
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,098
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Gyro PID Programming

Quote:
Originally Posted by Jefferson View Post
True, but if the setpoint is ever anything but zero, it may not take the shortest route.
That wasn't the question the OP was asking.


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;



Reply With Quote
  #12   Spotlight this post!  
Unread 01-02-2013, 03:12
UriF UriF is offline
Registered User
FRC #2213
 
Join Date: Jan 2012
Location: Israel
Posts: 27
UriF is an unknown quantity at this point
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.
Reply With Quote
  #13   Spotlight this post!  
Unread 01-02-2013, 08:28
Happy Birthday! Jefferson Jefferson is offline
Registered User
AKA: Jeff Clements
FRC #0016 (Bomb Squad)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Mountain Home, AR
Posts: 258
Jefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond repute
Re: Gyro PID Programming

Quote:
[+] 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.
Either way should work. I was just trying to help you avoid building your own PIDController. Whichever you are more comfortable with is the right answer.
Reply With Quote
  #14   Spotlight this post!  
Unread 01-02-2013, 09:13
UriF UriF is offline
Registered User
FRC #2213
 
Join Date: Jan 2012
Location: Israel
Posts: 27
UriF is an unknown quantity at this point
Re: Gyro PID Programming

OK. I would still like to know about tuning the PID Gains.
Reply With Quote
  #15   Spotlight this post!  
Unread 01-02-2013, 09:52
Happy Birthday! Jefferson Jefferson is offline
Registered User
AKA: Jeff Clements
FRC #0016 (Bomb Squad)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Mountain Home, AR
Posts: 258
Jefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond reputeJefferson has a reputation beyond repute
Re: Gyro PID Programming

Quote:
Originally Posted by UriF View Post

[+] 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?
A p of .1 would take an error of 10 to create a 1.0 (100%) output. The error is calculated by subtracting the setpoint from the current position. If you are using,
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.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 03:30.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi