Generating Real-Time Graphs in PSLab Android App

In PSLab Android App, we need to log data from the sensors and correspondingly generate real-time graphs. Real-time graphs mean a data streaming chart that automatically updates itself after every n second. This was different from what we did in Oscilloscope’s graph, here we need to determine the relative time at which the data is recorded from the sensor by the PSLab. Another thing we need to take care of was the range of x axis. Since the data to be streamed is ever growing, setting a large range of the x axis will only make reading sensor data tedious for the user. For this, the solution was to make real time rolling window graph. It’s like when the graph exceeds the maximum range of x axis, the graph doesn’t show the initial plots. For example, if I set that graph should show the data only for the 10-second window when the 11th-second data would be plot, the 1st-second data won’t be shown by the graph and maintains the difference between the maximum and the minimum range of the graph. The graph library we are going to use is MPAndroidChart. Let’s break-down the implementation step by step. First, we create a long variable, startTime which records the time at which the entire process starts. This would be the reference time. Flags make sure when to reset this time. if (flag == 0) { startTime = System.currentTimeMillis(); flag = 1; }   We used Async Tasks approach in which the data is from the sensors is acquired in the background thread and the graph is updated in the UI thread. Here we consider an example of the HMC5883L sensor, which is actually Magnetometer. We are calculating time elapsed by subtracting current time with the sartTime and the result is taken as the x coordinate. private class SensorDataFetch extends AsyncTask<Void, Void, Void> { ArrayList<Double> dataHMC5883L = new ArrayList<Double>(); long timeElapsed; @Override protected Void doInBackground(Void... params) { timeElapsed = (System.currentTimeMillis() - startTime) / 1000; entriesbx.add(new Entry((float) timeElapsed, dataHMC5883L.get(0).floatValue())); entriesby.add(new Entry((float) timeElapsed, dataHMC5883L.get(1).floatValue())); entriesbz.add(new Entry((float) timeElapsed, dataHMC5883L.get(2).floatValue())); return null; }   As we need to create a rolling window graph we require to add few lines of code with the standard implementation of the graph using MPAndroidChart. This entire code is placed under onPostExecute method of AsyncTasks. The following code sets data set for the Line Chart and tells the Line Chart that a new data is acquired. It’s very important to call notifyDataSetChanged, without this the things won’t work. mChart.setData(data); mChart.notifyDataSetChanged();   Now, we will set the visible range of x axis. This means that the graph window of the graph won’t change until and unless the range set by this method is not achieved. Here we are setting it to be 10 as we need a 10-second window. mChart.setVisibleXRangeMaximum(10); Then we will call moveViewToX method to move the view to the latest entry of the graph. Here, we have passed data.getEntryCount method which returns the no. of data points in the data…

Continue ReadingGenerating Real-Time Graphs in PSLab Android App

Real time Sensor Data Analysis on PSLab Android

PSLab device has the capacity to connect plug and play sensors through the I2C bus. The sensors are capable of providing data in real time. So, the PSLab Android App and the Desktop app need to have the feature to fetch real time sensor values and display the same in the user interface along with plotting the values on a simple graph. The UI was made following the guidelines of Google’s Material Design and incorporating some ideas from the Science Journal app. Cards are used for making each section of the UI. There are segregated sections for real time updates and plotting where the real time data can be visualised. A methods for fetching the data are run continuously in the background which receive the data from the sensor and then update the screen. The following section denotes a small portion of the UI responsible for displaying the data on the screen continuously and are quite simple enough. There are a number of TextViews which are being constantly updated on the screen. Their number depends on the type and volume of data sent by the sensor. <TextView android:layout_width="wrap_content" android:layout_height="30dp" android:layout_gravity="start" android:text="@string/ax" android:textAlignment="textStart" android:textColor="@color/black" android:textSize="@dimen/textsize_edittext" android:textStyle="bold" /> <TextView android:id="@+id/tv_sensor_mpu6050_ax" android:layout_width="wrap_content" android:layout_height="30dp" android:layout_gravity="start" android:textAlignment="textStart" android:textColor="@color/black" android:textSize="@dimen/textsize_edittext" android:textStyle="bold" />   The section here represents the portion of the UI responsible for displaying the graph. Like all other parts of the UI of PSLab Android, MPAndroidChart is being used here for plotting the graph. <LinearLayout android:layout_width="match_parent" android:layout_height="160dp" android:layout_marginTop="40dp"> <com.github.mikephil.charting.charts.LineChart android:id="@+id/chart_sensor_mpu6050" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000" /> </LinearLayout>   Since the updates needs to continuous, a process should be continuously run for updating the display of the data and the graph. There are a variety of options available in Android in this regard like using a Timer on the UI thread and keep updating the data continuously, using ASyncTask to run a process in the background etc. The issue with the former is that since all the processes i.e. fetching the data and updating the textviews & graph will run on the UI thread, the UI will become laggy. So, the developer team chose to use ASyncTask and make all the processes run in the background so that the UI thread functions smoothly. A new class SensorDataFetch which extends AsyncTask is defined and its object is created in a runnable and the use of runnable ensures that the thread is run continuously till the time the fragment is used by the user. scienceLab = ScienceLabCommon.scienceLab; i2c = scienceLab.i2c; try { MPU6050 = new MPU6050(i2c); } catch (IOException e) { e.printStackTrace(); } Runnable runnable = new Runnable() { @Override public void run() { while (true) { if (scienceLab.isConnected()) { try { sensorDataFetch = new SensorDataFetch(); } catch (IOException e) { e.printStackTrace(); } sensorDataFetch.execute(); } } } }; new Thread(runnable).start();   The following is the code for the ASyncTask created. There are two methods defined here doInBackground and onPostExecute which are responsible for fetching the data and updating the display respectively. The raw data is fetched using the getRaw…

Continue ReadingReal time Sensor Data Analysis on PSLab Android