Skip to content

Commit f611ac7

Browse files
authored
Merge pull request #955 from Courseplay/940-harvester-back-measurement
fix: more robust harvester back measurement
2 parents 5edc7a4 + be819b8 commit f611ac7

5 files changed

Lines changed: 35 additions & 16 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/parameters/AIParameterSettingList.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ end
394394
---@return boolean value is not valid and could not be set.
395395
function AIParameterSettingList:setFloatValue(value, epsilon, noEventSend)
396396
local failed = setValueInternal(self, value, function(a, b)
397-
local epsilon = epsilon or self.data.incremental or 0.1
397+
epsilon = epsilon or self.data.incremental or 0.1
398398
if a == nil or b == nil then return false end
399399
return a > b - epsilon / 2 and a <= b + epsilon / 2 end)
400400
if not failed and not noEventSend then
@@ -450,7 +450,7 @@ function AIParameterSettingList:setDefault(noEventSend)
450450
local value = g_vehicleConfigurations:get(object, configName)
451451
if value then
452452
if tonumber(value) then
453-
self:setFloatValue(value, noEventSend)
453+
self:setFloatValue(value, nil, noEventSend)
454454
else
455455
self:setValue(value, noEventSend)
456456
end
@@ -462,7 +462,7 @@ function AIParameterSettingList:setDefault(noEventSend)
462462
end
463463
--- If default values were setup use these.
464464
if self.data.default ~=nil then
465-
AIParameterSettingList.setFloatValue(self, self.data.default, noEventSend)
465+
AIParameterSettingList.setFloatValue(self, self.data.default, nil, noEventSend)
466466
self:debug("set to default %s", self.data.default)
467467
return
468468
end

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

scripts/courseGenerator/Headland.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ function Headland:connectTo(other, ix, workingWidth, turningRadius, headlandFirs
190190
length, transitionEndIx, i)
191191
return transitionEndIx
192192
else
193-
self.logger:warning('Generated path to next headland too long (%.1f > %.1f), try %d.',
193+
self.logger:debug('Generated path to next headland too long (%.1f > %.1f), try %d.',
194194
length, maxPlausiblePathLength, i)
195195
end
196196
if 0.8 * radius > turningRadius then

0 commit comments

Comments
 (0)