forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrainTransportAIDriver.lua
167 lines (140 loc) · 5.33 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
--[[
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)
courseplay.debugVehicle(11,vehicle,'GrainTransportAIDriver:init()')
AIDriver.init(self, vehicle)
self.mode = courseplay.MODE_GRAIN_TRANSPORT
self.runCounter = 0
-- just for backwards compatibility
end
function GrainTransportAIDriver:setHudContent()
AIDriver.setHudContent(self)
courseplay.hud:setGrainTransportAIDriverContent(self.vehicle)
end
function GrainTransportAIDriver:start(ix)
self.vehicle:setCruiseControlMaxSpeed(self.vehicle:getSpeedLimit() or math.huge)
self:beforeStart()
AIDriver.start(self, ix)
self:setDriveUnloadNow(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
self:searchForTipTriggers()
allowedToDrive = self:load(allowedToDrive)
allowedToDrive, giveUpControl = self:onUnLoadCourse(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')
self:setDriveUnloadNow(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
--temp solution till load_tippers is refactored
-- and we can have a non cyclic value that the loading process is finished
if newIx == 4 and self:getDriveUnloadNow() then
self:incrementRunCounter()
end
end
-- TODO: move this into onWaypointPassed() instead
function GrainTransportAIDriver:checkLastWaypoint()
local allowedToDrive = true
if self.ppc:getCurrentWaypointIx() == self.course:getNumberOfWaypoints() then
courseplay:openCloseCover(self.vehicle, not courseplay.SHOW_COVERS)
if self.runCounter >= self.vehicle.cp.maxRunNumber 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)
else
-- continue at the first waypoint
self.ppc:initialize(1)
self:debug('Finished run %d, continue with next.', self.runCounter)
end
end
return allowedToDrive
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:updateLights()
self.vehicle:setBeaconLightsVisibility(false)
end
function GrainTransportAIDriver:getCanShowDriveOnButton()
return self:isNearFillPoint()
end
function GrainTransportAIDriver:getIsRunCounterActive()
return self.vehicle.cp.runCounterActive;
end
function GrainTransportAIDriver:incrementRunCounter()
if self:getIsRunCounterActive() then
self.runCounter = self.runCounter + 1
end
end
function GrainTransportAIDriver:resetRunCounter()
self.runCounter = 0
end