forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrainTransportAIDriver.lua
281 lines (242 loc) · 9.98 KB
/
GrainTransportAIDriver.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
--[[
This file is part of Courseplay (https://github.com/Courseplay/courseplay)
Copyright (C) 2018 Peter Vajko
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
]]
---@class GrainTransportAIDriver : AIDriver
GrainTransportAIDriver = CpObject(AIDriver)
--- Constructor
function GrainTransportAIDriver:init(vehicle)
AIDriver.init(self, vehicle)
self.mode = courseplay.MODE_GRAIN_TRANSPORT
self.runCounter = 0
-- just for backwards compatibility
self.vehicle.cp.runCounter = self.runCounter
end
function GrainTransportAIDriver:start(ix)
self.vehicle:setCruiseControlMaxSpeed(self.vehicle:getSpeedLimit() or math.huge)
self:beforeStart()
AIDriver.start(self, ix)
self.runCounter = 0
-- due to lack of understanding what exactly isLoaded means and where is it set to false in mode 1,
-- we just set it to false here so load_tippers() will actually attempt to load the tippers...
courseplay:setIsLoaded(self.vehicle, false);
end
function GrainTransportAIDriver:isAlignmentCourseNeeded(ix)
-- never use alignment course for grain transport mode
return false
end
function GrainTransportAIDriver:drive(dt)
-- make sure we apply the unload offset when needed
self:updateOffset()
-- update current waypoint/goal point
self.ppc:update()
self:updateInfoText()
-- RESET TRIGGER RAYCASTS from drive.lua.
-- TODO: Not sure how raycast can be called twice if everything is coded cleanly.
self.vehicle.cp.hasRunRaycastThisLoop['tipTrigger'] = false
self.vehicle.cp.hasRunRaycastThisLoop['specialTrigger'] = false
courseplay:updateFillLevelsAndCapacities(self.vehicle)
-- should we give up control so some other code can drive?
local giveUpControl = false
-- should we keep driving?
local allowedToDrive = self:checkLastWaypoint()
-- TODO: are these checks really necessary?
if self.vehicle.cp.totalFillLevel ~= nil
and self.vehicle.cp.tipRefOffset ~= nil
and self.vehicle.cp.workToolAttached then
local lx, lz = self:getDirectionToGoalPoint()
self:searchForTipTrigger(lx, lz)
allowedToDrive = self:load(allowedToDrive)
allowedToDrive, giveUpControl = self:unLoad(allowedToDrive, dt)
else
self:debug('Safety check failed')
end
-- TODO: clean up the self.allowedToDrives above and use a local copy
if self.state == self.states.STOPPED or not allowedToDrive then
self:hold()
end
if giveUpControl then
-- unload_tippers does the driving
return
else
-- collision detection
self:detectCollision(dt)
-- we drive the course as usual
self:driveCourse(dt)
end
self:resetSpeed()
end
function GrainTransportAIDriver:onWaypointChange(newIx)
self:debug('On waypoint change %d', newIx)
AIDriver.onWaypointChange(self, newIx)
if self.course:isLastWaypointIx(newIx) then
self:debug('Reaching last waypoint')
-- this is needed to trigger the loading. No idea why. No idea what isLoaded specifically means,
-- no idea why start_stop.start sets it to true. frustrating
courseplay:setIsLoaded(self.vehicle, false);
courseplay:changeRunCounter(self.vehicle, false)
end
-- Close cover after leaving the silo, assuming the silo is at waypoint 1
if not self:hasTipTrigger() and not self:isNearFillPoint() then
courseplay:openCloseCover(self.vehicle, courseplay.SHOW_COVERS)
end
end
function GrainTransportAIDriver:hasTipTrigger()
-- TODO: come up with something better?
return self.vehicle.cp.currentTipTrigger ~= nil
end
function GrainTransportAIDriver:getSpeed()
if self:hasTipTrigger() then
-- slow down around the tip trigger
if self:getIsInBunksiloTrigger() then
return self.vehicle.cp.speeds.reverse
else
return 10
end
else
return AIDriver.getSpeed(self)
end
end
function GrainTransportAIDriver:getIsInBunksiloTrigger()
return self.vehicle.cp.backupUnloadSpeed ~= nil
end
function GrainTransportAIDriver:checkLastWaypoint()
local allowedToDrive = true
if self.ppc:reachedLastWaypoint() then
courseplay:openCloseCover(self.vehicle, not courseplay.SHOW_COVERS)
-- Don't make life too complicated. Whenever we restart the course, we just
-- increment the run counter
-- TODO: check if it makes sense to use the totalFillLevel changing to 0 as a trigger.
self.runCounter = self.runCounter + 1
if self.runCounter >= self.vehicle.cp.runNumber then
-- stop at the last waypoint when the run counter expires
allowedToDrive = false
self:stop('END_POINT_MODE_1')
self:debug('Last run (%d) finished, stopping.', self.runCounter)
self.runCounter = 0
else
-- continue at the first waypoint
self.ppc:initialize(1)
self:debug('Finished run %d, continue with next.', self.runCounter)
end
-- just for backwards compatibility
self.vehicle.cp.runCounter = self.runCounter
end
return allowedToDrive
end
function GrainTransportAIDriver:searchForTipTrigger(lx, lz)
if not self.vehicle.cp.hasAugerWagon
and not self:hasTipTrigger()
and self.vehicle.cp.totalFillLevel > 0
and self.ppc:getCurrentWaypointIx() > 2
and not self.ppc:reachedLastWaypoint()
and not self.ppc:isReversing() then
local nx, ny, nz = localDirectionToWorld(self.vehicle.cp.DirectionNode, lx, -0.1, lz)
-- raycast start point in front of vehicle
local x, y, z = localToWorld(self.vehicle.cp.DirectionNode, 0, 1, 3)
courseplay:doTriggerRaycasts(self.vehicle, 'tipTrigger', 'fwd', true, x, y, z, nx, ny, nz)
end
end
function GrainTransportAIDriver:load(allowedToDrive)
-- Loading
-- tippers are not full
if self:isNearFillPoint() and self.vehicle.cp.totalFillLevel <= self.vehicle.cp.totalCapacity then
allowedToDrive = courseplay:load_tippers(self.vehicle, allowedToDrive);
courseplay:setInfoText(self.vehicle, string.format("COURSEPLAY_LOADING_AMOUNT;%d;%d",courseplay.utils:roundToLowerInterval(self.vehicle.cp.totalFillLevel, 100),self.vehicle.cp.totalCapacity));
courseplay:openCloseCover(self.vehicle, not courseplay.SHOW_COVERS)
end
return allowedToDrive
end
function GrainTransportAIDriver:unLoad(allowedToDrive, dt)
-- Unloading
local takeOverSteering = false
-- If we are an auger wagon, we don't have a tip point, so handle it as an auger wagon in mode 3
-- This should be in drive.lua on line 305 IMO --pops64
if self.vehicle.cp.hasAugerWagon then
courseplay:handleMode3(self.vehicle, allowedToDrive, dt);
else
--handle cover
if self:hasTipTrigger() then
courseplay:openCloseCover(self.vehicle, not courseplay.SHOW_COVERS)
end
-- done tipping?
if self:hasTipTrigger() and self.vehicle.cp.totalFillLevel == 0 then
courseplay:resetTipTrigger(self.vehicle, true);
end
self:cleanUpMissedTriggerExit()
-- tipper is not empty and tractor reaches TipTrigger
if self.vehicle.cp.totalFillLevel > 0
and self:hasTipTrigger()
and not self:isNearFillPoint() then
allowedToDrive, takeOverSteering = courseplay:unload_tippers(self.vehicle, allowedToDrive, dt);
courseplay:setInfoText(self.vehicle, "COURSEPLAY_TIPTRIGGER_REACHED");
end
end
return allowedToDrive, takeOverSteering;
end;
function GrainTransportAIDriver:cleanUpMissedTriggerExit() -- at least that's what it seems to be doing
-- damn, I missed the trigger!
if self:hasTipTrigger() then
local t = self.vehicle.cp.currentTipTrigger;
local trigger_id = t.triggerId;
if t.specialTriggerId ~= nil then
trigger_id = t.specialTriggerId;
end;
if t.isPlaceableHeapTrigger then
trigger_id = t.rootNode;
end;
if trigger_id ~= nil then
local trigger_x, _, trigger_z = getWorldTranslation(trigger_id)
local ctx, _, ctz = getWorldTranslation(self.vehicle.cp.DirectionNode)
local distToTrigger = courseplay:distance(ctx, ctz, trigger_x, trigger_z)
-- Start reversing value is to check if we have started to reverse
-- This is used in case we already registered a tipTrigger but changed the direction and might not be in that tipTrigger when unloading. (Bug Fix)
local startReversing = self.course:switchingToReverseAt(self.ppc:getCurrentWaypointIx() - 1)
if startReversing then
courseplay:debug(string.format("%s: Is starting to reverse. Tip trigger is reset.", nameNum(self.vehicle)), 13);
end
local isBGA = t.bunkerSilo ~= nil
local triggerLength = Utils.getNoNil(self.vehicle.cp.currentTipTrigger.cpActualLength, 20)
local maxDist = isBGA and (self.vehicle.cp.totalLength + 55) or (self.vehicle.cp.totalLength + triggerLength);
if distToTrigger > maxDist or startReversing then --it's a backup, so we don't need to care about +/-10m
courseplay:resetTipTrigger(self.vehicle)
courseplay:debug(string.format("%s: distance to currentTipTrigger = %d (> %d or start reversing) --> currentTipTrigger = nil", nameNum(self.vehicle), distToTrigger, maxDist), 1);
end
else
courseplay:resetTipTrigger(self.vehicle)
end;
end;
end
--- Update the unload offset from the current settings and apply it when needed
function GrainTransportAIDriver:updateOffset()
local currentWaypointIx = self.ppc:getCurrentWaypointIx()
local useOffset = false
if not self.vehicle.cp.hasAugerWagon and (currentWaypointIx > self.course:getNumberOfWaypoints() - 6 or currentWaypointIx <= 4) then
-- around the fill trigger (don't understand the auger wagon part though)
useOffset = true
elseif self.course:hasWaitPointAround(currentWaypointIx, 6, 3) then
-- around wait points
useOffset = true
elseif self.course:hasUnloadPointAround(currentWaypointIx, 6, 3) then
-- around unload points
useOffset = true
end
if useOffset then
self.ppc:setOffset(self.vehicle.cp.loadUnloadOffsetX, self.vehicle.cp.loadUnloadOffsetZ)
else
self.ppc:setOffset(0, 0)
end
end
function GrainTransportAIDriver:updateLights()
self.vehicle:setBeaconLightsVisibility(false)
end