Pages: [1]   Go Down
Author Topic: Float value through Serial Port..  (Read 6825 times)
0 Members and 1 Guest are viewing this topic.
Baja California and California
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Tururu!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, i'm decoding an IMU (Xsens), which send me float values (in 4 bytes) thru the UART port.

Ok im receiving all the bytes correctly, but how i can concatenate this 4 bytes in a float variable?

I tried in the traditional mode ( variable << 8 , etc, etc), but i can't, well arduino don't let me  smiley-razz ...

Please help me... Thanks =)

BTW the floats are IEEE 754 standard...
« Last Edit: April 03, 2008, 12:17:28 pm by jordi » Logged

New Zealand
Offline Offline
God Member
*****
Karma: 0
Posts: 999
Arduino pebbles
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Ok im receiving all the bytes correctly, but how i can concatenate this 4 bytes in a float variable?

I tried in the traditional mode ( variable << 8 , etc, etc), but i can't, well arduino don't let me  smiley-razz ...
I think there's a couple of ways you could approach this...

The first is to use a data type known as union, which I think would work something a little like this (type-as-I-think,-probably-won't-run-code :-)):

Code:
union {
byte asBytes[4];
float asFloat;
} foo;

So, you'd fill the byte array using the bytes you get (e.g. foo.asBytes[0] = 0x00) and then read the value as float (e.g. foo.asFloat)--however, this requires the format of the float to be the same as what the computer sends.

Alternatively you could try casting (a pointer?) to the float as a byte array and accessing the bytes individually.

I'm a little hazy on the exact details and it's possible the actual result may be "undefined" in certain situations, but hopefully it's a pointer in some direction... :-)

--Phil.

Logged

Baja California and California
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Tururu!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

mmmh.. ok.......  :-? ????
Thanks for the answer but at first view i'm very confused about that function.. Now i have two questions hehe  smiley-grin ..

Another easy way, or an example of the code above? please...  :smiley
Logged

Baja California and California
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Tururu!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok.. ok... about the "union" function i need any library to do this???
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Ok.. ok... about the "union" function i need any library to do this???

A union is not a fuction .... it is a union  smiley

Look up the union construct in C++ (which is really what we are using on the Arduino), for example http://www.cplusplus.com/doc/tutorial/other_data_types.html

A union is a structure where the members share the same location in memory. So we can access it as different data types. In this case assign as bytes and read as float.  smiley
Logged

Central Florida, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
union u_tag {
    int ival;
    float fval;
} u;

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

yours might look like:

Code:
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.  smiley-razz

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

Code:
void setup()
{
  Serial.begin(9600);
  
  // prints title with ending line break
  Serial.println("Floats coming out as bytes!!");
 
  // wait for the long string to be sent
  delay(100);
}
  
    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 );
  
   Serial.println();
  
   myVelocity  += 1.0;
  
  delay(100); // allow some time for the Serial data to be sent
}

« Last Edit: April 05, 2008, 07:47:03 pm by kBit » Logged

Baja California and California
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Tururu!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!!!  smiley-grin
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Jordi...


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?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: