View previous topic :: View next topic |
Author |
Message |
ReprapGeek
Joined: 16 Mar 2011 Posts: 2
|
Grab floating points from string |
Posted: Wed Mar 16, 2011 7:58 am |
|
|
Dear all.
This is my first post on the CCS forums so I hope you all will treat me nicely.
I'm by far a novice code monkey but I have never really worked in ANSI C / C++. Hence when I started working with PIC microcontrollers and the CCS compiler I feel somewhat restrained in my capabilities. Especially when it comes to interpreting sting values sent to the MCU from ex. an RS232 interface.
I am trying to do some string operations for a project where I try to make a hardware CNC interpreter for a milling machine. A typical G-code command to interpret could be one of the below:
Code: |
char myGcode[] = "G1 X130.0 Y5.0 Z10.250 F1500.0 ;Machine movement";
|
I am trying to put together an algorithm that can return me X Y and Z as floats.
I've made something that works but that is far from ideal. There's an abundance of switches, do whiles, and strcmp() in my code.
Any suggestions as to how this can be done smarter? Maybe a code example?
Thank you very much!
David |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Mar 16, 2011 11:51 am |
|
|
Instead of floats, I would recommend you use INT16s or INT32s for your dimensions. Keep everything in thousandths if you are using inches, or whatever your smallest unit is. Only convert to inches in your display functions.
Floats are notorious for inaccuracy and it can be hard to predict when rounding or truncation can occur.
As far as reading the numbers, can you count on there always being a decimal point? If so I would find the decimal point and read the two halves of the number on each side of it. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 16, 2011 12:11 pm |
|
|
Conceptually, you use strtok() to get the values and then check each
string to validate that it's in the correct format. Then run atof() on the
numeric portion of each string. This method might produce the
minimum lines of source code, to solve the problem. It might not be
minimal in terms of ROM space, though. And, as SherpaDoug says,
you won't always get a accurate translation of your values into floats.
Your string is somewhat similar to NMEA-0183 messages, except that
spaces are used as delimiters instead of commas. You could look at
sample code to parse NMEA-0183 messages. |
|
|
ReprapGeek
Joined: 16 Mar 2011 Posts: 2
|
Thanks! |
Posted: Mon Mar 21, 2011 2:56 am |
|
|
Thanks guys!
I'll give the code an effort today and see if I can work something out! |
|
|
n-squared
Joined: 03 Oct 2006 Posts: 99
|
|
Posted: Mon Mar 21, 2011 10:41 pm |
|
|
Hi,
It has been a long while since I posted a message here.
ReprapGeek,
The following is a link to a sscanf() source code which will allow you to read floating point numbers as well as other numerical data.
http://www.ccsinfo.com/forum/viewtopic.php?t=20225&highlight=sscanf
BR
Noam _________________ Every solution has a problem. |
|
|
|