Posts: 6
Threads: 1
Joined: Mar 2018
Mar-28-2018, 09:42 PM
(This post was last modified: Mar-29-2018, 08:35 AM by buran.)
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])
Posts: 12,030
Threads: 485
Joined: Sep 2016
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]))
Posts: 8,159
Threads: 160
Joined: Sep 2016
Mar-29-2018, 08:41 AM
(This post was last modified: Mar-29-2018, 08:43 AM by buran.)
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
Posts: 6
Threads: 1
Joined: Mar 2018
Mar-29-2018, 08:03 PM
(This post was last modified: Mar-29-2018, 08:07 PM by buran.)
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
Posts: 8,159
Threads: 160
Joined: Sep 2016
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
Posts: 6
Threads: 1
Joined: Mar 2018
Mar-30-2018, 05:25 PM
(This post was last modified: Mar-30-2018, 06:12 PM by buran.)
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
Posts: 8,159
Threads: 160
Joined: Sep 2016
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.
Posts: 6
Threads: 1
Joined: Mar 2018
Mar-30-2018, 08:22 PM
(This post was last modified: Mar-31-2018, 05:43 AM by buran.)
here the input file, maybe you can ckeck (cant add file as attachement.
G00Z1.0
X-0.31Y10.0
Z0.1
G01Z0.0F10.0
X-0.31Y11,0F25,0
X0.024Y11.0
X0.167Y10.952
X0.262Y10.857
X0.31Y10.762
X0.357Y10.619
X0.357Y10.381
X0.31Y10.238
X0.262Y10.143
X0.167Y10.048
X0.024Y10.0
X-0.31Y10.0
G00Z1.0
X0.767Y10.642
Z0.1
G01Z0.0F10.0
X0.721Y10.168F25.0
X0.755Y10.022
X0.845Y9.965
X0.987Y9.951
X1.087Y9.989
X1.243Y10.118
G00Z1.0
X1.288Y10.592
Z0.1
G01Z0.0F10.0
X1.224Y9.928F25.0
G00Z1.0
X2.478Y10.721
Z0.1
G01Z0.0F10.0
X2.229Y9.752F25.0
G00Z1.0
X2.36Y10.259
Z0.1
G01Z0.0F10.0
X2.475Y10.328F25.0
X2.58Y10.35
X2.718Y10.315
X2.798Y10.245
X2.867Y10.129
X2.878Y9.979
X2.854Y9.887
X2.772Y9.76
X2.656Y9.692
X2.552Y9.669
X2.414Y9.705
X2.334Y9.774
X2.265Y9.89
G00Z1.0
X3.371Y10.471
Z0.1
G01Z0.0F10.0
X3.401Y10.411F25.0
X3.461Y10.441
X3.431Y10.501
X3.371Y10.471
G00Z1.0
X3.313Y10.139
Z0.1
G01Z0.0F10.0
X3.106Y9.506F25.0
G00Z1.0
X4.112Y9.691
Z0.1
G01Z0.0F10.0
X4.102Y9.797F25.0
X3.987Y9.894
X3.854Y9.946
X3.704Y9.955
X3.624Y9.883
X3.634Y9.777
X3.705Y9.698
X3.909Y9.566
X3.98Y9.487
X3.989Y9.381
X3.971Y9.337
X3.892Y9.266
X3.742Y9.274
X3.609Y9.326
X3.494Y9.423
X3.484Y9.529
G00Z1.0
X4.704Y9.944
Z0.1
G01Z0.0F10.0
X4.355Y9.213F25.0
X4.336Y9.064
X4.401Y8.98
X4.487Y8.939
G00Z1.0
X4.431Y9.705
Z0.1
G01Z0.0F10.0
X4.732Y9.561F25.0
G00Z1.0
X5.463Y8.832
Z0.1
G01Z0.0F10.0
X5.941Y8.518F25.0
X5.993Y8.597
X6.005Y8.703
X5.992Y8.769
X5.938Y8.861
X5.819Y8.94
X5.713Y8.952
X5.581Y8.925
X5.463Y8.832
X5.411Y8.752
X5.372Y8.607
X5.399Y8.475
X5.453Y8.383
X5.572Y8.304
X5.678Y8.292
X5.81Y8.319
G00Z1.0
X6.566Y8.826
Z0.1
G01Z0.0F10.0
X6.575Y8.759F25.0
X6.642Y8.769
X6.632Y8.835
X6.566Y8.826
G00Z1.0
X6.404Y8.531
Z0.1
G01Z0.0F10.0
X6.003Y7.997F25.0
G00Z1.0
X6.749Y8.264
Z0.1
G01Z0.0F10.0
X6.315Y7.758F25.0
G00Z1.0
X6.625Y8.119
Z0.1
G01Z0.0F10.0
X6.827Y8.135F25.0
X6.93Y8.109
X7.038Y8.016
X7.08Y7.918
X7.023Y7.778
X6.713Y7.417
G00Z1.0
X7.436Y6.694
Z0.1
G01Z0.0F10.0
X8.201Y7.338F25.0
X7.866Y6.184
X8.631Y6.828
G00Z1.0
X8.759Y6.088
Z0.1
G01Z0.0F10.0
X8.666Y6.14F25.0
X8.533Y6.165
X8.389Y6.123
X8.31Y6.069
X8.219Y5.949
X8.194Y5.817
X8.209Y5.711
X8.29Y5.594
X8.383Y5.542
X8.515Y5.517
X8.66Y5.559
X8.738Y5.612
X8.829Y5.732
X8.854Y5.865
X8.84Y5.97
X8.759Y6.088
G00Z1.0
X9.28Y5.26
Z0.1
G01Z0.0F10.0
X9.192Y5.32F25.0
X9.062Y5.357
X8.914Y5.329
X8.831Y5.283
X8.729Y5.171
X8.693Y5.042
X8.697Y4.936
X8.767Y4.811
X8.855Y4.751
X8.985Y4.714
X9.133Y4.742
X9.216Y4.789
X9.317Y4.9
X9.354Y5.029
X9.349Y5.136
X9.28Y5.26
G00Z1.0
X9.95Y4.698
Z0.1
G01Z0.0F10.0
X9.036Y4.292F25.0
G00Z1.0
X9.515Y4.505
Z0.1
G01Z0.0F10.0
X9.64Y4.456F25.0
X9.722Y4.388
X9.78Y4.258
X9.775Y4.151
X9.727Y4.026
X9.616Y3.924
X9.529Y3.886
X9.379Y3.871
X9.253Y3.92
X9.171Y3.988
X9.113Y4.118
X9.118Y4.225
X9.166Y4.35
G00Z1.0
X10.307Y3.842
Z0.1
G01Z0.0F10.0
X10.279Y3.781F25.0
X10.34Y3.753
X10.368Y3.814
X10.307Y3.842
G00Z1.0
X10.011Y3.682
Z0.1
G01Z0.0F10.0
X9.385Y3.452F25.0
G00Z1.0
X10.305Y2.768
Z0.1
G01Z0.0F10.0
X9.665Y2.579F25.0
G00Z1.0
X10.168Y2.728
Z0.1
G01Z0.0F10.0
X10.232Y2.846F25.0
X10.251Y2.951
X10.21Y3.088
X10.138Y3.166
X10.019Y3.23
X9.869Y3.235
X9.777Y3.208
X9.654Y3.122
X9.589Y3.004
X9.571Y2.899
X9.611Y2.762
X9.684Y2.684
X9.802Y2.62
G00Z1.0
X10.413Y2.327
Z0.1
G01Z0.0F10.0
X9.759Y2.197F25.0
G00Z1.0
X10.226Y2.29
Z0.1
G01Z0.0F10.0
X10.394Y2.177F25.0
X10.459Y2.093
X10.487Y1.953
X10.459Y1.85
X10.328Y1.776
X9.861Y1.683
G00Z1.0
X9.982Y0.669
Z0.1
G01Z0.0F10.0
X10.982Y0.705F25.0
X10.994Y0.372
X10.951Y0.227
X10.86Y0.128
X10.766Y0.077
X10.625Y0.025
X10.387Y0.016
X10.243Y0.059
X10.146Y0.103
X10.047Y0.195
X9.994Y0.336
X9.982Y0.669
G00Z1.0
X10.631Y-0.907
Z0.1
G01Z0.0F10.0
X9.966Y-0.867F25.0
G00Z1.0
X10.489Y-0.898
Z0.1
G01Z0.0F10.0
X10.589Y-0.809F25.0
X10.643Y-0.717
X10.651Y-0.574
X10.61Y-0.476
X10.52Y-0.375
X10.381Y-0.319
X10.286Y-0.314
X10.14Y-0.352
X10.039Y-0.442
X9.986Y-0.534
X9.977Y-0.676
X10.019Y-0.774
X10.108Y-0.875
G00Z1.0
X10.596Y-1.258
Z0.1
G01Z0.0F10.0
X9.895Y-1.444F25.0
X10.514Y-1.823
G00Z1.0
X10.785Y-2.167
Z0.1
G01Z0.0F10.0
X10.728Y-2.204F25.0
X10.765Y-2.26
X10.822Y-2.223
X10.785Y-2.167
G00Z1.0
X10.448Y-2.146
Z0.1
G01Z0.0F10.0
X9.795Y-2.012F25.0
G00Z1.0
X10.538Y-3.166
Z0.1
G01Z0.0F10.0
X9.573Y-2.901F25.0
G00Z1.0
X10.079Y-3.04
Z0.1
G01Z0.0F10.0
X10.196Y-2.974F25.0
X10.267Y-2.894
X10.305Y-2.757
X10.284Y-2.652
X10.217Y-2.535
X10.092Y-2.451
X10.0Y-2.426
X9.85Y-2.434
X9.733Y-2.501
X9.662Y-2.58
X9.624Y-2.718
X9.645Y-2.822
X9.711Y-2.939
G00Z1.0
Frank
Posts: 8,159
Threads: 160
Joined: Sep 2016
Mar-30-2018, 08:40 PM
(This post was last modified: Mar-30-2018, 08:41 PM by buran.)
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.
Posts: 6
Threads: 1
Joined: Mar 2018
haha ok, i think i made mistakes with StickFontv1.1 Program that makes some GCode. I have to check first.....
|