Go Down

Topic: Float value through Serial Port.. (Read 9 times) previous topic - next topic


Apr 06, 2008, 02:44 am Last Edit: Apr 06, 2008, 02:47 am by kBit Reason: 1
If the "OR in a byte then shift, repeat" thing didn't work, it may be an "endian" issue. In short, the order of the bytes was backwards.

Debugging: Can you set the IMU on its side (or whatever) so it will send you a 1G accelleration signal on Y axis and a 0G accelleration signal on the X axis? On the Arduino, you need to print out / send back to the PC (in ASCII) the bytes in the order they are received. You will need to hand code the proper floating point value for 0 and 1 and be able to recognise small (noise) variations.

This will give you a clue as to what order the bytes are coming in. They may be completely backwards, or just the pairs backwards. OTOH, if that is OK, then post your "oring in and shifting" code for us to look for errors.

A generic union is accessed like this:

Code: [Select]

union u_tag {
   int ival;
   float fval;
} u;

u.ival = 14;
u.fval = 31.3;

yours might look like:

Code: [Select]

float velocity;

union u_tag {
   byte b[4];
   float fval;
} u;

u.b[0] = ...
u.b[1] = ...
u.b[3] = ...
u.b[3] = ...

velocity = u.fval;

It might compile.  :P

Here is some code reading out the float value a byte at a time. It compiles and runs.  8-)

Code: [Select]

void setup()
 // prints title with ending line break
 Serial.println("Floats coming out as bytes!!");

 // wait for the long string to be sent
   float myVelocity = 0.0;

 union u_tag {
   byte b[4];
   float fval;
 } u;

void loop()
  u.fval = myVelocity;
  Serial.print("Byte 1: ");
  Serial.println(u.b[0], DEC );

  Serial.print("Byte 2: ");
  Serial.println(u.b[1], DEC );
  Serial.print("Byte 3: ");
  Serial.println(u.b[2], DEC );
  Serial.print("Byte 4: ");
  Serial.println(u.b[3], DEC );
  myVelocity  += 1.0;
 delay(100); // allow some time for the Serial data to be sent


Super thanks now is working =)... And learn something new =)

But i found a very hidden option in the IMU that send you the float variables, but as "long int", for example if you have axis in degrees originally like this: 63.0982 (float), you can change the format and receive it like this "630982", and is easier to concatenate  and easier to process for the MCU..

Thanks a lot!!!  :D



Ok with the code part - but can you please note the electronic elements you are using? Are you just connecting Vcc, Gnd, RX, TX with the UART of the Arduino or something more?


For DIY i would like to Hook-up one of these X-sens Mti devices to Arduino Mega and later on send the data wireless (almost realtime) by Digi RF.
Adding some additional info like temperature and voltage. Maybe with analog in on the X-sens sensor.
Can you tell me if the X-sens connection to the Arduino works fine and the processor is fast enough?Are there any sample codes available for arduino already? I use latest firmware 2.5.1, i would like to keep high samplerate on the sensor 57.6K and lower this to 20hz with the arduino before sending to have higher accuracy and lower duty cycle on RF

Go Up