Serial attach

30 Sep 2010

hello!

Every 0.02 (50Hz) I have to read a serial data. I didn't want to wait for the start of the line so I used an attach fuction to read the signal every 0.02 seconds. I did this with a GPS module and had no problems but now I haven't got a clue of what I am doing wrong. Here is the code:

#include "mbed.h"


Ticker signalrazor;
Serial pc(USBTX, USBRX); // tx, rx
Serial razor(NC,p14);

float roll, pitch, yaw; 


void razorLine() {
    while (razor.getc() != '!');
    razor.scanf("ANG:%f,%f,%f", &roll, &pitch, &yaw);  
    return;
}

void ini_razor() {  
    razor.baud(57600);
    while (razor.getc() != '!');
}

void inicializacion() {  
    pc.baud(115200);
    ini_razor(); //main loop runs at 50Hz
    wait(0.019);
    signalrazor.attach(&razorLine, 0.02);
}

int main(){
    inicializacion();
    while(1){
       wait(1);
       pc.printf("%f\r\n",roll);
    }
}
But it doesn't work. When I modify "wait(1);" from the main loop to "wait(0.02);" I get this:

2.190000
2.790000
3.250000
3.360000
3.330000
3.160000
2.930000
2.970000
3.060000
3.030000
2.670000
1.930000
0.870000
-0.040000
-0.430000
0.020000
0.200000
0.210000
0.140000
0.170000
0.160000
0.130000
At this point  the transmision stops. When I increase the "wait" number I get less and less data till I get no data at all.

Is this the best way to do this? What I am doing wrong?

Thanks!

30 Sep 2010

Your function:-

void razorLine() {
    while (razor.getc() != '!');
    razor.scanf("ANG:%f,%f,%f", &roll, &pitch, &yaw);  
    return;
}

is spending most of it's time waiting on RX characters into the Uart. However:-

signalrazor.attach(&razorLine, 0.02);

It's called in an "interrupt context". Basically your ticker will fire via a timer interrupt and call your razorLine function which then sits there waiting for characters. You need to return from your ticker function before the next ticker goes off otherwise you're going to start stacking interrupts and something will have to give. Better still, return from your interrupt called function ASAP. It's never a good idea to sit waiting (blocking) on something coming in whilst in an interrupt context.

It seems from your code that the line you expect "begins" with '!'. Do you know how it ends? "\n" perhaps? A better sollution is to store the incoming characters into a buffer and when you detect the end of the message, set a flag that your buffer needs processing with sscanf() rather than the scanf() associated with the Serial object.

 

 

 

 

 

 

02 Oct 2010

Thanks for your reply!

I am going to check this: http://mbed.org/forum/mbed/topic/181/