Go to Post FIRST is about family, making new friendships, belonging somewhere, doing something you love, and finding out what your real love is in life, so when you're old and wrinkly you can look back at high school and say "This is when I found myself." - Winged Wonder [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 16-01-2016, 14:35
curtis0gj curtis0gj is offline
Registered User
FRC #5033 (Beavertronics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Canada
Posts: 121
curtis0gj will become famous soon enough
Question Using Encoder to Drive a Certain Distance

Hi I am looking for some resources and places to get start to use one encoder to drive a certain distance during autonomous. I found this link http://wpilib.screenstepslive.com/s/...or-other-shaft. It has been slightly helpful with understanding how the encoder works and got me started. Here's what I have so far. Our team will be getting a second encoder soon, I hope I can still get this working with just the one encoder for now. Thanks!

Code:
public class Robot extends IterativeRobot {
	RobotDrive tankDrive = new RobotDrive(0, 1);
	Encoder encoder = new Encoder(0, 1, true, EncodingType.k4X);

	double desiredDistance = 1;

	public void robotInit() {

	}

	public void autonomousInit() {
		/**
		 * What I want is for the robot to drive one foot. I am certain that I
		 * am going to have to do some calculations. We are using 8" pneumatic
		 * wheels.
		 */
	        encoder.reset();
		encoder.setDistancePerPulse(5);
		encoder.getDistance();
	}

	public void autonomousPeriodic() {
		double encodersReading = encoder.get();
		tankDrive.arcadeDrive(0.25, 0);
		if (encodersReading > desiredDistance) {
			tankDrive.arcadeDrive(0, 0);
		}

	}

Last edited by curtis0gj : 16-01-2016 at 15:33.
Reply With Quote
  #2   Spotlight this post!  
Unread 16-01-2016, 15:43
techkid86's Avatar
techkid86 techkid86 is offline
Magic Programer
FRC #3044 (0xBE4)
Team Role: Alumni
 
Join Date: Jan 2011
Rookie Year: 2010
Location: ballston spa
Posts: 58
techkid86 is an unknown quantity at this point
Re: Using Encoder to Drive a Certain Distance

Remember to use the encoder reset, and start function when initializing (This was a mistake my team made when first getting into them). It looks as though you have all you need other than that though. Just use if statements to progress through your autonomous when target distances are met. best of luck

Also, be careful about what encoders you use. The standard andymark encoders are very... finicky. Make sure if you use them, you don't EVER have to disassemble the drive system. (don't take them off)
__________________
"you can build a perfect machine out of imperfect parts" -Urza

Last edited by techkid86 : 16-01-2016 at 15:46. Reason: Added Warning
Reply With Quote
  #3   Spotlight this post!  
Unread 17-01-2016, 15:44
curtis0gj curtis0gj is offline
Registered User
FRC #5033 (Beavertronics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Canada
Posts: 121
curtis0gj will become famous soon enough
Re: Using Encoder to Drive a Certain Distance

Quote:
Originally Posted by techkid86 View Post
Remember to use the encoder reset, and start function when initializing (This was a mistake my team made when first getting into them). It looks as though you have all you need other than that though. Just use if statements to progress through your autonomous when target distances are met. best of luck

Also, be careful about what encoders you use. The standard andymark encoders are very... finicky. Make sure if you use them, you don't EVER have to disassemble the drive system. (don't take them off)
Just curious about the set distance per pulse function. Is this function the ratio between how far each pulse will take the robot?

For example if I were to calculate the circumference of the wheel (in inches). Then divide the circumference of the wheel by the number of pulses in a full revolution (which is 1440 I think if I am using x4 encoding). Then set the distance per pulse to the circumference/number of pulses in one revolution would it make one full revolution move the robot one inch?

Sorry if this is not clear enough I can try to clarify if it makes zero sense.
Reply With Quote
  #4   Spotlight this post!  
Unread 17-01-2016, 19:30
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,567
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: Using Encoder to Drive a Certain Distance

Quote:
Originally Posted by curtis0gj View Post
Just curious about the set distance per pulse function. Is this function the ratio between how far each pulse will take the robot?

For example if I were to calculate the circumference of the wheel (in inches). Then divide the circumference of the wheel by the number of pulses in a full revolution (which is 1440 I think if I am using x4 encoding). Then set the distance per pulse to the circumference/number of pulses in one revolution would it make one full revolution move the robot one inch?

Sorry if this is not clear enough I can try to clarify if it makes zero sense.
That's the right process. However, wpilib normalizes pulses, so it won't change by decoding type. You should use 360 pulses in a full revolution, but it will increment in 1/4 increments in 4x mode. In addition, you'll want to take into account any gearing between the encoder and the wheel.

Code:
	//Encoder Distance Constants
    public static final double wheelDiameter = 6;
    public static final double pulsePerRevolution = 360;
    public static final double encoderGearRatio = 3;
    public static final double gearRatio = 64.0/20.0;
    public static final double Fudgefactor = 1.0;


