0

我在lua中遇到了麻烦。

问题一: 每次我运行以下程序时,控制台都会告诉我:

预计在“本地”附近出现“结束”(在第 1 行关闭“功能”)

请注意我在所有大写注释中标记了有关错误的详细信息

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"))

我计算了我的“结束”语句,在任何一个代码示例中都找不到它们的问题。在这一点上,我完全失去了想法,请帮忙。谢谢。

4

1 回答 1

2

线return outputVal在错误的位置。将其移入函数 valFromLine。您不能在函数之外返回。

正确的:

function someFunction()
  -- do something
  local something = "something"
  return something
end

错误的:

function someFunction()
  -- do something
  local something = "something"
end
return something

用函数定义全局函数也不是很干净,使用局部函数。

于 2016-03-12T13:03:59.730 回答