PROCESSING + ARDUINO

Hi everyone,
can you guide for me the way to create 2 line segments (joint together) on processing.
Each segment simulate for 1 sensor as the figure below.
thanks!

rtjh.JPG

can you guide for me the way to create 2 line segments (joint together) .

I don't think the Arduino is capable of creating line segments. Processing has it's own forum (and a shitload of examples).

PaulS:

can you guide for me the way to create 2 line segments (joint together) .

I don't think the Arduino is capable of creating line segments. Processing has it's own forum (and a shitload of examples).

i also ask on processing forrum but i see they don't have enthusiasm as arduino forum.
i thinh arduino and processing be alway together in transfer data to diisplay and in this forum: someone will have many experience about processing.
So, i decide ask in this forum.
hope that someone can help me.

hope that someone can help me.

Where is your code, and what have you tried? Drawing a line is trivial.

Where is your code, and what have you tried? Drawing a line is trivial.

i haven’t writen code because i don’t know how to draw it.
i wanna send data from sensor to ‘’ segment’’ on processing to simulate it as figure i attach .
could you guide for me?

rtjh.JPG

i haven’t writen code because i don’t know how to draw it.

    line(x1, y1, x2, y2);

All you need to do is supply values for x1, y1, x2 and y2.

i wanna send data from sensor

What sensor?

i wanna joint 2 lines, not 1 line.
each line is (GY -521 + GY271)

i wanna joint 2 lines, not 1 line.

So, go right ahead. Call the function twice.

i don'n understand.

Put this in draw() and see what it does.

int x1=0;
int y1 = 0;

int x2 = 2;
int y2 = 2;

int x3 = 5;
int y3 = 2;

line(x1, y1, x2, y2);
line(x2, y2, x3, y3);

Two calls to line()!

PaulS:
Put this in draw() and see what it does.

int x1=0;

int y1 = 0;

int x2 = 2;
int y2 = 2;

int x3 = 5;
int y3 = 2;

line(x1, y1, x2, y2);
line(x2, y2, x3, y3);



Two calls to line()!

I don't know how to send data ( euler angle) from arduino to sengment on processing.
please tell me.

I don't know how to send data ( euler angle) from arduino to sengment on processing.

Then this project is not for you.

After i can read the pitch, roll, yaw from arduino code
i try to run processing code :

But it have some mistake:

error, disabling serialEvent() for //./COM4
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at processing.serial.Serial.serialEvent(Unknown Source)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
at ds.serialEvent(ds.java:105)
... 8 more

Could you guide me to repair it?

float stringKalmanX, stringKalmanY,stringKalmanZ;

This was as far as I read. If the variable IS a float, get rid of string from the name. These names look stupid, which reflects on you!

   println(Serial.list()); // Use this to print connected serial devices

What did this show? Is your Arduino really connected to the second port in the list?

float stringKalmanX, stringKalmanY,stringKalmanZ;

This was as far as I read. If the variable IS a float, get rid of string from the name. These names look stupid, which reflects on you!

It is a variable name, so it doesn't effect.. When it is: String stringKalmanX, stringKalmanY,stringKalmanZ;>>> error>> so, i change it to float but i forgot change the name of it from stringkalman to kalman

   println(Serial.list()); // Use this to print connected serial devices

What did this show? Is your Arduino really connected to the second port in the list?

here is code to draw a plane but it shows a black table.
i'm sure it is connected to COM which i setup.

import processing.serial.*;

Serial myPort;  
 float [] hq = null;
float [] kalman = new float [3]; // psi, theta, phi
 



byte[] inBuffer = new byte[22]; // this is the number of chars on each line from the Arduino (including /r/n)

PFont font;
//final int VIEW_SIZE_X = 1024, VIEW_SIZE_Y = 768;
final int VIEW_SIZE_X = 800, VIEW_SIZE_Y = 600;
 


void setup() 
{
  size(VIEW_SIZE_X, VIEW_SIZE_Y, P3D);
  textureMode(NORMAL);
  fill(255);
  stroke(color(44,48,32));
  
  
  
println(Serial.list()); // Use this to print connected serial devices
  myPort = new Serial(this, Serial.list()[1], 115200);
  myPort.bufferUntil('\n'); // Buffer until line fee d
  
  // The font must be located in the sketch's "data" directory to load successfully
  font = loadFont("CourierNew36.vlw"); 
  
  
  // Loading the textures to the cube
  // The png files alow to put the board holes so can increase  realism 
  
  
  
  
  
  delay(100);
  myPort.clear();
  myPort.write("1");
}


 float decodeFloat(String inString) {
  byte [] inData = new byte[3];
  
  if(inString.length() == 8) {
    inData[0] = (byte) unhex(inString.substring(0, 2));
    inData[1] = (byte) unhex(inString.substring(2, 4));
    inData[2] = (byte) unhex(inString.substring(4, 6));
    
  }
      
  int intbits = (inData[2] << 16) | ((inData[1] & 0xff) << 8) | ((inData[0] & 0xff) );
  return Float.intBitsToFloat(intbits);
}


