9 Replies - 1284 Views - Last Post: 14 June 2012 - 02:27 AM

#1 jbzy324  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-May 12

Help getting started with text/code analysis program

Posted 08 May 2012 - 01:50 PM

Hey all; So I'm working on my senior Mechanical Engineering project, a mechatronic/robotic painting table. A control system basically interprets a language called g-code, which is more or less a glorified dot-to-dot that tells the control system a series of points to drive the brush head to. I run into an issue with re-dipping the brush - it needs to be done on certain intervals, that are different for every drawing, and requires insertion of extra g-code lines into the original g-code, which just maps the pathways of the lines to be drawn. G-code runs from start to finish, so I need t

My programming knowledge is limited and only in C, so I hope this is a good place to start looking for help. Essentially I need to make a program that goes line by line through this g-code, and when it recognizes a certain character (Z, for instance), inserts some predisposed lines of g-code, and continues. I'll also need to be recognizing and storing different numbers along the way for calculations. Unfortunately, the scope of my small knowledge base is limited to the more math side of the language. My question is, is C a good language to code this type of program, or is there another that may be more streamlined/convenient/user friendly? And more or less, what does the structure of a code that reads through a text file line by line look like? I'm definitely not asking anyone for code, just help getting pointed in the right direction, and maybe some of the useful commands you guys think I'll have to implement. Thank you VERY much in advance.

jbzy

Is This A Good Question/Topic? 0
  • +

Replies To: Help getting started with text/code analysis program

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Help getting started with text/code analysis program

Posted 08 May 2012 - 03:23 PM

Quote

My question is, is C a good language to code this type of program
Probably not. Your requirements sound like nothing more than basic text parsing and manipulating, and C is a terrible language for that.

I know Python, so that's what I would use. Python, Ruby, Perl, all three would work. Or shell scripts if that's your thing? But not C.

Quote

And more or less, what does the structure of a code that reads through a text file line by line look like?
I have no idea what kind of meaningful answer would be given here. It's basically a repeated call to read a line from the file, with appropriate parsing and handling afterwards.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: Help getting started with text/code analysis program

Posted 08 May 2012 - 03:24 PM

Moved to Software Development.

You might be better off with a higher-level language like Python for something like this.
Was This Post Helpful? 0
  • +
  • -

#4 jbzy324  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-May 12

Re: Help getting started with text/code analysis program

Posted 08 May 2012 - 04:22 PM

Thanks Oler1s, like I said, that at least points me in the right direction.

View PostOler1s, on 08 May 2012 - 03:23 PM, said:

I have no idea what kind of meaningful answer would be given here. It's basically a repeated call to read a line from the file, with appropriate parsing and handling afterwards.


I know this was a weird question in the style of "Here's an all-too-vague description of what I need to do, but could you give me a basic skeleton?" But I was more thinking how I would implement it in C, as most of the commands I know are math based and have no idea how to effectively analyze text. I've heard from others that python would be the best way to go as well, so I'll start in that direction, and hopefully will have more pointed questions as some code gets written. I'm working off of a (very) loose pseudo code that I have written up, but unfortunately I don't know how "Look through the text until you see the letter 'z'" and "If you see an 'x' character, store the number directly after it, but that number could be any character length so keep reading numbers until you see a space" translate into python commands. So, I was hoping someone might offer a useful command/combination of commands in C that I might try; however, hopefully looking into parsing with python will help with some of that. I'm sure I'll have some more specific questions through the coding process. Thank you again for the help!

jbzy
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: Help getting started with text/code analysis program

Posted 08 May 2012 - 06:04 PM

Here's a place to start.

You're interested in input/output and string functions.

I also have some C entries in my blog, and we have tutorials.
Was This Post Helpful? 0
  • +
  • -

#6 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: Help getting started with text/code analysis program

Posted 08 May 2012 - 09:24 PM

Sedwould do that quite easily too.
There is also a windows version of sed.

This is a starting point.

Awk is probably a bit heavy.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,748
  • Joined: 19-March 11

Re: Help getting started with text/code analysis program

Posted 08 May 2012 - 10:47 PM

Quote

Sed would do that quite easily too. ...Awk is probably a bit heavy.


It's like they say: when all is sed and awk, there's a lot more sed than awk.


I'd reach for perl in this situation, myself, but perl-style regular expressions (which is what you need) are available in just about any language now, so you could do this in pretty much any modern language.

