|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
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. |
|
#2
|
||||
|
||||
|
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? |
|
#3
|
||||
|
||||
|
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 |
|
#4
|
|||
|
|||
|
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.
|
|
#5
|
||||
|
||||
|
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 |
|
#6
|
||||
|
||||
|
Re: Encoders with Java
That sample code still works. Try playing with the PWM values. If it still doesnt work, contact your electrical team
|
|
#7
|
||||
|
||||
|
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? |
|
#8
|
||||
|
||||
|
Re: Encoders with Java
Quote:
|
|
#9
|
||||
|
||||
|
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).
|
|
#10
|
||||
|
||||
|
Re: Encoders with Java
Could you post your code? I dont think your doing anything wrong...
|
|
#11
|
|||
|
|||
|
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. |
|
#12
|
||||
|
||||
|
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. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|