Go Down

Topic: [Solved] Problem with float from 8-bit Arduino to Due (Read 677 times) previous topic - next topic

ruizo

Apr 01, 2013, 10:32 pm Last Edit: Apr 03, 2013, 07:45 pm by ruizo Reason: 1
Hi, I´m trying to receive 3 float from an 8-bit Arduino Mini, which I know that it manage 4 bytes float, in a 12 byte array.
But I´m having troubles to convert it to Due´s float because I do not understand at all, how many bytes have a float in the Due,
(I think the float in Due it´s like an double, 8 bytes). I hope someone could confirm me that the Due´s float are 8 byte in memory and
tell me how to convert a 4 bytes float to a 8 bytes float, I´m trying to do it with "union" with no results.

Thanks and regards.

Update: I found "Sizeof" function in the reference, I will test it on the Due and I will post the results.

ruizo

#1
Apr 03, 2013, 07:44 pm Last Edit: Apr 03, 2013, 07:49 pm by ruizo Reason: 1
Finally it works  XD

sizeof() told me that float are 4 bytes long and double are 8 bytes long on Due, I didn't understood well in the reference  :smiley-roll-sweat:

Code: [Select]
//  Test code to send binary data from 9 DOF Razor IMU to Arduino Due
//
//  Programa de Prueba para comprobar que el Arduino Due recibe correctamente
//  los datos del 9 DOF Razor IMU y los procesa.
//
//  Miguel Angel Ruiz 2013

const int TOTAL_BYTES = 12 ; // the total bytes in a message
char MESSAGE [12] = {'N','N','N','N','N','N','N','N','N','N','N','N',};  //  String for testing

union y_tag {
   byte by[4];
   float yval;
} y;

union p_tag {
   byte bp[4];
   float pval;
} p;

union r_tag {
   byte br[4];
   float rval;
} r;

float yaw;
float pitch;
float roll;

void setup(){
 
 delay(3000);
 
 Serial.begin(115200);
 Serial1.begin(57600);
 
 Serial.println("Iniciando IMU");
 Serial1.print("#oe0"); // Disable Error Output
 Serial1.print("#ob");  // Binary Output Format Selected
 Serial1.print("#o0");  // Disable Continuos Data Streaming
 
 delay(3000);
 Serial.println("Arranque listo, comenzando");
 Serial.print("Mensaje de prueba -> ");
 for(int i=0; i<12; i++){
   Serial.print(MESSAGE[i], HEX);
   }
 Serial.println("");
 Serial.print("->");
 Serial1.print("#f");  // Request one frame of data
 

}// Fin de setup

void loop(){
 
 if ( Serial1.available() >= TOTAL_BYTES){
       
   
       y.by[0] = Serial1.read();
       y.by[1] = Serial1.read();
       y.by[2] = Serial1.read();
       y.by[3] = Serial1.read();
       yaw = y.yval;
       
       p.bp[0] = Serial1.read();
       p.bp[1] = Serial1.read();
       p.bp[2] = Serial1.read();
       p.bp[3] = Serial1.read();
       pitch = p.pval;
       
       r.br[0] = Serial1.read();
       r.br[1] = Serial1.read();
       r.br[2] = Serial1.read();
       r.br[3] = Serial1.read();
       roll = r.rval;
       
       Serial.print("Received IMU msg, Yaw = ");
       Serial.print(yaw, 2);
       Serial.print(", pitch = ");
       Serial.print(pitch, 2);
       Serial.print(", roll = ");
       Serial.println(roll, 2);
   
/*    for(int i=0; i<12; i++){          //  Old code for testing
      MESSAGE[i] = Serial1.read();
      Serial.print(MESSAGE[i],HEX);
      }
      Serial.println("");
      Serial1.print("#f");*/
   Serial1.print("#f");                 //  Request another frame of data
 }//
}// Fin del loop

Go Up