Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Encoders with Java (http://www.chiefdelphi.com/forums/showthread.php?t=112993)

Clint3176 07-02-2013 20:00

Encoders with Java
 
Hello,
My team is using encoders (E4P-360-250-D-H-D-B) for the very first time and we are writing in Java. We really don't know where to even begin. We do believe that the encoders are wired correctly. Sample code would be very much appreciated.

Arhowk 07-02-2013 21:07

Re: Encoders with Java
 
Encoder encoder = new Encoder(portA, portB); //ask your electrical team for the pots
encoder.start();
encoder.reset();
System.out.println(encoder.get());

Where are your encoders setup? on the drive train motors?

Clint3176 08-02-2013 18:34

Re: Encoders with Java
 
The encoders are placed on the drive train (wheel axle) and are wired to the digital sidecar. We tried the code you posted and when we run the code we only receive a constant number (most of the time its 0 but sometimes it will gives us numbers such as -2, -32, 1)

P.S
Thank you very much for your quick response and help

The_Colonel 11-02-2013 15:44

Re: Encoders with Java
 
Use the function setDistancePerPulse() and set it to 360 (this is based on the model you are using). That should help.

Clint3176 11-02-2013 17:57

Re: Encoders with Java
 
We tried that but it seems not matter what we do we only get a value of zero for everything. Could you re-post some working sample code?

Thanks again though

Arhowk 11-02-2013 20:58

Re: Encoders with Java
 
Quote:

Originally Posted by Clint3176 (Post 1231812)
We tried that but it seems not matter what we do we only get a value of zero for everything. Could you re-post some working sample code?

Thanks again though

That sample code still works. Try playing with the PWM values. If it still doesnt work, contact your electrical team

Clint3176 13-02-2013 18:17

Re: Encoders with Java
 
We wired a new encoder to different PWM pins using the code you've (graciously) provided. We will get a single number over and over until we disable then re-enable which will then give us the updated value. Is there a way to update the
System.out.println(encoders.get());
function?
Could this problem be occurring for any other reason?

Arhowk 13-02-2013 19:03

Re: Encoders with Java
 
Quote:

Originally Posted by Clint3176 (Post 1232902)
We wired a new encoder to different PWM pins using the code you've (graciously) provided. We will get a single number over and over until we disable then re-enable which will then give us the updated value. Is there a way to update the
System.out.println(encoders.get());
function?
Could this problem be occurring for any other reason?

Are you moving the robot..? Is the C-RIO updated to 2013 along with the driver station?

Clint3176 13-02-2013 19:18

Re: Encoders with Java
 
Yes, everything is up to date and we are moving the robot (and the encoder disk is moving with the axle).

Arhowk 13-02-2013 19:47

Re: Encoders with Java
 
Quote:

Originally Posted by Clint3176 (Post 1232917)
Yes, everything is up to date and we are moving the robot (and the encoder disk is moving with the axle).

Could you post your code? I dont think your doing anything wrong...

Clint3176 14-02-2013 18:11

Re: Encoders with Java
 
package BasicMecanumDrive;


import edu.wpi.first.wpilibj.CANJaguar;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.can.CANTimeoutException;
import edu.wpi.first.wpilibj.*;


public class BasicMecanumDrive extends IterativeRobot {
CANJaguar frontLeft, frontRight, backLeft, backRight;

Joystick leftStick, rightStick;
RobotDrive robotDrive;

private final Encoder encoder = new Encoder(13,14);


public void robotInit() {

leftStick = new Joystick(1);
rightStick = new Joystick(2);



try {
frontLeft = new CANJaguar(4);
frontRight = new CANJaguar(1);
backLeft = new CANJaguar(3);
backRight = new CANJaguar(2);

encoder.start();
encoder.reset();

robotDrive = new RobotDrive(frontLeft, backLeft, frontRight, backRight);
} catch (CANTimeoutException ex) {
ex.printStackTrace();
}


}



public void autonomousPeriodic() {

}


public void teleopInit () {


}
public void teleopPeriodic() {
robotDrive.mecanumDrive_Cartesian(rightStick.getX( ), rightStick.getY(), -leftStick.getY(), 0);


if (encoder.getRaw()> 20000) {
System.out.println("Gots It");
}
System.out.println(encoder.get());

DriverStationLCD.getInstance().println(DriverStati onLCD.Line.kUser2, 2, "Encoder"+ encoder.get());
DriverStationLCD.getInstance().updateLCD();



}

}



Here's our code currently. We are just running a few things to eliminate potential problems from other systems.

kidteddy 12-03-2013 02:05

Re: Encoders with Java
 
Hi,
I also started using encoders this year. After reading this thread and researching online in the past few weeks, I understood part of programming encoders. Now, I'm just wondering how to move the bot x feet in autonomous using encoders on drivetrain. What methods do I use if I want the bot to move forward, let's say, for 7 feet? If you provide me with sample code for this, it would be greatly appreciated. I'm writing in java commandbased, and drivetrain that consists of left and right motors where encoders mounted on each side.


All times are GMT -5. The time now is 11:20.

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