【发布时间】:2021-07-19 14:30:45
【问题描述】:
所以我在构造上设置了一个多维数组。该数组是我对占用网格的尝试,如果我按文字索引数组,那完全没问题。但是,在主循环期间,我根据数组计算出播放器的位置,然后围绕网格中的该元素循环一定的正方形半径并将该部分打印到文件中。它一直有效,直到玩家从我计算出的原始网格位置移动,这非常奇怪,我不确定我哪里出错了。
构造函数:
occupancyGrid = {}
gridSize = 500 -- Each element in the grid is equivalent to 0.1 units of the environment space
gridElementSize = 0.1 -- The number of units in the evironment 1 element consists of
sensorRange = 5
for i=1,gridSize,1 do
occupancyGrid[i] = {}
for j=1,gridSize,1 do
occupancyGrid[i][j] = 1
end
end
主循环:
print("[" .. simTime .. "] Saving partial map to file.")
file = io.open(folderPath.."robotSurroundings.txt", "w+")
io.output(file)
local robotPosInGrid = getGridIndex({ robotPosition[1], robotPosition[2] })
x=robotPosInGrid[1]
y=robotPosInGrid[2]
print(x .. ", " .. y)
for i=y,y+10,1 do
for j=x,x+10,1 do
if(occupancyGrid[j][i] ~= nil) then
io.write(occupancyGrid[i][j])
end
end
io.write('\n')
end
io.close(file)
我很抱歉那里仍然有很多测试代码,但代码仍然很清晰。但是我根据网格计算出机器人的位置,然后在两个方向上写下接下来的十个元素。机器人从 [250][250] 的网格空间开始,但一旦移动到 [249][250],它就会崩溃并给我
221: attempt to index field '?' (a nil value)
在 io.write 行。我一辈子都想不通为什么,因为这里的逻辑似乎是正确的,如果我用数字文字索引网格,即使我这样写一个 if 语句也很好
if(occupancyGrid[j][i] == 0) then ...
我在这里遗漏了什么明显的东西吗?
控制台照片
整个代码函数
function saveGrid(wholeGrid)
if(wholeGrid) then
print("[" .. simTime .. "] Saving whole map to file.")
file = io.open(folderPath.."wholeMap.txt", "w+")
io.output(file)
-- Write the entire grid to file
for i=1,gridSize,1 do
for j=1,gridSize,1 do
io.write(occupancyGrid[j][i])
end
io.write('\n')
end
else
print("[" .. simTime .. "] Saving partial map to file.")
file = io.open(folderPath.."robotSurroundings.txt", "w+")
io.output(file)
local robotPosInGrid = getGridIndex({ robotPosition[1], robotPosition[2] })
x=robotPosInGrid[2]
y=robotPosInGrid[1]
print(x .. ", " .. y)
for i=y,y+10,1 do
for j=x,x+10,1 do
if(occupancyGrid[j][i] ~= nil) then
io.write(occupancyGrid[j][i])
end
end
io.write('\n')
end
end
io.close(file)
结束
【问题讨论】:
-
print(x .. ", " .. y)打印什么?您的代码适用于 x=249, y = 250 使用 500x500 网格 -
崩溃前打印 249,250
-
你修改了两个sn-ps之间的网格吗? Lua 值不会自行消失。
-
不,除了构造函数之外,唯一涉及网格的函数是保存网格函数,它有两个部分,一个是保存整个网格,另一个是部分。整个网格保存是相同的代码,但迭代整个网格并且没有任何问题
-
我已经编辑了我的主要帖子,它现在包含所有引用网格的代码。