lethaixuong 0 Posted August 30, 2014 Share Posted August 30, 2014 Someone please tells me how to add this function. I've tried to but #include <stdlib.h> but no luck. thanks Quote Link to post Share on other sites
tripwire 139 Posted August 30, 2014 Share Posted August 30, 2014 Have you tried the strtol function to see if that works (also in stdlib.h). I'm wondering if the problem is due to lack of support for "double" type. Quote Link to post Share on other sites
lethaixuong 0 Posted August 31, 2014 Author Share Posted August 31, 2014 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. Or please tell me any way to convert a string to a float or double Quote Link to post Share on other sites
David Bender 28 Posted August 31, 2014 Share Posted August 31, 2014 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. Quote Link to post Share on other sites
abecedarian 330 Posted August 31, 2014 Share Posted August 31, 2014 TM4C123 and 1294 both have hardware FPU. Quote Link to post Share on other sites
Rickta59 589 Posted August 31, 2014 Share Posted August 31, 2014 I'm guessing he is using a stellaris or tiva. If that is the case, just use strtof() so you get full use of the hardware floating point. If he is using the msp430, he will probably have to floor() or ceil() -rick Quote Link to post Share on other sites
pabigot 355 Posted August 31, 2014 Share Posted August 31, 2014 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. Quote Link to post Share on other sites
lethaixuong 0 Posted September 1, 2014 Author Share Posted September 1, 2014 Sorry for missing information I'm using msp430g2553. So your advice is giving my project up ;( Quote Link to post Share on other sites
roadrunner84 466 Posted September 1, 2014 Share Posted September 1, 2014 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. Quote Link to post Share on other sites
lethaixuong 0 Posted September 1, 2014 Author Share Posted September 1, 2014 (edited) 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 September 2, 2014 by bluehash [ADMIN] Code formatting. Quote Link to post Share on other sites
roadrunner84 466 Posted September 1, 2014 Share Posted September 1, 2014 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; } Quote Link to post Share on other sites
lethaixuong 0 Posted September 1, 2014 Author Share Posted September 1, 2014 (edited) Thanks for quick reply. I've some useful stuff at arduino union u_tag { byte b[4]; float fval; } _x, _y; Use this union to convert. Awesome. Edited September 2, 2014 by bluehash [ADMIN Code formatting. Quote Link to post Share on other sites
roadrunner84 466 Posted September 1, 2014 Share Posted September 1, 2014 Thanks for quick reply. I've some useful stuff at arduino union u_tag { byte b[4]; float fval; } _x, _y; Use this union to convert. Awesome. Yes, unions basically cast the byte array to a float depending on tag.b[] or tag.f being used. STILL A BAD IDEA THOUGH Quote Link to post Share on other sites
lethaixuong 0 Posted September 1, 2014 Author Share Posted September 1, 2014 Deleted... Everything is fine. This is my mistake. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.