/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Comunicacao;
import Interface.AcompanhamentoSessao;
import java.beans.PropertyChangeSupport;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.tinyos.message.Message;
import net.tinyos.message.MessageListener;
import net.tinyos.message.MoteIF;
import net.tinyos.packet.BuildSource;
import net.tinyos.packet.PhoenixError;
import net.tinyos.packet.PhoenixSource;
import net.tinyos.util.PrintStreamMessenger;
/**
*
* @author Renan
*/
public class ThreadReceptoraTinyOS extends PropertyChangeSupport implements MessageListener, PhoenixError{
private static final String propertyChangeName = "threadReceptora";
// private InfoSessao infoSessao;
private BufferedWriter logger;
private PhoenixSource ps = null;
private Thread thread = null;
private MoteIF mote;
private int version = 0;
private boolean magEnabled = true;
public ThreadReceptoraTinyOS (/*InfoSessao infoSessao, File log,*/ Object listener)
{
super(listener);
// this.log = log;
}
/* Main entry point */
// public void run() {
// System.out.println("ThreadReceptoraTinyOS - run");
// mote = new MoteIF(PrintStreamMessenger.out);
// startListening(log);
// }
public boolean run(File log) {
return run(log, "serial@com9:telosb", -1);
}
public boolean run(File log, String comm, int intervalo) {
System.out.println("ThreadReceptoraTinyOS - run");
try{
ps = BuildSource.makePhoenix(comm, PrintStreamMessenger.err);
ps.setPacketErrorHandler(this);
thread = Thread.currentThread();
mote = new MoteIF(ps);
}catch(Exception ex) {
mote = null;
ex.printStackTrace();
return false;
}
return true;
// if (!thread.isInterrupted())
// startListening(log, intervalo);
}
public boolean run(File log, String comm, int intervalo, boolean magEnabled) {
this.magEnabled = magEnabled;
return run(log, comm, intervalo);
}
private void sendIntervalo(int intervalo){
try {
if (intervalo > 0 && mote != null){
MagAccelShimmerMsg msg = new MagAccelShimmerMsg();
msg.set_interval(intervalo);
version++;
msg.set_version(version);
mote.send(MoteIF.TOS_BCAST_ADDR, msg);
}
} catch (IOException ex) {
Logger.getLogger(ThreadReceptoraTinyOS.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public /*synchronized*/ void messageReceived(int dest_addr, Message msg) {
//TODO "engasgando" será que é aqui?
if (msg instanceof MagAccelShimmerMsg) {
MagAccelShimmerMsg omsg = (MagAccelShimmerMsg)msg;
String logLine = "timestamp:" + System.currentTimeMillis() + ";id:" + omsg.get_id() + ";eixo0:" + omsg.getElement_acc_readings(0, 0) + ";eixo1:" + omsg.getElement_acc_readings(1, 0) + ";eixo2:" + omsg.getElement_acc_readings(2, 0);
try {
if (logger!=null){
logger.write(logLine);
logger.newLine();
}
} catch (IOException ex) {
ex.printStackTrace();
System.exit(-1);
}
/*
System.out.println("ThreadReceptoraTinyOS mag id:" + omsg.get_id() +
" Eixo0: " + omsg.getElement_mag_readings(0, 2) +
" Eixo1: " + omsg.getElement_mag_readings(1, 2) +
" Eixo2: " + omsg.getElement_mag_readings(2, 2));
System.out.println("ThreadReceptoraTinyOS mag id:" + omsg.get_id() +
" Eixo0: " + omsg.getElement_mag_readings(0, 2) +
" Eixo1: " + omsg.getElement_mag_readings(1, 2) +
" Eixo2: " + omsg.getElement_mag_readings(2, 2));
*/
/*Posições dos vetores:
* Eixo x: 1
* Eixo y: 0
* Eixo z: 2
*/
double[] vetorGravidade = new double[] {
(double) omsg.getElement_acc_readings(1, 0),
(double) omsg.getElement_acc_readings(0, 0),
(double) omsg.getElement_acc_readings(2, 0)
};
/*Posições dos vetores (magnetometro:
* Eixo x: 1
* Eixo y: 0
* Eixo z: 2
*/
double[] vetorMagnetico = new double[] {//102, 012, 120
(double) omsg.getElement_mag_readings(1, 0),
(double) omsg.getElement_mag_readings(0, 0),
(double) omsg.getElement_mag_readings(2, 0)
};
ajusta_offset_acc(omsg.get_id(), vetorGravidade);
if (magEnabled)
ajusta_offset_mag(omsg.get_id(), vetorMagnetico);
else
vetorMagnetico = new double[] {1, 0, 0};
//Força valores do acelerometro/magnetometro para testes
// vetorGravidade = new double[] {0, 0, -1};
// vetorMagnetico = new double[] {1, 0, 0};
// if (omsg.get_id() == 2){
// vetorGravidade = new double[] {643, 43, 725};
// vetorMagnetico = new double[] {-45, 108, -36};
// }
// if (omsg.get_id() == 1){
// vetorGravidade = new double[] {441, 53, 809};
// vetorMagnetico = new double[] {72, 115, 164};
// }
System.out.println("ThreadReceptoraTinyOS mag id:" + omsg.get_id() +
String.format("mag: Eixo0: %+04.2f Eixo1: %+04.2f Eixo2: %+04.2f norma: %+04.2f",
vetorMagnetico[0],
vetorMagnetico[1],
vetorMagnetico[2],
(new Interface.Vetor(vetorMagnetico).norma()) )
+
String.format("acc: Eixo0: %+04.2f Eixo1: %+04.2f Eixo2: %+04.2f norma: %+04.2f",
vetorGravidade[0],
vetorGravidade[1],
vetorGravidade[2],
(new Interface.Vetor(vetorGravidade).norma()) ));
//TODO frequencia cardiaca
firePropertyChange(propertyChangeName, null, new InfoSensor(omsg.get_id(), vetorGravidade, vetorMagnetico));
} else {
}
}
private void ajusta_offset_acc(int id, double[] acc){
switch (id){
case 6:
acc[0] -= (1950.0 + 160);
acc[0] *= 0.85;
acc[1] -= (2140 - 170);
acc[1] *= 0.85;
acc[2] -= (1760);
acc[2] *= 0.9;
break;
case 5:
acc[0] *= 0.7;
acc[0] -= (1860.0 - 350);
acc[1] *= 0.7;
acc[1] -= (2040.0 - 660);
acc[2] *= 0.9;
acc[2] -= (2100.0 - 420);
break;
case 4:
acc[0] -= 1950;
acc[1] -= 2100;
acc[2] -= 1903;
break;
case 3:
acc[0] -= 1950;
acc[1] -= 2150;
acc[2] -= 1800;
break;
case 2:
// acc[2] -= (2850.4830508474574 + 1002.7902097902098)/2;
// acc[2] /= (2850.4830508474574 - 1002.7902097902098)/2;
//
// acc[1] -= (3124.5132743362833 + 1171.7087378640776)/2;
// acc[1] /= (3124.5132743362833 - 1171.7087378640776)/2;
//
// acc[0] -= (3036.3274336283184 + 1070.4907407407406)/2;
// acc[0] /= (3036.3274336283184 - 1070.4907407407406)/2;
acc[2] -= 1780;
acc[0] -= 2155;
acc[1] -= 1995;
break;
case 1:
// acc[2] -= (2732.246376811594 + 842.8428571428572)/2;
// acc[2] /= (2732.246376811594 - 842.8428571428572)/2;
// acc[1] -= (3123.4473684210525 + 1169.4285714285713)/2;
// acc[1] /= (3123.4473684210525 - 1169.4285714285713)/2;
// acc[0] -= (2966.431818181818 + 1002.9798994974874)/2;
// acc[0] /= (2966.431818181818 - 1002.9798994974874)/2;
acc[2] -= 1920;
acc[0] -= 2165;
acc[1] -= 2070;
break;
default:
acc[2] -= (2732.246376811594 + 842.8428571428572)/2;
acc[2] /= (2732.246376811594 - 842.8428571428572)/2;
acc[1] -= (3123.4473684210525 + 1169.4285714285713)/2;
acc[1] /= (3123.4473684210525 - 1169.4285714285713)/2;
acc[0] -= (2966.431818181818 + 1002.9798994974874)/2;
acc[0] /= (2966.431818181818 - 1002.9798994974874)/2;
break;
}
}
private void ajusta_offset_mag(int id, double[] mag){
switch (id){
case 1:
// mag[2] -= (1715.0 + 2153.0)/2;
// mag[1] -= (1835.0 + 2250.0)/2;
// mag[0] -= (1693.0 + 2129.0)/2;
// mag[0] -= (2190.0 + 1656.0)/2; // 1923
// mag[1] -= (2303.0 + 1775.0)/2; // 2039
// mag[2] -= (2196.0 + 1664.0)/2; // 1930
mag[0] -= 1962.0;
mag[1] -= 1984.0;
mag[2] -= 1806.0;
// mag[0] -= 2000;
// mag[1] -= 2000;
// mag[2] -= 2000;
break;
case 2:
// mag[2] -= (1894.0 + 2275.0)/2;
// mag[1] -= (1896.0 + 2277.0)/2;
// mag[0] -= (1804.0 + 2186.0)/2;
// mag[0] -= (2281.0 + 1712.0)/2;
// mag[1] -= (2370.0 + 1804.0)/2;
// mag[2] -= (2359.0 + 1793.0)/2;
// mag[0] -= (2267.0 + 1726.0)/2; // 1996.5
// mag[1] -= (2363.0 + 1825.0)/2; // 2094
// mag[2] -= (2354.0 + 1817.0)/2; // 2085.5
mag[0] -= 2131.0;
mag[1] -= 2066.0;
mag[2] -= 2164.0;
break;
case 3:
mag[0] -= 1990;
mag[1] -= 2090;
mag[2] -= 2035;
break;
case 4:
mag[0] -= 2060;
mag[1] -= 2130;
mag[2] -= 2000+200;
break;
case 5:
mag[0] -= (2257.0 + 1890.0)/2;
mag[1] -= (2361.0 + 2001.0)/2;
mag[2] -= (2224.0 + 1883.0)/2;
break;
case 6:
mag[0] -= 2080;
mag[1] -= 2180;
mag[2] -= 2030;
break;
default:
mag[0] -= 2000;
mag[1] -= 2000;
mag[2] -= 2000;
break;
}
}
void printVetor(String titulo, double[] vetor){
System.out.println(titulo);
for(int i = 0; i < 3; i++)
System.out.print(i + " - " + vetor[i] + " ");
}
public String getPropertyChangeName() {
return propertyChangeName;
}
public synchronized void stopListening(){
if (mote != null) mote.deregisterListener(new MagAccelShimmerMsg(), this);
flushLog();
}
public synchronized void startListening(File log, int intervalo, boolean magEnabled){
this.magEnabled = magEnabled;
if (mote != null){
mote.registerListener(new MagAccelShimmerMsg(), this);
sendIntervalo(intervalo);
try {
logger = new BufferedWriter(new FileWriter(log));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private void flushLog() {
if (logger != null)
try {
logger.flush();
logger.close();
} catch (IOException ex) {
Logger.getLogger(ThreadReceptoraTinyOS.class.getName()).log(Level.SEVERE, null, ex);
}
logger = null;
}
@Override
public synchronized void error(IOException ioe) {
// ioe.printStackTrace();
mote = null;
ps.shutdown();
thread.interrupt();
for (Object o : getPropertyChangeListeners()){
if (o instanceof AcompanhamentoSessao){
((AcompanhamentoSessao)o).serialError();
break;
}
}
}
}