Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Problem with NavX to turn around. (http://www.chiefdelphi.com/forums/showthread.php?t=143717)

Oblarg 16-04-2016 11:32

Re: Problem with NavX to turn around.
 
Quote:

Originally Posted by rich2202 (Post 1573691)
In theory, PID is great. In practice, not so much.

In this exercise, you are trying to turn a robot to a set angle. PID will increase the speed until you get there, and then oscillate back and forth trying to hit the mark. The easier way to do it is to turn at a controlled speed until you get there.

In the original code, I'm guessing that the robot is rotating too fast, and overshoots the error range (self.desired to self.desired+10) that one time in 10. The solution is to turn slower, check the angle more frequently, wider error range. Maybe when you get close, you slow down the turn.

This is kind of (erm, very) misleading.

One can easily cap the max speed of a P loop (let's be honest, for turn-to-angle you're probably not going to need any I or D) or tune the P term to avoid overshoot. In fact, one of the appeals of a P loop is precisely that the output ramps down as you approach the setpoint, such that you do not overshoot. In this capacity, it is quite a bit nicer than bang-bang control (which is what you are describing).

Ether 16-04-2016 12:43

Re: Problem with NavX to turn around.
 
Quote:

Originally Posted by DanielHa (Post 1573655)
at certain absolute angles, if you simply add or subtract 180 (or in your case the slightly altered values), then you might get a value which is outside the range of the navX.

Let S be your sensor angle reading (in degrees, can be any range, even outside the range +/-360)

Let T be your desired angle position (in degrees, can be any range, even outside the range +/-360)

the angle between them (let's call it ERR) is found by the equation:

ERR = (T-S) - 360*floor(0.5+(T-S)/360);

the above returns a value for ERR between -180 and +180 degrees,
the shortest angle path to the target. Don't believe me? Try it.

So it tells you which direction to rotate as well as how much to rotate.

To use this with a PID, use processVariable = setpoint-ERR



Oblarg 16-04-2016 15:02

Re: Problem with NavX to turn around.
 
Quote:

Originally Posted by Ether (Post 1573836)
Let S be your sensor angle reading (in degrees, can be any range, even outside the range +/-360)

Let T be your desired angle position (in degrees, can be any range, even outside the range +/-360)

the angle between them (let's call it ERR) is found by the equation:

ERR = (T-S) - 360*floor(0.5+(T-S)/360);

the above returns a value for ERR between -180 and +180 degrees,
the shortest angle path to the target. Don't believe me? Try it.

So it tells you which direction to rotate as well as how much to rotate.

To use this with a PID, use processVariable = setpoint-ERR



This is unnecessary with the NavX and WPILib, as one can simply call AHRS pidGet() (which always ranges from -180 to 180) and ensure that the PIDController object is set to have a continuous range and not have to ever explicitly do any of the math.

Ether 16-04-2016 15:16

Re: Problem with NavX to turn around.
 
Quote:

Originally Posted by Oblarg (Post 1573892)
This is unnecessary with the NavX and WPILib, as one can simply call AHRS pidGet() (which always ranges from -180 to 180) and ensure that the PIDController object is set to have a continuous range and not have to ever explicitly do any of the math.

Yes.

I was responding to DanielHa's post, to show a simple way to compute shortest angle... which would be useful to know for situations where the library functions or the hardware don't do the math for you.



Foster 16-04-2016 18:27

Re: Problem with NavX to turn around.
 
Quote:

Originally Posted by Ether (Post 1573894)
Yes.

I was responding to DanielHa's post, to show a simple way to compute shortest angle... which would be useful to know for situations where the library functions or the hardware don't do the math for you.

Exactly! Always good to go "Umm I need to do this, looks like that library does that, oh wait it won't work in all cases".

There are few things that I hate more than debugging code "What does this do?" "Dunno, think it turns the robot". /sigh.

Quote:

Originally Posted by Jared Russell
I would argue that PID is much more elegant in practice than in theory.

A well tuned PID is a thing of beauty. Most people that hate PID have not taken the time to dial them in.

Oblarg 16-04-2016 19:32

Re: Problem with NavX to turn around.
 
Quote:

Originally Posted by Foster (Post 1573947)
Exactly! Always good to go "Umm I need to do this, looks like that library does that, oh wait it won't work in all cases".

I will opine that, while it is often a useful pedagogical exercise (and it's often useful to know how what a library is doing under the hood), reinventing the wheel is not generally good coding practice and if you have a library that does what you want (in this case, you do) it is good to use it.

Sometimes, of course, libraries are inadequate and you have to build your code from the ground up. But this is not always the case.

Ether 16-04-2016 19:49

Re: Problem with NavX to turn around.
 
Quote:

Originally Posted by Oblarg (Post 1573956)
reinventing the wheel is not generally good coding practice and if you have a library that does what you want (in this case, you do) it is good to use it.

In case you have inferred otherwise, nothing I posted in this thread should be construed as antithetical to the above.



Oblarg 16-04-2016 19:51

Re: Problem with NavX to turn around.
 
Quote:

Originally Posted by Ether (Post 1573965)
In case you have inferred otherwise, nothing I posted in this thread should be construed as antithetical to the above.

Nah, that post was purely in response to Foster's, not yours.

Foster 16-04-2016 19:53

Re: Problem with NavX to turn around.
 
While not speaking for Ether, I don't think either one of us was recommending that they reinvent the code / wheel. But the OP wanted to know the details and Ether supplied them. From his simple explanation it would be easy to write the code / PID function.

But I think both of us are recommending understanding how things work for the coders.


All times are GMT -5. The time now is 04:58.

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