Hi, my problem is I can’t find from where the OutOfBoundsException error below comes from. Processing and Arduino code attached.
Using Processing debugger I think the issue appears at line 81, with “try”.
java.lang.ArrayIndexOutOfBoundsException: 1
at Accel_Gyro_Magnet_Datas_to_dat.serialEvent(Accel_Gyro_Magnet_Datas_to_dat.java:122)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at processing.serial.Serial.serialEvent(Unknown Source)
at jssc.SerialPort$LinuxEventThread.run(SerialPort.java:1299)
Processing code :
/*
Saving Values from Arduino to a .csv File Using Processing - Pseduocode
This sketch provides a basic framework to read data from Arduino over the serial port and save it to .csv file on your computer.
The .csv file will be saved in the same folder as your Processing sketch.
This sketch takes advantage of Processing 2.0's built-in Table class.
This sketch assumes that values read by Arduino are separated by commas, and each Arduino reading is separated by a newline character.
Each reading will have it's own row and timestamp in the resulting csv file. This sketch will write a new file a set number of times. Each file will contain all records from the beginning of the sketch's run.
This sketch pseduo-code only. Comments will direct you to places where you should customize the code.
This is a beginning level sketch.
The hardware:
* Sensors connected to Arduino input pins
* Arduino connected to computer via USB cord
The software:
*Arduino programmer
*Processing (download the Processing software here: https://www.processing.org/download/
*Download the Software Serial library from here: http://arduino.cc/en/Reference/softwareSerial
Created 12 November 2014
By Elaine Laguerta
http://url/of/online/tutorial.cc
*/
import processing.serial.*;
Serial myPort; //creates a software serial port on which you will listen to Arduino
Table table = new Table(); //table where we will read in and store values. You can name it something more creative!
int numReadings = 50; //keeps track of how many readings you'd like to take before writing the file.
int readingCounter = 0; //counts each reading to compare to numReadings.
String val;
String fileName;
void setup()
{
printArray(Serial.list());
String portName = Serial.list()[9];
//CAUTION: your Arduino port number is probably different! Mine happened to be 1. Use a "handshake" sketch to figure out and test which port number your Arduino is talking on. A "handshake" establishes that Arduino and Processing are listening/talking on the same port.
//Here's a link to a basic handshake tutorial: https://processing.org/tutorials/overview/
myPort = new Serial(this, portName, 115200); //set up your port to listen to the serial port
//table.addColumn("Packet number"); //This column stores a unique identifier for each record. We will just count up from 0 - so your first reading will be ID 0, your second will be ID 1, etc.
//the following adds columns for time. You can also add milliseconds. See the Time/Date functions for Processing: https://www.processing.org/reference/
//table.addColumn("year");
//table.addColumn("month");
//table.addColumn("day");
//table.addColumn("hour");
//table.addColumn("minute");
//table.addColumn("second");
table.addColumn("milli");
//the following are dummy columns for each data value. Add as many columns as you have data values. Customize the names as needed. Make sure they are in the same order as the order that Arduino is sending them!
table.addColumn("Accelerometer X (m/s)");
table.addColumn("Accelerometer Y (m/s)");
table.addColumn("Accelerometer Z (m/s)");
table.addColumn("Gyroscope X (rad/s)");
table.addColumn("Gyroscope Y (rad/s)");
table.addColumn("Gyroscope Z (rad/s)");
table.addColumn("Magnetometer X (uT)");
table.addColumn("Magnetometer Y (uT)");
table.addColumn("Magnetometer Z (uT)");
while (myPort.available() > 0)
{
int inByte = myPort.read();
if (inByte == 'a');
{
println('a');
}
}
}
void serialEvent(Serial myPort)
{
val = myPort.readStringUntil('\n'); //The newline separator separates each Arduino loop. We will parse the data by each newline separator.
try
{
if (val!= null) //We have a reading! Record it.
{
//val = trim(val); //gets rid of any whitespace or Unicode nonbreakable space
println(val); //Optional, useful for debugging. If you see this, you know data is being sent. Delete if you like.
float sensorVals[] = float(split(val, '\t')); //parses the packet from Arduino and places the values into the sensorVals array. I am assuming floats. Change the data type to match the datatype coming from Arduino.
TableRow newRow = table.addRow(); //add a row for this new reading
//newRow.setFloat("Packet Number", sensorVals[0]); //record a unique identifier (the packet's number)
//record time stamp
//newRow.setInt("year", year());
//newRow.setInt("month", month());
//newRow.setInt("day", day());
//newRow.setInt("hour", hour());
//newRow.setInt("minute", minute());
//newRow.setInt("second", second());
newRow.setInt("milli", millis());
//record sensor information.
// Enregistrer les valeurs de l'IMU
newRow.setFloat("Accelerometer X (m/s)", sensorVals[0]);
newRow.setFloat("Accelerometer Y (m/s)", sensorVals[1]);
newRow.setFloat("Accelerometer Z (m/s)", sensorVals[2]);
newRow.setFloat("Gyroscope X (rad/s)", sensorVals[3]);
newRow.setFloat("Gyroscope Y (rad/s)", sensorVals[4]);
newRow.setFloat("Gyroscope Z (rad/s)", sensorVals[5]);
newRow.setFloat("Magnetometer X (uT)", sensorVals[6]);
newRow.setFloat("Magnetometer Y (uT)", sensorVals[7]);
newRow.setFloat("Magnetometer Z (uT)", sensorVals[8]);
//Keeping track of packet number
readingCounter++;
//saves the table as a csv in the same folder as the sketch once the target number of packets is reached.
if (readingCounter == numReadings)
{
//fileName = str(year()) + str(month()) + str(day()) + str(table.lastRowIndex()); //this filename is of the form year+month+day+readingCounter deleting irrelevant str(table.lastRowIndex())
fileName = str(year()) + "_" + str(month()) + "_" + str(day()) + "@" + str(hour()) + "h_" + str(minute()) + "m_" + str(second()) + "s.csv"; //this filename is of the form year_month_day@hour_minute_second.csv
saveTable(table, fileName); //Woo! save it to your computer. It is ready for all your spreadsheet dreams.
System.exit(0);
}
}
}
catch(RuntimeException e)
{
e.printStackTrace();
}
}
void draw()
{
//visualize your sensor data in real time here! In the future we hope to add some cool and useful graphic displays that can be tuned to different ranges of values.
}
Arduino code :
/*
Basic_I2C.ino
Brian R Taylor
brian.taylor@bolderflight.com
Copyright (c) 2017 Bolder Flight Systems
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "MPU9250.h"
// an MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68
// Un object MPU9250 avec le capteur MPU-9250 sensor sur le bus I2C N°0 bus avec l'addresse 0x68
MPU9250 IMU(Wire,0x68);
int status;
String message;
void setup()
{
// serial to display data
// Initialisation de la communication serial à 115200 bps :
Serial.begin(115200);
while(!Serial) {}
// start communication with IMU
// Demarrer la communication avec l'IMU
status = IMU.begin();
if (status < 0)
{
Serial.println("IMU initialization unsuccessful");
Serial.println("Check IMU wiring or try cycling power");
Serial.print("Status: ");
Serial.println(status);
while(1) {}
}
// serial comm check-up - acknowledgement routine
// Vérification de la communication serial - routine de reconnaissance
Serial.println('a'); // Envoi d'un charactere au PC
char a = 'b';
while (a !='a')
{
// Attente d'une réponse specifique du PC
a=Serial.read();
}
}
void loop()
{
// read the sensor
// Lire le capteur
IMU.readSensor();
// concatenate values in a single string
// Concatener les valeurs dans une chaine de caracteres
message = String(IMU.getAccelX_mss(),6) + "\t" + String(IMU.getAccelY_mss(),6) + "\t" + String(IMU.getAccelZ_mss(),6) + "\t" +
String(IMU.getGyroX_rads(),6) + "\t" + String(IMU.getGyroY_rads(),6) + "\t" + String(IMU.getGyroZ_rads(),6) + "\t" +
String(IMU.getMagX_uT(),6) + "\t" + String(IMU.getMagY_uT(),6) + "\t" + String(IMU.getMagZ_uT(),6);
// display the data
// Envoyer les donnees
Serial.println(message);
// wait 1 milisecond before next loop
// Attendre 1 milliseconde avant la prochaine boucle
delay(1);
}