Skip to content

Commit 102e521

Browse files
author
Peter Vaiko
committed
fix: more robust harvester back measurement
Measuring the length of harvesters with raycast did not work as expected, because some, like the Oxbo is "transparent" at the default 1.5 m height. Measure at multiple heights, also consider side offset for towed harvesters now. Measure every 10 seconds to account for a towed harvester at an angle when starting. Should help with unloaders approaching the harvesters from further back. #940
1 parent 6e4b6c7 commit 102e521

3 files changed

Lines changed: 31 additions & 12 deletions

File tree

scripts/CpUtil.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,10 @@ function CpUtil.getDefaultCollisionFlags()
492492
CollisionFlag.VEHICLE + CollisionFlag.BUILDING + CollisionFlag.STATIC_OBJECT
493493
end
494494

495+
function CpUtil.getVehicleCollisionFlags()
496+
return CollisionFlag.VEHICLE
497+
end
498+
495499
--- Removes all event listeners of a given Spec Class.
496500
function CpUtil.removeEventListenersBySpecialization(vehicle, specClass)
497501
for _, listeners in pairs(vehicle.eventListeners) do

scripts/ai/Markers.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ local function setBackMarkerNode(vehicle, measuredBackDistance)
4545
lastImplement, backMarkerOffset = AIUtil.getLastAttachedImplement(vehicle)
4646
referenceNode = AIUtil.getDirectionNode(vehicle)
4747
CpUtil.debugVehicle(CpDebug.DBG_IMPLEMENTS, vehicle, 'Using the last implement\'s rear distance for the back marker node, %d m from root node', backMarkerOffset)
48-
elseif measuredBackDistance then
48+
elseif measuredBackDistance and measuredBackDistance ~= 0 then
4949
referenceNode = AIUtil.getDirectionNode(vehicle)
5050
backMarkerOffset = -measuredBackDistance
5151
CpUtil.debugVehicle(CpDebug.DBG_IMPLEMENTS, vehicle, 'back marker node on measured back distance %.1f', measuredBackDistance)

scripts/ai/strategies/AIDriveStrategyCombineCourse.lua

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ end
234234

235235
function AIDriveStrategyCombineCourse:getDriveData(dt, vX, vY, vZ)
236236
self:handlePipe(dt)
237+
self:measureBackDistance()
237238
Markers.refreshMarkerNodes(self.vehicle, self.measuredBackDistance)
238239
if self.temporaryHold:get() then
239240
self:setMaxSpeed(0)
@@ -2058,34 +2059,48 @@ end
20582059
--- Proximity
20592060
------------------------------------------------------------------------------------------------------------------------
20602061

2061-
AIDriveStrategyCombineCourse.maxBackDistance = 10
2062+
AIDriveStrategyCombineCourse.maxBackDistance = 20
20622063

20632064
function AIDriveStrategyCombineCourse:getMeasuredBackDistance()
20642065
return self.measuredBackDistance
20652066
end
20662067

20672068
--- Determine how far the back of the combine is from the direction node
2068-
-- TODO: attached/towed harvesters
20692069
function AIDriveStrategyCombineCourse:measureBackDistance()
2070-
self.measuredBackDistance = 0
2071-
-- raycast from a point behind the vehicle forward towards the direction node
2072-
local nx, ny, nz = localDirectionToWorld(AIUtil.getDirectionNode(self.vehicle), 0, 0, 1)
2073-
local x, y, z = localToWorld(AIUtil.getDirectionNode(self.vehicle), 0, 1.5, -self.maxBackDistance)
2074-
raycastAll(x, y, z, nx, ny, nz, self.maxBackDistance, 'raycastBackCallback', self)
2070+
-- check and adjust every 10 seconds, mainly for the towed harvesters which may be at an angle when
2071+
-- the driver starts and our raycast may miss it.
2072+
if not self.lastBackMeasurementDue then
2073+
self.lastBackMeasurementDue = CpTemporaryObject(true)
2074+
end
2075+
if not self.lastBackMeasurementDue:get() then
2076+
return
2077+
end
2078+
self.lastBackMeasurementDue:set(false, 10000)
2079+
self.measuredBackDistance = self.measuredBackDistance or 0
2080+
local previousMeasuredBackDistance = self.measuredBackDistance
2081+
-- raycast from a point behind the vehicle forward towards the direction node, in different heights as harvesters
2082+
-- can be very tricky, such as the Oxbo, which we'll get no hits at all at exactly 1.5 meters...
2083+
for height = 1.3, 2.5, 0.2 do
2084+
local nx, ny, nz = localDirectionToWorld(AIUtil.getDirectionNode(self.vehicle), 0, 0, 1)
2085+
-- use the tool offset, that should cover offset towed harvesters as well
2086+
local x, y, z = localToWorld(AIUtil.getDirectionNode(self.vehicle),
2087+
self.settings.toolOffsetX:getValue(), height, -self.maxBackDistance)
2088+
raycastAll(x, y, z, nx, ny, nz, self.maxBackDistance, 'raycastBackCallback', self, CpUtil.getVehicleCollisionFlags())
2089+
end
2090+
if previousMeasuredBackDistance ~= self.measuredBackDistance then
2091+
self:debug('Measured back distance changed from %.1f to %.1f m', previousMeasuredBackDistance, self.measuredBackDistance)
2092+
end
20752093
end
20762094

20772095
-- I believe this tries to figure out how far the back of a combine is from its direction node.
20782096
function AIDriveStrategyCombineCourse:raycastBackCallback(hitObjectId, x, y, z, distance, nx, ny, nz, subShapeIndex)
20792097
if hitObjectId ~= 0 then
20802098
local object = g_currentMission:getNodeObject(hitObjectId)
2081-
if object and object == self.vehicle then
2099+
if object and object.getRootVehicle and object:getRootVehicle() == self.vehicle then
20822100
local d = self.maxBackDistance - distance
20832101
if d > self.measuredBackDistance then
20842102
self.measuredBackDistance = d
2085-
self:debug('Measured back distance is %.1f m', self.measuredBackDistance)
20862103
end
2087-
else
2088-
return true
20892104
end
20902105
end
20912106
end

0 commit comments

Comments
 (0)