Jump to content
43oh

I can not use strtod() function in Energia


Recommended Posts

First of all, nobody knows your target platform (Tiva or MSP430)?

 

If you are using MSP430, I strongly advise you (I'm sure others will too) to NOT use float or doubles.

You will be very unhappy if you do. Instead used fixed point integers.

 

Perhaps someone else can advise if Tiva supports floating point.

Link to post
Share on other sites

It appears that strtol is declared in my scope but not strtod().

I've opened stdlib.h and only strtol is declared.

How to add strtod as another complie supports it.

The image shows you using mspgcc, which has very limited support for floating point. If Energia supports using TI's version of msp430-elf, try using that; newlib should have the function as long as the people building the distribution didn't disable it.

 

In general, though, @@David Bender is correct and you should avoid floating point on MSP430.

Link to post
Share on other sites

Sorry for missing information

I'm using msp430g2553.

 

So your advice is giving my project up ;(

No, we advice you to reconsider your premises. Do you really need floating point, or would fixed point or even integer values work (almost) just as good?

If for example you read times in microseconds where a unit is 1 second, then you'll have 6 digits after the decimal point.

Instead of reading that value as a double containing both seconds and microseconds, you could also read them as two integers, one containing seconds and the other microseconds.

Then if you need to do calculations with them (eg. time delta), subtract microseconds from each other, if negative, add 1 second in microseconds and subtract 1 second from your seconds-delta.

Link to post
Share on other sites

I've changed my mind. I decided to process values on my computer than transmit float numbers to MSP430. But I have no idea how to transmit float to MSP430. It doesn't work.

But if I transmit 2 bytes of integer it works fine.

Someone please show me how to receive 4 bytes of float in MSP430.

This is a piece of my code.

For int

    int x = Serial.read();

    x += Serial.read() << 8;
    int y = Serial.read();
    y += Serial.read() << 8;
 
Work fine
 
For Float:
  byte _x[4], _y[4];

  _x[0] = Serial.read();
  _x[1] = Serial.read();
  _x[2] = Serial.read();
  _x[3] = Serial.read();


  _y[0] = Serial.read();
  _y[1] = Serial.read();
  _y[2] = Serial.read();
  _y[3] = Serial.read();
    
   float x = buildUp(_x);
   float y = buildUp(_y);


float buildUp(unsigned char outbox[])
{
  unsigned long d;


  d =  (outbox[3] << 24) | (outbox[2] << 16)
    | (outbox[1] << 8) | (outbox[0]);
  float member = *(float *)&d;
  return member;
}
 
Not work
 
I really need floating point so please help.
Edited by bluehash
[ADMIN] Code formatting.
Link to post
Share on other sites

 

I've changed my mind. I decided to process values on my computer than transmit float numbers to MSP430. But I have no idea how to transmit float to MSP430. It doesn't work.

But if I transmit 2 bytes of integer it works fine.

Someone please show me how to receive 4 bytes of float in MSP430.

This is a piece of my code.

For int

    int x = Serial.read();
    x += Serial.read() << 8;
    int y = Serial.read();
    y += Serial.read() << 8;
 
Work fine
 
For Float:
  byte _x[4], _y[4];
  _x[0] = Serial.read();
  _x[1] = Serial.read();
  _x[2] = Serial.read();
  _x[3] = Serial.read();
 
  _y[0] = Serial.read();
  _y[1] = Serial.read();
  _y[2] = Serial.read();
  _y[3] = Serial.read();
    
   float x = buildUp(_x);
   float y = buildUp(_y);
 
float buildUp(unsigned char outbox[])
{
  unsigned long d;
 
  d =  (outbox[3] << 24) | (outbox[2] << 16)
    | (outbox[1] << 8) | (outbox[0]);
  float member = *(float *)&d;
  return member;
}
 
Not work
 
I really need floating point so please help.

 

There is no clean way to do this, the problem is that you must specify the order in which you pass your floating point segments over the line (this is called endianness). If you pass the bytes in the order that the MSP430 supports, you could cast the memory region to float. It still is a bad idea, why do you need/want floats, integers are much faster and compact. In addition, floats are less accurate (but more precise).

float buildUp(unsigned char outbox[])
{
  unsigned long d;
  d =  (outbox[3] << 24) | (outbox[2] << 16) // any char << 24 will be 0, it's still a char!
    | (outbox[1] << 8) | (outbox[0]);
  float member = *(float *)&d;
  return member;
}

float buildUp(unsigned char outbox[])
{
  unsigned long d;
  d = outbox[3];
  d <<= 8;
  d |= outbox[2];
  d <<= 8;
  d |= outbox[1];
  d <<= 8;
  d |= outbox[0];
  // now we first put the char in the space for the long and then shift it. STILL A BAD IDEA THOUGH!
  float member = *(float *)&d;
  return member;
}
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...