*tap*

Sure thing, here it is, but first some notes:

*I don't remember where I got this code, I think it was from the ftc forum, but I modified it so that when you tilt the phone forward, the motors move forward, sideways the robot turns, ect... you can change it if you want, it's at the very bottom of the file. The base program just displays the values to the telemetry and.. *it mentions geomagnetic stuff in the comment, but I don't know anything about that. If you can explain it to me or know what it is, please let me know I'm curious.

package com.qualcomm.ftcrobotcontroller.opmodes;

import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager;

import com.qualcomm.robotcore.eventloop.opmode.OpMode; import com.qualcomm.robotcore.hardware.DcMotor;

import java.text.SimpleDateFormat; import java.util.Date;

/** * An op mode that uses the geomagnetic and accelerometer values to calculate device * orientation and return those values in telemetry. * It makes use of getRotationMatrix() and getOrientation(), but does not use * remapCoordinateSystem() which one might want. * see: http://developer.android.com/reference/android/hardware/SensorManager.html#remapCoordinateSystem(float[], int, int, float[]) */ public class PhoneAccelerOp extends OpMode implements SensorEventListener { private String startDate; private SensorManager mSensorManager; private Sensor accelerometer; private Sensor magnetometer;

// orientation values
private float azimuth = 0.0f;      // value in radians
private float pitch = 0.0f;        // value in radians
private float roll = 0.0f;         // value in radians

private float[] mGravity;       // latest sensor values
private float[] mGeomagnetic;   // latest sensor values


DcMotor leftDrive;
DcMotor rightDrive;


/*
* Constructor
*/
public PhoneAccelerOp() {

}

/*
* Code to run when the op mode is first enabled goes here
* @see com.qualcomm.robotcore.eventloop.opmode.OpMode#init()
*/
@Override
public void init() {
    mSensorManager = (SensorManager) hardwareMap.appContext.getSystemService(Context.SENSOR_SERVICE);
    accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

    azimuth = 0.0f;      // value in radians
    pitch = 0.0f;        // value in radians
    roll = 0.0f;

    leftDrive = hardwareMap.dcMotor.get("left_drive");
    rightDrive = hardwareMap.dcMotor.get("right_drive");


}

/*
  • Code to run when the op mode is first enabled goes here
  • @see com.qualcomm.robotcore.eventloop.opmode.OpMode#start() */ @Override public void start() { startDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());

    // delay value is SENSOR_DELAY_UI which is ok for telemetry, maybe not for actual robot use
    mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
    mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);
    

    }

    /*

    • This method will be called repeatedly in a loop
    • @see com.qualcomm.robotcore.eventloop.opmode.OpMode#loop() */ @Override public void loop() { // telemetry.addData("1 Start", "OrientOp started at " + startDate); // telemetry.addData("2 note1", "values below are in degrees" ); // telemetry.addData("3 note2", "azimuth relates to magnetic north" ); // telemetry.addData("4 note3", " " ); telemetry.addData("azimuth", Math.round(Math.toDegrees(azimuth))); telemetry.addData("pitch", Math.round(Math.toDegrees(pitch))); telemetry.addData("roll", Math.round(Math.toDegrees(roll)));

    }

    /* * Code to run when the op mode is first disabled goes here * @see com.qualcomm.robotcore.eventloop.opmode.OpMode#stop() */ @Override public void stop() { mSensorManager.unregisterListener(this); }

    public void onAccuracyChanged(Sensor sensor, int accuracy) { // not sure if needed, placeholder just in case }

    public void onSensorChanged(SensorEvent event) { // we need both sensor values to calculate orientation // only one value will have changed when this method called, we assume we can still use the other value. if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { mGravity = event.values; } if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { mGeomagnetic = event.values; } if (mGravity != null && mGeomagnetic != null) { //make sure we have both before calling getRotationMatrix float R[] = new float[9]; float I[] = new float[9]; boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic); if (success) { float orientation[] = new float[3]; SensorManager.getOrientation(R, orientation); azimuth = orientation[0]; // orientation contains: azimuth, pitch and roll pitch = orientation[1]; roll = orientation[2];

            if(Math.round(Math.toDegrees(pitch)) > 15 && Math.round(Math.toDegrees(roll)) < 15 && Math.round(Math.toDegrees(roll)) > -15)  {
                leftDrive.setPower(0.7);
                rightDrive.setPower(0.7);
            }
            else if(Math.round(Math.toDegrees(pitch)) < -15 && Math.round(Math.toDegrees(roll)) < 15 && Math.round(Math.toDegrees(roll)) > -15) {
                leftDrive.setPower(-0.7);
                rightDrive.setPower(- 0.7);
            }
            else if(Math.round(Math.toDegrees(roll)) > 15 && Math.round(Math.toDegrees(pitch)) < 15 && Math.round(Math.toDegrees(pitch)) > -15) {
                leftDrive.setPower(0.7);
                rightDrive.setPower(-0.7);
            }
            else if(Math.round(Math.toDegrees(roll)) < -15 && Math.round(Math.toDegrees(pitch)) < 15 && Math.round(Math.toDegrees(pitch)) > -15) {
                leftDrive.setPower(-0.7);
                rightDrive.setPower( 0.7);
            }
            else {
                leftDrive.setPower(0);
                rightDrive.setPower(0);
            }
    
        }
    
    }
    

    } }

/r/funny Thread Link - i.imgur.com