Skip to content

Commit

Permalink
v6.01.00079 Small island circling improved.
Browse files Browse the repository at this point in the history
  • Loading branch information
pvaiko committed Feb 17, 2019
1 parent 906b3b8 commit d41bd07
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
2 changes: 0 additions & 2 deletions AIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ function AIDriver:init(vehicle)
self.allowedToDrive = true
self.collisionDetectionEnabled = false
self.collisionDetector = CollisionDetector(self.vehicle)
self.collisionDetector:adaptCollisHeight()
end

-- destructor. The reason for having this is the collisionDetector which creates nodes and
Expand Down Expand Up @@ -630,7 +629,6 @@ function AIDriver:detectCollision(dt)
-- if no detector yet, no problem, create it now.
if not self.collisionDetector then
self.collisionDetector = CollisionDetector(self.vehicle)
self.collisionDetector:adaptCollisHeight()
end

local isInTraffic, trafficSpeed = self.collisionDetector:getStatus(dt)
Expand Down
61 changes: 59 additions & 2 deletions FieldworkAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ function FieldworkAIDriver:onWaypointChange(ix)
self:debug('onWaypointChange %d', ix)
if self.state == self.states.ON_FIELDWORK_COURSE then
self:updateRemainingTime(ix)
self:calculateTightTurnOffset()
if self.fieldworkState == self.states.ON_CONNECTING_TRACK then
if not self.course:isOnConnectingTrack(ix) then
-- reached the end of the connecting track, back to work
Expand All @@ -299,7 +300,7 @@ function FieldworkAIDriver:onWaypointChange(ix)
end
end
if self.fieldworkState == self.states.TEMPORARY then
-- band aid to make sure we have our implements lowered by the time we end the
-- band aid to make sure we have our implements lowered by the time we end the`
-- temporary course
if ix == self.course:getNumberOfWaypoints() then
self:debug('temporary (alignment) course is about to end, start work')
Expand Down Expand Up @@ -455,7 +456,8 @@ end
function FieldworkAIDriver:updateFieldworkOffset()
-- (as lua passes tables by reference, we can directly change self.fieldworkCourse even if we passed self.course
-- to the PPC to drive)
self.fieldworkCourse:setOffset(self.vehicle.cp.totalOffsetX + self.aiDriverOffsetX, self.vehicle.cp.toolOffsetZ + self.aiDriverOffsetZ)
self.fieldworkCourse:setOffset(self.vehicle.cp.totalOffsetX + self.aiDriverOffsetX + (self.tightTurnOffset or 0),
self.vehicle.cp.toolOffsetZ + self.aiDriverOffsetZ)
end

function FieldworkAIDriver:hasSameCourse(otherVehicle)
Expand Down Expand Up @@ -612,3 +614,58 @@ function FieldworkAIDriver:calculateLoweringDuration()
self.startedLoweringAt = nil
end
end



--- If we are towing an implement, move to a bigger radius in tight turns
-- making sure that the towed implement's trajectory remains closer to the
-- course.
function FieldworkAIDriver:calculateTightTurnOffset()
local function smoothOffset(offset)
self.tightTurnOffset = (offset + 3 * (self.tightTurnOffset or 0 )) / 4
return self.tightTurnOffset
end
-- first of all, does the current waypoint have radius data?
local r = self.course:getWaypointRadius(self.ppc:getCurrentWaypointIx())
if not r then
return smoothOffset(0)
end

-- is there a wheeled implement behind the tractor and is it on a pivot?
local workTool = courseplay:getFirstReversingWheeledWorkTool(self.vehicle)
if not workTool or not workTool.cp.realTurningNode then
return smoothOffset(0)
end

-- get the distance between the tractor and the towed implement's turn node
-- (not quite accurate when the angle between the tractor and the tool is high)
local tractorX, _, tractorZ = getWorldTranslation( self.vehicle.cp.DirectionNode )
local toolX, _, toolZ = getWorldTranslation( workTool.cp.realTurningNode )
local towBarLength = courseplay:distance( tractorX, tractorZ, toolX, toolZ )

-- Is this really a tight turn? It is when the tow bar is longer than radius / 3, otherwise
-- we ignore it.
if towBarLength < r / 3 then
return smoothOffset(0)
end

-- Ok, looks like a tight turn, so we need to move a bit left or right of the course
-- to keep the tool on the course.
local rTractor = math.sqrt( r * r + towBarLength * towBarLength ) -- the radius the tractor should be on
local offset = rTractor - r

-- figure out left or right now?
local nextAngle = self.course:getWaypointAngleDeg(self.ppc:getCurrentWaypointIx() + 1)
local currentAngle = self.course:getWaypointAngleDeg(self.ppc:getCurrentWaypointIx())
if not nextAngle or not currentAngle then
return smoothOffset(0)
end

if getDeltaAngle(math.rad(nextAngle), math.rad(currentAngle)) > 0 then offset = -offset end

-- smooth the offset a bit to avoid sudden changes
smoothOffset(offset)
self:debug('Tight turn, r = %.1f, tow bar = %.1f m, currentAngle = %.0f, nextAngle = %.0f, offset = %.1f, smoothOffset = %.1f', r, towBarLength, currentAngle, nextAngle, offset, self.tightTurnOffset )
-- remember the last value for smoothing
return self.tightTurnOffset
end
2 changes: 1 addition & 1 deletion TrafficCollision.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function CollisionDetector:init(vehicle, course)
self.requiredNumTriggers = 4
self.trafficCollisionTriggers = {}
self:createTriggers()

self:adaptCollisHeight()
end

-- destructor
Expand Down
7 changes: 6 additions & 1 deletion Waypoint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function Waypoint:set(cpWp, cpIndex)
self.x = cpWp.cx or cpWp.posX or cpWp.x or 0
self.z = cpWp.cz or cpWp.posZ or cpWp.z or 0
self.angle = cpWp.angle or nil
self.radius = cpWp.radius or nil
self.rev = cpWp.rev or false
self.speed = cpWp.speed
self.cpIndex = cpIndex or 0
Expand Down Expand Up @@ -364,13 +365,17 @@ function Course:getWaypointLocalPosition(node, ix)
end

function Course:getWaypointAngleDeg(ix)
return self.waypoints[ix].angle
return self.waypoints[math.min(#self.waypoints, ix)].angle
end

function Course:getRidgeMarkerState(ix)
return self.waypoints[ix].ridgeMarker or 0
end

function Course:getWaypointRadius(ix)
return self.waypoints[ix].radius
end

--- Get the average speed setting across n waypoints starting at ix
function Course:getAverageSpeed(ix, n)
local total, count = 0, 0
Expand Down
2 changes: 1 addition & 1 deletion modDesc.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<modDesc descVersion="40">
<version>6.01.00078</version>
<version>6.01.00079</version>
<author><![CDATA[Courseplay.devTeam]]></author>
<title>
<br>CoursePlay SIX</br>
Expand Down

0 comments on commit d41bd07

Please sign in to comment.