Announcement

Collapse
No announcement yet.

modifying tensorflow code for movement

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

  • modifying tensorflow code for movement

    We have used the Machine Learning tool to find our team marker. However, now we can't figure out where to put our code to move the robot based on finding the marker. Whenever we add a motor we get the following message:

    EMERGENCY STOP
    NullPointerException - attempt to invoke interface method 'void com.qualcomm.robotcore.hardware.DcMotor.setPower(d ouble)" on a null object reference

    We are stumped! Any help would be appreciated!

    Here is our code:

    package org.firstinspires.ftc.teamcode;

    import com.qualcomm.robotcore.eventloop.opmode.Disabled;
    import com.qualcomm.robotcore.hardware.DcMotor;
    import com.qualcomm.robotcore.hardware.Servo;
    import com.qualcomm.robotcore.util.ElapsedTime;
    import com.qualcomm.robotcore.eventloop.opmode.Autonomous ;
    import com.qualcomm.robotcore.eventloop.opmode.LinearOpMo de;
    import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
    import java.util.List;
    import org.firstinspires.ftc.robotcore.external.ClassFact ory;
    import org.firstinspires.ftc.robotcore.external.hardware. camera.WebcamName;
    import org.firstinspires.ftc.robotcore.external.navigatio n.VuforiaLocalizer;
    import org.firstinspires.ftc.robotcore.external.tfod.TFOb jectDetector;
    import org.firstinspires.ftc.robotcore.external.tfod.Reco gnition;


    @Autonomous(name = "TeamMarker_Copy", group="Pushbot")
    public class TeamMarker_Copy extends LinearOpMode {

    HardwarePushbot robot = new HardwarePushbot(); // Use a Pushbot's hardware
    private ElapsedTime runtime = new ElapsedTime();


    private static final String TFOD_MODEL_ASSET = "/sdcard/FIRST/tflitemodels/TeamMarker.tflite";
    private static final String[] LABELS = {

    "8866 blackmarker"
    };

    private static final String VUFORIA_KEY =
    "ATBwnHz/////AAABmaYm4AIIrEqulUeWD4cZAW0CSF1au9oYF6vdACrE+L00+r WyciBZXCbClFlboAVmZF5M6+nzoTRlIrFnB0Gktjunha9CshUq KxjwHUh8T1E46W89i+4T3koHHCQ5qPMU0cbjVFtb3q/JL/1k2IWvZsDMwfJ3+CPrIv9g7pQeNC4X3mvG1KH7WjKHmLyufb4c JobkBZm2IySlrbneNKcrrOnMtmEjtC9RZpH7UO7EAt8QfS3MEo JXvcyY9zQprDcAlt7v+gjnnmbTOZ7HcLgtfoZaH9HTHEn5wjhw 1hpV7wi95N4emMEocPTjr9N+t5moqHMtHj6BnJfroITnrzt4NT 1uAShabrcmcljty3KySO76";

    private VuforiaLocalizer vuforia;

    private TFObjectDetector tfod;

    @Override
    public void runOpMode() {

    initVuforia();
    initTfod();

    if (tfod != null) {
    tfod.activate();
    tfod.setZoom(2.5, 16.0/9.0);
    }

    /** Wait for the game to begin */
    telemetry.addData(">", "Press Play to start op mode");
    telemetry.update();
    waitForStart();

    if (opModeIsActive()) {
    while (opModeIsActive()) {
    if (tfod != null) {
    // getUpdatedRecognitions() will return null if no new information is available since
    // the last time that call was made.
    List<Recognition> updatedRecognitions = tfod.getUpdatedRecognitions();
    if (updatedRecognitions != null)

    //if it sees the team marker spin the duck

    robot.rightduck.setPower(-.4);

    sleep(5000);


    robot.rightduck.setPower(0);
    {
    telemetry.addData("# Object Detected", updatedRecognitions.size());
    // step through the list of recognitions and display boundary info.
    int i = 0;
    //open the hand

    for (Recognition recognition : updatedRecognitions) {
    //open hand to drop the block

    telemetry.addData(String.format("label (%d)", i), recognition.getLabel());
    telemetry.addData(String.format(" left,top (%d)", i), "%.03f , %.03f",
    recognition.getLeft(), recognition.getTop());
    telemetry.addData(String.format(" right,bottom (%d)", i), "%.03f , %.03f",
    recognition.getRight(), recognition.getBottom());
    i++;

    //
    }
    telemetry.update();
    }
    }
    }
    }
    }

    /**
    * Initialize the Vuforia localization engine.
    */
    private void initVuforia() {
    /*
    * Configure Vuforia by creating a Parameter object, and passing it to the Vuforia engine.
    */
    VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters();

    parameters.vuforiaLicenseKey = VUFORIA_KEY;
    parameters.cameraName = hardwareMap.get(WebcamName.class, "Webcam 1");

    // Instantiate the Vuforia engine
    vuforia = ClassFactory.getInstance().createVuforia(parameter s);

    // Loading trackables is not necessary for the TensorFlow Object Detection engine.
    }

    /**
    * Initialize the TensorFlow Object Detection engine.
    */
    private void initTfod() {
    int tfodMonitorViewId = hardwareMap.appContext.getResources().getIdentifie r(
    "tfodMonitorViewId", "id", hardwareMap.appContext.getPackageName());
    TFObjectDetector.Parameters tfodParameters = new TFObjectDetector.Parameters(tfodMonitorViewId);
    tfodParameters.minResultConfidence = 0.8f;
    tfodParameters.isModelTensorFlow2 = true;
    tfodParameters.inputSize = 320;
    tfod = ClassFactory.getInstance().createTFObjectDetector( tfodParameters, vuforia);
    tfod.loadModelFromFile(TFOD_MODEL_ASSET, LABELS);
    }
    }

  • #2
    Can't tell for sure without seeing all of your code, but it looks as though robot.rightduck has not been instantiated.

    Comment


    • #3
      Thank You!! That fixed it (kind of). Now the motor spins twice instead of once. Here is the new code. We tried turning off the camera as soon as the motor moved but it doesn't seem to work.
      package org.firstinspires.ftc.teamcode;

      import com.qualcomm.robotcore.eventloop.opmode.Disabled;
      import com.qualcomm.robotcore.hardware.DcMotor;
      import com.qualcomm.robotcore.hardware.Servo;
      import com.qualcomm.robotcore.util.ElapsedTime;
      import com.qualcomm.robotcore.eventloop.opmode.Autonomous ;
      import com.qualcomm.robotcore.eventloop.opmode.LinearOpMo de;
      import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
      import java.util.List;
      import org.firstinspires.ftc.robotcore.external.ClassFact ory;
      import org.firstinspires.ftc.robotcore.external.hardware. camera.WebcamName;
      import org.firstinspires.ftc.robotcore.external.navigatio n.VuforiaLocalizer;
      import org.firstinspires.ftc.robotcore.external.tfod.TFOb jectDetector;
      import org.firstinspires.ftc.robotcore.external.tfod.Reco gnition;


      @Autonomous(name = "TeamMarker_Copy", group="Pushbot")
      public class TeamMarker_Copy extends LinearOpMode {

      HardwarePushbot robot = new HardwarePushbot(); // Use a Pushbot's hardware
      private ElapsedTime runtime = new ElapsedTime();


      private static final String TFOD_MODEL_ASSET = "/sdcard/FIRST/tflitemodels/TeamMarker.tflite";
      private static final String[] LABELS = {

      "8866 blackmarker"
      };


      private static final String VUFORIA_KEY =
      "ATBwnHz/////AAABmaYm4AIIrEqulUeWD4cZAW0CSF1au9oYF6vdACrE+L00+r WyciBZXCbClFlboAVmZF5M6+nzoTRlIrFnB0Gktjunha9CshUq KxjwHUh8T1E46W89i+4T3koHHCQ5qPMU0cbjVFtb3q/JL/1k2IWvZsDMwfJ3+CPrIv9g7pQeNC4X3mvG1KH7WjKHmLyufb4c JobkBZm2IySlrbneNKcrrOnMtmEjtC9RZpH7UO7EAt8QfS3MEo JXvcyY9zQprDcAlt7v+gjnnmbTOZ7HcLgtfoZaH9HTHEn5wjhw 1hpV7wi95N4emMEocPTjr9N+t5moqHMtHj6BnJfroITnrzt4NT 1uAShabrcmcljty3KySO76";

      private VuforiaLocalizer vuforia;

      private TFObjectDetector tfod;

      @Override
      public void runOpMode() {
      robot.init(hardwareMap);
      initVuforia();
      initTfod();

      if (tfod != null) {
      tfod.activate();
      tfod.setZoom(2.5, 16.0/9.0);
      }

      /** Wait for the game to begin */
      telemetry.addData(">", "Press Play to start op mode");
      telemetry.update();
      waitForStart();

      if (opModeIsActive()) {
      while (opModeIsActive()) {
      if (tfod != null) {
      // getUpdatedRecognitions() will return null if no new information is available since
      // the last time that call was made.
      List<Recognition> updatedRecognitions = tfod.getUpdatedRecognitions();
      if (updatedRecognitions != null){
      telemetry.addData("# Object Detected", updatedRecognitions.size());

      // step through the list of recognitions and display boundary info.
      int i = 0;

      for (Recognition recognition : updatedRecognitions) {

      //if the marker is visible spin a duck motor
      if (recognition.getLabel() == "8866 blackmarker")

      {
      robot.leftduck.setPower(1);
      sleep(500);
      robot.leftduck.setPower(0);

      sleep(1000);

      vuforia.close();
      tfod.deactivate();
      }



      i++;

      }
      telemetry.addData(">","No Marker");
      telemetry.update();

      }
      }
      }
      }
      }

      /**
      * Initialize the Vuforia localization engine.
      */
      private void initVuforia() {
      /*
      * Configure Vuforia by creating a Parameter object, and passing it to the Vuforia engine.
      */
      VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters();

      parameters.vuforiaLicenseKey = VUFORIA_KEY;
      parameters.cameraName = hardwareMap.get(WebcamName.class, "Webcam 1");

      // Instantiate the Vuforia engine
      vuforia = ClassFactory.getInstance().createVuforia(parameter s);

      // Loading trackables is not necessary for the TensorFlow Object Detection engine.
      }

      /**
      * Initialize the TensorFlow Object Detection engine.
      */
      private void initTfod() {
      int tfodMonitorViewId = hardwareMap.appContext.getResources().getIdentifie r(
      "tfodMonitorViewId", "id", hardwareMap.appContext.getPackageName());
      TFObjectDetector.Parameters tfodParameters = new TFObjectDetector.Parameters(tfodMonitorViewId);
      tfodParameters.minResultConfidence = 0.8f;
      tfodParameters.isModelTensorFlow2 = true;
      tfodParameters.inputSize = 320;
      tfod = ClassFactory.getInstance().createTFObjectDetector( tfodParameters, vuforia);
      tfod.loadModelFromFile(TFOD_MODEL_ASSET, LABELS);
      }
      }

      Comment

      Working...
      X