Author Topic: Energia - Float to string?  (Read 14541 times)

0 Members and 1 Guest are viewing this topic.

Offline Jon86Topic starter

  • Frequent Contributor
  • **
  • Posts: 526
  • Country: gb
Energia - Float to string?
« on: November 01, 2013, 10:45:15 pm »
My project's come to a halt now. I'm trying to do something very simple, take the output of a temperature sensor and write it to an SD Card. I've got all the code working, 100 sample averaging and I can write strings to the card just fine, but I can't convert the float average temperature value to a string needed by the card library...

Also, I'm using the Energia IDE and an MSP430G2553.
Death, taxes and diode losses.
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 27580
  • Country: nl
    • NCT Developments
Re: Energia - Float to string?
« Reply #1 on: November 01, 2013, 10:48:49 pm »
Use snprintf

float value
char buf[20]
snprintf(buf, sizeof(buf), "%f", value);

After this buf will have a string representing the value.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline Jon86Topic starter

  • Frequent Contributor
  • **
  • Posts: 526
  • Country: gb
Re: Energia - Float to string?
« Reply #2 on: November 01, 2013, 10:59:46 pm »
No luck, I just get      %f ó ÒР tÀžÃtÀ      on the SD Card...

Code: [Select]
        float value = 21.54;
        char buf[20];
        snprintf(buf, sizeof(buf), "%f", value);
rc = FatFs.write(buf, 20, &bw);
Death, taxes and diode losses.
 

Offline Jon86Topic starter

  • Frequent Contributor
  • **
  • Posts: 526
  • Country: gb
Re: Energia - Float to string?
« Reply #3 on: November 01, 2013, 11:37:20 pm »
If I swap out 'buf' in the write statement to "21.54" it works fine and writes to the card, but if I use 21.54 it gives me an incompatibility error, and with buf it seems to just write out whatever's in the format section of snprintf.
Death, taxes and diode losses.
 

Offline tanstaafl

  • Contributor
  • Posts: 33
  • Country: gb
Re: Energia - Float to string?
« Reply #4 on: November 01, 2013, 11:46:02 pm »
If I swap out 'buf' in the write statement to "21.54" it works fine and writes to the card, but if I use 21.54 it gives me an incompatibility error, and with buf it seems to just write out whatever's in the format section of snprintf.

That it writes out the format section tells me that your compiler isn't set to using full printf support, that is usually switched off when building for microcontrollers since it takes quite a lot of space to support floats...
 

Offline Jon86Topic starter

  • Frequent Contributor
  • **
  • Posts: 526
  • Country: gb
Re: Energia - Float to string?
« Reply #5 on: November 01, 2013, 11:47:51 pm »
If I swap out 'buf' in the write statement to "21.54" it works fine and writes to the card, but if I use 21.54 it gives me an incompatibility error, and with buf it seems to just write out whatever's in the format section of snprintf.

I was suggesting replacing value (not buf) with "21.54" (with quotes).
snprintf(buf, sizeof(buf), "%f", "21.54");          <-- i expect the compiler to sort out the correct parm addressing format.
and then trying
snprintf(buf, sizeof(buf), "%f", *value);            <--this is just a guess of sorts

Ah sorry I misread your last post, I'm incredibly tired...
First one does exactly the same thing as before, and the second throws me a compiler error.
Death, taxes and diode losses.
 

Offline Jon86Topic starter

  • Frequent Contributor
  • **
  • Posts: 526
  • Country: gb
Re: Energia - Float to string?
« Reply #6 on: November 01, 2013, 11:48:37 pm »
If I swap out 'buf' in the write statement to "21.54" it works fine and writes to the card, but if I use 21.54 it gives me an incompatibility error, and with buf it seems to just write out whatever's in the format section of snprintf.

That it writes out the format section tells me that your compiler isn't set to using full printf support, that is usually switched off when building for microcontrollers since it takes quite a lot of space to support floats...

Aha! Is there any way to turn it on in Energia?  :-//
Death, taxes and diode losses.
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 27580
  • Country: nl
    • NCT Developments
Re: Energia - Float to string?
« Reply #7 on: November 01, 2013, 11:55:53 pm »
If I swap out 'buf' in the write statement to "21.54" it works fine and writes to the card, but if I use 21.54 it gives me an incompatibility error, and with buf it seems to just write out whatever's in the format section of snprintf.

