我在lua遇到了麻烦。
问题一: 每次我运行以下程序时,控制台都会告诉我:
'端'预期(关闭'功能'在第1行)靠近' local'
请注意我在所有大写评论中标记错误的详细信息
function m11()
local inst = mc.mcGetInstance() -- controller instance
local gcodeLineNbr = mc.mcCntlGetGcodeLineNbr(inst) -- get current Gcode line number
local gcodeLineStr = mc.mcCntlGetGcodeLine(inst, gcodeLineNbr) -- get current Gcode line
function valFromLine(string, axis)
local startPoint = string.find(string, axis) + 1
local outputVal = ""
local isNum = true
while isNum do
local num = string.sub(string, startPoint, startPoint)
startPoint = startPoint + 1
if num ~= " " then
outputVal = outputVal .. num
else
isNum = false
end
end
outputVal = tonumber(outputVal)
end
return outputVal
--COMPILER HIGHLIGHTS FOLLOWING LINE AS LOCATION OF ERROR
local gcodeLocX = valFromLine(gcodeLineStr, "X")
local curLocX = mc.mcAxisGetPos(inst, 0) -- get current X axis value
local curLocY = mc.mcAxisGetPos(inst, 1) -- get current Y axis value
local curLocZ = mc.mcAxisGetPos(inst, 2) -- get current Z axis value
local curAngB = mc.mcAxisGetPos(inst, 4) -- get current C axis value
local curAngC = mc.mcAxisGetPos(inst, 5) -- get current C axis value
local toolOffset = mc.mcCntlGetToolOffset(inst, 2) -- get tool offset for axis Z
function rotateToolB()
local comHypot = toolOffset * math.sin(angle) -- get XY planar dist from C pivot to tool centre point
local compDestinX = comHypot * math.sin(math.rad(90) - curAxisC + curLocX
end
end
--END OF M11() FUNCTION SHOULD BE HERE
if (mc.mcInEditor() == 1) then
m11()
end
我无法理解为什么期待m11()如此早地关闭。
问题二: 我在一个完全独立的文件中重写了valFromLine(),我得到了:
' EOF'预计在' print'
附近
function valFromLine(string, axis)
local startPoint = string.find(string, axis) + 1
local outputVal = ""
local isNum = true
while isNum do
local num = string.sub(string, startPoint, startPoint)
startPoint = startPoint + 1
if num ~= " " then
outputVal = outputVal .. num
else
isNum = false
end
end
outputVal = tonumber(outputVal)
end
return outputVal
print(valFromLine("GO1 X254.348 Y1039.456 Z150.13 B90.23 C13 M11", "X"))
我认为我的结局是'声明,我无法在任何代码示例中找到它们的错误。我现在完全失去了想法,请帮忙。感谢。
答案 0 :(得分:2)
第return outputVal
行位置错误。将其移动到函数valFromLine中。
你不能在函数之外返回。
正确的:
function someFunction()
-- do something
local something = "something"
return something
end
错误:
function someFunction()
-- do something
local something = "something"
end
return something
使用函数定义全局函数也不是很干净,使用本地函数。