CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Grab floating points from string

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ReprapGeek



Joined: 16 Mar 2011
Posts: 2

View user's profile Send private message

Grab floating points from string
PostPosted: Wed Mar 16, 2011 7:58 am     Reply with quote

Dear all.

This is my first post on the CCS forums so I hope you all will treat me nicely. Rolling Eyes

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

View user's profile Send private message

PostPosted: Wed Mar 16, 2011 11:51 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 16, 2011 12:11 pm     Reply with quote

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

View user's profile Send private message

Thanks!
PostPosted: Mon Mar 21, 2011 2:56 am     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Mon Mar 21, 2011 10:41 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group