Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code dont work
#1
Hello Everyone,

im new here and have no experience in programming. I have a python 2 program which wont run:
I have installed python 2.7 and want to start it from windows cmd like :
Error:
C:\Python27>python convertGCode.py noobian.nc noobian.cpp Traceback (most recent call last): File "convertGCode.py", line 81, in <module> run(sys.argv[1], sys.argv[2]) File "convertGCode.py", line 66, in run newposx = float(parts[0][1:]) ValueError: invalid literal for float(): -0,31
What is wrong with that code. Helping would be very great !

Greetz Frank
----------------------

# Simple GCode to Arduino hex format converter.
# It only understands G00 and G01 codes, nothing fancy!
#
# It will automatically scale the object to the full 12 bit
# range for my Arduino laser project, to change that
# you have to modify the scale in createObject().
#
# Typical files I worked with have been generated with
# http://ncplot.com/stickfont/stickfont.htm (StickFont 1.1)
#
# Usage: python convertGCode.py inputfile.nc outputfile.cpp

import math
import sys

def createObject(name, cmds):
  minx = miny = 10000000
  maxx = maxy = 0
  string = ""
  for cmd in cmds:
    if cmd[0] == 2:
      minx = min(minx,cmd[1])
      miny = min(miny,cmd[2])
      maxx = max(maxx,cmd[1])
      maxy = max(maxy,cmd[2])

  string += "const unsigned short draw_" + name + "[] PROGMEM = {\n";
  laserState = False
  
  biggestSide = max(maxx-minx, maxy-miny)
  # scale to the laser range
  scale = 4095. / biggestSide;
  print "bounding box x: ", minx, maxx
  print "bounding box y: ", miny, maxy
  print "scale: ", scale
  for cmd in cmds:
    if cmd[0] == 0:laserState = False
    if cmd[0] == 1:laserState = True
    if cmd[0] == 2:
      x = int(math.floor((cmd[1]-minx) * scale))
      y = int(math.floor((cmd[2]-miny) * scale))
      if laserState:
        x += 0x8000
      string += hex(x) + "," + hex(y) + ",\n"
  string += "};\n"
  return string

def run(input, output):
  result = ""
  f = open(input);
  lines = f.readlines()
  drawing = False
  posx = posy = 0.
    
  cmds = []
  for l in lines:
    if l.startswith("G00"):
      if drawing:
        cmds.append((0,))
      drawing = False
    elif l.startswith("G01"):
      drawing = True
      cmds.append((1,))
    elif l.startswith("X"):
      parts = l.split("Y")
      newposx = float(parts[0][1:])
      newposy = float(parts[1])
      cmds.append((2,newposx,newposy))
      posx = newposx
      posy = newposy

  result = createObject("object", cmds)
  
  o = open(output,"w")
  o.write(result)

if __name__ == "__main__":
  if len(sys.argv) < 3:
    print "Usage: convertGCode.py inputfile.nc outputfile.cpp"
  else:
    run(sys.argv[1], sys.argv[2])
Reply
#2
you are getting a value error from line 66:
newposx = float(parts[0][1:])
Add the following on line 65 and run again so you can see why:
print('parts[0]: {}'.format(parts[0]))
Reply
#3
from your traceback - the format of the number in your input file is not correct - it uses comma as decimal point (-0,31), not dot (-0.31)

>>> float('-0,31')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): -0,31
>>> float('-0.31')
-0.31
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Error:
C:\Python27>python convertGCode.py noobian.nc noobian.cpp File "convertGCode.py", line 65 parts = l.split("Y") print('parts[0]: {}'.format(parts[0])) ^ SyntaxError: invalid syntax
_______________

i dont know the right syntax and

regarding -0,31 should i replace all , now with .?

Regards
Reply
#5
the print function, suggested by Larz60+ must be on next line
parts = l.split("Y")
print('parts[0]: {}'.format(parts[0]))
but even without it it's clear what the problem is.

(Mar-29-2018, 08:03 PM)FrankWhite Wrote: regarding -0,31 should i replace all , now with .?

unless you can change the format of the input file, yes, you need to replace , with . before converting to float
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
hello again,

buran, i replaced all of the , with . in the input File noobian.nc, then i run the program again here the output:


Error:
c:\Python27>convertGCode.py noobian.nc noonian.cpp Traceback (most recent call last): File "C:\Python27\convertGCode.py", line 81, in <module> run(sys.argv[1], sys.argv[2]) File "C:\Python27\convertGCode.py", line 67, in run newposy = float(parts[1]) ValueError: invalid literal for float(): 11.0F25.0
Frank
Reply
#7
Obviously you cannot convert 11.0F25.0 to float. I don't have idea what your input data looks like. You need to parse it and pass to float strings that can be converted.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
here the input file, maybe you can ckeck (cant add file as attachement.
Frank
Reply
#9
that's great, but could you elaborate further
on some lines you have X and Y
on some X, Y and F
on some G and Z
on some just Z

your code takes care for rows starting with G0 and for some cases when starting with X (assuming only x and Y). However there are cases when there is also F. What your code is expected to do with F values?
also what about lines starting with Z?
one possible simple solution, when row starts with X, first split by F, take element 0, and continue working with it like you do now.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
haha ok, i think i made mistakes with StickFontv1.1 Program that makes some GCode. I have to check first.....
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  hi need help to make this code work correctly atulkul1985 5 799 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 694 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Beginner: Code not work when longer list raiviscoding 2 829 May-19-2023, 11:19 AM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,806 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 1,447 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 896 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  I dont know why my function won't work? MehHz2526 3 1,200 Nov-28-2022, 09:32 PM
Last Post: deanhystad
  Something the code dont work AlexPython 13 2,265 Oct-17-2022, 08:34 PM
Last Post: AlexPython
  cannot get code to work Led_Zeppelin 10 2,465 Jun-30-2022, 06:28 PM
Last Post: deanhystad
  why I dont get any output from this code William369 2 1,134 Jun-23-2022, 09:18 PM
Last Post: William369

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020