da notare
Serial.print(orizzonte.q0);
Serial.print(" ");
Serial.print(orizzonte.q1);
Serial.print(" ");
Serial.print(orizzonte.q2);
Serial.print(" ");
Serial.print(orizzonte.q3);
Serial.println();
questa parte viene ricevuta da un programma Java che disegna un parallelepipedo 3d per mostrare il comportamento dall'algoritmo IMU.
È necessaria la libreria JME 2
http://www.jmonkeyengine.org/ per la grafica 3d e rxtx per le comunicazioni seriali.
Il codice è molto basico perchè mi serviva solo come test. 4 numeri separati da " " e seguiti da "\n" vengono considerati quaternione di rotazione e applicati. Quindi se dovete scrivere dei valori per sicurezza fateli seguire da una scritta (senza andare a capo) e siete sicuri che non verranno erroneamente interpretati
main.java
package orizzonteartificiale;
import com.jme.math.Quaternion;
/**
*
* @author lesto
*/
public class Main {
public static void main(String[] args) {
GraphicsThread g = new GraphicsThread();
new Thread(g).start();
ReadSerial r = new ReadSerial();
String s;
String v[];
float a, b, c, d;
float a1=0, b1=0, c1=0, d1=0;
boolean set = false;
while (true){
s=r.pollCommands();
if (r.pollSize()>100){
System.out.println("PollSize:"+r.pollSize());
}
//System.out.println("Comando ok:"+s);
v=s.split(" ");
if (v.length==4){
//we have a quaternion
try{
a = Float.parseFloat(v[0]);
b = Float.parseFloat(v[1]);
c = Float.parseFloat(v[2]);
d = Float.parseFloat(v[3]);
g.setBox( new Quaternion(a, b, c, d) );
double roll=Math.atan2(2*(a*b+c*d), 1-2*(b*b+c*c));
double pitch=Math.asin( 2*(a*c-b*d) );
double yaw=Math.atan2(2*(a*d+b*c), 1-2*(c*c+d*d));
System.out.println("Angoli:"+roll+" "+pitch+" "+yaw);
}catch(Exception e){
System.out.println("Errore: "+e);
}
}
}
}
}
ReadSerial.java:
package orizzonteartificiale;
/**
*
* @author lesto
*/
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.concurrent.LinkedBlockingQueue;
public class ReadSerial implements Runnable{
LinkedBlockingQueue<String> comandi = new LinkedBlockingQueue<String>();
boolean finito=false;
InputStream in;
public ReadSerial() {
boolean portFound = false;
String defaultPort;
// determine the name of the serial port on several operating systems
String osname = System.getProperty("os.name", "").toLowerCase();
System.out.println(osname);
if (osname.startsWith("windows")) {
// windows
defaultPort = "COM1";
} else if (osname.startsWith("linux")) {
// linux
defaultPort = "/dev/ttyUSB0";
} else if (osname.startsWith("mac")) {
// mac
defaultPort = "????";
} else {
System.out.println("Sorry, your operating system is not supported");
return;
}
System.out.println("Set default port to " + defaultPort);
Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId;
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println("Serial found port: " + defaultPort);
if (portId.getName().equals(defaultPort)) {
System.out.println("Found port: " + defaultPort);
portFound = true;
}
}
}
if (!portFound) {
System.out.println("port " + defaultPort + " not found.");
System.exit(0);
}
try {
System.out.println("connessione");
connect(defaultPort);
System.out.println("connessione fine");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void connect(String portName) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier
.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Error: Port is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(),
2000);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
in = serialPort.getInputStream();
// OutputStream out = serialPort.getOutputStream();
Thread t= new Thread(this, "Reader");
t.start();
} else {
System.out.println("Error: Only serial ports are handled.");
}
}
}
public String pollCommands(){
try {
return comandi.take();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public void run() {
System.out.println("starting:");
String inputstring;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
while (!finito) {
try {
while (bufferedReader.ready()) {
inputstring = bufferedReader.readLine();
if (inputstring!=null){
try {
comandi.put(inputstring);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.print("input: ");
//System.out.println(inputstring);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("End");
}
public void stop(){
finito = true;
}
public int pollSize() {
return comandi.size();
}
}
HelloWorld.java ;D
package orizzonteartificiale;
import com.jme.app.SimpleGame;
import com.jme.math.Quaternion;
import com.jme.scene.shape.Box;
import com.jme.math.Vector3f;
/**
*
* @author mauro
*/
public class HelloWorld extends SimpleGame {
Box b=null;
public HelloWorld(){
b = new Box("Mybox", new Vector3f(-2, -5, -5), new Vector3f(2, 5, 5));
setConfigShowMode(ConfigShowMode.AlwaysShow);
}
protected void simpleInitGame() {
rootNode.attachChild(b); // Put it in the scene graph
}
public void setBox(float angle, Vector3f axis){
b.getLocalRotation().fromAngleAxis(angle, axis);
System.out.println("setting rotation:"+angle);
}
public void setBox(Quaternion q){
b.setLocalRotation(q);
System.out.println("setting quaternion:"+q);
}
}
GraphicsThread.java
package orizzonteartificiale;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
/**
*
* @author mauro
*/
public class GraphicsThread implements Runnable{
HelloWorld app=null;
public void run() {
app = new HelloWorld(); // Create Object
if (app!=null)
System.out.println("app is ok");
else
System.out.println("app is null creation");
app.start();
}
public void setBox(float angle, Vector3f axis){
if (app!=null)
app.setBox(angle, axis);
else
System.out.println("app is null");
}
public void setBox(Quaternion q){
if (app!=null)
app.setBox(q);
else
System.out.println("app is null");
}
}