Skip to content

Commit

Permalink
fix: reverse offset bug
Browse files Browse the repository at this point in the history
Yet another bug that would never have happened
if we were using a compiled language...
  • Loading branch information
pvaiko committed Sep 29, 2024
1 parent cb5a2c4 commit f8438e2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion scripts/Course.lua
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function Course:enrichWaypointData(startIx)
self.waypoints[i].curvature = i == 1 and 0 or 1 / self:calculateSignedRadius(i)
if (self:isReverseAt(i) and not self:switchingToForwardAt(i)) or self:switchingToReverseAt(i) then
-- X offset must be reversed at waypoints where we are driving in reverse
self.waypoints[i]:setReverseOffset()
self.waypoints[i]:setReverseOffset(true)
end
end
-- make the last waypoint point to the same direction as the previous so we don't
Expand Down
6 changes: 3 additions & 3 deletions scripts/Waypoint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function Waypoint:getOffsetPosition(offsetX, offsetZ, dx, dz)
-- check for NaN
if deltaX and deltaZ and deltaX == deltaX and deltaZ == deltaZ then
-- X offset should be inverted if we drive reverse here (left is always left regardless of the driving direction)
local reverse = self.rev and -1 or 1
local reverse = self.reverseOffset and -1 or 1
x = x - deltaZ * reverse * offsetX + deltaX * offsetZ
z = z + deltaX * reverse * offsetX + deltaZ * offsetZ
end
Expand Down Expand Up @@ -203,8 +203,8 @@ function Waypoint:setPosition(x, z, y)
end
end

function Waypoint:setReverseOffset()
self.reverseOffset = true
function Waypoint:setReverseOffset(reverseOffset)
self.reverseOffset = reverseOffset
end

function Waypoint:translate(dx, dz)
Expand Down
7 changes: 3 additions & 4 deletions scripts/ai/turns/TurnManeuver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,7 @@ end
---@param dBack number distance in meters to move the course back (positive moves it backwards!)
---@param ixBeforeEndingTurnSection number index of the last waypoint of the actual turn, if we can finish the turn
--- before we reach the vehicle position at turn end, there's no reversing needed at the turn end.
---@param endingTurnLength number length of the straight ending turn section into the next row
function TurnManeuver:adjustCourseToFitField(course, dBack, ixBeforeEndingTurnSection, endingTurnLength)
function TurnManeuver:adjustCourseToFitField(course, dBack, ixBeforeEndingTurnSection)
self:debug('moving course back: d=%.1f', dBack)
local reversingOffset = self:getReversingOffset()
-- generate a straight reverse section first (less than 1 m step should make sure we always end up with
Expand All @@ -284,7 +283,7 @@ function TurnManeuver:adjustCourseToFitField(course, dBack, ixBeforeEndingTurnSe
self:debug('Reverse to work start (implement in back)')
-- vehicle in front of the work start node at turn end
local forwardAfterTurn = Course.createFromNode(self.vehicle, self.turnContext.vehicleAtTurnEndNode, 0,
dFromTurnEnd, dFromTurnEnd + self.steeringLength, 0.8, false)
dFromTurnEnd + 1, dFromTurnEnd + 1 + self.steeringLength, 0.8, false)
courseWithReversing:append(forwardAfterTurn)
-- allow early direction change when aligned
TurnManeuver.setTurnControlForLastWaypoints(courseWithReversing, forwardAfterTurn:getLength(),
Expand Down Expand Up @@ -340,7 +339,7 @@ function AnalyticTurnManeuver:init(vehicle, turnContext, vehicleDirectionNode, t
end
local ixBeforeEndingTurnSection = self.course:getNumberOfWaypoints()
endingTurnLength = 10 --self.turnContext:appendEndingTurnCourse(self.course, steeringLength, self.applyTightTurnOffset)
self.course = self:adjustCourseToFitField(self.course, dBack, ixBeforeEndingTurnSection, endingTurnLength)
self.course = self:adjustCourseToFitField(self.course, dBack, ixBeforeEndingTurnSection)
else
if self.applyTightTurnOffset then
self.course:setUseTightTurnOffsetForLastWaypoints(
Expand Down

0 comments on commit f8438e2

Please sign in to comment.