Go to Post For every hour you spend screwing pieces of your robot together, 20 hours of important stuff goes on behind your back. - Eric Bareiss [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rating: Thread Rating: 53 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 13-11-2014, 21:36
772programming 772programming is offline
Registered User
FRC #0772
 
Join Date: Nov 2014
Location: Canada
Posts: 2
772programming is an unknown quantity at this point
PID function to reduce drift

Hello. So my team has been trying to write code to get the robot to drive in a straight line for a certain distance, which we managed to do using encoders and gyro input; however, once the encoders register that we have traveled the ideal distance and the motor shuts off, our robot drifts a considerable amount. We're trying to come up with a solution using PIDs, as shown below.
Any help with deciding on what the Kp, Ki and Kd values should be or with
any potential logic flaws we have would be appreciated. Also, if anyone has any better ideas than PIDs to reduce drifting, feel free to comment them. Ask for more code if you need to see anything else.Thanks!
Code:
//Initialization code for the gyrodrive command that is called to drive
the robot forward along a straight line until it reaches the desired dist.
protected void initialize() {
        log("Initialized GyroDriveCommand"); // prints to show that
        CommandBasedRobot.resetEncoders();   // resets encoders
        drivesub.enable();                   //
        gyroCalc();                          //
        
    }
//isFinished method for gyroDrive to try to stop without passing the desired
position
protected boolean isFinished() {
    
        return(drivesub.getPosition() - setpoint) < 1; // finishes when pos - setpoint < 1
            
        
    }

Declarations for the Kp, Ki and Kd values as well as the PIDInput and
PID output methods:
Code:
    private static final double Kp = 3;
    private static final double Ki = 0.2;
    private static final double Kd = 0.0;

protected double returnPIDInput() {
        return EncoderInfo.distanceL;//returns the current encoder distance
    }

    protected void usePIDOutput(double d) {
        System.out.println(d);
        start(d, 0);//gives the speed value to arcade drive
    }
public DriveTrainSubsystem(){
        super("DriveTrainSubsystem", Kp, Ki, Kd);
//OI code for calling the gyrodrivecommand
                                                               //dist, speed, setpoint
button1.whenPressed(new GyroDriveCommand(24, 0.70, 24));
    }
Reply With Quote
  #2   Spotlight this post!  
Unread 13-11-2014, 21:41
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,043
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: PID function to reduce drift

Quote:
Originally Posted by 772programming View Post
once the encoders register that we have traveled the ideal distance and the motor shuts off, our robot drifts a considerable amount
sounds like your motor controllers are in coast mode.


Reply With Quote
  #3   Spotlight this post!  
Unread 13-11-2014, 21:47
randantor randantor is offline
Registered User
AKA: James Y
FRC #0624 (CRyptonite)
Team Role: Alumni
 
Join Date: Jun 2013
Rookie Year: 2012
Location: Katy, TX
Posts: 48
randantor is a glorious beacon of lightrandantor is a glorious beacon of lightrandantor is a glorious beacon of lightrandantor is a glorious beacon of lightrandantor is a glorious beacon of lightrandantor is a glorious beacon of light
Re: PID function to reduce drift

When we use a PID for driving a certain distance, we check for the encoder speed as well as the distance to be below a certain threshold before stopping the PID.

This keeps it from drifting past the setpoint at the end because the command won't finish until the robot is no longer moving.
Reply With Quote
  #4   Spotlight this post!  
Unread 14-11-2014, 08:56
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,715
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
Re: PID function to reduce drift

Quote:
Originally Posted by Ether View Post
sounds like your motor controllers are in coast mode.


I agree with Ether, switching the motors to brake mode will help alleviate this issue. We usually have one motor controller on each side set to brake while the other is set to coast.
Reply With Quote
  #5   Spotlight this post!  
Unread 14-11-2014, 10:05
Brian Selle's Avatar
Brian Selle Brian Selle is offline
Mentor
FRC #3310 (Black Hawk Robotics)
Team Role: Engineer
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Texas
Posts: 162
Brian Selle has a spectacular aura aboutBrian Selle has a spectacular aura aboutBrian Selle has a spectacular aura about
Re: PID function to reduce drift

Click image for larger version

Name:	PID_varyingP.jpg
Views:	50
Size:	9.1 KB
ID:	17466

Right now your isFinished() criteria will turn off the PID loop as soon as the output gets close to the setpoint. To be able to stop precisely at the setpoint your robot will need to start slowing down before it gets there. Try letting the PID loop run for n cycles or delta time past your current criteria in the isFinished() method to let the dynamics settle. Then plot the encoder distance output vs time (SmartDashboard or print/Excel) so you can get a better idea of how well your Kp, Ki, Kd coefficients are set. If you have a lot of overshoot like the purple line in the attached graph the robot will continue to coast/skid past the setpoint no matter what you do. Play with the coefficients until you reach the optimal time/position for your system.
__________________
2015 Newton Semi-Finalist (3130, 2468, 3310, 537)
2015 Lubbock Regional Winner (2468, 3310, 4799)
2014 Galileo Quarter-Finalist (2052, 70, 3310, 3360)
2014 Colorado Regional Winner (1138, 3310, 2543)
2013 Texas Robot Roundup Winner (3310, 624, 2848)
2013 Archimedes Semi-Finalist (126, 3310, 1756)
2013 Dallas Regional Winner (148, 3310, 4610)
2012 Dallas West Regional Winner (935, 3310, 4206)
Reply With Quote
  #6   Spotlight this post!  
Unread 15-11-2014, 21:43
772programming 772programming is offline
Registered User
FRC #0772
 
Join Date: Nov 2014
Location: Canada
Posts: 2
772programming is an unknown quantity at this point
Re: PID function to reduce drift

Thanks for the suggestions. We`re not actually in coast mode, but it still drifts past the point that we want it to stop at. We're going to try testing out some new P, I and D values on Monday, so hopefully that clears things up.
Reply With Quote
  #7   Spotlight this post!  
Unread 16-11-2014, 13:27
Jared's Avatar
Jared Jared is offline
Registered User
no team
Team Role: Programmer
 
Join Date: Aug 2013
Rookie Year: 2012
Location: Connecticut
Posts: 602
Jared has a reputation beyond reputeJared has a reputation beyond reputeJared has a reputation beyond reputeJared has a reputation beyond reputeJared has a reputation beyond reputeJared has a reputation beyond reputeJared has a reputation beyond reputeJared has a reputation beyond reputeJared has a reputation beyond reputeJared has a reputation beyond reputeJared has a reputation beyond repute
Re: PID function to reduce drift

You should not shut off your pid loop when you reach your target. Instead, you should make sure that your error is near zero, and the rate at which your error is changing is near zero.

You should also increase the D gain of your PID controller to decrease the overshoot you're describing. The D gain compensates for the robot's tendency to keep moving when power applied to the motors is zero.
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 10:40.

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