Advertisement
jmcdonne

Untitled

Dec 6th, 2016
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.60 KB | None | 0 0
  1. /* Copyright (c) 2015 Qualcomm Technologies Inc
  2.  
  3. All rights reserved.
  4.  
  5. Redistribution and use in source and binary forms, with or without modification,
  6. are permitted (subject to the limitations in the disclaimer below) provided that
  7. the following conditions are met:
  8.  
  9. Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11.  
  12. Redistributions in binary form must reproduce the above copyright notice, this
  13. list of conditions and the following disclaimer in the documentation and/or
  14. other materials provided with the distribution.
  15.  
  16. Neither the name of Qualcomm Technologies Inc nor the names of its contributors
  17. may be used to endorse or promote products derived from this software without
  18. specific prior written permission.
  19.  
  20. NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
  21. LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  25. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  31.  
  32. package org.firstinspires.ftc.teamcode;
  33.  
  34. import android.app.Activity;
  35. import android.graphics.Color;
  36. import android.view.View;
  37.  
  38. import com.qualcomm.ftcrobotcontroller.R;
  39. import com.qualcomm.hardware.modernrobotics.ModernRoboticsI2cGyro;
  40. import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
  41. import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
  42. import com.qualcomm.hardware.modernrobotics.ModernRoboticsI2cColorSensor;
  43. import com.qualcomm.robotcore.util.ElapsedTime;
  44.  
  45. /*
  46.  *
  47.  * This is an example LinearOpMode that tests the update speed of the gyro sensor
  48.  * when a color sensor is enabled for callbacks and disabled.
  49.  *
  50.  * The op mode assumes that the color sensor
  51.  * is configured with a name of "color" and the gyro with a name of "gyro".
  52.  *
  53.  * You can use the X button on gamepad1 to toggle the color's sensor's LED state.
  54.  * Turning the LED off disables the color sensor's callback.
  55.  *
  56.  */
  57. @TeleOp(name = "Gyro Heading with Color Sensor", group = "Sensor")
  58. public class Team5873_SensorMRColorEnableDisable extends LinearOpMode {
  59.  
  60.  
  61.   public void runOpMode() {
  62.  
  63.     ElapsedTime timer = new ElapsedTime();
  64.     timer.reset();
  65.  
  66.     // The time since the color or gyro sensor was last updated
  67.     double dPrevColorMillis = 0;
  68.     double dCurrColorMillis = 0;
  69.     double dPrevGyroMillis = 0;
  70.     double dCurrGyroMillis = 0;
  71.  
  72.     // The previous and current values of the color sensor (red only) and gyro heading
  73.     double dPrevRed = 0.0;
  74.     double dCurrRed = 0.0;
  75.     int iPrevAngleZ = 0;
  76.     int iCurrAngleZ = 0;
  77.  
  78.     // The previous and current state of gamepad1's X button.
  79.     boolean bPrevState = false;
  80.     boolean bCurrState = false;
  81.  
  82.     // The state of the LED on the color sensor.
  83.     boolean bLedOn = true;
  84.  
  85.     // get a reference to our ColorSensor object.
  86.     ModernRoboticsI2cColorSensor colorSensor = (ModernRoboticsI2cColorSensor)this.hardwareMap.get("color");
  87.     colorSensor.resetDeviceConfigurationForOpMode();
  88.  
  89.     // Set the LED to ON / true in the beginning
  90.     colorSensor.enableLed(bLedOn);
  91.  
  92.     ModernRoboticsI2cGyro gyro;   // Hardware Device Object
  93.  
  94.     // get a reference to a Modern Robotics GyroSensor object.
  95.     gyro = (ModernRoboticsI2cGyro)hardwareMap.gyroSensor.get("gyro");
  96.  
  97.     // start calibrating the gyro.
  98.     telemetry.addData(">", "Gyro Calibrating. Do Not move!");
  99.     telemetry.update();
  100.     gyro.calibrate();
  101.  
  102.     // make sure the gyro is calibrated.
  103.     while (!isStopRequested() && gyro.isCalibrating())  {
  104.       sleep(50);
  105.       idle();
  106.     }
  107.  
  108.     telemetry.addData(">", "Gyro Calibrated.  Press Start.");
  109.     telemetry.update();
  110.  
  111.     waitForStart();
  112.  
  113.     // while the op mode is active, loop and read the RGB data.
  114.     // Note we use opModeIsActive() as our loop condition because it is an interruptible method.
  115.     while (opModeIsActive()) {
  116.  
  117.       bCurrState = gamepad1.x;
  118.       dCurrColorMillis = timer.milliseconds();
  119.       dCurrGyroMillis = dCurrColorMillis;
  120.       dCurrRed = colorSensor.red() * 1.0;
  121.       iCurrAngleZ  = gyro.getIntegratedZValue();
  122.  
  123.       // check for button state transitions.
  124.       if ((bCurrState) && (bCurrState != bPrevState))  {
  125.  
  126.         // button is transitioning its state. So toggle the bLedOn
  127.         bLedOn = !bLedOn;
  128.         // Enable or disable the color sensor and LED at the same time
  129.         if (bLedOn) {
  130.           colorSensor.getI2cController().registerForI2cPortReadyCallback(colorSensor, colorSensor.getPort());
  131.           colorSensor.enableLed(bLedOn);
  132.         }
  133.         else {
  134.           colorSensor.enableLed(bLedOn);
  135.           colorSensor.getI2cController().deregisterForPortReadyCallback(colorSensor.getPort());
  136.         }
  137.       }
  138.  
  139.       // update previous state variable.
  140.       bPrevState = bCurrState;
  141.  
  142.       // send the info back to driver station using telemetry function.
  143.       telemetry.addData("LED/Color Callback", bLedOn ? "On" : "Off");
  144.       telemetry.addData("Clear", colorSensor.alpha());
  145.       telemetry.addData("R/G/B", (Double.toString(dCurrRed) + "/" + Double.toString(colorSensor.green()) + "/" + Double.toString(colorSensor.blue())));
  146.  
  147.       // If the red value changes, show the milliseconds since last update
  148.       // Write a line of telemetry in any case so the values don't jump around the screen
  149.       if (dPrevRed == dCurrRed) {
  150.         telemetry.addData("No Color Update", 0);
  151.       }
  152.       else {
  153.         telemetry.addData("Color Update Time", dCurrColorMillis - dPrevColorMillis);
  154.         dPrevColorMillis = dCurrColorMillis;
  155.         dPrevRed = dCurrRed;
  156.       }
  157.  
  158.       // If the heading changes, show the milliseconds since last update
  159.       telemetry.addData("1", "Int. Ang. %03d", iCurrAngleZ);
  160.       if (iPrevAngleZ == iCurrAngleZ) {
  161.         telemetry.addData("No Heading Update", 0);
  162.       }
  163.       else {
  164.         telemetry.addData("Heading Update Time", dCurrGyroMillis - dPrevGyroMillis);
  165.         dPrevGyroMillis = dCurrGyroMillis;
  166.         iPrevAngleZ = iCurrAngleZ;
  167.       }
  168.  
  169.       telemetry.update();
  170.     }
  171.   }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement