Accelerometer and gyroscope

I have recently bought a Metamotion R 10 IMU axis module and am having problems with the coding part, Can you provide a proper documentation of the accelerometer and gyroscope codes cause the available documentation codes arent being debugged by the android studio and multiple errors are shown because of the new 3.0.0 update. Kindly send me  a detailed syntax documentation on BMI160 accelerometer and gyroscope.

Comments

  • edited March 2017
    Links to documentation and example code are provided in the API v3 thread.

  • I have already referred to that documentation but im getting some errors, don't you have a more detailed reference?

  • What kind of errors are you getting and what parts of the documentation is not clear to you?  How are you attempting to use the sensors?

    You can also refer example projects linked in the thread as well, such as the MetaWear app, to see working code.
  • Im trying to get data from accelerometer and gyroscope and store it in 3 variables(1 for each axis) and i tried the code from the data route directory that streams the data from accelerometer. Android studio does not even recognise the keyword stream and the code cannot be build. Can you tell me how to access the data from BMI160 accelerometer and gyroscope?
  • edited March 2017
    Other than a few syntax errors (missing assignment operator, should be addRouteAsync), that is how you access sensor data.  The metawear app code, example apps, and unit tests all access data as outlined in the documentation.

    What code do you have so far and what is the exact error reported by Android Studio (screen cap or post the message)?  Have you tried rebuilding the project?  Furthermore, can you compile any of the example projects?
  • thanks we got the errors sorted out but could you tell us the syntax for using the IMUplus config of sensor fusion to obtain angular velocity?

    and also if we use multiple threshold filters will it work as a nested if else function?
  • @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
    BtleService.LocalBinder serviceBinder = (BtleService.LocalBinder) service;
    AccelerationDataProducer acceleration = board.getModule(Accelerometer.class).acceleration();

    String mwMacAddress= "E9:55:7B:9D:23:B6"; ///< Put your board's MAC address here
    BluetoothManager btManager= (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
    BluetoothDevice btDevice= btManager.getAdapter().getRemoteDevice(mwMacAddress);

    mwBoard= serviceBinder.getMetaWearBoard(btDevice);
    mwBoard.connectAsync().onSuccessTask(task -> {
    accelerometer = mwBoard.getModule(Accelerometer.class);
    accelerometer.configure()
    .odr(50f)
    .commit();
    acceleration.addRoute(new RouteBuilder() {
    @Override
    public void configure(RouteComponent source) {
    source.stream(new Subscriber() {
    @Override
    public void apply(Data data, Object... env) {
    Log.i("MainActivity", data.value(Acceleration.class).toString());
    }
    });
    }
    });
  • In this code the red words arent being recognised by the android studio, can you tell us which class to import from metawear to resolve this?

  • 1) Use correctedAngularVelocity.  The other sensor fusion outputs are also listed in the javadocs page.

    2) Depends on how you are using them.  Are you chaining them together or feeding the same data to multiple threshold filters with a multicast?

    3) You don't need the first erred line.  Update the second erred code chunk to match provided snippet in the Accelerometer section:
  • I am feeding the same data to multiple thresholds. Basically we want to simulate an if else construct like -


    if(a>x&&b>y......)
    {some logic;}
    else if(a<x&&b>y......)
    {some logic;}
    .
    .
    .

    and so on,  Is there a way to simulate this?

  • Chaining filters together will behave like a logical AND and creating branches functions like a logical OR.  The only thing to be mindful of is that all branches that accept the input data will let the data through rather than just the first valid branch.
  • can u give me the corresponding code to this-
    if(xaxisacceleration>x1,yaxisacceleration>y1,zaxisacceleration>z1)
    {
    if(xaxisangularvelocity>x2,yaxisangularvelocity>y2,zaxisangularvelocity>z2)
    {
    //a gsm call to be made from the phone to an emergency contact
    }
    }



    i cant form the nested loop with the threshold functions, can u send the code for this logic cause we are on a deadline- being this wednesday
  • Combining results from multiple outputs is not easily implemented with the on-board data processing.  It can be done but it is very messy; see these posts:


    Since you have to maintain a connection to the phone to trigger the call, the easiest solution is to stream the data to the phone and use actual if-else statements.

    If you need custom / dedicated support for your project, please contact us directly with a proposal of what kind of support you would like from MbientLab.
  • I have tried the stream code, but i dont know how to refer to the each axis value and then compare it with fixed values in the if else statements.

  • basically i want the data in variables with which i can perform comparing dunctions in the if statements

  • This is the code i tried to access the data in the form of variables to compare but in the first sentences it mentions an error saying a bracket and a semi colon at the red spots, is missing whereas it is not - 
    Acceleration acceleration = new Acceleration(float x;float y;float z);

    if (acceleration.x() > 0.5f) {
    try {
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
    r.play();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    Can you determine if the approach is right and what the error is in this code. Please reply urgently as our deadline is approaching
  • edited April 2017
    The first line of code is completely nonsensical and I cannot discern what you are attemping to do.  Regardless, you do not create the Acceleration object; the API creates one for each data sample received from the accelerometer and passes it to you through the Subscriber's apply function.  This is illustrated in the Accelerometer section of the documentation..
  • Can you tell us how to transfer each axis data onto variables of float type so that we can perform logic operations on them?
  • edited April 2017
    Setup a data route for the acceleration data producer, add a stream component, then retrieve the Acceleration object encapsulated by the data parameter.  Again, see the example code in the Accelerometer section:  

    The code in your previous post, disregarding the first line, is fine, you just need to place it in the data route.

    As mentioned in my first post, you should look over the tutorials and demo apps, specifically the MetaWear Android app.  They provide full, working examples of the API.
  • can you illustrate how you do this, i did exactly what u said and the compiler doesnt recognise the keyword acceleration
      
  • The sample code in the linked documentation is how you set it up, you had started setting it up in your earlier post.  Please post your code in its entirety because this sounds like you have not properly declared some variables or there is some syntax errors.
  • public void configure(RouteComponent source) {
    source.stream(new Subscriber() {
    @Override


    public void apply(Data data, Object... env) {
    final SensorFusionBosch.CorrectedAngularVelocity value1 = data.value(SensorFusionBosch.CorrectedAngularVelocity.class);
    if((value1.x()>100||value1.x()<-100)&&(value1.y()>100||value1.y()<-100)&&(value1.z()>100||value1.z()<-100)) {
    b=1;





    }
    if(a==1&&b==1)
    {
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
    r.play();

    }
    Log.i("MainActivity", "Ang = " + data.value(SensorFusionBosch.CorrectedAngularVelocity.class));

    }
    });
    }
    }).continueWith(new Continuation<Route, Void>() {
    @Override
    public Void then(Task<Route> task) throws Exception {
    sensorFusion.correctedAngularVelocity().start();
    sensorFusion.start();
    return null;

    }
    });
    return null;
    });
  • In the above code I want to break out of the code during the condition if(a==1&&b==1) and execute another sms code. Can you tell us how to change the control flow of the compiler from the continous execution of the code to send the sms.
  • Use a boolean variable to toggle whether or not to skip the apply function.

    new Subscriber() {
        @Override
        public void apply(Data data, Object... env) {
            if (!skip) {
                // execute code here if skip == false
                if (a == 1 &amp;&amp; b == 1) {
                    skip = true;
                    // reset "skip = false" later
                }
            }
        }
    }
    If on the other hand you want to stop data from being transmitted during this time frame, either stop the sensor or use a passthrough limiter in conditional mode to stop the data flow.
This discussion has been closed.