0

As a team, we decided to drive our robot with the help of encoders and a joystick. We want to drive our robot straight even though it is controlled by the joystick. We do not know how to achieve this as one motor is obviously slower. Also, we are displaying the encoder values on a screen but they seem to stop after an integer around 25, whereas they should increase as they show the count of rotations. Can you guys help us with this? We are using FRC library. library here

public class Robot extends IterativeRobot {
        Victor Motor0= new Victor(0);
        Victor Motor1= new Victor(1);
        DifferentialDrive drive= new DifferentialDrive(Motor0,Motor1);
        Joystick stick=new Joystick(0);
        double dist =0.5*3.14/1024;
        Encoder esit= new Encoder(0,1,true,Encoder.EncodingType.k4X);
        Encoder esit1 = new Encoder(2,3,true,Encoder.EncodingType.k4X);
        double m0 = esit.get();
        double m1 = esit1.get();
        double fark = m1 - m0;
        double gidilen_mesafe1 = esit.getDistance();
        double gidilen_mesafe2 = esit1.getDistance();


        public void robotInit() {
            esit.setDistancePerPulse(dist);
            esit1.setDistancePerPulse(dist);
            esit.reset();
            esit1.reset();
        }

        public void autonomousInit() {

        }

        public void autonomousPeriodic() {

        }

        public void teleopPeriodic() {
            drive.arcadeDrive(stick.getRawAxis(0), stick.getRawAxis(1));

            SmartDashboard.putNumber("Tekerlek1", esit.get());
            SmartDashboard.putNumber("Tekerlek2", esit1.get());
            SmartDashboard.putNumber("Aradaki Fark", fark);
            SmartDashboard.putNumber("Mesafe 1", gidilen_mesafe1);
            SmartDashboard.putNumber("Mesafe 2", gidilen_mesafe2);


            boolean m_stop = esit.getStopped();
            boolean m1_stop = esit1.getStopped();
            if(m_stop==false && m1_stop==false) {
                esit.reset();
                esit1.reset();
            }

        }
        public void testPeriodic() {
        }
    }
  • "We do not know how to achieve this as one motor is obviously slower." - why is one motor slower? – Jaroslaw Pawlak Jan 11 '19 at 17:39
  • to be honest we are not sure but the current might not be divided equally but we think that the best way to fix it is by fixing the software. But, we only guess :) – X-Sharc Jan 11 '19 at 18:09
  • it can also be a problem related to the code but that is exactly why we need help :/ – X-Sharc Jan 11 '19 at 18:10

1 Answers1

1

I'm not sure completely sure what's happening in your program but it seems like you're only reading encoder values once -> this would explain why the value is unchanging. You'd probably want to be reading the encoder values in some sort of loop so your readings are constantly being updated.

With regard to moving straight, you can try using a PID (proportional, integral, derivative) controller where the difference in encoder values is the error. Based on the difference, your program can decide how sharply to turn (i.e. scale the motor speeds) to correct for the drift.

A good starting point would be this post: https://robotics.stackexchange.com/questions/1711/approach-to-using-pid-to-get-a-differential-robot-driving-straight

Justin Borromeo
  • 1,201
  • 3
  • 13
  • 26