This post has been edited by jon.kiparsky: 08 May 2012 - 10:48 PM

Was This Post Helpful? 0
  • +
  • -

#8 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Help getting started with text/code analysis program

Posted 15 May 2012 - 01:38 PM

Hey Jbzy, I'm really interested in machining and G is pretty popular in the areas I'm interested in.

I'd love to help you work something out (I'm actually looking for a new project to jump into having completed my from-scratch(motor controllers/light controllers/etc) PC driven arduino/Xbee powered RC car).

I'm good with Python, and I have a pretty good idea how to whip something like you're talking about up, but obviously I'll need to see the g-code to know what to parse and when.

If you're capable of writing code in some language, I can definitely get you going in Python in short order.

This post has been edited by Python_4_President: 15 May 2012 - 01:40 PM

Was This Post Helpful? 0
  • +
  • -

#9 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Help getting started with text/code analysis program

Posted 15 May 2012 - 02:11 PM

Forgot to ask.....

What's your prefered way to communicate with people that may or may not live anywhere near you?



Also, some starting points...

Here's a complete python program and the command that executes it:

Program! (filename=myfirst.py)
print "This is my first Python program... holy crap, no includes."



COMMAND (for Window$!)
c:\python[version?]\python.exe myfirst.py



COMMAND (For Linux!)
python myfirst.py





WHAT STRINGS LOOK LIKE:
"Double quoted string"
'Single quoted string'
"""triple quoted string"""
type("") == type('') == type("""""")




HOW TO THINK ABOUT STRINGS LIKE ARRAYS IN C:
MyString = "This is my string"
MyString[0] == "T" or 'T' or """T"""
MyString[1] == "h" or 'h' or """h"""
MyString[2] == "i" or 'i' or """i"""



WHY PYTHON IS MORE AWESOMER:
MyString = "Yes, that is pretty cool."
MyString[-1] == "." or '.' or """."""
MyString[-2] == "l" or 'l' or """l"""



HOW DO I ACCOUNT FOR ' in a string, then?
You could be lame and do:
'Yes, I like being lame. It\'s fun.'



BUT,
"This is much better. ' != \""
"""This way is also possible. It's cool with 's or "s."""



HERE'S SOME RAW POWER:
"This string can be indexed and SLICED!, because it is a string object..."[5:11] == "string" 
"This string starts with This... as seen here ->".startswith("This") == True
"This string ends with -> as seen here ->".endswith("->") == True



WHAT YOU'LL USE A LOT, BECAUSE IT WINS:
gcodez_i_care_about = "some gcodez"
gcodez_i_am_looking_at = open("gcodez.gext", 'r').read()
if gcodez_i_care_about in gcodez_i_am_looking_at:
    print "I foundz useful gcodez!"



^^^ The in operator is just awesome. It tells you if the thing you're looking for is in the thing you're looking through.

Case sensitivity can be managed with the string method lower().

yay = "I LIKE CAPSLOCK, IT MAKES ME LOOK SMART."
stop_yelling_at_me = yay.lower()


^^^ There's also an upper(), which does what you'd expect.

This post has been edited by Python_4_President: 15 May 2012 - 02:13 PM

Was This Post Helpful? 0
  • +
  • -

#10 peace_fixation  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 190
  • Joined: 01-November 11

Re: Help getting started with text/code analysis program

Posted 14 June 2012 - 02:27 AM

In C you can read/write a text file like this, the syntax is loose but you should get the gist:

input_file = fopen("your_file.txt");
output_file = fopen("new_file.txt");
int ch;
while ((ch = fgetc(input_file)) != EOF)
{
  // read char by char until you hit End Of File
  // write each char to a new file, unless it's a special char
  // in which case, write the matching G-Code

  if (replacement_char(ch) == FALSE)
  {
    fputc(output_file); // put single char
  }
  else // replacement_char(ch) == TRUE
  {
    switch(c)
    {
    case 'Z':
      fprintf(output_file, "some G-Code"); // put G-Code string
      break;
    case 'A':
      fprintf(output_file, "some other G-Code");
      break;
    }
  }
}

fclose(input_file);
fclose(output_file); // now output_file == input_file with G-Codes replaced


This post has been edited by peace_fixation: 14 June 2012 - 02:33 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1