Announcement

Collapse
No announcement yet.

Encoder's not working

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Encoder's not working

    Hi,

    This is our team's first time using encoders. I wrote a small program to test use of encoders. My intention is to calculate the distance and make the motor to move a certain distance. I failed both of them while the motor and the servo could move. We now using the Legacy Module. Because we never use encodes before, so I'm not sure whether the program or hardware was wrong. Could someone help me take a look?

    Thanks a lot!

    Here's my code:
    Code:
    package com.qualcomm.ftcrobotcontroller.opmodes;
    
    import com.qualcomm.robotcore.eventloop.opmode.OpMode;
    import com.qualcomm.robotcore.hardware.DcMotor;
    import com.qualcomm.robotcore.hardware.DcMotorController;
    import com.qualcomm.robotcore.hardware.Servo;
    import com.qualcomm.robotcore.util.Range;
    
    
    public class testOp extends OpMode {
    
    	DcMotor motor_1;
    	Servo servo_1;
    	DcMotorController control;
    	DcMotorController.DeviceMode devMode;
    
    	int numOpLoops = 1;
    
    	@Override
    	public void init() {
    		motor_1 = hardwareMap.dcMotor.get("motor1");//the motor for test
    		servo_1=hardwareMap.servo.get("servo1");//the servo for test
                    control=hardwareMap.dcMotorController.get("motor");//HiTechnic DC Motor Controller
    	}
    
    	@Override
    	public void init_loop() {
    
    		devMode = DcMotorController.DeviceMode.WRITE_ONLY;
    
    		motor_1.setChannelMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
    	}
    	@Override
    	public void loop() {
    
            if (allowedToWrite())
    		{
    
    			if (gamepad1.a) {
    				motor_1.setChannelMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
    				motor_1.setPower(gamepad1.left_stick_y*0.1);//low speed function
    			}
    			else {
    				motor_1.setChannelMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
    				motor_1.setPower(gamepad1.left_stick_y);
    			}
    
    			if (gamepad1.b) servo_1.setPosition(0.0);//check servo
    
    			if (gamepad1.y) servo_1.setPosition(0.5);//check servo
    
    			if (gamepad1.x) {
    				motor_1.setChannelMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
    				motor_1.setTargetPosition(1000);
    			}// I failed
    		}
    
    		if (numOpLoops % 17 == 0)
    			control.setMotorControllerDeviceMode(DcMotorController.DeviceMode.READ_ONLY);
    
    		if (control.getMotorControllerDeviceMode() == DcMotorController.DeviceMode.READ_ONLY) {
    			telemetry.addData("encoder", motor_1.getCurrentPosition());
    			control.setMotorControllerDeviceMode(DcMotorController.DeviceMode.WRITE_ONLY);
    			numOpLoops = 0;
    		}
    		devMode = control.getMotorControllerDeviceMode();
    		numOpLoops++;
    	}
    
    	private boolean allowedToWrite(){
    		return (devMode == DcMotorController.DeviceMode.WRITE_ONLY);
    	}
    
    }
    I think maybe I installed the encoder incorrectly. Here are 2 pictures of the encoder.

  • #2
    I think maybe I installed the encoder incorrectly. Here are 2 pictures of the encoder. Pic1 Pic2

    Comment


    • #3
      The connectors are keyed, so you shouldn't be able to connect anything backwards. I did notice that you're directly setting your power from the joystick input, and the joystick returns a negative value when pressed forward. Do you get any response at all when you run the program, including if you pull the joystick all the way back instead of forward?

      Comment


      • #4
        Originally posted by Jerry ZXR View Post
        Hi,

        This is our team's first time using encoders. I wrote a small program to test use of encoders. My intention is to calculate the distance and make the motor to move a certain distance. I failed both of them while the motor and the servo could move. We now using the Legacy Module. Because we never use encodes before, so I'm not sure whether the program or hardware was wrong. Could someone help me take a look?

        Thanks a lot!

        Here's my code:
        Code:
        package com.qualcomm.ftcrobotcontroller.opmodes;
        
        import com.qualcomm.robotcore.eventloop.opmode.OpMode;
        import com.qualcomm.robotcore.hardware.DcMotor;
        import com.qualcomm.robotcore.hardware.DcMotorController;
        import com.qualcomm.robotcore.hardware.Servo;
        import com.qualcomm.robotcore.util.Range;
        
        
        public class testOp extends OpMode {
        
        	DcMotor motor_1;
        	Servo servo_1;
        	DcMotorController control;
        	DcMotorController.DeviceMode devMode;
        
        	int numOpLoops = 1;
        
        	@Override
        	public void init() {
        		motor_1 = hardwareMap.dcMotor.get("motor1");//the motor for test
        		servo_1=hardwareMap.servo.get("servo1");//the servo for test
                        control=hardwareMap.dcMotorController.get("motor");//HiTechnic DC Motor Controller
        	}
        
        	@Override
        	public void init_loop() {
        
        		devMode = DcMotorController.DeviceMode.WRITE_ONLY;
        
        		motor_1.setChannelMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
        	}
        	@Override
        	public void loop() {
        
                if (allowedToWrite())
        		{
        
        			if (gamepad1.a) {
        				motor_1.setChannelMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
        				motor_1.setPower(gamepad1.left_stick_y*0.1);//low speed function
        			}
        			else {
        				motor_1.setChannelMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
        				motor_1.setPower(gamepad1.left_stick_y);
        			}
        
        			if (gamepad1.b) servo_1.setPosition(0.0);//check servo
        
        			if (gamepad1.y) servo_1.setPosition(0.5);//check servo
        
        			if (gamepad1.x) {
        				motor_1.setChannelMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
        				motor_1.setTargetPosition(1000);
        			}// I failed
        		}
        
        		if (numOpLoops % 17 == 0)
        			control.setMotorControllerDeviceMode(DcMotorController.DeviceMode.READ_ONLY);
        
        		if (control.getMotorControllerDeviceMode() == DcMotorController.DeviceMode.READ_ONLY) {
        			telemetry.addData("encoder", motor_1.getCurrentPosition());
        			control.setMotorControllerDeviceMode(DcMotorController.DeviceMode.WRITE_ONLY);
        			numOpLoops = 0;
        		}
        		devMode = control.getMotorControllerDeviceMode();
        		numOpLoops++;
        	}
        
        	private boolean allowedToWrite(){
        		return (devMode == DcMotorController.DeviceMode.WRITE_ONLY);
        	}
        
        }
        I think maybe I installed the encoder incorrectly. Here are 2 pictures of the encoder.

        You have to reset the encoders and wait one or more hardware cycles until they register as 0. There's more. Search "RUN_USING_ENCODERS" in forum for examples. Examples also in Pushbot opmodes.
        -David

        Comment


        • #5
          Originally posted by btipton View Post
          The connectors are keyed, so you shouldn't be able to connect anything backwards.
          Well, the ports on the motor controller are keyed, but that doesn't mean that all encoder cables are necessarily keyed. For example, the AndyMark NeveRest encoder cables are compatible but aren't keyed. If it doesn't work the first time, I would recommend reversing the cable and seeing if that fixes it. If you have, for example, a Tetrix encoder cable (keyed) to compare it to, that will make it a lot easier.

          Comment

          Working...
          X