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

Use gyroscope pitch/roll data to control circle in OpenFrameworks app

$
0
0

@Rii wrote:

Hi there,

I’m kinda new to OpenFrameworks and I was wondering how can I use the pitch/roll data I’m reading from my gyroscope (Particle photon) via WiFi in order to control the movements of a white circle I have drawn in OpenFrameworks.
I have the gyroscope and OpenFrameworks app connected together and I’m getting the pitch/roll data already.

What I’m looking for is to know how can I feed this pitch/roll as movements to the WhiteBall in my code?

Thanks in advance for your help!

#include "ofApp.h"

#define RECONNECT_TIME 400

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

	//HERE WE ARE SETTING THE RATE AT WHICH WE THE PROGRAM WILL RUN (AND HOW MANY TIMES PER SECOND THE UPDATE AND DRAW METHODS ARE CALLED IT IN FRAMES PER SECOND)
	ofSetFrameRate(60);
	ofSetBackgroundColor(230);

	msgTx = "";
	msgRx = "";
	
	roll = "";
	pitch = "";

	ofxTCPSettings settings("192.168.1.103", 23);

	tcpClient.setup(settings);
	tcpClient.setMessageDelimiter("\n");

	connectTime = 0;
	deltaTime = 0;

	//NOW WE TELL THE FIRST BALL HOW IT LOOKS LIKE:

	//FIRST WE GIVE IT A RANDOM POSITION
	WhiteBall.position.x = ofGetWidth() / 2;
	WhiteBall.position.y = ofGetHeight() / 2;
	GreenBall.position.x = ofRandom(0, ofGetWidth() / 2);
	GreenBall.position.y = ofRandom(0, ofGetHeight() / 2);
	RedBall.position.x = ofRandom(0, ofGetWidth() / 2);
	RedBall.position.y = ofRandom(0, ofGetHeight() / 2);

	//AND A RADIUS
	WhiteBall.radius = 30;
	GreenBall.radius = 10;
	RedBall.radius = 10;

	//AND THE COLOR
	WhiteBall.color = ofColor(255, 255, 255);
	GreenBall.color = ofColor(0, 255, 0);
	RedBall.color = ofColor(255, 0, 0);

	//AND A DIRECTION/SPEED
	//WhiteBall.direction.x = 3, 10;
	//WhiteBall.direction.y = 3, 10;
}

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

	if (tcpClient.isConnected()) {
		// we are connected - lets try to receive from the server
		//cout << "Connected to photon." << endl;
		string str = tcpClient.receiveRaw();
		if (str.length() > 0) {
			if (str != "^") {
				vector<string> splitString = ofSplitString(str, ";");
				roll = splitString[0].c_str();
				pitch = splitString[1].c_str();
				//cout << str << endl;
				cout << roll << endl;
				cout << pitch << endl;
				msgRx = str;
			}
		}
	}
	else {
		msgTx = "";
		// if we are not connected lets try and reconnect every 5 seconds
		deltaTime = ofGetElapsedTimeMillis() - connectTime;

		if (deltaTime > 5000) {
			tcpClient.setup("192.168.1.103", 23);
			tcpClient.setMessageDelimiter("\n");
			connectTime = ofGetElapsedTimeMillis();
		}

	}



	//HERE WE CHECK IF WE REACHED THE BORDERS OF THE WINDOW AND BOUNCE IT BACK
	if (WhiteBall.position.x < 0) {
	WhiteBall.direction.x = abs(WhiteBall.direction.x);
	}
	if (WhiteBall.position.y < 0) {
	WhiteBall.direction.y = abs(WhiteBall.direction.y);
	}

	if (WhiteBall.position.x > ofGetWidth()) {
	WhiteBall.direction.x = -abs(WhiteBall.direction.x);
	}

	if (WhiteBall.position.y > ofGetHeight()) {
	WhiteBall.direction.y = -abs(WhiteBall.direction.y);
	}




	//HERE WE UPDATE THE POSITION OF THE BALL BASED ON THE SPEED/DIRECTION
	WhiteBall.position.x = WhiteBall.position.x + WhiteBall.direction.x;
	WhiteBall.position.y = WhiteBall.position.y + WhiteBall.direction.y;
	GreenBall.position = GreenBall.position;
	RedBall.position = RedBall.position;

}

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

	ofSetColor(20);
	ofDrawBitmapString("openFrameworks Project", 15, 30);

	if (tcpClient.isConnected()) {
		ofDrawBitmapString("IMU=Roll;Pitch " + msgRx, 15, 700);
	}
	else {
		ofDrawBitmapString("status: server not found. launch server app and check ports!\n\nreconnecting in " + ofToString((5000 - deltaTime) / 1000) + " seconds", 15, 55);
	}

	//AND NOW WE CAN DRAW A CIRCLE AT THE POSITION OF THE BALL WITH ITS RADIUS
	ofSetColor(WhiteBall.color);
	ofDrawCircle(WhiteBall.position, WhiteBall.radius);
	ofSetColor(GreenBall.color);
	ofDrawCircle(GreenBall.position, GreenBall.radius);
	ofSetColor(RedBall.color);
	ofDrawCircle(RedBall.position, RedBall.radius);

}

//--------------------------------------------------------------
void ofApp::keyPressed(ofKeyEventArgs & 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) {

}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles