Go to Post Everyone together now, say 'thank you'. - ebarker [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 12-02-2016, 18:20
team-4480's Avatar
team-4480 team-4480 is offline
Debug? What's that?
FRC #4480
 
Join Date: Jan 2015
Rookie Year: 2013
Location: Minnesooota
Posts: 218
team-4480 will become famous soon enoughteam-4480 will become famous soon enough
Problem with NavX to turn around.

Hi,

This year, our shooter direction is opposite of our front of our robot. I decided that we should use the NavX we just got to semi-accurately turn ourselves around. We don't have any encoders on our motors so I just made a simple function that would have the motors turn until it hit a 10 degree range in which case it would stop.

The problem is that it doesn't always work. It works 9/10 times but that one time it just spins endlessly in a circle and never stops. This, of course, is not something you want to happen. I am asking if there is a better way to go about programming this so it ALWAYS works? Here is the current function:

Code:
        #if button pressed, start
        if self.turn_button.get():
            self.turn_state=0
            yaw=self.navx.getYaw()
            #Our NavX is not completely centered on the robot so 
            #that is why it isn't 180 degrees for turning around
            if yaw > 0:
               
                self.desired=self.navx.getYaw()-227
            else:
                self.desired=self.navx.getYaw()+133
 
 
        current=self.navx.getYaw()
        if self.turn_state==0:
            self.turn_state=1
        elif self.turn_state==1:
            if current < (self.desired+10) and current > self.desired:
                self.turn_state=2
            else:
                self.drive2.set(.7)
                self.drive1.set(.5)
Any help would be greatly appreciated!
Reply With Quote
  #2   Spotlight this post!  
Unread 12-02-2016, 18:46
Jalerre's Avatar
Jalerre Jalerre is offline
Registered User
FRC #4965 (FIRE)
Team Role: Alumni
 
Join Date: Apr 2014
Rookie Year: 2013
Location: Anderson, SC
Posts: 147
Jalerre is on a distinguished road
Re: Problem with NavX to turn around.

The best way to do this would be to use PID. Link
__________________
Vex
2013: 415 - Fabrication, Programming, Electrical
2014: 415D - Captain, Programming Lead
FTC
2014: 7354 - Programming Lead
FRC
2013: 415 - Safety Animation, Fabrication
2014-2015: 4965 - Programming & Electrical
2016: 4965 - Captain, Programming Lead, Electrical
Reply With Quote
  #3   Spotlight this post!  
Unread 12-02-2016, 18:55
team-4480's Avatar
team-4480 team-4480 is offline
Debug? What's that?
FRC #4480
 
Join Date: Jan 2015
Rookie Year: 2013
Location: Minnesooota
Posts: 218
team-4480 will become famous soon enoughteam-4480 will become famous soon enough
Re: Problem with NavX to turn around.

Quote:
Originally Posted by Jalerre View Post
The best way to do this would be to use PID. Link
Unfortunately we don't have encoders on our robot to be able to do that.
Reply With Quote
  #4   Spotlight this post!  
Unread 12-02-2016, 19:21
slibert slibert is offline
Software Mentor
AKA: Scott Libert
FRC #2465 (Kauaibots)
Team Role: Mentor
 
Join Date: Oct 2011
Rookie Year: 2005
Location: Kauai, Hawaii
Posts: 343
slibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud of
Re: Problem with NavX to turn around.

Quote:
Originally Posted by team-4480 View Post
Unfortunately we don't have encoders on our robot to be able to do that.
You won't need encoders to implement a "rotate to angle" PID using navX-MXP.

Take a look at the "Rotate to Angle" examples. Online description of these is here.

You can find an example java project that implements this after you run the navX-MXP setup program. The sample for "Rotate to Angle" is installed at:

C:\Users\<your_username>\navx-mxp\java\examples\RotateToAngle

This example shows how to implement this, using Mecanum drive.

Some PID Tuning is required, so you'll want to read up on how to use PID (there are lots of links to good info here on ChiefDelphi). You should be able to implement something that works reasonably well by only using a P coefficient, though to get it to work well w/your specific drive system you may need to do additional tuning of the I and D coefficients.
Reply With Quote
  #5   Spotlight this post!  
Unread 12-02-2016, 19:46
team-4480's Avatar
team-4480 team-4480 is offline
Debug? What's that?
FRC #4480
 
Join Date: Jan 2015
Rookie Year: 2013
Location: Minnesooota
Posts: 218
team-4480 will become famous soon enoughteam-4480 will become famous soon enough
Re: Problem with NavX to turn around.

Quote:
Originally Posted by slibert View Post
Take a look at the "Rotate to Angle" examples. Online description of these is...
Oh I guess I just under the assumption we needed encoders to do PID. Cool that we don't!

So the online description is not the full code? If not, do you have a Github page or something because we use Python and don't have the necessary software to install nax-mxp example.
Reply With Quote
  #6   Spotlight this post!  
Unread 12-02-2016, 20:06
slibert slibert is offline
Software Mentor
AKA: Scott Libert
FRC #2465 (Kauaibots)
Team Role: Mentor
 
Join Date: Oct 2011
Rookie Year: 2005
Location: Kauai, Hawaii
Posts: 343
slibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud of
Re: Problem with NavX to turn around.

Quote:
Originally Posted by team-4480 View Post
Oh I guess I just under the assumption we needed encoders to do PID. Cool that we don't!

So the online description is not the full code? If not, do you have a Github page or something because we use Python and don't have the necessary software to install nax-mxp example.
You would need encoders if you wanted to have PID control of each individual wheel's speed. Note that this can be a useful thing to have (kind of like traction control on a car).

But for rotate-to-angle, your feedback that drives the PID controller is the Yaw angle from the navX-MXP.

Anyways, the RotateToAngle Java example code is available on GitHub at:

https://github.com/kauailabs/navxmxp...team2465/robot

My understanding is that the java and python are very similar, so this should be a good starting point for you. There are some comments in the example code that discuss tuning the PID a bit.

PID is a _very_ useful thing to learn and get used to for programming control systems like those used in FIRST and in the real world, so I encourage you to spend the time to learn it. Focus first on understanding how the P (proportional) coefficient works. Then, once you understand that and the concept of undershoot and overshoot well, begin working on the I (integral) coefficient. Finally, if you find this is not good enough for your needs, begin looking at the D (derivative) coefficient.

A final bit of advice when tuning PID coefficients is to only change one coefficient at a time. The different coefficients "mix" together in ways that aren't always intuitive, so go slowly and take careful notes as you tune them.
Reply With Quote
  #7   Spotlight this post!  
Unread 12-02-2016, 21:02
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,050
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Re: Problem with NavX to turn around.

Lucky you, the single NavX MXP example that I ported a few weeks ago was the rotate to angle sample. It even works if you run it in the simulator:

https://github.com/robotpy/robotpy-w...otate_to_angle
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
Reply With Quote
  #8   Spotlight this post!  
Unread 15-04-2016, 17:19
team-4480's Avatar
team-4480 team-4480 is offline
Debug? What's that?
FRC #4480
 
Join Date: Jan 2015
Rookie Year: 2013
Location: Minnesooota
Posts: 218
team-4480 will become famous soon enoughteam-4480 will become famous soon enough
Re: Problem with NavX to turn around.

Quote:
Originally Posted by virtuald View Post
Lucky you, the single NavX MXP example that I ported a few weeks ago was the rotate to angle sample. It even works if you run it in the simulator:

https://github.com/robotpy/robotpy-w...otate_to_angle
I am revisiting this topic and am having trouble using the example with arcadeDrive. What values, besides the joysticks, do I pass to arcadeDrive so it will turn if the button is pressed? I pass in the currentRotationRate into arcade Drive but in the simulator, the motors still don't do anything.

Thanks!
Reply With Quote
  #9   Spotlight this post!  
Unread 15-04-2016, 18:32
jreneew2's Avatar
jreneew2 jreneew2 is offline
Alumni of Team 2053 Tigertronics
AKA: Drew Williams
FRC #2053 (TigerTronics)
Team Role: Programmer
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Vestal, NY
Posts: 195
jreneew2 has a spectacular aura aboutjreneew2 has a spectacular aura aboutjreneew2 has a spectacular aura about
Re: Problem with NavX to turn around.

We have found that a simple piece wise function is accurate enough to use for rotating to angles. We have a tolerance of two degrees. It works very well. We call this little set of functions Tigerdrive.

Here is a link: it is probably more simple to implement. https://github.com/team2053tigertron...src/TigerDrive
Reply With Quote
  #10   Spotlight this post!  
Unread 15-04-2016, 18:50
team-4480's Avatar
team-4480 team-4480 is offline
Debug? What's that?
FRC #4480
 
Join Date: Jan 2015
Rookie Year: 2013
Location: Minnesooota
Posts: 218
team-4480 will become famous soon enoughteam-4480 will become famous soon enough
Re: Problem with NavX to turn around.

Quote:
Originally Posted by jreneew2 View Post
We have found that a simple piece wise function is accurate enough to use for rotating to angles. We have a tolerance of two degrees. It works very well. We call this little set of functions Tigerdrive.

Here is a link: it is probably more simple to implement. https://github.com/team2053tigertron...src/TigerDrive
Nice! I would use this but I am looking for practice with PIDs since we haven't ever used them.

Thanks tho!
Reply With Quote
  #11   Spotlight this post!  
Unread 15-04-2016, 19:22
DanielHa's Avatar
DanielHa DanielHa is offline
Lead of Software
AKA: Daniel Hathcock
FRC #1619 (Up-A-Creek-Robotics)
Team Role: Programmer
 
Join Date: Oct 2014
Rookie Year: 2012
Location: Longmont, CO
Posts: 4
DanielHa is an unknown quantity at this point
Re: Problem with NavX to turn around.

We are using a well tuned PID loop on the angle returned by the navX in order to turn accurately. An issue that we ran into, which I think might explain what you described (your code working only 9/10 times), is that 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. We use the navX getFusedHeading() method as opposed to the getYaw() method, so I'm not sure how the values returned by these two methods differ. But essentially, since the navX returns values modulo 360 (for the getFusedHeading() method), if you want any value you calculate to be within the range 0-360, you can perform some mathematical modulo function on it. For instance, ((desired angle + 360) % 360) will map any angle greater than -360 to an angle between 0 and 360. On the other hand, (((desired angle + 540) % 360) - 180) will map any angle greater than -360 to an angle between -180 and 180 if you would prefer that. You can work out what would be best for you, but I hope this might help explain your issue.

In regards to PID, I would highly suggest, like most everyone else replying here, that you look into it. It is fairly simple and can drastically increase the accuracy, speed, and smoothness of your turning.

EDIT: I actually looked at the reference, and found that getYaw() returns values between -180 and 180. Notice that if it is greater than 0, you subtract 227. This would suggest that you could possibly be trying to turn to the angle -226, which the getYaw() method will never return. Instead you might want to take the angle and map it to -180 to 180 using a function like the second one I described. This way, -226 will be turned into +134, which is a value which will work with what the navX returns.

Last edited by DanielHa : 15-04-2016 at 19:29.
Reply With Quote
  #12   Spotlight this post!  
Unread 15-04-2016, 21:41
rich2202 rich2202 is offline
Registered User
FRC #2202 (BEAST Robotics)
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Wisconsin
Posts: 1,171
rich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond repute
Re: Problem with NavX to turn around.

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.
Reply With Quote
  #13   Spotlight this post!  
Unread 15-04-2016, 22:14
Jared Russell's Avatar
Jared Russell Jared Russell is offline
Taking a year (mostly) off
FRC #0254 (The Cheesy Poofs), FRC #0341 (Miss Daisy)
Team Role: Engineer
 
Join Date: Nov 2002
Rookie Year: 2001
Location: San Francisco, CA
Posts: 3,077
Jared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond repute
Re: Problem with NavX to turn around.

Quote:
Originally Posted by rich2202 View Post
In theory, PID is great. In practice, not so much.
I would argue that PID is much more elegant in practice than in theory
Reply With Quote
  #14   Spotlight this post!  
Unread 16-04-2016, 09:08
RSNovi RSNovi is offline
Registered User
FRC #6081
 
Join Date: Jan 2016
Location: Grass Lake
Posts: 12
RSNovi is an unknown quantity at this point
Re: Problem with NavX to turn around.

We use the Analog Devices supplied gyro without encoders with a PI controller for an algorithm to turn the robot to a set absolut degree. With a PI controller you can easily get within a half of a degree or better.
Reply With Quote
  #15   Spotlight this post!  
Unread 16-04-2016, 11:26
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,050
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Re: Problem with NavX to turn around.

I've added a navx_rotate_to_angle_arcade example to the set of samples. If you look at the diff, there's basically two changes that I made:
  • Changed the physics implementation to call the appropriate functions for a 4 wheel drivebase instead of mecanum (2 small changes)
  • instead of calling mecanumDrive_Cartesian it calls arcadeDrive and passes the rotation rate as the second argument
You'll note that arcade drive does not accept the same arguments as mecanum drive -- in particular, it does not accept extra 'rotation' or 'gyroangle' arguments.

Hope that helps!
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
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 05:00.

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