上下文:我正在为Minecraft mod OpenComputers添加的移动计算机实现导航系统。对于那些不熟悉该模块的人,它基本上增加了各种Lua可编程的,可升级的计算机,包括移动计算机,即机器人,无人机和平板电脑。尝试对机器人和无人机进行编程以执行自主任务时,经常会遇到的众多挑战之一就是确保他们始终知道自己的坐标。
最简单的解决方案是使用导航升级,它确实做到了-为计算机提供相对于其制作的地图中心的精确坐标。但是,它有两个主要缺点-占用了Tier II升级空间,这并不是一件小事,并且仅限于地图区域。后者或多或少是可以接受的,但是在某些使用情况下仍然无法使用此导航方法。
另一种解决方案是让计算机一次记住其坐标,然后跟踪其运动,但这也有许多潜在的警告-您必须通过自定义子例程控制所有运动,或者使用黑客来拦截组件调用,您每次必须手动输入坐标就无法移动计算机,无人驾驶飞机会出现一些精度错误,这对于平板电脑根本不起作用。
第三种方法-我正在研究的方法-与现实生活中的GPS类似。它基于这样一个事实,即计算机可以通过无线网卡进行升级,从而能够在400块的相当大的距离内相互发送消息,并且它们与消息本身一起可以接收到精确的距离(浮点数,以块为单位) )在发送方和接收方之间。如果我们将某些固定计算机指定为不断广播其位置的“卫星”,那么我们就可以使移动计算机能够使用来自4颗以上卫星的信息来确定其确切位置。
这种方法是可扩展的(您可以继续向网络添加更多卫星以扩大其覆盖范围),而不会仅出于导航目的而占用额外的升级插槽(因为许多移动计算机已经使用无线网卡进行了升级),并且精确,这比其他两种方法有明显的优势。但是,它需要进行一些令人惊讶的复杂计算,这就是我遇到的问题。
问题:我需要找到一种三边测量算法(理想情况下附带一个代码示例),该算法将允许任何移动计算机计算其位置(在大约0.25块的误差范围内),并且知道指定“卫星”的坐标及其到它们的距离。我们假设所有计算机和卫星都配备了Tier II无线网卡(即它们可以在400个块的总范围内相互发送消息,并且知道发件人与自身之间的距离,并且具有float32数字所允许的精度)。该解决方案将使用纯Lua进行编码,而无需访问任何第三方服务,因此Mathematica之类的数据包是不行的。目前,我在押注某种合适的拟合方法,尽管我不知道如何实现,以及如何适应范围内某些卫星广播错误位置的可能性。
在最基本的水平上,我们可以假设有4颗卫星,它们不断且正确地广播其位置,彼此之间的距离适中,并且不在单个2D平面上。算法理想情况下应具有一些可选条件,请参见下文。
奖励积分:
我尝试过的事情:除了尝试自己解决问题外,我还尝试在互联网上寻找合适的解决方案。但是,我找不到适合自己的解决方案。
如果我不清楚一些要点,请发表评论,以便我改善问题。预先感谢!
答案 0 :(得分:2)
这种三边测量系统已经为另一个名为ComputerCraft的模块开发。由于它可能与您的特定问题不兼容,因此您必须修改和调整其逻辑,但算法本身应该可以工作。
以下是源代码
CHANNEL_GPS = 65534
local function trilaterate( A, B, C )
local a2b = B.vPosition - A.vPosition
local a2c = C.vPosition - A.vPosition
if math.abs( a2b:normalize():dot( a2c:normalize() ) ) > 0.999 then
return nil
end
local d = a2b:length()
local ex = a2b:normalize( )
local i = ex:dot( a2c )
local ey = (a2c - (ex * i)):normalize()
local j = ey:dot( a2c )
local ez = ex:cross( ey )
local r1 = A.nDistance
local r2 = B.nDistance
local r3 = C.nDistance
local x = (r1*r1 - r2*r2 + d*d) / (2*d)
local y = (r1*r1 - r3*r3 - x*x + (x-i)*(x-i) + j*j) / (2*j)
local result = A.vPosition + (ex * x) + (ey * y)
local zSquared = r1*r1 - x*x - y*y
if zSquared > 0 then
local z = math.sqrt( zSquared )
local result1 = result + (ez * z)
local result2 = result - (ez * z)
local rounded1, rounded2 = result1:round( 0.01 ), result2:round( 0.01 )
if rounded1.x ~= rounded2.x or rounded1.y ~= rounded2.y or rounded1.z ~= rounded2.z then
return rounded1, rounded2
else
return rounded1
end
end
return result:round( 0.01 )
end
local function narrow( p1, p2, fix )
local dist1 = math.abs( (p1 - fix.vPosition):length() - fix.nDistance )
local dist2 = math.abs( (p2 - fix.vPosition):length() - fix.nDistance )
if math.abs(dist1 - dist2) < 0.01 then
return p1, p2
elseif dist1 < dist2 then
return p1:round( 0.01 )
else
return p2:round( 0.01 )
end
end
function locate( _nTimeout, _bDebug )
-- Let command computers use their magic fourth-wall-breaking special abilities
if commands then
return commands.getBlockPosition()
end
-- Find a modem
local sModemSide = nil
for n,sSide in ipairs( rs.getSides() ) do
if peripheral.getType( sSide ) == "modem" and peripheral.call( sSide, "isWireless" ) then
sModemSide = sSide
break
end
end
if sModemSide == nil then
if _bDebug then
print( "No wireless modem attached" )
end
return nil
end
if _bDebug then
print( "Finding position..." )
end
-- Open a channel
local modem = peripheral.wrap( sModemSide )
local bCloseChannel = false
if not modem.isOpen( os.getComputerID() ) then
modem.open( os.getComputerID() )
bCloseChannel = true
end
-- Send a ping to listening GPS hosts
modem.transmit( CHANNEL_GPS, os.getComputerID(), "PING" )
-- Wait for the responses
local tFixes = {}
local pos1, pos2 = nil, nil
local timeout = os.startTimer( _nTimeout or 2 )
while true do
local e, p1, p2, p3, p4, p5 = os.pullEvent()
if e == "modem_message" then
-- We received a reply from a modem
local sSide, sChannel, sReplyChannel, tMessage, nDistance = p1, p2, p3, p4, p5
if sSide == sModemSide and sChannel == os.getComputerID() and sReplyChannel == CHANNEL_GPS and nDistance then
-- Received the correct message from the correct modem: use it to determine position
if type(tMessage) == "table" and #tMessage == 3 then
local tFix = { vPosition = vector.new( tMessage[1], tMessage[2], tMessage[3] ), nDistance = nDistance }
if _bDebug then
print( tFix.nDistance.." metres from "..tostring( tFix.vPosition ) )
end
if tFix.nDistance == 0 then
pos1, pos2 = tFix.vPosition, nil
else
table.insert( tFixes, tFix )
if #tFixes >= 3 then
if not pos1 then
pos1, pos2 = trilaterate( tFixes[1], tFixes[2], tFixes[#tFixes] )
else
pos1, pos2 = narrow( pos1, pos2, tFixes[#tFixes] )
end
end
end
if pos1 and not pos2 then
break
end
end
end
elseif e == "timer" then
-- We received a timeout
local timer = p1
if timer == timeout then
break
end
end
end
-- Close the channel, if we opened one
if bCloseChannel then
modem.close( os.getComputerID() )
end
-- Return the response
if pos1 and pos2 then
if _bDebug then
print( "Ambiguous position" )
print( "Could be "..pos1.x..","..pos1.y..","..pos1.z.." or "..pos2.x..","..pos2.y..","..pos2.z )
end
return nil
elseif pos1 then
if _bDebug then
print( "Position is "..pos1.x..","..pos1.y..","..pos1.z )
end
return pos1.x, pos1.y, pos1.z
else
if _bDebug then
print( "Could not determine position" )
end
return nil
end
end
询问您是否对源代码有任何特定疑问。
答案 1 :(得分:1)
函数trilateration
需要卫星列表(它们的坐标和与移动计算机的距离)以及移动计算机的先前坐标。
仅收集您自己小组的卫星,不包括其他所有小组的卫星。
您的某些卫星可能发送了错误的数据,没关系。
如果没有足够的卫星可访问,该函数将返回nil
,因为它无法确定当前位置。
否则,该函数将返回移动计算机的当前坐标,并责怪卫星索引列表不正确。
如果有歧义,则将新位置选择为最接近移动计算机上一个位置的位置。
输出坐标为整数,Y坐标限制为0..255
为了适当的三边测量,应满足以下条件:
识别不正确的卫星是昂贵的CPU操作。
一旦卫星被识别为不正确,请将其存储在黑名单中,并将其从以后的所有计算中排除。
do
local floor, exp, max, min, abs, table_insert = math.floor, math.exp, math.max, math.min, math.abs, table.insert
local function try_this_subset_of_sat(satellites, is_sat_incorrect, X, Y, Z)
local last_max_err, max_err = math.huge
for k = 1, math.huge do
local oldX, oldY, oldZ = X, Y, Z
local DX, DY, DZ = 0, 0, 0
max_err = 0
for j = 1, #satellites do
if not is_sat_incorrect[j] then
local sat = satellites[j]
local dx, dy, dz = X - sat.x, Y - sat.y, Z - sat.z
local d = (dx*dx + dy*dy + dz*dz)^0.5
local err = sat.distance - d
local e = exp(err+err)
e = (e-1)/(e+1)/(d+1)
DX = DX + dx*e
DY = DY + dy*e
DZ = DZ + dz*e
max_err = max(max_err, abs(err))
end
end
if k % 16 == 0 then
if max_err >= last_max_err then
break
end
last_max_err = max_err
end
local e = 1/(1+(DX*DX+DY*DY+DZ*DZ)^0.5/max_err)
X = X + DX*e
Y = max(0, min(255, Y + DY*e))
Z = Z + DZ*e
if abs(oldX - X) + abs(oldY - Y) + abs(oldZ - Z) <= 1e-4 then
break
end
end
return max_err, floor(X + 0.5), floor(Y + 0.5), floor(Z + 0.5)
end
local function init_set(is_sat_incorrect, len, ctr)
for j = 1, len do
is_sat_incorrect[j] = (j <= ctr)
end
end
local function last_combination(is_sat_incorrect)
local first = 1
while not is_sat_incorrect[first] do
first = first + 1
end
local last = first + 1
while is_sat_incorrect[last] do
last = last + 1
end
if is_sat_incorrect[last] == nil then
return true
end
is_sat_incorrect[last] = true
init_set(is_sat_incorrect, last - 1, last - first - 1)
end
function trilateration(list_of_satellites, previous_X, previous_Y, previous_Z)
local N = #list_of_satellites
if N >= 3 then
local is_sat_incorrect = {}
init_set(is_sat_incorrect, N, 0)
local err, X, Y, Z = try_this_subset_of_sat(list_of_satellites, is_sat_incorrect, previous_X, previous_Y, previous_Z)
local incorrect_sat_indices = {}
if err < 0.1 then
return X, Y, Z, incorrect_sat_indices
end
for incorrect_ctr = 1, min(floor((N - 1) / 2), N - 4) do
init_set(is_sat_incorrect, N, incorrect_ctr)
repeat
err, X, Y, Z = try_this_subset_of_sat(list_of_satellites, is_sat_incorrect, previous_X, previous_Y, previous_Z)
if err < 0.1 then
for j = 1, N do
if is_sat_incorrect[j] then
table_insert(incorrect_sat_indices, j)
end
end
return X, Y, Z, incorrect_sat_indices
end
until last_combination(is_sat_incorrect)
end
end
end
end
用法示例:
-- assuming your mobile computer previous coordinates were 99 120 100
local previous_X, previous_Y, previous_Z = 99, 120, 100
-- assuming your mobile computer current coordinates are 111 112 113
local list_of_satellites = {
{x=22, y=55, z=77, distance=((111-22)^2+(112-55)^2+(113-77)^2)^0.5}, -- correct satellite
{x=35, y=99, z=42, distance=((111-35)^2+(112-99)^2+(113-42)^2)^0.5}, -- correct satellite
{x=44, y=44, z=44, distance=((111-94)^2+(112-94)^2+(113-94)^2)^0.5}, -- incorrect satellite
{x=10, y=88, z=70, distance=((111-10)^2+(112-88)^2+(113-70)^2)^0.5}, -- correct satellite
{x=54, y=54, z=54, distance=((111-64)^2+(112-64)^2+(113-64)^2)^0.5}, -- incorrect satellite
{x=91, y=33, z=15, distance=((111-91)^2+(112-33)^2+(113-15)^2)^0.5}, -- correct satellite
}
local X, Y, Z, list_of_incorrect_sat_indices = trilateration(list_of_satellites, previous_X, previous_Y, previous_Z)
if X then
print(X, Y, Z)
if #list_of_incorrect_sat_indices > 0 then
print("Satellites at the following indices are incorrect: "..table.concat(list_of_incorrect_sat_indices, ","))
end
else
print"Not enough satellites"
end
输出:
111 112 113
Satellites at the following indices are incorrect: 3,5