Go to Post I think they get nutrition from chain greese. - Lucid [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 Rate Thread Display Modes
  #1   Spotlight this post!  
Unread Today, 00:27
versole's Avatar
versole versole is offline
Registered User
FRC #6593
Team Role: Programmer
 
Join Date: Feb 2017
Rookie Year: 2017
Location: New York
Posts: 1
versole is an unknown quantity at this point
Can I use gyro to drive straight in teleop period?

I wanted to know if you can drive straight using the gyro sensor because we don't have encoder to correct the speed of each motor. We're using tankdrive and right now the problem we're facing is that it's not going in the direction we wanted. For instance, if we move left and right stick to up direction the robot doesn't go straight, and for some reason, I think the speed of left side of the robot is going faster then the right side.

Also, I wanted to know if we can use it in autonomous mode. Like I need help with turning the robot with gyro (corresponding the wheel with the gyro angle.)

This is our first year so we're rookie, and their no programming mentor to mentor us.
Reply With Quote
  #2   Spotlight this post!  
Unread Today, 02:01
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 682
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: Can I use gyro to drive straight in teleop period?

It would be very helpful with encoders but it is possible to do it without. Consider the differential of the two tank sticks is directly proportional to the turning rate of the robot. The bigger the difference, the faster the robot should turn. And the turning rate of the robot is the raw output of the gyro (rotational velocity). So in theory, you can do this:
Code:
double drivePower = (leftStick + rightStick)/2.0;
double stickDifferential = leftStick - rightStick;
double motorDifferential = Kp*(stickDifferential - gyro.getScaledRotationRate());
leftMotor.set(drivePower + motorDifferential);
rightMotor.set(drivePower - motorDifferential);
Note: this is pseudocode, not real code. It is intending to demonstrate the concept. If stickDifferential is zero, that means you want to go straight. If the gyro rotation rate is zero, that means your robot is going straight. Then motorDifferential is zero. If stickDifferential is zero but the robot is turning left, then gyro rotation rate is negative, then motorDifferential will be a positive number. Then the left motor will get more power than the right thus correcting the left drift. Does that make sense?
__________________

Last edited by mikets : Today at 02:54.
Reply With Quote
  #3   Spotlight this post!  
Unread Today, 02:06
soundfx's Avatar
soundfx soundfx is offline
Registered User
AKA: Aaron J
FRC #3238 (Cyborg Ferrets)
Team Role: Programmer
 
Join Date: Jan 2016
Rookie Year: 2014
Location: Anacortes, WA
Posts: 24
soundfx is an unknown quantity at this point
Re: Can I use gyro to drive straight in teleop period?

Quote:
Originally Posted by versole View Post
I wanted to know if you can drive straight using the gyro sensor because we don't have encoder to correct the speed of each motor. We're using tankdrive and right now the problem we're facing is that it's not going in the direction we wanted. For instance, if we move left and right stick to up direction the robot doesn't go straight, and for some reason, I think the speed of left side of the robot is going faster then the right side.

Also, I wanted to know if we can use it in autonomous mode. Like I need help with turning the robot with gyro (corresponding the wheel with the gyro angle.)

This is our first year so we're rookie, and their no programming mentor to mentor us.
Our team reuses this class as a basic implementation of a pi controller attempting to keep rotation to a minimum. We only usually only use it for holonomic drive trains, but it could easily be used for a tank drive. We only use a pi controller here, but you could easily modify this for a more accurate pid controller, or use the wpilib class.

EDIT:
I didn't internalize that you were using tank drive. You could still try to use this, but it might be a little harder to adapt. I suppose if you run this method only when both joystick values are within some value of each other, it might work.
__________________


"I got 99 problems but 0.999 ain't 1."
~Orteil

Last edited by soundfx : Today at 02:09. Reason: Didn't internalize whole post, needed to update.
Reply With Quote
  #4   Spotlight this post!  
Unread Today, 03:03
AustinSchuh AustinSchuh is offline
Registered User
FRC #0971 (Spartan Robotics) #254 (The Cheesy Poofs)
Team Role: Engineer
 
Join Date: Feb 2005
Rookie Year: 1999
Location: Los Altos, CA
Posts: 805
AustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond repute
Re: Can I use gyro to drive straight in teleop period?

Quote:
Originally Posted by soundfx View Post
Our team reuses this class as a basic implementation of a pi controller attempting to keep rotation to a minimum. We only usually only use it for holonomic drive trains, but it could easily be used for a tank drive. We only use a pi controller here, but you could easily modify this for a more accurate pid controller, or use the wpilib class.
I'd suggest here a reasonably stiff P controller. That'll get the OP most of the gain for fewer corner cases. We use essentially P gyro compensation.
Reply With Quote
  #5   Spotlight this post!  
Unread Today, 03:42
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 682
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: Can I use gyro to drive straight in teleop period?

Here is the code from our FTC library.
Code:
    /**
     * This method implements tank drive where leftPower controls the left motors and right power controls the right
     * motors.
     *
     * @param leftPower specifies left power value.
     * @param rightPower specifies right power value.
     * @param inverted specifies true to invert control (i.e. robot front becomes robot back).
     */
    public void tankDrive(double leftPower, double rightPower, boolean inverted)
    {
        leftPower = TrcUtil.clipRange(leftPower);
        rightPower = TrcUtil.clipRange(rightPower);

        if (inverted)
        {
            double swap = leftPower;
            leftPower = -rightPower;
            rightPower = -swap;
        }

        if (gyro != null)
        {
            //
            // Gyro assist is enabled.
            //
            double drivePower = (leftPower + rightPower)/2.0;
            double diffPower = (leftPower - rightPower)/2.0;
            double turnPower = gyroAssistKp*(diffPower - gyroRateScale*gyro.getZRotationRate().value);
            leftPower = drivePower + turnPower;
            rightPower = drivePower - turnPower;
            double maxMag = Math.max(Math.abs(leftPower), Math.abs(rightPower));
            if (maxMag > 1.0)
            {
                leftPower /= maxMag;
                rightPower /= maxMag;
            }
        }

        if (frontLeftMotor != null)
        {
            frontLeftMotor.setPower(leftPower);
        }

        if (frontRightMotor != null)
        {
            frontRightMotor.setPower(rightPower);
        }

        if (rearLeftMotor != null)
        {
            rearLeftMotor.setPower(leftPower);
        }

        if (rearRightMotor != null)
        {
            rearRightMotor.setPower(rightPower);
        }
    }   //tankDrive
__________________
Reply With Quote
  #6   Spotlight this post!  
Unread Today, 05:53
GeeTwo's Avatar
GeeTwo GeeTwo is online now
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,785
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: Can I use gyro to drive straight in teleop period?

Quote:
Originally Posted by versole View Post
...For instance, if we move left and right stick to up direction the robot doesn't go straight, and for some reason, I think the speed of left side of the robot is going faster then the right side...
I would look at this problem first. If your robot is pulling badly enough that the driver is having trouble going straight, something is amiss. Check out this recent thread for things to try.
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.
Reply With Quote
  #7   Spotlight this post!  
Unread Today, 16:03
Lesafian Lesafian is offline
Registered User
AKA: Jeremy Styma
FRC #6077 (Wiking Kujon)
Team Role: Programmer
 
Join Date: Feb 2016
Rookie Year: 2016
Location: Posen, Michigan
Posts: 41
Lesafian is an unknown quantity at this point
Re: Can I use gyro to drive straight in teleop period?

I have written working code for driving straight with the gyro in teleop. This works by holding a button and you can drive straight in the direction that you are facing when the button is pressed.

Code:
	@Override
	public void operatorControl() {
		drive.setSafetyEnabled(true);
		gyro.reset();
		drive.setMaxOutput(0.75);
		int reset = 0;

		while (isOperatorControl() && isEnabled()) {
			angle = gyro.getAngle() * kP;

			/*
			 * If the button is pressed (and held), drive straight based on the
			 * angle the robot was facing when the button was initially pressed.
			 */
			if (logitech.getRawButton(6)) {
				if (reset < 1) {
					gyro.reset();
					reset++;
					System.out.println("Success! Gyro was reset.");
				}
				if (Math.abs(logitech.getRawAxis(1)) > 0.15) {
					drive.mecanumDrive_Cartesian(logitech.getRawAxis(1), 0, angle, 0);
				} else if (Math.abs(logitech.getRawAxis(0)) > 0.15) {
					drive.mecanumDrive_Cartesian(0, logitech.getRawAxis(0), angle, 0);
				}

				/*
				 * If the button is not pressed, drive normally.
				 */
			} else {
				reset = 0;
				if (Math.abs(logitech.getRawAxis(1)) > 0.15 || Math.abs(logitech.getRawAxis(0)) > 0.15
						|| Math.abs(logitech.getRawAxis(2)) > 0.15) {
					drive.mecanumDrive_Cartesian(logitech.getRawAxis(1), logitech.getRawAxis(0),
							-logitech.getRawAxis(2) * 0.5, 0);
				}
			}
This is using mecanum drive, but you could edit it to work with your drive train.
Reply With Quote
  #8   Spotlight this post!  
Unread Today, 16:25
Donut Donut is offline
The Arizona Mentor
AKA: Andrew
FRC #2662 (RoboKrew)
Team Role: Engineer
 
Join Date: Mar 2005
Rookie Year: 2004
Location: Goodyear, AZ
Posts: 1,315
Donut has a reputation beyond reputeDonut has a reputation beyond reputeDonut has a reputation beyond reputeDonut has a reputation beyond reputeDonut has a reputation beyond reputeDonut has a reputation beyond reputeDonut has a reputation beyond reputeDonut has a reputation beyond reputeDonut has a reputation beyond reputeDonut has a reputation beyond reputeDonut has a reputation beyond repute
Re: Can I use gyro to drive straight in teleop period?

Another thing I would recommend for teleop driving straight is to put a deadband on your joystick turning amount so that you automatically engage gyro code to attempt to drive straight when your inputs are within a certain range of straight ahead. On a single joystick this might mean assuming straight whenever the y axis is within +-0.2 of zero, on a dual joystick setup it would be when left right differential from mikets' example is within +-0.2 of zero. This helps to avoid drift if you have joysticks that don't perfectly center or if your driver doesn't consistently push straight ahead on the stick when trying to drive straight. That last point is especially pertinent for a rookie since you will have inexperienced drivers and they tend to do this more under match pressure in my experience.

I like to always engage this code when driving and have a button or switch to disable when something breaks. If you have to remember to push a button to engage it many will forget to use the assist button consistently.
__________________
FRC Team 498 (Peoria, AZ), Student: 2004 - 2007
FRC Team 498 (Peoria, AZ), Mentor: 2008 - 2011
FRC Team 167 (Iowa City, IA), Mentor: 2012 - 2014
FRC Team 2662 (Tolleson, AZ), Mentor: 2014 - Present
Reply With Quote
  #9   Spotlight this post!  
Unread Today, 16:54
DonRotolo's Avatar
DonRotolo DonRotolo is offline
OMGweek5whyarentwedoneyet OMGgetgoin
FRC #0832
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Atlanta GA
Posts: 7,035
DonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond repute
Re: Can I use gyro to drive straight in teleop period?

Quote:
Originally Posted by versole View Post
the robot doesn't go straight

This is our first year so we're rookie.
These are the two very relevant points.

1. If the robot doesn't go straight, there is something wrong with the mechanical part of the robot. You must fix that. A gyro or encoder or any programming solution will only reduce the robots performance.

2. As rookies, with no programming mentor, stay away from the complex and advanced solution and go towards the simple. Not to discourage you, but get the mechanicals right first.

It is not unusual for rookies to make this mistake. Experienced teams know this already.
__________________

I am N2IRZ - What's your callsign?
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 23:34.

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