0
votes

I am currently trying to plot data which I receive through a bluetooth communication with a microcontroller board. Each data transmission (every 200 ms) sends a string of 4 characters (4 digits) to my android device where I am able to display the values with a textView that is updated each time there is some new data available. This is happening for 10 seconds in the MainActivity.

To get an array which I want to plot the data from I am saving each String in a string list like this:

// Create Array List to send to xyPlot activity
List<String> incomingStringData = new ArrayList<>();

// more code happening...

String loadCellString = recDataString.substring(1, 5); // get sensor value from string between indices 1-5
incomingStringData.add(loadCellString); // Adding each incoming substring to List<String> incomingStringData

// more code happening

// On button click send data to xyPlot activity
btnPlot.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) {

    Intent xyPlotScreen = new Intent(getApplicationContext(), xyPLot.class);

    //Sending data to another Activity
    String[] xyPlotStringArray = incomingStringData.toArray(new String[0]);
    xyPlotScreen.putExtra("string-array", xyPlotStringArray);

    // Start plotscreen (xyPlot) activity
    startActivity(xyPlotScreen);
  }
}

This Data ist sent to the activity xyPlot (which I copied from simple xyPlot example of androidplot, thanks btw) where it is processed like this:

// Get String Array from  Motor Control (MainActivity):
Intent xyPlotScreen = getIntent();
String[] thrustStringArray = xyPlotScreen.getStringArrayExtra("string-array");

// Convert String-Array into an Integer to be able to plot:
String[] parts = thrustStringArray[0].split(",");
Integer[] intThrust = new Integer[parts.length];

for(int n = 0; n < parts.length; n++) {
   intThrust[n] = Integer.parseInt(parts[n]);
}

// Create the Number series for plotting
Number[] thrustSeries = intThrust;

// Turn the above arrays into XYSeries':
// (Y_VALS_ONLY means use the element index as the x value
XYSeries thrustSeries = new SimpleXYSeries(Arrays.asList(thrustArray),
             SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED,"Thrust");

For now I used XY_VALS_INTERLEAVED just to know if I can plot the incoming data I am getting, even though it does not make sense (later on my string is going to be composed of a timestamp for the x-axis).

The data type "Number" (which also is an Integer right??) does of course not support String arrays. So I did a conversion from String into integer and then I could build the app.

When I start the plot activity I get this error message:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.controlcenter.controlcenter/com.controlcenter.controlcenter.xyPLot}: java.lang.IndexOutOfBoundsException: Cannot auto-generate series from odd-sized xy List.

Even when I change the frequency of the data incoming on my android device (1 value per second, so 10 values) I get the same error message. I guess the issue is somewhere around the part where I convert the string into an Integer. But I couldn't figure out a way to do this conversion the right way in order to plot the sensor data from a Number[] type of data.

Hope you guys can help me.

Thanks in advance! Chris

1

1 Answers

0
votes

The error you're seeing is simply saying that the array of data you're passing to Androidplot has an odd number of elements, which is illegal when using interleave mode: interleave means you are passing pairs of x/y values sequentially. If the size of this array is odd, it means either an x or y component is missing.

You can solve this problem quite a few ways. Probably the easiest thing to do just to get things working would be to modify your for loop to ignore the final value from your readings if its an odd number:

    int partsLen = parts.length;
    if(partsLen < 2) {
        // do something to gracefully avoid continuing as theres not enough data to plot.
    } else {
        if(partsLen % 2 == 1) {
            // if the size of the input is odd, ignore the last element to make it even:
            partsLen--;
        }
    }
    for(int n = 0; n < partsLen; n++) {
        intThrust[n] = Integer.parseInt(parts[n]);
    }