I was suggesting replacing value (not buf) with "21.54" (with quotes).
snprintf(buf, sizeof(buf), "%f", "21.54");          <-- i expect the compiler to sort out the correct parm addressing format.
and then trying
snprintf(buf, sizeof(buf), "%f", *value);            <--this is just a guess of sorts
Both completely wrong!

snprintf(buf, sizeof(buf), "%f", value); must work otherwise there are other (bigger problems).

@Jon86:
Put a line which says
buf[0]=0;
before the snprintf. This initialises buf to a zero length string. If you get no output then the snprintf is somehow ignored. Also change FatFs.write(buf, 20, &bw) to FatFs.write(buf, strlen(buf), &bw). Please get familiar with how C strings work!

It could be possible that the C library doesn't support floating point. Perhaps its best to post this question on the energia forum.
« Last Edit: November 01, 2013, 11:57:27 pm by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 12251
  • Country: us
Re: Energia - Float to string?
« Reply #8 on: November 02, 2013, 12:01:29 am »
If you are doing simple arithmetic on a microcontroller it may not be appropriate to bring in the complexity of floating point arithmetic and floating point library support.

You could consider using integers and fixed point arithmetic instead, and then conversion to a string would be much easier. You could use itoa() and some simple character processing to put the decimal point in the right place.
 

Offline Jon86Topic starter

  • Frequent Contributor
  • **
  • Posts: 526
  • Country: gb
Re: Energia - Float to string?
« Reply #9 on: November 02, 2013, 12:06:28 am »
Don't be too hard on me, I don't do much programming ;)
Anyway, now it just outputs %f
I've posted in the SD Card thread over on the Energia forums, but no reply yet. I might just make a thread tomorrow...
Death, taxes and diode losses.
 

Offline Jon86Topic starter

  • Frequent Contributor
  • **
  • Posts: 526
  • Country: gb
Re: Energia - Float to string?
« Reply #10 on: November 02, 2013, 12:08:03 am »
If you are doing simple arithmetic on a microcontroller it may not be appropriate to bring in the complexity of floating point arithmetic and floating point library support.

You could consider using integers and fixed point arithmetic instead, and then conversion to a string would be much easier. You could use itoa() and some simple character processing to put the decimal point in the right place.

That sounds like a good path to take. I've already had a look around but I can't really find anything on doing it that way, have you got any pointers?
Death, taxes and diode losses.
 

Offline tanstaafl

  • Contributor
  • Posts: 33
  • Country: gb
Re: Energia - Float to string?
« Reply #11 on: November 02, 2013, 12:09:04 am »
No idea, never used it...

For CCStudio it is a command line option: http://processors.wiki.ti.com/index.php/Printf_support_in_compiler. Could also be that you need to link to a different library.

Another possibility would be to not use floats at all, something like
http://forum.43oh.com/topic/1954-using-the-internal-temperature-sensor/
 

Offline Jon86Topic starter

  • Frequent Contributor
  • **
  • Posts: 526
  • Country: gb
Re: Energia - Float to string?
« Reply #12 on: November 02, 2013, 12:15:26 am »
No idea, never used it...

For CCStudio it is a command line option: http://processors.wiki.ti.com/index.php/Printf_support_in_compiler. Could also be that you need to link to a different library.

Another possibility would be to not use floats at all, something like
http://forum.43oh.com/topic/1954-using-the-internal-temperature-sensor/

Thanks, I'll look into that.
I'll try it without floats, it's only a bit of simple maths so I guess it shouldn't be too complicated. All that C code is making no sense to me though...
Death, taxes and diode losses.
 

Offline tanstaafl

  • Contributor
  • Posts: 33
  • Country: gb
 

Offline Jon86Topic starter

  • Frequent Contributor
  • **
  • Posts: 526
  • Country: gb
Re: Energia - Float to string?
« Reply #14 on: November 02, 2013, 12:31:37 am »
Now that makes a bit more sense  ;D
Thanks for your help, I'm going to get a couple of hours sleep then have another go at this tomorrow. I'll get back to you if it doesn't work, I'm sure it won't, Murphy's out to get me! ;)
Death, taxes and diode losses.
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4263
  • Country: us
