Exemple 2 d'une séance pratique (L'accéléromètre de Libelium et RXTX)

Télécharger le cours sur l'API RXTX

Cours Waspmote (en préparation)

barre

Tutoriel

barre

Programmation de la carte Libelium Waspmote

barre

barre

void setup() {

ACC.ON();
USB.begin();

}

void loop() {

byte check = ACC.check();
int x_acc = ACC.getX();
int y_acc = ACC.getY();
int z_acc = ACC.getZ();
USB.println(check, HEX);
USB.print(x_acc, DEC);
USB.print(" ");
USB.print(y_acc, DEC);
USB.print(" ");
USB.println(z_acc, DEC);
delay(50);

}

barre

 

Programme (RXTX)

barre

barre

Main.java


public class Main {

public static void main(String[] args) {

Accelerometre acc = new Accelerometre() ;

}

}

barre

Accelerometre.java

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Accelerometre implements SerialPortEventListener {

private SerialPort serialPort;
private BufferedReader input;
private JFrame jf ;
private MonPanneau mp ;
private JButton bouton ;
private String s = "" ;
private int x = 0 ;
private int y = 0 ;

public Accelerometre() {

initialize(); 
jf = new JFrame("Accéléromètre") ;
bouton = new JButton("Fermer");
bouton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
close() ;
System.exit(0);
}
});
mp = new MonPanneau();
jf.setLayout(new BorderLayout());
jf.add(bouton, BorderLayout.NORTH);
jf.add(mp, BorderLayout.CENTER);
jf.setBounds(100, 100, 800, 600);
jf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
jf.setVisible(true);

}

public void initialize() {

CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
System.out.println(currPortId.getName()); 
if (currPortId.getName().equals("/dev/cu.usbserial-A8003LVg")) {
portId = currPortId; 
break;
}
}
try {
serialPort = (SerialPort) portId.open(this.getClass().getName(), 2000);
serialPort.setSerialPortParams(38400,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream())) ;
serialPort.notifyOnDataAvailable(true);
serialPort.addEventListener(this);

} catch (Exception e) {
System.err.println(e.toString());
}



public void close() {

if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close(); 
}

}

@Override
public void serialEvent(SerialPortEvent event) {

if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
s = input.readLine() ;
s = input.readLine() ;
StringTokenizer st = new StringTokenizer(s) ;
x = Integer.parseInt(st.nextToken()) ;
y = Integer.parseInt(st.nextToken()) ;
/*System.out.println("X = "+x);
System.out.println("Y = "+y);
System.out.println("Z = "+z);
System.out.println("-----------------");*/
mp.maj(x, y); 
} catch (Exception e) {}

}

}

}

barre

MonPanneau.java

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class MonPanneau extends JPanel {

private double x ;
private double y ;
private int dx = 100 ;
private int dy = 200 ;
private int r = 150 ;
private int b = 5 ;

public MonPanneau() {

super();
this.setBackground(Color.black);

}

public void paintComponent(Graphics g) {

super.paintComponent(g); 
r = (int)( 150 * Math.cos(y));
g.setColor(Color.DARK_GRAY);
for(int i=0; i<1000; i+=50) { 
g.drawLine(0, i, 1000, i);
g.drawLine(i, 0, i, 1000);


Color c = new Color(0,150,150,r);
g.setColor(c);
g.fillOval(500, 50, 30, 30);

c = new Color(200,150,230,50);
g.setColor(c);
g.fillOval(dx, dy-r, r*2, r*2);

g.setColor(Color.DARK_GRAY);
g.drawOval(dx, dy-r, r*2, r*2);
g.setColor(Color.orange);
g.drawString("r = "+r, 500, 100);
g.drawString("alpha1 = "+x, 500, 130);
g.drawString("alpha2 = "+y, 500, 160);

g.setColor(Color.red);

int a1 = (int)((dx+r)-Math.cos(x)*r)-b ;
int b1 = (int)(dy+Math.sin(x)*r)-b ;
g.fillOval(a1, b1, b*2,b*2);
int a2 = (int)((dx+r)+Math.cos(x)*r)-b ;
int b2 = (int)(dy-Math.sin(x)*r)-b ;
g.fillOval(a2, b2, b*2,b*2);
g.drawLine(a1+b,b1+b,a2+b,b2+b);

}

public void maj(int x, int y) {

this.x = ((double)x)/752.0 ; 
this.y = ((double)y)/752.0 ;
repaint() ;

}

}

barre

Modifié le: mardi 23 octobre 2012, 00:11