Read out serial data (MinIMU-9 v2 and arduino)

hey, I’m trying to read out data using ofserial from an MinIMU-9 v2 (Gyro, Accelerometer, and Compass) connected to an arduino.

In the Arduino serial monitor the data looks like this:

!ANG:-0.13,-0.22,-106.23
!ANG:-0.26,-0.21,-106.26
!ANG:-0.38,-0.21,-106.28

the python program that comes with the arduino example code handles it like this:

  
  
  
roll=0  
pitch=0  
yaw=0  
while 1:  
    line = ser.readline()  
    if line.find("!ANG:") != -1:          # filter out incomplete (invalid) lines  
        line = line.replace("!ANG:","")   # Delete "!ANG:"  
        print line  
        f.write(line)                     # Write to the output log file  
        words = string.split(line,",")    # Fields split  
        if len(words) > 2:  
            try:  
                roll = float(words[0])*grad2rad  
                pitch = float(words[1])*grad2rad  
                yaw = float(words[2])*grad2rad  
  
  
  

i wonder if someone could help me to translate that into open frameworks or show me the best way to handle this situation. so far i’m using the example from joshua’s book, but i don’t know or don’t have the knowledge to get any further with the bytes thing.

  
  
#include "testApp.h"  
  
  
void testApp::setup(){  
    bSendSerialMessage = false;  
    //serial.enumerateDevices(); // this will print all the devices  
    // this is set to the port where your device is connected  
    serial.setup("/dev/tty.usbmodem1d11", 115200);  
}  
  
void testApp::update(){  
    if (bSendSerialMessage){  
		  
  
        // send a handshake to the Arduino serial  
        serial.writeByte('x');  
        // make sure there's something to write all the data to  
          unsigned char bytesReturned[NUM_BYTES];  
          
        memset(bytesReturned, 0, NUM_BYTES);  
		// keep reading bytes, until there's none left to read  
        while( serial.readBytes(bytesReturned, NUM_BYTES) > 0){}  
		bSendSerialMessage = false;  
          
        ofLog(OF_LOG_NOTICE, "serial = " + ofToString(bytesReturned));  
  
	}  
	// wait a few cycles before asking again  
      
      
      
	countCycles++;  
	if(countCycles == 5) {  
		bSendSerialMessage = true;  
		countCycles = 0;  
	}  
      
      
         
}  
  
void testApp::draw() {  
	ofPoint pt(ofGetWidth()/2, ofGetHeight()/2);  
  //   ofDrawBitmapString (ofToString(bytesReturned), pt);  
}  
  
  

Hi Nixon,

I don’t know about this ofxSerial addon. In a project once I was reading data coming from the Arduino, using the core ofSerial: http://www.openframeworks.cc/documentation/serial/ofSerial.html

Maybe you should check it out. I remember it was fairly easy to implement it looking at the documentation examples.
Unfortunately I cannot tell you in details as I don’t have my source code with me now.

But still, hope this will help you :slight_smile:

Hey sry my fault. I meant ofserial. Changed it.

ah ok :slight_smile:

So I could check this project source code and what I am doing to read the data from arduino (in the update loop) is:

  
void testApp::update() {  
  
byte2Read = serial.available();  
          
        if(byte2Read > 0) {        
            unsigned char bytesReturned[byte2Read];  
  
            // read all the buffer and store it  
            for(int i = 0; i < byte2Read; i++) {  
                bytesReturned[i] = serial.readByte();             
            }  
  
              
            // Then do something with your byte array bytesReturned  
  
        }  
}  

Basically first I check if there are some byte available from Arduino. If yes, I store each byte in an array and then I process them independently depending of what I stored there.

I was not getting continuous stream of data, such as accelerometer readings: my application was just getting button status once pushed and it was working great.

Cheers!

Thank you, I ll check it out when I am back at home. Cheers