Re: Energia - Float to string?
« Reply #15 on: November 02, 2013, 01:16:41 am »
Quote
snprintf(buf, sizeof(buf), "%f", value); must work
It's very common for small microcontroller environments to provide a version of printf/etc that does NOT include the floating point functions; this results in much smaller program sizes, especially if floating point is not used elsewhere.  There's usually a simple link option to get the full-featured printf, but simplified IDEs  like Arduino/Energia make that hard to modify.

You could do something like:
Code: [Select]
print_float(float f) {
   int int_part, fraction;
   int_part = (int) f;
   fraction = (f - int_part) * 1000.0;   // three digits after decimal.
   printf("%d.%03d", int_part, fraction);
}
 

Offline Jon86Topic starter

  • Frequent Contributor
  • **
  • Posts: 526
  • Country: gb
Re: Energia - Float to string?
« Reply #16 on: November 02, 2013, 08:32:53 am »
IT WORKS! (I think)
Ideal, thanks westfw :)
Death, taxes and diode losses.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Energia - Float to string?
« Reply #17 on: November 03, 2013, 01:25:35 am »
Using printf takes up lots of space / time.

I would use something like itoa() or a specialized version of it.
================================
https://dannyelectronics.wordpress.com/
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 27580
  • Country: nl
    • NCT Developments
Re: Energia - Float to string?
« Reply #18 on: November 03, 2013, 07:31:24 am »
That is not a good idea. You'll want some formatting so sooner or later you'll pull in vsprintf and in the meantime wasted lots of time of coming up with non-standard solutions which makes the software much harder to maintain. I've seen it happen and go wrong many times. The best way is to stick with a Posix compliant C environment (use the standard C library). In other words: if printf from the standard C library is too big, make your own printf which supports the bare minimum. Its not that hard: https://github.com/mludvig/mini-printf/blob/master/mini-printf.c
« Last Edit: November 03, 2013, 07:36:35 am by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline Jon86Topic starter

  • Frequent Contributor
  • **
  • Posts: 526
  • Country: gb
Re: Energia - Float to string?
« Reply #19 on: November 03, 2013, 08:21:57 am »
I'm sure you're right, but this is just a (kind of) simple project I want to do quickly and forget about, I'm moving to a different IDE after this so I guess I'll learn how to do all this properly when I do that. I expect this will be easy in code composer studio :)
Thanks for the help  :-+
Death, taxes and diode losses.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Energia - Float to string?
« Reply #20 on: November 03, 2013, 08:58:03 pm »
Quote
IT WORKS! (I think)

It will fail if the float type is negative.

But the general idea is correct: decompose the float type into an integer + fraction and then process each individual (preferably with itoa()) to minimize foot print.

Quote
I expect this will be easy in code composer studio

The solution is compiler dependent, regardless of the IDE used.
================================
https://dannyelectronics.wordpress.com/
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4263
  • Country: us
Re: Energia - Float to string?
« Reply #21 on: November 04, 2013, 03:11:36 am »
Quote
It will fail if the float type is negative.
Yep.  It also fails if the magnitude is too big for an int, or smaller than 0.001, which are well within the range handled by floats.  That's how you save space, by knowing something about your actual data.  Likewise, that's a lot of why the usual printf implementations are so large (in addition to sucking in FP libraries.)
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 27580
  • Country: nl
    • NCT Developments
Re: Energia - Float to string?
« Reply #22 on: November 04, 2013, 11:34:31 am »
And there are rounding issues as well. 0.0001 to 0.0009 will be printed as 0 because the decimals are truncated when converting from float to integer. You'd need to use the round function (slow) or add/stubstract 0.0005 (fast) to the floating point number for proper rounding.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline tszaboo

  • Super Contributor
  • ***
  • Posts: 7792
  • Country: nl
  • Current job: ATEX product design
Re: Energia - Float to string?
« Reply #23 on: November 07, 2013, 06:29:29 pm »
You can store the data in milikelvins, and then no floating point is needed. You should not need float for temperature, but I've seen others doing this on 8 bit (with compensation), because they are LAZY.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Energia - Float to string?
« Reply #24 on: November 07, 2013, 09:13:30 pm »
Quote
You can store the data in milikelvins

Why milikevins? It is too complicated. Why not just 2-based Kelvins? Like Kelvinx16, or Kelvinx8, etc.: much easier to process.
================================
https://dannyelectronics.wordpress.com/
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf