In this post we will simultaneously stream data from the accelerometer and gyroscope. This is a typical use case for analysing motion since the combination of data gives a much richer dataset upon which to perform machine learning classification.
We have not yet setup our gyroscope. The process is the same as for the accelerometer in that we need to import the relevant modules. At this point I will also highlight that the Accelerometer we have been using in the tutorial so far is the generic accelerometer class. If we want to start being more precise, we should specify the accelerometer on our board. To quote the docs:
The accelerometer class is a generic class that provides some high level access to an accelerometer. As this class is unaware of any sensor specifics, it only provides limited functionality and only suitable for apps intending to interact with any MetaWear type i.e. the sample app.
Depending on which board you are working with, you’ll want either the MMA8452Q (MetaWear R boards) or the BMI160 (MetaWear R Pro/RG, C/CPRO boards). For our purposes, we’ll use the BMI160.
Note that we also import with the * which is to also import configuration methods used later.
We create our variables for use later:
And once again use the getModule method within the connected try block of the ConnectionStateHandler:
We will be streaming data from both the accelerometer and the gyroscope, so let’s update the UI - we’ll call the switch “Toggle Both” and also add view elements to show the two readings on the screen
In activity_my.xml we move our connect and LED buttons up (by adjusting the marginTop property and removing the centerVertical property) and add two new text views for showing the sensor readings:
Your app should now look something like this:
Obviously this tutorial series is not about how to make the app UI look good. Tutorials for that sort of thing are a Google search away.
Now we’re ready to add our routes. We will need one for each sensor. Within the switch isClicked if statement, we add the following:
Next we need to subscribe to the routes we have just setup:
Note that the stream keys are different for each route. After the second routeManager, let’s add our start code for both sensors:
Try running the app - when you tap the “Toggle Both” button, you should see readings for both the Accelerometer and the Gyroscope in the logcat.
Now let’s also update the readings in the UI. Declare our variables first:
Then in onCreate hook them up:
If we just use a setText inside the switch code, we will trigger a WrongThreadException, so we will need to setup a little bit of extra code to avoid this. At the end of the activity, we add:
And now within our route subscriptions, we can call the sensorMsg method passing in the accel and gyro parameters accordingly:
Run the app again and you will see the UI displaying the sensor readings for the board accelerometer and gyroscope simultaneously, as well as in the logs.
Hopefully this post clears up one of the main hurdles for getting the most out of your board. Next we’ll look at how we can export our data to a csv.
Note You can view all these changes in the github repository on the branch version-0.6