Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

ofSerial&MPU6050 : Fail to receive data on OF

$
0
0

@Sherrie wrote:

Hi,
I’m a freshman here to learn to use OF to control my Arduino nano. And for now, I am working on my project with Arduino nano and Accelerometer MPU6050. I would like to receive MPU6050’s data through Arduino and visualize the sensors’ motion in OF. Before I wrote my code, I found an example connect Arduino, MPU6050 with Processing(https://maker.pro/arduino/tutorial/how-to-interface-arduino-and-the-mpu-6050-sensor[https://maker.pro/arduino/tutorial/how-to-interface-arduino-and-the-mpu-6050-sensor], the effect of which is really near my expectation. So I decided to rewrite it in OF., using ofSerial. Unfortunately, it didn’t work.

I can’t set the breakpoint cuz my Arduino needs some time to initialize so the breakpoint always suggests that the value is 0, or null. I used “printf” to check some key values but there is nothing I can see.

This is my code (to prevent missing some unknown stupid mistakes I made, please check of it.) draw a box and hope it can rotate as MPU6050 do.

This is ofApp.h.

#pragma once

#include "ofMain.h"

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{
   
    
	public:
		void setup();
		void update();
    
		void draw();

		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y );
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void mouseEntered(int x, int y);
		void mouseExited(int x, int y);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);
   private:
        ofQuaternion serialEvent(ofSerial port);
    
        ofSerial  serial;
        ofEasyCam cam;
        ofNode mpuNode
    
};

This is ofApp.cpp.

#include "ofApp.h"

unsigned char receivedData[14];

ofQuaternion mpuquat;

//--------------------------------------------------------------
ofQuaternion ofApp::serialEvent(ofSerial port){
    
    ofQuaternion quat;
    char teapotPacket[14];
    int serialCount = 0;                 // current packet byte position
    int aligned = 0;
    float q[4];
    
    if (port.available() > 0) {
        port.readBytes(receivedData, 14);
        char *tmp_data = (char*)receivedData;
        teapotPacket[0] = tmp_data[0];
        teapotPacket[1] = tmp_data[1];
        teapotPacket[2] = tmp_data[2];
        teapotPacket[3] = tmp_data[3];
        teapotPacket[4] = tmp_data[4];
        teapotPacket[5] = tmp_data[5];
        teapotPacket[6] = tmp_data[6];
        teapotPacket[7] = tmp_data[7];
        teapotPacket[8] = tmp_data[8];
        teapotPacket[9] = tmp_data[9];
        teapotPacket[10] = tmp_data[10];
        teapotPacket[11] = tmp_data[11];
        teapotPacket[12] = tmp_data[12];
        teapotPacket[13] = tmp_data[13];
        
        for(int n=0; n < 14; n++){
            printf("%d", teapotPacket[n]);
            if (teapotPacket[n] == '$') {serialCount = 0;}
            // this will help with alignment
            if (aligned < 4) {
                // make sure we are properly aligned on a 14-byte packet
                if (serialCount == 0) {
                    if (teapotPacket[n] == '$') aligned++; else aligned = 0;
                } else if (serialCount == 1) {
                    if (teapotPacket[n] == 2) aligned++; else aligned = 0;
                } else if (serialCount == 12) {
                    if (teapotPacket[n] == '\r') aligned++; else aligned = 0;
                } else if (serialCount == 13) {
                    if (teapotPacket[n] == '\n') aligned++; else aligned = 0;
                }
                //println(ch + " " + aligned + " " + serialCount);
                serialCount++;
                if (serialCount == 14) serialCount = 0;
            } else {
                if (serialCount > 0 || teapotPacket[n] == '$') {
                    teapotPacket[serialCount++] = teapotPacket[n];
                    if (serialCount == 14) {
                        serialCount = 0; // restart packet byte position
                        
                        // get quaternion from data packet
                        q[0] = ((teapotPacket[2] << 8) | teapotPacket[3]) / 16384.0f;
                        q[1] = ((teapotPacket[4] << 8) | teapotPacket[5]) / 16384.0f;
                        q[2] = ((teapotPacket[6] << 8) | teapotPacket[7]) / 16384.0f;
                        q[3] = ((teapotPacket[8] << 8) | teapotPacket[9]) / 16384.0f;
                        for (int i = 0; i < 4; i++) if (q[i] >= 2) q[i] = -4 + q[i];
                        
                        // set our toxilibs quaternion to new data
                        quat.set(q[0], q[1], q[2], q[3]);
                        
                    }
                }
            }
        }
    }
   // cout << teapotPacket[0] << teapotPacket[1] << teapotPacket[2] << teapotPacket[3] << endl;
    return quat;
    
}


void ofApp::setup(){
    serial.listDevices();
    vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();
    int baud = 115200;
    serial.setup("/dev/cu.usbserial-A100ASV2", baud);
    
    ofSetFrameRate(120);
    ofSetVerticalSync(true);
    ofSetLogLevel(OF_LOG_VERBOSE);
    
    cam.setOrientation(ofPoint(-20, 0, 0));
}

//--------------------------------------------------------------
void ofApp::update(){
    
    

}


//--------------------------------------------------------------
void ofApp::draw(){
    ofEnableDepthTest();
    cam.begin();
    mpuquat = serialEvent(serial);
    mpuNode.transformGL();
    mpuNode.setPosition(0,0,0);
    mpuNode.setOrientation(mpuquat);
    // ofRotateX(mpu[1]);
    // ofRotateY(mpu[0]);
    // ofRotateZ(mpu[2]);
    ofNoFill();
    ofSetColor(255, 0, 0);
    ofDrawBox(0, 0, 0, 100,10,100);
    mpuNode.restoreTransformGL();
    cam.end();
    

}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
    

}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){
    

}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
    

}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
    

    
}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
    

}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
    

}

//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
    

}

//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
    

}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
    

}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){ 

}


I built successfully and ran, my box had no rotation and just stayed still. For a lil while, in the console, OF told me something was wrong.I have lil experience in c/c++, thus I can’t know more from my code, yet I can’t fix these errors and receive data correctly.

In the end, this is some clues Xcode showed me after running it.

Thanks!!!(A Deep Bow)

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles