6

Ho un piccolo problema.valori seriali multipli da arduino al trattamento?

sto passando le informazioni da un arduino attaccato accelerometro a 3 assi + 3 assi magnetometro + bussola. Questi vengono regolate per intervalli midi (0-127).

ARDUINO: Viene trasferito in una stampa seriale con un formato simile 76a45b120c23d12e23f34g

Serial.print(shiftAx); 
    Serial.print("a"); 
    Serial.print(shiftAy); 
    Serial.print("b"); 
    Serial.print(shiftAz); 
    Serial.print("c"); 
    Serial.print(shiftMx); 
    Serial.print("d"); 
    Serial.print(shiftMy); 
    Serial.print("e"); 
    Serial.print(shiftMz); 
    Serial.print("f"); 
    Serial.print(shiftHead); 
    Serial.print("g"); 

posso vedere questo funziona utilizzando il mio monitor di serie. (Tuttavia non sono sicuro da stamparlo come println a "g" o no.)

elaborazione 2:

I buffer fino a quando g nel mio void setup()

port = new Serial(this, "/dev/tty.usbmodem411", 9600); 
port.bufferUntil('g'); 

ho la funzione

void serialEvent (Serial port) 
{ 

    data = port.readStringUntil('g'); 

    AxVal = data.substring(0, data.indexOf('a')); 

    AyVal = data.substring(data.indexOf("a") + 1, data.indexOf("b")); 

    AzVal = data.substring(data.indexOf("b") + 1, data.indexOf("c")); 

    MxVal = data.substring(data.indexOf("c") + 1, data.indexOf("d")); 

    MyVal = data.substring(data.indexOf("d") + 1, data.indexOf("e")); 

    MzVal = data.substring(data.indexOf("e") + 1, data.indexOf("f")); 

    HeadVal = data.substring(data.indexOf("f") + 1, data.indexOf("g")); 

} 

iL PROBLEMA Quindi questo non funziona. Nessun testo visualizzato. (è solo un semplice riempimento(), testo())

Non vedo perché. Il problema è il mio protocollo (se posso chiamarlo così), come sto disimballaggio la corda? O qualche altro problema.

Sono molto confuso.

posso farlo funzionare con solo due valori, come in questo tutorial

note: Il ciclo principale Arduino è in ritardo (100); arduino è a 9600 baud rate

Devo allegare gli schizzi completi se necessario per aiutarmi, che estenderò i miei ringraziamenti in anticipo.

CODICE:

TRATTAMENTO

import themidibus.*; //Import the library 
import javax.sound.midi.MidiMessage; //Import the MidiMessage classes http://java.sun.com/j2se/1.5.0/docs/api/javax/sound/midi/MidiMessage.html 
import javax.sound.midi.SysexMessage; 
import javax.sound.midi.ShortMessage; 
import processing.serial.*; 

MidiBus myBus; // The MidiBus 

Serial port; 

String AxVal = " ", AyVal = " ", AzVal = " "; 
String MxVal = " ", MyVal = " ", MzVal = " "; 
String HeadVal = " "; 
String AxString="Ax",AyString = "Ay",AzString = "Az"; 
String MxString="Mx",MyString = "My",MzString = "Mz"; 
String HeadString="Heading"; 
String data = " "; 
PFont font; 


int status_byte = 0xB0; // send control change 
    int channel_byte = 0; // On channel 0 
    int first_byte; // cc number; 
    int second_byte; // value 

void setup() 
{ 
    size(1000,500); 
    port = new Serial(this, "/dev/tty.usbmodem411", 9600); 
    port.bufferUntil('g'); 
    font = loadFont("NanumBrush-48.vlw"); 
    textFont(font, 48); 

    MidiBus.list(); // List all available Midi devices on STDOUT. This will show each device's index and name. 
    myBus = new MidiBus(this, 1, 0); // Create a new MidiBus object 

} 

void draw() 
{ 
    background(0,0,0); 

    //first_byte = 1; 
    //second_byte = int(AxVal); // But with less velocity 
    //myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte); 
    fill(46, 209, 2); 
    text(AxVal, 60, 75); 
    text(AxString, 60, 125); 

    //first_byte = 2; 
    //second_byte = int(AyVal); // But with less velocity 
    //myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte); 
    fill(0, 160, 153); 
    text(AyVal, 120, 75); 
    text(AyString,120,125); 



// first_byte = 3; 
    //second_byte = int(AzVal); // But with less velocity 
    //myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte); 
    fill(0, 160, 153); 
    text(AzVal, 180, 75); 
    text(AzString,180,125); 

    /* 
    first_byte = 4; 
    second_byte = int(MxVal); // But with less velocity 
    myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte); 
    fill(0, 160, 153); 
    text(MxVal, 240, 75); 
    text(MxString,240,125); 

    first_byte = 5; 
    second_byte = int(MyVal); // But with less velocity 
    myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte); 
    fill(0, 160, 153); 
    text(MyVal, 300, 75); 
    text(MyString,300,125); 

    first_byte = 6; 
    second_byte = int(MzVal); // But with less velocity 
    myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte); 
    fill(0, 160, 153); 
    text(MzVal, 360, 75); 
    text(MzString,360,125); 

    first_byte = 7; 
    second_byte = int(HeadVal); // But with less velocity 
    myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte); 
    fill(0, 160, 153); 
    text(HeadVal, 420, 75); 
    text(HeadString,420,125); 

    */ 

} 