final double distanceperpulse = Math.PI*wheelDiameter/pulsePerRevolution /
        		encoderGearRatio/gearRatio * Fudgefactor;
driveTrainEncoderL.setDistancePerPulse(distanceperpulse);
Reply With Quote
  #5   Spotlight this post!  
Unread 17-01-2016, 21:09
curtis0gj curtis0gj is offline
Registered User
FRC #5033 (Beavertronics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Canada
Posts: 121
curtis0gj will become famous soon enough
Re: Using Encoder to Drive a Certain Distance

Quote:
Originally Posted by Joe Ross View Post
You should use 360 pulses in a full revolution, but it will increment in 1/4 increments in 4x mode. In addition, you'll want to take into account any gearing between the encoder and the wheel.

Code:
    public static final double encoderGearRatio = 3;
Thanks so much this helped clarify things greatly. I just have two questions. First, I am trying to figure out if I should be using the k4x or k1x I am not sure I understand that 1/4 increments part you were speaking about. I was wondering if I needed to use k4x or k1x for this math. Second, I am trying to figure out what the encoderGearRatio double is for. Does it have to do with the shaft the encoder is mounted on. Thanks.
Reply With Quote
  #6   Spotlight this post!  
Unread 17-01-2016, 21:29
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,567
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: Using Encoder to Drive a Certain Distance

Quote:
Originally Posted by curtis0gj View Post
Thanks so much this helped clarify things greatly. I just have two questions. First, I am trying to figure out if I should be using the k4x or k1x I am not sure I understand that 1/4 increments part you were speaking about. I was wondering if I needed to use k4x or k1x for this math.
It does not matter which one you use. The scaling is the same. The only thing that changes is you get 4x the resolution when you choose k4x.

Quote:
Originally Posted by curtis0gj View Post
Second, I am trying to figure out what the encoderGearRatio double is for. Does it have to do with the shaft the encoder is mounted on. Thanks.
Yes. That year we used the VexPro Ball Shifters. There was a 3:1 ratio between the encoder and the shaft. http://www.vexrobotics.com/vexpro/mo.../217-1718.html
Reply With Quote
  #7   Spotlight this post!  
Unread 17-01-2016, 21:40
curtis0gj curtis0gj is offline
Registered User
FRC #5033 (Beavertronics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Canada
Posts: 121
curtis0gj will become famous soon enough
Re: Using Encoder to Drive a Certain Distance

Quote:
Originally Posted by Joe Ross View Post
Yes. That year we used the VexPro Ball Shifters. There was a 3:1 ratio between the encoder and the shaft. http://www.vexrobotics.com/vexpro/mo.../217-1718.html
Okay perfect just one final question . Our encoder is mounted onto the shaft from the KOP toughboxes. I don't think there is any gear ratio for the encoder however there is one for the motors (TB Mini Gearbox for AM14U, 10.71:1 Ratio ) so will I need to include a encoderGearRatio?
http://www.andymark.com/AM14U3-p/am-14u3.htm

Update:

Just tested the code and I the number I was getting from the encoder very off. I was trying to travel 36 inches but at the 36 inch mark the encoder is reading about 1.26.

This is the manual to the drive train we are using currently just to get things rolling. If you jump to page 4 it will tell you the gear ratio specifics. We are using the included 4 inch wheels.
http://files.andymark.com/AM14U_Asse...structions.pdf

I am having a hard time finding the exact model of encoder we are using. It is very similar to this: http://www.andymark.com/product-p/am-3132.htm It's just an optical encoder that we got in 2014.


Here is snippets of the code everything that I am doing with the encoder is included below.
Code:
 
public class Robot extends IterativeRobot {	
	RobotDrive tankDrive = new RobotDrive(0, 1);
	Encoder encoder = new Encoder(0, 1, true, EncodingType.k4X);

	public static final double WHEEL_DIAMETER = 4;
	public static final double PULSE_PER_REVOLUTION = 360;
	public static final double ENCODER_GEAR_RATIO = 0;
	public static final double GEAR_RATIO = 8.45 / 1;
	public static final double FUDGE_FACTOR = 1.0;

        public void autonomousInit() {	
                final double distancePerPulse = Math.PI * Defines.WHEEL_DIAMETER / Defines.PULSE_PER_REVOLUTION
				/ Defines.ENCODER_GEAR_RATIO / Defines.GEAR_RATIO * Defines.FUDGE_FACTOR;
	        encoder.setDistancePerPulse(distancePerPulse);

	}

	public void autonomousPeriodic() {
	        double encoderDistanceReading = encoder.getDistance();
		SmartDashboard.putNumber("encoder reading", encoderDistanceReading);
		
		tankDrive.drive(-0.25, 0);
		if (encoderDistanceReading > 36) {
			tankDrive.drive(0, 0);
		}
	}
}

Last edited by curtis0gj : 18-01-2016 at 10:47.
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 22:36.

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