Announcement

Collapse
No announcement yet.

Using on-board sensors from opmodes

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

  • Using on-board sensors from opmodes

    Is there a way to use the on-board gyroscope and magnetometer (compass) on the Moto G 2nd Gen from an OpMode in Android Studio? I know that you can use those sensors from a standard activity, but it would be a big help if we could use the onboard sensors instead of buying a MR Gyro.

  • #2
    Originally posted by MechaTech10617 View Post
    Is there a way to use the on-board gyroscope and magnetometer (compass) on the Moto G 2nd Gen from an OpMode in Android Studio? I know that you can use those sensors from a standard activity, but it would be a big help if we could use the onboard sensors instead of buying a MR Gyro.
    You can do this as if you were in an activity, since you actually are in an activity. To get an instance of an Android Context use hardwareMap.getContext(). To the best of my knowledge in this version the context *is* an activity so you can cast it if necessary.
    FTC6460 mentor (software+computer vision+electronics), FPGA enthusiast. In favor of allowing custom electronics on FTC bots.
    Co-founder of ##ftc live chat for FTC programming--currently you may need to join and wait some time for help--volunteer basis only.

    Comment


    • #3
      Our sample project contains some sample code for built-in phone sensors. You may find those useful.
      https://github.com/trc492/FtcSamples...idSensors.java

      Comment


      • #4
        Originally posted by hexafraction View Post
        You can do this as if you were in an activity, since you actually are in an activity. To get an instance of an Android Context use hardwareMap.getContext(). To the best of my knowledge in this version the context *is* an activity so you can cast it if necessary.
        I don't think there is a hardwareMap.getContext() method. I can't find it in Javadoc but you can cast the hardwareMap.appContext public variable into an activity.

        Comment


        • #5
          Originally posted by mikets View Post
          I don't think there is a hardwareMap.getContext() method. I can't find it in Javadoc but you can cast the hardwareMap.appContext public variable into an activity.
          Sorry, I think I mentally switched something around accidentally. It is in fact a field as you describe.
          FTC6460 mentor (software+computer vision+electronics), FPGA enthusiast. In favor of allowing custom electronics on FTC bots.
          Co-founder of ##ftc live chat for FTC programming--currently you may need to join and wait some time for help--volunteer basis only.

          Comment


          • #6
            You can use hardwareMap.appContext as follows:
            mSensorManager = (SensorManager) hardwareMap.appContext.getSystemService(Context.SE NSOR_SERVICE);
            accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELE ROMETER);
            magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNET IC_FIELD);

            Code for a simple opMode to display robot controller orientation as telemetry is at: https://github.com/acharraggi/my_ftc.../OrientOp.java

            Comment

            Working...
            X