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:
I think maybe I installed the encoder incorrectly. Here are 2 pictures of the encoder.
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); } }
Comment