Read barometer and write to uart

hi,
why is this code only displaying question marks?
when i do
[color=#008080]hal.uartC->printf("%1.5f\n",(double)barometer.get_altitude());
hal.scheduler->delay(1);[/color]
then it works more or less fine…

what i want to do is reading once (in the 50hz loop) the altitude and then
send characterwise the value over uart, one per loop…
but it doesn’t work…

[color=#008080]static int i;
static char baro[8];
if (i==0){
snprintf(baro,8,"%1.5f",(double)barometer.get_altitude());
}else{
hal.uartC->write(baro[i]);
hal.scheduler->delay(1);
i++;
}
if (i >= 7){
i=0;
hal.uartC->print("\n");
}
[/color]
i can and can not find the right way to do this…

has anyone an idea?

i found out…
-no need to call barometer.get_altitude(), this is called by arducopter.pde
-baro_alt, which stores the barometer.get_altitude() value, is something like a long int
-i forgot the terminating 0
-and most important probably, XBee’s working best with 57600 baud, like in the arducopter description…

so for the 50hz user hook…

[color=#808040]#if 1
static int i;
static char baro[32];
if (i==0){
snprintf(baro,32,"%li",baro_alt);
hal.scheduler->delay(1);
baro[31] = 0;
}
if(hal.uartC->available() > 0){
hal.uartC->printf("%c",baro[i]);
hal.scheduler->delay(1);
i++;
}
if ((i >= 31 || baro[i] == 0) && hal.uartC->available() > 0){
i=0;
hal.uartC->print("\n");
hal.scheduler->delay(1);
}
#endif[/color]

code above still buggy…
better version:
-no asking for availability, then the xbee starts transmitting automatically
-only need for delay after printing to uart…

[color=#BF4040]#if 1
static int i;
static char baro[16];

if (i==0){
	snprintf(baro,16,"%li",baro_alt);
	baro[15] = 0;
}
hal.uartC->printf("%c",baro[i]);
hal.scheduler->delay(1);
i++;
if (i >= 15 || baro[i] == 0){
	i=0;
	hal.uartC->print("\n");
}

#endif[/color]

  1. include UARTDriver.h in the UserCode.pde
  2. put this after the include
/* setup UART at 57600 ---------------------------------------- */
static void setup_uart(AP_HAL::UARTDriver *uart, const char *name)
{
    if (uart == NULL)
    {
        // that UART doesn't exist on this platform
        return;
    }
    ///begin(baudrate,Rx,Tx)
    uart->begin(57600, 256, 256);
}
  1. this in the init
   /* Setup UartC ------------- */
   setup_uart(hal.uartC, "uartC");

    /* set bool variables on their default value */
static bool   b_baro         = false;
  1. this in the fastloop, proposition
   char c = 0;
   static char buf[16];
   static unsigned int i;
    while(hal.uartC->available() > 0 && c != '\n' && i < sizeof(buf)){
      c = hal.uartC->read();
      buf[i++] = c;
   }
   if(i > 0 && (buf[i-1] == '\n' || i >= sizeof(buf))){
      buf[i-1] = 0;
      i        = 0;
      if(strcmp(buf, "baro"    )==0){
         b_baro = true;
      }
  1. this in the 50Hz, proposition
   if ( b_baro ){
      hal.uartC->printf("Pressure: %li\n",
      baro_alt);
      b_baro      = false;
   }

like this you can simply communicate with a XBee over uartC. APM now reads incomming words over uart, reads the desired value and sends the value back over uart.