Gyroscope TK 64 and Processing (arduino)
in
Integration and Hardware
•
1 year ago
Hello!
I have connected arduinoUno with tinkerkitShiekd and tinkerkitGyroscope(64). The sensor works fine and I get the reading in arduiniIDE but I need to graph the outcome so I have installed Processing and I have tried with the coded posed in arduino.cc for tk gyro and processing. No lucky.
I get the below error:
Exception in thread "Animation Thread" java.lang.NullPointerException
at GyroGUIfromArduino.draw(GyroGUIfromArduino.java:100)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:662)
I know that this forum is more for processing in Java than for arduino, but someone herte has got the answer for my problem
The code is as follows (Processing marks the line text("[Y] GYRO_Y speed: "+temp[1]+"°/s", 10, 70); ):
/* -------+ TinkerKit! Gyroscope Module T000062 Dashboard +-------
This processing sketch grab data from an analog 2-axis Gyroscope
connected to Arduino, plot the angular rate readings into a graph;
then transform them into an angular position (inclination) trought
integration over time, dislpaying them into gauges.
Look on the product page for the Gyroscope specifiations:
http://store.arduino.cc/ww/index.php?main_page=product_info&cPath=16&products_id=94
Connect the gyroscope with two Toolkit wires to the Tinkerkit!
sensor shield into I0 and I1.
Mount the shield over an Arduino board then upload on it the
"Standard Firmata" sketch, then you are ready to go.
Processing, in order to work with Firmata protocol requires that
there is a library installed on your environment. Find the library here:
http://www.arduino.cc/playground/Interfacing/Processing
created 17 Sept 2011
by Federico Vanzati
This example code is in the public domain.
*/
import processing.serial.*; // Enable processing to use the serial port
Serial myPort; // Create object from Serial class
String myString = null;
String[] temp;
int lf = 10;
// Timing variables
int refreshScreen = 0;
// Variables to transform data into physical measurement
int rollSpeed, rollAngle, pitchSpeed, pitchAngle;
int offsetX, offsetY;
// Variables for displayng data
int[] y_rateVideoBuffer, x_rateVideoBuffer;
String str_yAngle, str_yGyroRate, str_xAngle, str_xGyroRate;
void setup()
{
size(700, 440);
String portName = Serial.list()[2];
myPort = new Serial(this, portName, 57600);
// wait a little to give time to Arduino to be initialized
delay(500);
frameRate(30);
y_rateVideoBuffer = new int[width];
x_rateVideoBuffer = new int[width];
PFont font;
font = loadFont("Monospaced.bold-26.vlw");
textFont(font);
smooth(); // smoothed shapes looks better!
}
void draw() {
background(0);
fill(#088D9D);
stroke(255);
//NOTA: misure prese dal centro
ellipse(width-70, height-300, 120, 120); // the circles
ellipse(width-70, height-100, 120, 120); // -> our gauges!
// Strings composition
textSize(26);
text("[Y] GYRO_Y speed: "+temp[1]+"°/s", 10, 70);
text("[X] GYRO_X speed: "+temp[5]+"°/s", 10, 270);
text("Angle", (width-120), 70);
text("Angle", (width-120), 270);
text(temp[3]+"°", (width-120), (height-210));
text(temp[7]+"°", (width-120), (height-10) );
textSize(28);
fill(#C63906);
text("TinkerKit! 2-Axis Gyroscope [T000062]", 28, 28);
textSize(15);
text("Reset Arduino to calibrate and restart this GUI", 10, (height -5));
// Draw the gauges indicator. Will display the inclination
strokeWeight(3);
float gyroY_radians = radians(-rollAngle)-HALF_PI;
line( (width-70), (height-300), (width-70)+(60*cos(gyroY_radians)), (height-300)+(60*sin(gyroY_radians)) );
float gyroX_radians = radians(-pitchAngle)-PI;
line( (width-70), (height-100), (width-70)+(60*cos(gyroX_radians)), (height-100)+(60*sin(gyroX_radians)) );
strokeWeight(1);
// call the function that plot the angular velocities, with few screen settings
graphMeThis(rollSpeed, y_rateVideoBuffer, 140, 300, 140);
graphMeThis(pitchSpeed, x_rateVideoBuffer, 140, 100, 140);
}
void serialEvent(Serial p) {
myString = p.readStringUntil(lf);
if (myString != null) {
temp = myString.split(" |\t");
rollSpeed = parseInt(temp[1]);
rollAngle = parseInt(temp[3]);
pitchSpeed = parseInt(temp[5]);
pitchAngle = parseInt(temp[7]);
println(rollSpeed+" "+rollAngle+" "+pitchSpeed+" "+pitchAngle);
}
}
/*
This function plot data over screen in a xPos and yPos given position.
[ySpan] is expressed in pixel and indicates the heigth that you give to the graph
*/
void graphMeThis(float inData, int[] graphBuffer, int xPos, int yPos, int ySpan)
{
// For each frame the buffer that contains the older samples are shifted by one position
for (int i=0; i<(width-xPos)-1; i++)
graphBuffer = graphBuffer[i+1];
// Store the new entry data
graphBuffer[(width-xPos)-1] = (int)map(inData, -3000, 3000, -ySpan/2, ySpan/2); //salvo il nuovo dato...che plotto alla fine del grafico
stroke(#E2F520, 150);
strokeWeight(2);
// plot the graph that goes from right to left
for (int x=(width-xPos); x>1; x--)
line( x, (height-yPos)-graphBuffer[x-1], x+1, (height-yPos)-graphBuffer
strokeWeight(1);
}
Thanks a lot!
I have connected arduinoUno with tinkerkitShiekd and tinkerkitGyroscope(64). The sensor works fine and I get the reading in arduiniIDE but I need to graph the outcome so I have installed Processing and I have tried with the coded posed in arduino.cc for tk gyro and processing. No lucky.
I get the below error:
Exception in thread "Animation Thread" java.lang.NullPointerException
at GyroGUIfromArduino.draw(GyroGUIfromArduino.java:100)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:662)
I know that this forum is more for processing in Java than for arduino, but someone herte has got the answer for my problem
The code is as follows (Processing marks the line text("[Y] GYRO_Y speed: "+temp[1]+"°/s", 10, 70); ):
/* -------+ TinkerKit! Gyroscope Module T000062 Dashboard +-------
This processing sketch grab data from an analog 2-axis Gyroscope
connected to Arduino, plot the angular rate readings into a graph;
then transform them into an angular position (inclination) trought
integration over time, dislpaying them into gauges.
Look on the product page for the Gyroscope specifiations:
http://store.arduino.cc/ww/index.php?main_page=product_info&cPath=16&products_id=94
Connect the gyroscope with two Toolkit wires to the Tinkerkit!
sensor shield into I0 and I1.
Mount the shield over an Arduino board then upload on it the
"Standard Firmata" sketch, then you are ready to go.
Processing, in order to work with Firmata protocol requires that
there is a library installed on your environment. Find the library here:
http://www.arduino.cc/playground/Interfacing/Processing
created 17 Sept 2011
by Federico Vanzati
This example code is in the public domain.
*/
import processing.serial.*; // Enable processing to use the serial port
Serial myPort; // Create object from Serial class
String myString = null;
String[] temp;
int lf = 10;
// Timing variables
int refreshScreen = 0;
// Variables to transform data into physical measurement
int rollSpeed, rollAngle, pitchSpeed, pitchAngle;
int offsetX, offsetY;
// Variables for displayng data
int[] y_rateVideoBuffer, x_rateVideoBuffer;
String str_yAngle, str_yGyroRate, str_xAngle, str_xGyroRate;
void setup()
{
size(700, 440);
String portName = Serial.list()[2];
myPort = new Serial(this, portName, 57600);
// wait a little to give time to Arduino to be initialized
delay(500);
frameRate(30);
y_rateVideoBuffer = new int[width];
x_rateVideoBuffer = new int[width];
PFont font;
font = loadFont("Monospaced.bold-26.vlw");
textFont(font);
smooth(); // smoothed shapes looks better!
}
void draw() {
background(0);
fill(#088D9D);
stroke(255);
//NOTA: misure prese dal centro
ellipse(width-70, height-300, 120, 120); // the circles
ellipse(width-70, height-100, 120, 120); // -> our gauges!
// Strings composition
textSize(26);
text("[Y] GYRO_Y speed: "+temp[1]+"°/s", 10, 70);
text("[X] GYRO_X speed: "+temp[5]+"°/s", 10, 270);
text("Angle", (width-120), 70);
text("Angle", (width-120), 270);
text(temp[3]+"°", (width-120), (height-210));
text(temp[7]+"°", (width-120), (height-10) );
textSize(28);
fill(#C63906);
text("TinkerKit! 2-Axis Gyroscope [T000062]", 28, 28);
textSize(15);
text("Reset Arduino to calibrate and restart this GUI", 10, (height -5));
// Draw the gauges indicator. Will display the inclination
strokeWeight(3);
float gyroY_radians = radians(-rollAngle)-HALF_PI;
line( (width-70), (height-300), (width-70)+(60*cos(gyroY_radians)), (height-300)+(60*sin(gyroY_radians)) );
float gyroX_radians = radians(-pitchAngle)-PI;
line( (width-70), (height-100), (width-70)+(60*cos(gyroX_radians)), (height-100)+(60*sin(gyroX_radians)) );
strokeWeight(1);
// call the function that plot the angular velocities, with few screen settings
graphMeThis(rollSpeed, y_rateVideoBuffer, 140, 300, 140);
graphMeThis(pitchSpeed, x_rateVideoBuffer, 140, 100, 140);
}
void serialEvent(Serial p) {
myString = p.readStringUntil(lf);
if (myString != null) {
temp = myString.split(" |\t");
rollSpeed = parseInt(temp[1]);
rollAngle = parseInt(temp[3]);
pitchSpeed = parseInt(temp[5]);
pitchAngle = parseInt(temp[7]);
println(rollSpeed+" "+rollAngle+" "+pitchSpeed+" "+pitchAngle);
}
}
/*
This function plot data over screen in a xPos and yPos given position.
[ySpan] is expressed in pixel and indicates the heigth that you give to the graph
*/
void graphMeThis(float inData, int[] graphBuffer, int xPos, int yPos, int ySpan)
{
// For each frame the buffer that contains the older samples are shifted by one position
for (int i=0; i<(width-xPos)-1; i++)
graphBuffer = graphBuffer[i+1];
// Store the new entry data
graphBuffer[(width-xPos)-1] = (int)map(inData, -3000, 3000, -ySpan/2, ySpan/2); //salvo il nuovo dato...che plotto alla fine del grafico
stroke(#E2F520, 150);
strokeWeight(2);
// plot the graph that goes from right to left
for (int x=(width-xPos); x>1; x--)
line( x, (height-yPos)-graphBuffer[x-1], x+1, (height-yPos)-graphBuffer
- );
strokeWeight(1);
}
Thanks a lot!
1