void readQ() {
  if(myPort.available() >= 18) {
    String inputString = myPort.readStringUntil('\n');
    //print(inputString);
    if (inputString != null && inputString.length() > 0) {
      String [] inputStringArr = split(inputString, ",");
      if(inputStringArr.length >= 4) {
        kalman[0] = decodeFloat(inputStringArr[0]);
        kalman[1] = decodeFloat(inputStringArr[1]);
        kalman[2] = decodeFloat(inputStringArr[2]);
         
      }
    }
  }
}
void buildBoxShape() {
  //box(60, 10, 40);
  noStroke();
  beginShape(QUADS);
  
  //Z+ (to the drawing area)
  fill(#00ff00);
  vertex(-30, -5, 20);
  vertex(30, -5, 20);
  vertex(30, 5, 20);
  vertex(-30, 5, 20);
  
  //Z-
  fill(#0000ff);
  vertex(-30, -5, -20);
  vertex(30, -5, -20);
  vertex(30, 5, -20);
  vertex(-30, 5, -20);
  
  //X-
  fill(#ff0000);
  vertex(-30, -5, -20);
  vertex(-30, -5, 20);
  vertex(-30, 5, 20);
  vertex(-30, 5, -20);
  
  //X+
  fill(#ffff00);
  vertex(30, -5, -20);
  vertex(30, -5, 20);
  vertex(30, 5, 20);
  vertex(30, 5, -20);
  
  //Y-
  fill(#ff00ff);
  vertex(-30, -5, -20);
  vertex(30, -5, -20);
  vertex(30, -5, 20);
  vertex(-30, -5, 20);
  
  //Y+
  fill(#00ffff);
  vertex(-30, 5, -20);
  vertex(30, 5, -20);
  vertex(30, 5, 20);
  vertex(-30, 5, 20);
  
  endShape();
}



void drawCube() {  
  pushMatrix();
    translate(VIEW_SIZE_X/2, VIEW_SIZE_Y/2 + 50, 0);
    scale(5,5,5);
    
    // a demonstration of the following is at 
    // http://www.varesano.net/blog/fabio/ahrs-sensor-fusion-orientation-filter-3d-graphical-rotating-cube
    rotateZ(-kalman[2]);
    rotateX(-kalman[1]);
    rotateY(-kalman[0]);
    
    buildBoxShape();
    
  popMatrix();
}


void draw() {
  background(#000000);
  fill(#ffffff);
  
  readQ();
  
  if(hq != null) { // use home quaternion
    
    text(" ", 20, VIEW_SIZE_Y - 30);
  }
  else {
     
    text(" ", 20, VIEW_SIZE_Y - 30);
  }
  
  textFont(font, 20);
  textAlign(LEFT, TOP);
  
  text("kalman Angles:\nYaw (psi)  : " + degrees(kalman[0]) + "\nPitch (theta): " + degrees(kalman[1]) + "\nRoll (phi)  : " + degrees(kalman[2]), 200, 20);
 
 
  drawCube(); 
}

I modify code on internet and try apply for my project but when i move sensor > >> my processing doésn’t change.

See that commented out print statement in the readQ function. Un comment it an see if anything is being recieved from your arduino.
You. Eed to post your arduino code as well.

Grumpy_Mike:
See that commented out print statement in the readQ function. Un comment it an see if anything is being recieved from your arduino.
You. Eed to post your arduino code as well.

here is arduino code

#include <Wire.h>
#include "Kalman.h"  
#define RESTRICT_PITCH // Comment out to restrict roll to ±90deg instead - please read: http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf

Kalman kalmanX, kalmanY, kalmanZ; // Create the Kalman instances

const uint8_t MPU6050 = 0x68; // If AD0 is logic low on the PCB the address is 0x68, otherwise set this to 0x69
const uint8_t HMC5883L = 0x1E; // Address of magnetometer

/* IMU Data */
double accX, accY, accZ;
double gyroX, gyroY, gyroZ;
double magX, magY, magZ;
int16_t tempRaw;

double roll, pitch, yaw; // Roll and pitch are calculated using the accelerometer while yaw is calculated using the magnetometer

double gyroXangle, gyroYangle, gyroZangle; // Angle calculate using the gyro only
double compAngleX, compAngleY, compAngleZ; // Calculated angle using a complementary filter
double kalAngleX, kalAngleY, kalAngleZ; // Calculated angle using a Kalman filter

uint32_t timer;
uint8_t i2cData[14]; // Buffer for I2C data

// TODO: Lav zero rutine til accelerometer, magnetomer og gyroscope
#define MAG0MAX 603
#define MAG0MIN -578

#define MAG1MAX 542
#define MAG1MIN -701

#define MAG2MAX 547
#define MAG2MIN -556

float magOffset[3] = { (MAG0MAX + MAG0MIN) / 2, (MAG1MAX + MAG1MIN) / 2, (MAG2MAX + MAG2MIN) / 2 };
double magGain[3];

void setup() {
  delay(100); // Wait for sensors to get ready

  Serial.begin(115200);
  Wire.begin();
  TWBR = ((F_CPU / 400000L) - 16) / 2; // Set I2C frequency to 400kHz

  i2cData[0] = 7; // Set the sample rate to 1000Hz - 8kHz/(7+1) = 1000Hz
  i2cData[1] = 0x00; // Disable FSYNC and set 260 Hz Acc filtering, 256 Hz Gyro filtering, 8 KHz sampling
  i2cData[2] = 0x00; // Set Gyro Full Scale Range to ±250deg/s
  i2cData[3] = 0x00; // Set Accelerometer Full Scale Range to ±2g
  while (i2cWrite(MPU6050, 0x19, i2cData, 4, false)); // Write to all four registers at once
  while (i2cWrite(MPU6050, 0x6B, 0x01, true)); // PLL with X axis gyroscope reference and disable sleep mode

  while (i2cRead(MPU6050, 0x75, i2cData, 1));
  if (i2cData[0] != 0x68) { // Read "WHO_AM_I" register
    Serial.print(F("Error reading sensor"));
    while (1);
  }

  while (i2cWrite(HMC5883L, 0x02, 0x00, true)); // Configure device for continuous mode
  // TODO: Sæt sample rate
 

  delay(100); // Wait for sensors to stabilize

  /* Set Kalman and gyro starting angle */
  updateMPU6050();
  updateHMC5883L();
  updatePitchRoll();
  updateYaw();

  kalmanX.setAngle(roll); // First set roll starting angle
  gyroXangle = roll;
  compAngleX = roll;

  kalmanY.setAngle(pitch); // Then pitch
  gyroYangle = pitch;
  compAngleY = pitch;

  kalmanZ.setAngle(yaw); // And finally yaw
  gyroZangle = yaw;
  compAngleZ = yaw;

  timer = micros(); // Initialize the timer
}

void loop() {
  /* Update all the IMU values */
  updateMPU6050();
  updateHMC5883L();

  double dt = (double)(micros() - timer) / 1000000; // Calculate delta time
  timer = micros();


  /* Roll and pitch estimation */
  updatePitchRoll();
  double gyroXrate = gyroX / 131.0; // Convert to deg/s
  double gyroYrate = gyroY / 131.0; // Convert to deg/s

#ifdef RESTRICT_PITCH
  // This fixes the transition problem when the accelerometer angle jumps between -180 and 180 degrees
  if ((roll < -90 && kalAngleX > 90) || (roll > 90 && kalAngleX < -90)) {
    kalmanX.setAngle(roll);
    compAngleX = roll;
    kalAngleX = roll;
    gyroXangle = roll;
  } else
    kalAngleX = kalmanX.getAngle(roll, gyroXrate, dt); // Calculate the angle using a Kalman filter

  if (abs(kalAngleX) > 90)
    gyroYrate = -gyroYrate; // Invert rate, so it fits the restricted accelerometer reading
  kalAngleY = kalmanY.getAngle(pitch, gyroYrate, dt);
#else
  // This fixes the transition problem when the accelerometer angle jumps between -180 and 180 degrees
  if ((pitch < -90 && kalAngleY > 90) || (pitch > 90 && kalAngleY < -90)) {
    kalmanY.setAngle(pitch);
    compAngleY = pitch;
    kalAngleY = pitch;
    gyroYangle = pitch;
  } else
    kalAngleY = kalmanY.getAngle(pitch, gyroYrate, dt); // Calculate the angle using a Kalman filter

  if (abs(kalAngleY) > 90)
    gyroXrate = -gyroXrate; // Invert rate, so it fits the restricted accelerometer reading
  kalAngleX = kalmanX.getAngle(roll, gyroXrate, dt); // Calculate the angle using a Kalman filter
#endif


  /* Yaw estimation */
  updateYaw();
  double gyroZrate = gyroZ / 131.0; // Convert to deg/s
  // This fixes the transition problem when the yaw angle jumps between -180 and 180 degrees
  if ((yaw < -90 && kalAngleZ > 90) || (yaw > 90 && kalAngleZ < -90)) {
    kalmanZ.setAngle(yaw);
    compAngleZ = yaw;
    kalAngleZ = yaw;
    gyroZangle = yaw;
  } else
    kalAngleZ = kalmanZ.getAngle(yaw, gyroZrate, dt); // Calculate the angle using a Kalman filter


  /* Estimate angles using gyro only */
  gyroXangle += gyroXrate * dt; // Calculate gyro angle without any filter
  gyroYangle += gyroYrate * dt;
  gyroZangle += gyroZrate * dt;
  //gyroXangle += kalmanX.getRate() * dt; // Calculate gyro angle using the unbiased rate from the Kalman filter
  //gyroYangle += kalmanY.getRate() * dt;
  //gyroZangle += kalmanZ.getRate() * dt;

  /* Estimate angles using complimentary filter */
  compAngleX = 0.93 * (compAngleX + gyroXrate * dt) + 0.07 * roll; // Calculate the angle using a Complimentary filter
  compAngleY = 0.93 * (compAngleY + gyroYrate * dt) + 0.07 * pitch;
  compAngleZ = 0.93 * (compAngleZ + gyroZrate * dt) + 0.07 * yaw;

  // Reset the gyro angles when they has drifted too much
  if (gyroXangle < -180 || gyroXangle > 180)
    gyroXangle = kalAngleX;
  if (gyroYangle < -180 || gyroYangle > 180)
    gyroYangle = kalAngleY;
  if (gyroZangle < -180 || gyroZangle > 180)
    gyroZangle = kalAngleZ;



Serial.print(kalAngleX); Serial.print("\t");
   Serial.print(kalAngleY); Serial.print("\t");
  Serial.print(kalAngleZ); Serial.print("\t");
  

  delay(10);
}

void updateMPU6050() {
  while (i2cRead(MPU6050, 0x3B, i2cData, 14)); // Get accelerometer and gyroscope values
  accX = ((i2cData[0] << 8) | i2cData[1]);
  accY = -((i2cData[2] << 8) | i2cData[3]);
  accZ = ((i2cData[4] << 8) | i2cData[5]);
  tempRaw = (i2cData[6] << 8) | i2cData[7];
  gyroX = -(i2cData[8] << 8) | i2cData[9];
  gyroY = (i2cData[10] << 8) | i2cData[11];
  gyroZ = -(i2cData[12] << 8) | i2cData[13];
}

void updateHMC5883L() {
  while (i2cRead(HMC5883L, 0x03, i2cData, 6)); // Get magnetometer values
  magX = ((i2cData[0] << 8) | i2cData[1]);
  magZ = ((i2cData[2] << 8) | i2cData[3]);
  magY = ((i2cData[4] << 8) | i2cData[5]);
}

void updatePitchRoll() {
  
#ifdef RESTRICT_PITCH // Eq. 25 and 26
  roll = atan2(accY, accZ) * RAD_TO_DEG;
  pitch = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;
#else // Eq. 28 and 29
  roll = atan(accY / sqrt(accX * accX + accZ * accZ)) * RAD_TO_DEG;
  pitch = atan2(-accX, accZ) * RAD_TO_DEG;
#endif
}

void updateYaw() { / 
  magX *= -1; // Invert axis - this it done here, as it should be done after the calibration
  magZ *= -1;

  magX *= magGain[0];
  magY *= magGain[1];
  magZ *= magGain[2];

  magX -= magOffset[0];
  magY -= magOffset[1];
  magZ -= magOffset[2];

  double rollAngle = kalAngleX * DEG_TO_RAD;
  double pitchAngle = kalAngleY * DEG_TO_RAD;

  double Bfy = magZ * sin(rollAngle) - magY * cos(rollAngle);
  double Bfx = magX * cos(pitchAngle) + magY * sin(pitchAngle) * sin(rollAngle) + magZ * sin(pitchAngle) * cos(rollAngle);
  yaw = atan2(-Bfy, Bfx) * RAD_TO_DEG;

  yaw *= -1;
}

On the Arduino end:

Serial.print(kalAngleX); Serial.print("\t");
   Serial.print(kalAngleY); Serial.print("\t");
  Serial.print(kalAngleZ); Serial.print("\t");

On the Processing end:

  myPort.bufferUntil('\n'); // Buffer until line fee d

Since you never bother to send a carriage return or line feed, hell is going to freeze over before the serialEvent() method gets called.

can you guide me repair it?