Snippet Verwendung zweier Arduino 33 nano IoT und Java/Processing am Laptop
(EN google-translate)
(PL google-translate)
Für das nachfolgend beschriebene Experiment verwendete Programme:
WiFi_IMU_Proc005_DOPPEL.zip -- verwendetes Processing-Projekt.
WiFi_IMU_Ardu001b.zip -- Programm für ersten Arduino, port=2390.
WiFi_IMU_Ardu001b_y_2391.zip -- Programm für zweiten Arduino, port=2391.
|
Processing-seitige Sicht:
int port = 2390; // the destination port
int port2 = 2391; // the destination port
double alpha=0.0;
double beta=0.0;
double alpha2=0.0;
double beta2=0.0;
int TICK=0;
void draw()
{
if(TICK%2==0)
udp.send( message, ip, port );
else
udp.send( message, ip, port2 );
TICK++;
...
Code 0-1: Abwechselnde Anfrage bei verschiedenen Arduinos durch Wechsel des Ports.
void receive( byte[] data, String ip, int port )
{
data = subset(data, 0, data.length-2);
String message = new String( data );
//println( "receive: \""+message+"\" from "+ip+" on port "+port );
if(port==2390 && message.length()>=4*6)
{
...
werte[i]=(double)x;
...
}
else if(port==2391 && message.length()>=4*6)
{
...
werte2[i]=(double)x;
...
}
...
Code 0-2: Unterschiedliche Verarbeitung, je nach Port, auf dem empfangen wird.
Arduino-seitige Sicht:
|
unsigned int localPort = 2391; // local port to listen on
Code 0-3: Einzige Änderung in Zeile 30 bei Programmvariante.
Bild 0-1: Processing-Fenster: Links untereinander die Visualisierung der beiden vom ersten Sensor rekonstruierten Winkel, rechts die beiden Winkel vom zweiten Sensor.
Bild 0-2: Testanordnung mit zwei aktiven Arduino 33 nano IoT, die gleichzeitig mit dem Processing-Programm kommunizieren.
Modularisierung
|
WiFi_IMU_Proc006_MODULAR_OOP.zip -- Modularisierte Variante
import hypermedia.net.*;
public class DoppelIMU
{
UDP udp; // define the UDP object
double[] werte = {0.0,0.0,0.0,0.0,0.0,0.0};
double[] werte2 = {0.0,0.0,0.0,0.0,0.0,0.0};
String message = "x;
"; // the message to send
String ip = "10.42.0.255"; // Broadcastadresse nutzen. So keine Anpassungen nötig.
int port = 2390; // the destination port
int port2 = 2391; // the destination port
double alpha=0.0;
double beta=0.0;
double alpha2=0.0;
double beta2=0.0;
int TICK=0;
public DoppelIMU(String ip, int port, int port2)
{
this.ip = ip;
this.port = port;
this.port2 = port2;
udp = new UDP( this, 6000 );
udp.listen( true );
}
public double getAlpha() {return alpha;}
public double getBeta() {return beta;}
public double getAlpha2() {return alpha2;}
public double getBeta2() {return beta2;}
public void update()
{
if(TICK%2==0)
udp.send( message, ip, port );
else
udp.send( message, ip, port2 );
TICK++;
//Berechnung von alpha und beta:
double x = werte[0];
double y = werte[1];
double z = werte[2];
double x2 = werte2[0];
double y2 = werte2[1];
double z2 = werte2[2];
if(Math.sqrt(x*x+z*z)>0.0)
{
double alpha_neu = Math.acos(z/Math.sqrt(x*x+z*z));
if(x<0.0) alpha_neu = -alpha_neu;
alpha = alpha_neu*180.0/Math.PI;
}
if(Math.sqrt(x2*x2+z2*z2)>0.0)
{
double alpha_neu2 = Math.acos(z2/Math.sqrt(x2*x2+z2*z2));
if(x2<0.0) alpha_neu2 = -alpha_neu2;
alpha2 = alpha_neu2*180.0/Math.PI;
}
if(Math.sqrt(z*z+y*y)>0.0)
{
double beta_neu = Math.acos(z/Math.sqrt(z*z+y*y));
if(y<0.0) beta_neu = -beta_neu;
beta = beta_neu*180.0/Math.PI;
}
if(Math.sqrt(z2*z2+y2*y2)>0.0)
{
double beta_neu2 = Math.acos(z2/Math.sqrt(z2*z2+y2*y2));
if(y2<0.0) beta_neu2 = -beta_neu2;
beta2 = beta_neu2*180.0/Math.PI;
}
}
public void receive( byte[] data, String ip, int port )
{
data = subset(data, 0, data.length-2);
String message = new String( data );
//println( "receive: \""+message+"\" from "+ip+" on port "+port );
if(port==2390 && message.length()>=4*6)
{
for(int i=0;i<werte.length;i++)
{
int x = (int)message.charAt(i*4+1)-(int)'0';
x*=10;
x += (int)message.charAt(i*4+2)-(int)'0';
x*=10;
x += (int)message.charAt(i*4+3)-(int)'0';
if(message.charAt(i*4+0)=='-')
x=-x;
werte[i]=(double)x;
}
}
else if(port==2391 && message.length()>=4*6)
{
for(int i=0;i<werte.length;i++)
{
int x = (int)message.charAt(i*4+1)-(int)'0';
x*=10;
x += (int)message.charAt(i*4+2)-(int)'0';
x*=10;
x += (int)message.charAt(i*4+3)-(int)'0';
if(message.charAt(i*4+0)=='-')
x=-x;
werte2[i]=(double)x;
}
}
}//void receive( byte[] data, String ip, int port )
}//public class DoppelIMU
Code 0-4: Klasse zur Bereitstellung der Winkelpaare.