void serialEvent (Serial port) 
{ 

    data = port.readStringUntil('g'); 
    data = data.substring(0, data.length() - 1); 

    AxVal = data.substring(0, data.indexOf('a')); 


    AyVal = data.substring(data.indexOf("a") + 1, data.indexOf("b")); 


    AzVal = data.substring(data.indexOf("b") + 1, data.length()); 

/* 
    index = data.indexOf("c")+1; 
    MxVal = data.substring(index, data.indexOf("d")); 

    index = data.indexOf("d")+1; 
    MyVal = data.substring(index, data.indexOf("e")); 

    index = data.indexOf("e")+1; 
    MzVal = data.substring(index, data.indexOf("f")); 

    index = data.indexOf("f")+1; 
    HeadVal = data.substring(index, data.indexOf("g")); 
    */ 

} 
/* 
void serialEvent (Serial port) 
{ 

    data = port.readStringUntil('g'); 

    AxVal = data.substring(0, data.indexOf('a')); 

    AyVal = data.substring(data.indexOf("a") + 1, data.indexOf("b")); 

    AzVal = data.substring(data.indexOf("b") + 1, data.indexOf("c")); 

    MxVal = data.substring(data.indexOf("c") + 1, data.indexOf("d")); 

    MyVal = data.substring(data.indexOf("d") + 1, data.indexOf("e")); 

    MzVal = data.substring(data.indexOf("e") + 1, data.indexOf("f")); 

    HeadVal = data.substring(data.indexOf("f") + 1, data.indexOf("g")); 

} 

ARDUINO:

// Add lastvalue check 

#include <Wire.h> 
#include <LSM303DLH.h> 

LSM303DLH glove; 

//set max min magnetometer 
     int maxMx = +353, maxMy = +527, maxMz = 426; 
    int minMx = -700, minMy = -477, minMz = -561; 

     int maxA = 2019; 
    int minAx = -1043, minAy = -2048, minAz = -2048; 

     int shiftMx,shiftMy,shiftMz; 
     int shiftAx,shiftAy,shiftAz;  
     float shiftHeadTemp; 
     int shiftHead; 



void setup() 
{ 
Wire.begin(); 
    glove.enableDefault(); 
    Serial.begin(9600); 
} 

void loop() 
{ 



glove.read(); 


    shiftMx = ((glove.m.x - minMx)/(maxMx - minMx)) * 127; 
    shiftMy = ((glove.m.y - minMy)/(maxMy - minMy)) * 127; 
    shiftMz = ((glove.m.z - minMz)/(maxMz - minMz)) * 127; 

    shiftAx = ((glove.a.x - minAx)/(maxA - minAx)) * 127; 
    shiftAy = ((glove.a.y - minAy)/(maxA - minAy)) * 127; 
    shiftAz = ((glove.a.z - minAz)/(maxA - minAz)) * 127; 

    shiftHeadTemp = (glove.heading((LSM303DLH::vector){0,-1,0})); 

    shiftHead = (shiftHeadTemp/360)*127; 

    if (shiftMx < 0){shiftMx=0;} 
    if (shiftMx >127){shiftMx=127;} 
    if (shiftMy < 0){shiftMy=0;} 
    if (shiftMy >127){shiftMy=127;} 
    if (shiftMz < 0){shiftMz=0;} 
    if (shiftMz >127){shiftMz=127;} 

    if (shiftAx < 0){shiftAx=0;} 
    if (shiftAx >127){shiftAx=127;} 
    if (shiftAy < 0){shiftAy=0;} 
    if (shiftAy >127){shiftAy=127;} 
    if (shiftAz < 0){shiftAz=0;} 
    if (shiftAz >127){shiftAz=127;} 

    if (shiftHead < 0){shiftHead=0;} 
    if (shiftHead >127){shiftHead=127;} 




    Serial.print(shiftAx); 
    Serial.print("a"); 
    Serial.print(shiftAy); 
    Serial.print("b"); 
    Serial.print(shiftAz); 
    Serial.print("c"); 
    Serial.print(shiftMx); 
    Serial.print("d"); 
    Serial.print(shiftMy); 
    Serial.print("e"); 
    Serial.print(shiftMz); 
    Serial.print("f"); 
    Serial.print(shiftHead); 
    Serial.println("g"); 



    delay(100); 


} 

risposta

7

non riesco a testare il codice in questo momento, ma qui ci sono alcune idee.

È possibile inviare JSON dal proprio Arduino all'elaborazione. Ecco l'esempio che sto utilizzando in un mio progetto, ma si potrebbe facilmente adattarsi in base alle proprie esigenze:

void sendJson(){ 
    String json; 

    json = "{\"accel\":{\"x\":"; 
    json = json + getXYZ(0); 
    json = json + ",\"y\":"; 
    json = json + getXYZ(1); 
    json = json + ",\"z\":"; 
    json = json + getXYZ(2); 
    json = json + "},\"gyro\":{\"yaw\":"; 
    json = json + getYPR(0); 
    json = json + ",\"pitch\":"; 
    json = json + getYPR(1); 
    json = json + ",\"roll\":"; 
    json = json + getYPR(2); 
    json = json + "}}"; 

    Serial.println(json); 
} 

La cosa buona di JSON è che è facilmente "analizzabile" con l'elaborazione utilizzando il getType() funzione. Non più readUntil, tutto va come questo: (si noti che in void setup() è necessario modificare port.bufferUntil('g');-port.bufferUntil('\n'); perché nel Arduino, json viene inviato con Serial.println();)

void serialEvent (Serial port){ 
     String json = port.readString(); 

     shiftAx = getInt("shiftAx"); 
     shiftAy = getInt("shiftAy"); // and so on... 
} 

Si dovrà modificare il codice un po ', ma sarà molto più gestibile nel lungo periodo, ad esempio se si aggiungono più sensori.Dovrai solo cambiare il json in arduino e ottenere semplicemente il valore in elaborazione.

Un'altra cosa: è possibile utilizzare la funzione di vincolo per limitare il valore tra 0 e 127.

value = constrain(value, 0, 127); 

Spero che sia d'aiuto!

+0

Beh, questo è esattamente quello che volevo. Il modo migliore per farlo. Merci. modifica * Vorrei votare ma nessun rappresentante, qualcuno con rappresentante, dare a quest'uomo qualche rappresentante – ash

+0

Dovresti solo accettare la risposta. Il piccolo segno giusto –

0

Ho lo stesso problema in C# (il secondo parametro, in C# è la lunghezza della stringa di sottrazione). Tuttavia, ho visto che se non avessi aspettato un po 'di tempo, la stringa trasmessa non è sempre nella stessa forma: tale che: somtime, la tua stringa sarà (valore) a (valore) b (valore) c (valore) d (valore) e (valore) f (valore) g, MA altre volte saranno: (valore) a (valore) b (valore) c (valore) d (valore) e la stringa successiva sarà e (valore) f (valore) g (valore) a (valore) b (valore) e così via ... Dopo tutto, tutti i dati saranno trasmessi, ma non avrà la stessa struttura, quindi ho aggiunto un po 'di ritardo prima di ogni lettura seriale (per essere sicuro che la stringa seriale precedente sia stata letta fino al e ad ogni timpe ottengo la stringa nella stessa forma), e funziona bene per me. Spero che sia utile anche per te.