Skip to content

Commit

Permalink
fix: recovery turn logic
Browse files Browse the repository at this point in the history
Also, proximity controller calls the blocked object listener
once only, then restarts the timer.

#3512
  • Loading branch information
pvaiko committed Oct 27, 2024
1 parent 4320647 commit a6c1991
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions scripts/CpObject.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ end

--- Set temporary value for object
---@param value any the temporary value
---@param expiryMs number for expiryMs milliseconds after startMs, the object will return the value set above,
---@param expiryMs number|nil for expiryMs milliseconds after startMs, the object will return the value set above,
--- valueWhenExpired otherwise. When nil, it'll remain value forever
---@param startMs number after starMs milliseconds from now, the object will return the value set above
---@param startMs number|nil after starMs milliseconds from now, the object will return the value set above
--- (for expiryMs milliseconds). When nil, the value is set immediately.
function CpTemporaryObject:set(value, expiryMs, startMs)
self.value = value
Expand Down
1 change: 1 addition & 0 deletions scripts/ai/ProximityController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ function ProximityController:getDriveData(maxSpeed, moveForwards)
-- have been blocked by an object long enough
CpUtil.debugVehicle(CpDebug.DBG_TRAFFIC, self.vehicle, 'An object has been blocking us for a while at %.1f m', d)
self:onBlockingObject(not moveForwards)
self.blockingObject:set(false, nil, nil)
elseif not self.blockingObject:isPending() then
-- first time we are being blocked
CpUtil.debugVehicle(CpDebug.DBG_TRAFFIC, self.vehicle, 'An object blocking us at %.1f m', d)
Expand Down
14 changes: 9 additions & 5 deletions scripts/ai/turns/AITurn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,18 @@ function AITurn:turn()
return nil, nil, nil, self:getForwardSpeed()
end

function AITurn:onBlocked()
function AITurn:startRecoveryTurn(...)
-- clean up before giving back the control to the drive strategy
-- unregister here before the AITurn object is destructed
self.proximityController:unregisterBlockingObjectListener()
-- restore onWaypoint* listeners before giving back control to the drive strategy, otherwise they'll be lost
-- when the recovery turn is instantiated and registers its own listeners
self.ppc:restorePreviouslyRegisteredListeners()
self.driveStrategy:startRecoveryTurn(1 * self.turningRadius)
self.driveStrategy:startRecoveryTurn(...)
end

function AITurn:onBlocked()
self:startRecoveryTurn(1 * self.turningRadius)
end

function AITurn:onWaypointChange(ix)
Expand Down Expand Up @@ -834,14 +839,13 @@ function RecoveryTurn:onWaypointPassed(ix, course)
end

function RecoveryTurn:onBlocked()
-- unregister here before the AITurn object is destructed
self.proximityController:unregisterBlockingObjectListener()
if self.retryCount < 1 then
self:debug('Recovering from blocked turn unsuccessful after %d tries, trying again.', self.retryCount + 1)
-- back up a bit more and see if that works
self.driveStrategy:startRecoveryTurn(0.5 * self.turningRadius, self.retryCount + 1)
self:startRecoveryTurn(0.5 * self.turningRadius, self.retryCount + 1)
else
self:debug('Recovering from blocked turn unsuccessful, giving up after %d tries.', self.retryCount + 1)
self:resumeFieldworkAfterTurn(self.turnContext.turnEndWpIx)
end
end

Expand Down

0 comments on commit a6c1991

Please sign in to comment.