Processing & Arduino: multiple duplex communication problem

edited May 2014 in Arduino

Hello,

First of all, I visited many threads and website to solve my problem. But, somehow I'm doing something wrong. The hardware is a 6-DOF IMU, a MPU-6050. The IMU is connected to the Arduino. There will be two complete IMU modules, everyone has one MPU-6050 and an Arduino. The software fetches data (floats) and sends them to Processing using a standard Serial communication. Processing doesn't receive the correct data. The serial monitor of the Arduino shows a correct output.

Arduino Code http://pastebin.com/cnXKbpAH

Processing Code http://pastebin.com/YPt6pVuj -> Main code http://pastebin.com/1VBtXzb9 -> GUI

Answers

  • Serial expects data represented as bytes (/ASCII characters). Your data variable contains information represented as floating point (https://en.wikipedia.org/wiki/Single-precision_floating-point_format), so each of those float values (assuming 32 bit) are getting broken up into 4 8-bit bytes, and sent over your port. So you're going to get 4 "garbage" characters (I'm guessing) for each of those floating point values.

    void SendData(){
      float data[10] = {
        (float)accX, (float)accY, (float)accZ, (float)gyroX, (float)gyroY, (float)gyroZ, (float)tempRaw, (float)temp, (float)compAngleX, (float)compAngleY  };
    
      // send sensor values:
      for(int i = 0; i < 10; i++){
        Serial.println(data[i],2);
      }
    }
    

    You have a few options... one is to recombine those bytes to a 32 bit value then convert it to a float (non-trivial, I'm guessing?). The easiest option is to probably convert each floating point value to a String (byte/char array). So if you have the value data[0] = 3.14159, you convert it to a char array of {'3','.','1','4','1', etc ...} and send that over followed by a delimiter character (e.g. ',') so you know the start/stop point of each number of you transmit. In Processing, you'll receive the byte/char array, recombine to a String, and call something like Float.valueOf(String) to push it back to a floating-point represented value.

Sign In or Register to comment.