Announcement

Collapse
No announcement yet.

Need help with calling robot hardware class

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

  • Need help with calling robot hardware class

    In my team's autonomous code, we are receiving an error whenever we try to call our robot hardware class. The program keeps on telling us our variables are set to private. Here is the code if any one wants to take a look:
    package org.firstinspires.ftc.teamcode; import com.qualcomm.robotcore.hardware.DcMotor; import com.qualcomm.robotcore.hardware.HardwareMap; import com.qualcomm.robotcore.util.ElapsedTime; public class Robot { /* Public OpMode members. */ private DcMotor left_drive = null; // left drive front private DcMotor right_drive = null; // left drive back private DcMotor right_drive1 = null; // right drive front private DcMotor left_drive1 = null;// right drive back private DcMotor climber; private DcMotor arm1; // base motor private DcMotor arm2; // top motor /* local OpMode members. */ HardwareMap hwMap = null; private ElapsedTime period = new ElapsedTime(); /* Constructor */ public Robot(){ } /* Initialize standard Hardware interfaces */ public void init(HardwareMap ahwMap) { // Save reference to Hardware map hwMap = ahwMap; // Define and Initialize Motors left_drive = hwMap.get(DcMotor.class, "l2"); right_drive = hwMap.get(DcMotor.class, "r1"); left_drive1 = hwMap.get(DcMotor.class, "l2"); right_drive1 = hwMap.get(DcMotor.class, "r2"); arm1 = hwMap.get(DcMotor.class, "a1"); arm2 = hwMap.get(DcMotor.class, "a2"); climber = hwMap.get(DcMotor.class, "c"); arm1.setDirection(DcMotor.Direction.REVERSE); arm2.setDirection(DcMotor.Direction.REVERSE); right_drive.setDirection(DcMotor.Direction.REVERSE );// Set to FORWARD if using AndyMark motors left_drive.setDirection(DcMotor.Direction.FORWARD) ; right_drive1.setDirection(DcMotor.Direction.REVERS E); left_drive1.setDirection(DcMotor.Direction.FORWARD ); climber.setDirection(DcMotor.Direction.FORWARD); // Set all motors to zero power left_drive.setPower(0); right_drive.setPower(0); left_drive1.setPower(0); right_drive1.setPower(0); // Set all motors to run without encoders. // May want to use RUN_USING_ENCODERS if encoders are installed. left_drive.setMode(DcMotor.RunMode.RUN_USING_ENCOD ER); right_drive.setMode(DcMotor.RunMode.RUN_USING_ENCO DER); left_drive1.setMode(DcMotor.RunMode.RUN_USING_ENCO DER); right_drive1.setMode(DcMotor.RunMode.RUN_USING_ENC ODER); arm1.setMode(DcMotor.RunMode.RUN_USING_ENCODER); arm2.setMode(DcMotor.RunMode.RUN_USING_ENCODER); // Define and initialize ALL installed servos. // leftClaw = hwMap.get(Servo.class, "left_hand"); // rightClaw = hwMap.get(Servo.class, "right_hand"); // leftClaw.setPosition(MID_SERVO); // rightClaw.setPosition(MID_SERVO); } }

  • #2
    If there's not a nuance that I'm not seeing -- should be as easy as removing the "private" from in front of your variable declarations ie. "private DcMotor left_drive = null; // left drive front" becomes "DcMotor left_drive = null; // left drive front" -- then you should be able to access them. If it's something more than that - send more details.
    Team Phoenix Force - FTC 10100
    www.FTCRobotics.com

    Comment


    • #3
      If your actual OpMode class is in the same package as your "Robot" class, then just removing "private" from your variable declarations will work--they will then have the default access of "package-private". If your OpMode class is in a different package from "Robot", then you'll need to declare those variables "public".

      It can be really useful to declare public instance methods in "Robot" that provide the higher level robot behaviors you need. Examples would include

      A single method that sets the powers of all drive motors to the specified levels e.g. setDrivePowers(pLeft, pRight);
      A method that causes the robot to drive straight at a specified speed;
      A method that causes the robot to turn in place at a specified speed;

      All kinds of things are possible, depending on the configuration of your robot. In our competition OpModes, we rely completely on these higher-level methods and never access the motors individually from the OpMode class. We still keep the motor variables public so we can test them individually (but it would be better practice to use "getter" methods and keep the variables private).

      It's hard to read your code because of the formatting, but it looks like you are using the same string "l2", to get two different motors (left_drive and left_drive1).

      Comment


      • #4
        Thank you! That fixed the compiling issue.

        Comment

        Working...
        X