This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 525
/
Copy pathturn.lua
2307 lines (1993 loc) · 120 KB
/
turn.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local abs, min, max, floor, ceil, square, pi, rad, deg = math.abs, math.min, math.max, math.floor, math.ceil, math.sqrt, math.pi, math.rad, math.deg;
local _; --- The _ is an discard character for values not needed. Setting it to local, prevent's it from being an global variable.
--- SET VALUES
local wpDistance = 1.5; -- Waypoint Distance in Straight lines
local wpCircleDistance = 1; -- Waypoint Distance in circles
-- if the direction difference between turnStart and turnEnd is bigger than this then
-- we consider that as a turn when switching to the next up/down lane and assume that
-- after the turn we'll be heading into the opposite direction.
local laneTurnAngleThreshold = 150
---@param turnContext TurnContext
function courseplay:turn(vehicle, dt, turnContext)
-- TODO: move this to TurnContext?
local realDirectionNode = AIDriverUtil.getDirectionNode(vehicle)
local moveForwards = true;
local lx, lz = 0, 1;
local dtpX, dtpZ = 0, 1;
local wpChangeDistance = 3;
local reverseWPChangeDistance = 5;
local reverseWPChangeDistanceWithTool = 3;
local isHarvester = Utils.getNoNil(
courseplay:isCombine(vehicle) or
courseplay:isChopper(vehicle) or
vehicle.cp.driver.isACombineAIDriver, false);
--- This is in case we use manually recorded fieldswork course and not generated.
if not vehicle.cp.courseWorkWidth then
vehicle.cp.courseWorkWidth = vehicle.cp.courseGeneratorSettings.workWidth:getAutoWorkWidth();
end;
--- Get front and back markers
local frontMarker = turnContext.frontMarkerDistance
local backMarker = turnContext.backMarkerDistance
local _, vehicleY, _ = getWorldTranslation(realDirectionNode);
----------------------------------------------------------
-- Debug prints
----------------------------------------------------------
if courseplay.debugChannels[courseplay.DBG_TURN] then
if #vehicle.cp.turnTargets > 0 then
-- Draw debug points for waypoints.
for index, turnTarget in ipairs(vehicle.cp.turnTargets) do
if index < #vehicle.cp.turnTargets then
local nextTurnTarget = vehicle.cp.turnTargets[index + 1];
local posY = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, turnTarget.posX, 300, turnTarget.posZ);
local nextPosY = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, nextTurnTarget.posX, 300, nextTurnTarget.posZ);
if turnTarget.turnReverse then
local color = { r = 0, g = 1, b = 0}; -- Green Line
--if not nextTurnTarget.turnReverse then
-- color["r"], color["g"], color["b"] = 0, 1, 1; -- Light Blue Line
--end
cpDebug:drawLine(turnTarget.posX, posY + 3, turnTarget.posZ, color["r"], color["g"], color["b"], nextTurnTarget.posX, nextPosY + 3, nextTurnTarget.posZ); -- Green Line
if turnTarget.revPosX and turnTarget.revPosZ then
nextPosY = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, turnTarget.revPosX, 300, turnTarget.revPosZ);
cpDebug:drawLine(turnTarget.posX, posY + 3, turnTarget.posZ, 1, 0, 0, turnTarget.revPosX, nextPosY + 3, turnTarget.revPosZ); -- Red Line
end;
else
local color = { r = 0, g = 1, b = 1}; -- Light Blue Line
if nextTurnTarget.changeDirectionWhenAligned then
color["r"], color["g"], color["b"] = 1, 0.706, 0; -- Orange Line
end
cpDebug:drawLine(turnTarget.posX, posY + 3, turnTarget.posZ, color["r"], color["g"], color["b"], nextTurnTarget.posX, nextPosY + 3, nextTurnTarget.posZ); -- Light Blue Line
end;
elseif turnTarget.turnReverse and turnTarget.revPosX and turnTarget.revPosZ then
local posY = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, turnTarget.posX, 300, turnTarget.posZ);
local nextPosY = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, turnTarget.revPosX, 300, turnTarget.revPosZ);
cpDebug:drawLine(turnTarget.posX, posY + 3, turnTarget.posZ, 1, 0, 0, turnTarget.revPosX, nextPosY + 3, turnTarget.revPosZ); -- Red Line
end;
end;
end;
if turnContext and turnContext.turnStartWpNode and turnContext.turnEndWpNode then
DebugUtil.drawDebugNode(turnContext.turnStartWpNode.node, 'Start')
DebugUtil.drawDebugNode(turnContext.turnEndWpNode.node, 'End')
end
end;
--- Get the directionNodeToTurnNodeLength used for reverse turn distances
-- this is the distance between the tractor's directionNode and the real turning node of the implement (?)
local directionNodeToTurnNodeLength = courseplay:getDirectionNodeToTurnNodeLength(vehicle);
--- Get the firstReverseWheledWorkTool used for reversing
local reversingWorkTool = courseplay:getFirstReversingWheeledWorkTool(vehicle);
--- Reset reverseWPChangeDistance if we don't have an trailed implement
if reversingWorkTool then
reverseWPChangeDistance = reverseWPChangeDistanceWithTool;
end;
--- While driving (Stage 2 & 3), do we need to use the reversing WP change distance
local curTurnTarget = vehicle.cp.turnTargets[vehicle.cp.curTurnIndex];
if curTurnTarget and curTurnTarget.turnReverse then
wpChangeDistance = reverseWPChangeDistance;
end;
----------------------------------------------------------
-- Create Turn maneuver (Creating waypoints to follow)
----------------------------------------------------------
--- Cleanup in case we already have old info
courseplay:clearTurnTargets(vehicle); -- Make sure we have cleaned it from any previus usage.
--- Setting default turnInfo values
local turnInfo = {};
turnInfo.directionNode = realDirectionNode
turnInfo.frontMarker = frontMarker;
turnInfo.backMarker = backMarker;
turnInfo.halfVehicleWidth = 2.5;
turnInfo.directionNodeToTurnNodeLength = directionNodeToTurnNodeLength + 0.5; -- 0.5 is to make the start turn point just a tiny in front of the tractor
-- when PPC is driving we don't have to care about wp change distances, PPC takes care of that. Still use
-- a small value to make sure none of the turn generator functions end up with overlapping waypoints
turnInfo.wpChangeDistance = 0.5
turnInfo.reverseWPChangeDistance = 0.5
turnInfo.direction = -1;
turnInfo.haveHeadlands = courseplay:haveHeadlands(vehicle);
-- find out the headland height to figure out if we have enough room on the headland to make turns
if vehicle.cp.courseWorkWidth and vehicle.cp.courseWorkWidth > 0 and vehicle.cp.courseNumHeadlandLanes and vehicle.cp.courseNumHeadlandLanes > 0 then
-- First headland is only half the work width
turnInfo.headlandHeight = vehicle.cp.courseWorkWidth / 2 + ((vehicle.cp.courseNumHeadlandLanes - 1) * vehicle.cp.courseWorkWidth)
else
turnInfo.headlandHeight = 0
end
-- if the headland is not perpendicular, we have less room to turn
turnInfo.headlandHeight = turnInfo.headlandHeight * math.cos(turnContext:getHeadlandAngle())
-- Headland height in the waypoint overrides the generic headland height calculation. This is for the
-- short edge headlands where we make 180 turns on te headland course. The generic calculation would use
-- the number of headlands and think there is room on the headland to make the turn.
-- Therefore, the course generator will add a headlandHeightForTurn = 0 for these turn waypoints to make
-- sure on field turns are calculated correctly.
turnInfo.headlandHeight = turnContext.turnStartWp.headlandHeightForTurn and
turnContext.turnStartWp.headlandHeightForTurn or turnInfo.headlandHeight;
turnInfo.numLanes ,turnInfo.onLaneNum = courseplay:getLaneInfo(vehicle);
turnInfo.turnOnField = vehicle.cp.settings.turnOnField:is(true);
turnInfo.reverseOffset = 0;
turnInfo.extraAlignLength = 6;
turnInfo.haveWheeledImplement = reversingWorkTool ~= nil;
if turnInfo.haveWheeledImplement then
turnInfo.reversingWorkTool = reversingWorkTool;
turnInfo.extraAlignLength = turnInfo.extraAlignLength + directionNodeToTurnNodeLength * 2;
end;
turnInfo.isHarvester = isHarvester;
turnInfo.noReverse = g_vehicleConfigurations:getRecursively(vehicle, 'noReverse')
-- headland turn data
vehicle.cp.headlandTurn = turnContext:isHeadlandCorner() and {} or nil
-- direction halfway between dir of turnStart and turnEnd
turnInfo.halfAngle = math.deg( getAverageAngle( math.rad( turnContext.turnEndWp.angle ),
math.rad( turnContext.turnStartWp.angle )))
-- delta between turn start and turn end
turnInfo.deltaAngle = math.pi - ( math.rad( turnContext.turnEndWp.angle )
- math.rad( turnContext.turnStartWp.angle ))
turnInfo.startDirection = turnContext.turnStartWp.angle
--- Get the turn radius either by the automatic or user provided turn circle.
local extRadius = 0.5 + (0.15 * directionNodeToTurnNodeLength); -- The extra calculation is for dynamic trailer length to prevent jackknifing;
turnInfo.turnRadius = vehicle.cp.settings.turnDiameter:get() * 0.5 + extRadius;
turnInfo.turnDiameter = turnInfo.turnRadius * 2;
--- Create temp target node and translate it.
turnInfo.targetNode = turnContext.turnEndWpNode.node
local cx,cz = turnContext.turnEndWp.x, turnContext.turnEndWp.z
--- Debug Print
if courseplay.debugChannels[courseplay.DBG_TURN] then
local x,y,z = getWorldTranslation(turnInfo.targetNode);
local ctx,_,ctz = localToWorld(turnInfo.targetNode, 0, 0, 20);
--drawDebugLine(x, y+5, z, 1, 0, 0, ctx, y+5, ctz, 0, 1, 0);
cpDebug:drawLine(x, y+5, z, 1, 0, 0, ctx, y+5, ctz);
-- this is an test
courseplay:debug(("%s:(Turn) wp%d=%.1f°, wp%d=%.1f°, directionChangeDeg = %.1f° halfAngle = %.1f"):format(nameNum(vehicle),
turnContext.beforeTurnStartWp.cpIndex, turnContext.beforeTurnStartWp.angle, turnContext.turnEndWp.cpIndex, turnContext.turnEndWp.angle, turnContext.directionChangeDeg, turnInfo.halfAngle), 14);
end;
--- Get the local delta distances from the tractor to the targetNode
turnInfo.targetDeltaX, _, turnInfo.targetDeltaZ = worldToLocal(turnInfo.directionNode, cx, vehicleY, cz);
courseplay:debug(string.format("%s:(Turn) targetDeltaX=%.2f, targetDeltaZ=%.2f", nameNum(vehicle), turnInfo.targetDeltaX, turnInfo.targetDeltaZ), courseplay.DBG_TURN);
--- Get the turn direction
if turnContext:isHeadlandCorner() then
-- headland corner turns have a targetDeltaX around 0 so use the direction diff
if turnContext.directionChangeDeg > 0 then
turnInfo.direction = 1;
end
else
if turnInfo.targetDeltaX > 0 then
turnInfo.direction = 1;
end;
end
-- Relative position of the turn start waypoint from the vehicle.
-- Note that as we start the turn when the backMarkerOffset reaches the turn start point, this zOffset
-- is the same as the backMarkerOffset
_, _, turnInfo.zOffset = worldToLocal(turnInfo.directionNode, turnContext.turnStartWp.x, vehicleY, turnContext.turnStartWp.z);
-- remember this as we'll need it later
turnInfo.deltaZBetweenVehicleAndTarget = turnInfo.targetDeltaZ
-- targetDeltaZ is now the delta Z between the turn start and turn end waypoints.
turnInfo.targetDeltaZ = turnInfo.targetDeltaZ - turnInfo.zOffset;
-- Calculate reverseOffset in case we need to reverse.
-- This is used in both wide turns and in the question mark turn
local offset = turnInfo.zOffset;
-- only if all implements are in the front
if turnInfo.frontMarker > 0 and turnInfo.backMarker > 0 then
offset = -turnInfo.zOffset - turnInfo.frontMarker;
end;
if turnInfo.turnOnField and not turnInfo.isHarvester and not turnInfo.noReverse then
turnInfo.reverseOffset = max((turnInfo.turnRadius + turnInfo.halfVehicleWidth - turnInfo.headlandHeight), offset);
elseif turnInfo.isHarvester and turnInfo.frontMarker > 0 then
-- without fully understanding this reverseOffset, correct it for combines so they don't make
-- unnecessarily wide turns (and hit trees outside the field)
turnInfo.reverseOffset = -turnInfo.frontMarker
else
-- the weird thing about this is that reverseOffset here equals to zOffset and this is why
-- the wide turn works at all, even if there's no reversing.
turnInfo.reverseOffset = offset;
end;
courseplay:debug(("%s:(Turn Data) frontMarker=%q, backMarker=%q, halfVehicleWidth=%q, directionNodeToTurnNodeLength=%q, wpChangeDistance=%q"):format(nameNum(vehicle), tostring(turnInfo.frontMarker), tostring(backMarker), tostring(turnInfo.halfVehicleWidth), tostring(turnInfo.directionNodeToTurnNodeLength), tostring(turnInfo.wpChangeDistance)), courseplay.DBG_TURN);
courseplay:debug(("%s:(Turn Data) reverseWPChangeDistance=%q, direction=%q, haveHeadlands=%q, headlandHeight=%q"):format(nameNum(vehicle), tostring(turnInfo.reverseWPChangeDistance), tostring(turnInfo.direction), tostring(turnInfo.haveHeadlands), tostring(turnInfo.headlandHeight)), courseplay.DBG_TURN);
courseplay:debug(("%s:(Turn Data) numLanes=%q, onLaneNum=%q, turnOnField=%q, reverseOffset=%q"):format(nameNum(vehicle), tostring(turnInfo.numLanes), tostring(turnInfo.onLaneNum), tostring(turnInfo.turnOnField), tostring(turnInfo.reverseOffset)), courseplay.DBG_TURN);
courseplay:debug(("%s:(Turn Data) haveWheeledImplement=%q, reversingWorkTool=%q, turnRadius=%q, turnDiameter=%q"):format(nameNum(vehicle), tostring(turnInfo.haveWheeledImplement), tostring(turnInfo.reversingWorkTool), tostring(turnInfo.turnRadius), tostring(turnInfo.turnDiameter)), courseplay.DBG_TURN);
courseplay:debug(("%s:(Turn Data) targetNode=%q, targetDeltaX=%q, targetDeltaZ=%q, zOffset=%q"):format(nameNum(vehicle), tostring(turnInfo.targetNode), tostring(turnInfo.targetDeltaX), tostring(turnInfo.targetDeltaZ), tostring(turnInfo.zOffset)), courseplay.DBG_TURN);
courseplay:debug(("%s:(Turn Data) reverseOffset=%q, isHarvester=%q, noReverse=%q"):format(nameNum(vehicle), tostring(turnInfo.reverseOffset), tostring(turnInfo.isHarvester), tostring(turnInfo.noReverse)), courseplay.DBG_TURN);
if not turnContext:isHeadlandCorner() then
----------------------------------------------------------
-- SWITCH TO THE NEXT LANE
----------------------------------------------------------
courseplay:debug(string.format("%s:(Turn) Direction difference is %.1f, this is a lane switch.", nameNum(vehicle), turnContext.directionChangeDeg), courseplay.DBG_TURN);
----------------------------------------------------------
-- WIDE TURNS (Turns where the distance to next lane is bigger than the turning Diameter)
----------------------------------------------------------
if abs(turnInfo.targetDeltaX) >= turnInfo.turnDiameter then
if abs(turnInfo.targetDeltaX) >= (turnInfo.turnDiameter * 2) and abs(turnInfo.targetDeltaZ) >= (turnInfo.turnRadius * 3) then
courseplay:generateTurnTypeWideTurnWithAvoidance(vehicle, turnInfo);
else
courseplay:generateTurnTypeWideTurn(vehicle, turnInfo);
end
----------------------------------------------------------
-- NARROW TURNS (Turns where the distance to next lane is smaller than the turning Diameter)
----------------------------------------------------------
else
--- If we have wheeled implement, then do turns based on that.
if turnInfo.haveWheeledImplement then
--- Get the Triangle sides
local centerOffset = abs(turnInfo.targetDeltaX) / 2;
local sideC = turnInfo.turnDiameter;
local sideB = centerOffset + turnInfo.turnRadius;
local centerHeight = square(sideC^2 - sideB^2);
--- Check if there is enough space to make Ohm turn on the headland.
local useOhmTurn = false;
if (-turnInfo.zOffset + centerHeight + turnInfo.turnRadius + turnInfo.halfVehicleWidth) < turnInfo.headlandHeight then
useOhmTurn = true;
end;
--- Ohm Turn
if useOhmTurn or turnInfo.isHarvester or turnInfo.noReverse or not turnInfo.turnOnField then
courseplay:generateTurnTypeOhmTurn(vehicle, turnInfo);
else
--- Questionmark Turn
courseplay:generateTurnTypeQuestionmarkTurn(vehicle, turnInfo);
end;
--- If not wheeled implement, then do the short turns.
else
--- Get the Triangle sides
turnInfo.centerOffset = (turnInfo.targetDeltaX * turnInfo.direction) - turnInfo.turnRadius;
local sideC = turnInfo.turnDiameter;
local sideB = turnInfo.turnRadius + turnInfo.centerOffset; -- which is exactly targetDeltaX, see above
turnInfo.centerHeight = square(sideC^2 - sideB^2);
local neededSpace = abs(turnInfo.targetDeltaZ) + turnInfo.zOffset + 1 + turnInfo.centerHeight + (turnInfo.reverseWPChangeDistance * 1.5);
--- Forward 3 Point Turn
if neededSpace < turnInfo.headlandHeight or turnInfo.isHarvester or not turnInfo.turnOnField then
courseplay:generateTurnTypeForward3PointTurn(vehicle, turnInfo);
--- Reverse 3 Point Turn
else
courseplay:generateTurnTypeReverse3PointTurn(vehicle, turnInfo);
end;
end;
end
else
-------------------------------------------------------------
-- A SHARP TURN, LIKELY ON THE HEADLAND BUT NOT A LANE SWITCH
-------------------------------------------------------------
courseplay.debugVehicle(courseplay.DBG_TURN, vehicle, "%s:(Turn) Direction difference is %.1f, this is a corner.",
turnContext.directionChangeDeg)
vehicle.cp.turnCorner = turnContext:createCorner(vehicle, turnInfo.turnRadius)
courseplay.generateTurnTypeHeadlandCornerReverseStraightTractor(vehicle, turnInfo)
end
courseplay.debugLine(courseplay.DBG_TURN, 1);
courseplay:debug(string.format("%s:(Turn) Generated %d Turn Waypoints", nameNum(vehicle), #vehicle.cp.turnTargets), courseplay.DBG_TURN);
courseplay.debugLine(courseplay.DBG_TURN, 3);
end
function courseplay:generateTurnTypeWideTurn(vehicle, turnInfo)
courseplay.debugLine(courseplay.DBG_TURN, 3);
courseplay:debug(string.format("%s:(Turn) Using Wide Turn", nameNum(vehicle)), courseplay.DBG_TURN);
courseplay.debugLine(courseplay.DBG_TURN, 3);
local posX, posZ;
local fromPoint, toPoint = {}, {};
local canTurnOnHeadland = false;
local center1, center2, startDir, intersect1, intersect2, stopDir = {}, {}, {}, {}, {}, {};
-- Check if we can turn on the headlands
if (-turnInfo.zOffset + turnInfo.turnRadius + turnInfo.halfVehicleWidth) <= turnInfo.headlandHeight then
canTurnOnHeadland = true;
end;
--- Get the center height offset
if not turnInfo.haveHeadlands and not turnInfo.isHarvester and not turnInfo.noReverse and turnInfo.turnOnField then
turnInfo.reverseOffset = turnInfo.reverseOffset + abs(turnInfo.targetDeltaZ * 0.75);
end;
--- Add extra length to the directionNodeToTurnNodeLength if there is an pivoted tool behind the tractor.
-- This is to prevent too sharp turning when reversing to the first reverse point.
local directionNodeToTurnNodeLength = turnInfo.directionNodeToTurnNodeLength;
if turnInfo.haveWheeledImplement and turnInfo.reversingWorkTool.cp.isPivot then
directionNodeToTurnNodeLength = directionNodeToTurnNodeLength * 1.25;
end;
--- Extra WP 1 - Reverse back so we can turn inside the field (Only if we can't turn inside the headlands)
if not canTurnOnHeadland and not turnInfo.isHarvester and not turnInfo.noReverse and turnInfo.turnOnField then
-- Reverse back
fromPoint.x, _, fromPoint.z = localToWorld(turnInfo.directionNode, 0, 0, -directionNodeToTurnNodeLength);
toPoint.x, _, toPoint.z = localToWorld(turnInfo.directionNode, 0, 0, turnInfo.zOffset - turnInfo.reverseOffset - directionNodeToTurnNodeLength - turnInfo.reverseWPChangeDistance);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint, true, nil, turnInfo.reverseWPChangeDistance);
end;
-- Get the 2 circle center coordinate
-- I don't understand that turnInfo.zOffset here. That is our distance from the turn start WP, and with no reverseOffset it'll put the
-- circle behind us. I think this is buggy, and only works with that magic at the beginning where we end up with the reverseOffset == zOffset
center1.x,_,center1.z = localToWorld(turnInfo.directionNode, turnInfo.turnRadius * turnInfo.direction, 0, turnInfo.zOffset - turnInfo.reverseOffset);
center2.x,_,center2.z = localToWorld(turnInfo.targetNode, turnInfo.turnRadius * turnInfo.direction, 0, turnInfo.reverseOffset);
-- Get the circle intersection points
intersect1.x, intersect1.z = center1.x, center1.z;
intersect2.x, intersect2.z = center2.x, center2.z;
intersect1, intersect2 = courseplay:getTurnCircleTangentIntersectionPoints(intersect1, intersect2, turnInfo.turnRadius, turnInfo.targetDeltaX > 0);
--- Set start and stop dir for first turn circle
startDir.x,_,startDir.z = localToWorld(turnInfo.directionNode, 0, 0, turnInfo.zOffset - turnInfo.reverseOffset);
stopDir.x,_,stopDir.z = localToWorld(turnInfo.targetNode, 0, 0, turnInfo.reverseOffset);
--- Generate turn circle 1
courseplay:generateTurnCircle(vehicle, center1, startDir, intersect1, turnInfo.turnRadius, turnInfo.direction, true);
--- Generate points between the 2 circles
courseplay:generateTurnStraightPoints(vehicle, intersect1, intersect2);
--- Generate turn circle 2
courseplay:generateTurnCircle(vehicle, center2, intersect2, stopDir, turnInfo.turnRadius, turnInfo.direction, true);
--- Extra WP 2 - Reverse back to field edge
if not canTurnOnHeadland and not turnInfo.isHarvester and not turnInfo.noReverse and turnInfo.turnOnField then
-- Move a bit more forward
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, 0, 0, turnInfo.reverseOffset + directionNodeToTurnNodeLength + turnInfo.extraAlignLength + turnInfo.wpChangeDistance);
courseplay:generateTurnStraightPoints(vehicle, stopDir, toPoint, nil, nil, nil, true);
-- Reverse back
if turnInfo.reverseOffset - 3 > 0 then
fromPoint.x, _, fromPoint.z = localToWorld(turnInfo.targetNode, 0, 0, turnInfo.reverseOffset - 3);
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, 0, 0, 0);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint, true, true, turnInfo.frontMarker + directionNodeToTurnNodeLength + turnInfo.reverseWPChangeDistance);
else
posX, _, posZ = localToWorld(turnInfo.targetNode, 0, 0, 0);
local revPosX, _, revPosZ = localToWorld(turnInfo.targetNode, 0, 0, -(turnInfo.frontMarker + directionNodeToTurnNodeLength + turnInfo.reverseWPChangeDistance));
courseplay:addTurnTarget(vehicle, posX, posZ, true, true, revPosX, revPosZ);
end;
--- Extra WP 3 - Turn End
else
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, 0, 0, -turnInfo.reverseOffset + turnInfo.directionNodeToTurnNodeLength + 5);
courseplay:generateTurnStraightPoints(vehicle, stopDir, toPoint, nil, true);
end;
end;
function courseplay:generateTurnTypeWideTurnWithAvoidance(vehicle, turnInfo)
courseplay.debugLine(courseplay.DBG_TURN, 3);
courseplay:debug(string.format("%s:(Turn) Using Wide Turn With Corner Avoidance", nameNum(vehicle)), courseplay.DBG_TURN);
courseplay.debugLine(courseplay.DBG_TURN, 3);
local posX, posZ;
local fromPoint, toPoint = {}, {};
local canTurnOnHeadland = false;
local center, startDir, stopDir = {}, {}, {};
-- Check if we can turn on the headlands
if (-turnInfo.zOffset + turnInfo.turnRadius + turnInfo.halfVehicleWidth) < turnInfo.headlandHeight then
canTurnOnHeadland = true;
end;
--- Add extra length to the directionNodeToTurnNodeLength if there is an pivoted tool behind the tractor.
-- This is to prevent too sharp turning when reversing to the first reverse point.
local directionNodeToTurnNodeLength = turnInfo.directionNodeToTurnNodeLength;
if turnInfo.haveWheeledImplement and turnInfo.reversingWorkTool.cp.isPivot then
directionNodeToTurnNodeLength = directionNodeToTurnNodeLength * 1.25;
end;
--- Extra WP 1 - Reverse back so we can turn inside the field (Only if we can't turn inside the headlands)
if not canTurnOnHeadland and not turnInfo.isHarvester and not turnInfo.noReverse and turnInfo.turnOnField then
-- Reverse back
fromPoint.x, _, fromPoint.z = localToWorld(turnInfo.directionNode, 0, 0, -directionNodeToTurnNodeLength);
toPoint.x, _, toPoint.z = localToWorld(turnInfo.directionNode, 0, 0, turnInfo.zOffset - turnInfo.reverseOffset - directionNodeToTurnNodeLength - turnInfo.reverseWPChangeDistance);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint, true, nil, turnInfo.reverseWPChangeDistance);-- Reverse back
end;
----------------------------------------------------------
-- If new lane is in front of us, Do the 90-90-180 turn
----------------------------------------------------------
if turnInfo.targetDeltaZ > 0 then
--- Generate the first turn circles
center.x,_,center.z = localToWorld(turnInfo.directionNode, turnInfo.turnRadius * turnInfo.direction, 0, turnInfo.zOffset - turnInfo.reverseOffset);
startDir.x,_,startDir.z = localToWorld(turnInfo.directionNode, 0, 0, turnInfo.zOffset - turnInfo.reverseOffset);
stopDir.x,_,stopDir.z = localToWorld(turnInfo.directionNode, turnInfo.turnRadius * turnInfo.direction, 0, turnInfo.zOffset - turnInfo.reverseOffset + turnInfo.turnRadius);
courseplay:generateTurnCircle(vehicle, center, startDir, stopDir, turnInfo.turnRadius, turnInfo.direction);
--- Generate line between first and second turn circles
fromPoint.x, fromPoint.z = stopDir.x, stopDir.z
toPoint.x, _, toPoint.z = localToWorld(turnInfo.directionNode, turnInfo.targetDeltaX - turnInfo.direction * (turnInfo.turnRadius + turnInfo.turnDiameter), 0, turnInfo.zOffset - turnInfo.reverseOffset + turnInfo.turnRadius);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint);
--- Generate the second turn circles
center.x,_,center.z = localToWorld(turnInfo.directionNode, turnInfo.targetDeltaX - turnInfo.direction * (turnInfo.turnRadius + turnInfo.turnDiameter), 0, turnInfo.zOffset - turnInfo.reverseOffset + turnInfo.turnDiameter);
startDir.x, startDir.z = toPoint.x, toPoint.z;
stopDir.x,_,stopDir.z = localToWorld(turnInfo.directionNode, turnInfo.targetDeltaX - turnInfo.direction * turnInfo.turnDiameter, 0, turnInfo.zOffset - turnInfo.reverseOffset + turnInfo.turnDiameter);
courseplay:generateTurnCircle(vehicle, center, startDir, stopDir, turnInfo.turnRadius, turnInfo.direction * -1);
--- Generate line between second and third turn circles
fromPoint.x, fromPoint.z = stopDir.x, stopDir.z;
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, turnInfo.turnDiameter * turnInfo.direction, 0, turnInfo.reverseOffset);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint);
--- Generate the third turn circles
center.x,_,center.z = localToWorld(turnInfo.targetNode, turnInfo.turnRadius * turnInfo.direction, 0, turnInfo.reverseOffset);
startDir.x,_,startDir.z = localToWorld(turnInfo.targetNode, turnInfo.turnDiameter * turnInfo.direction, 0, turnInfo.reverseOffset);
stopDir.x,_,stopDir.z = localToWorld(turnInfo.targetNode, 0, 0, turnInfo.reverseOffset);
courseplay:generateTurnCircle(vehicle, center, startDir, stopDir, turnInfo.turnRadius, turnInfo.direction, true);
----------------------------------------------------------
-- If new lane is behind us, Do the 180-90-90 turn
----------------------------------------------------------
else
--- Generate the first turn circles
center.x,_,center.z = localToWorld(turnInfo.directionNode, turnInfo.turnRadius * turnInfo.direction, 0, turnInfo.zOffset - turnInfo.reverseOffset);
startDir.x,_,startDir.z = localToWorld(turnInfo.directionNode, 0, 0, turnInfo.zOffset - turnInfo.reverseOffset);
stopDir.x,_,stopDir.z = localToWorld(turnInfo.directionNode, turnInfo.turnDiameter * turnInfo.direction, 0, turnInfo.zOffset - turnInfo.reverseOffset);
courseplay:generateTurnCircle(vehicle, center, startDir, stopDir, turnInfo.turnRadius, turnInfo.direction);
--- Generate line between first and second turn circles
fromPoint.x, fromPoint.z = stopDir.x, stopDir.z
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, turnInfo.targetDeltaX - turnInfo.direction * turnInfo.turnDiameter, 0, turnInfo.reverseOffset - turnInfo.turnDiameter);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint);
--- Generate the second turn circles
center.x,_,center.z = localToWorld(turnInfo.targetNode, turnInfo.targetDeltaX - turnInfo.direction * (turnInfo.turnRadius + turnInfo.turnDiameter), 0, turnInfo.reverseOffset - turnInfo.turnDiameter);
startDir.x, startDir.z = toPoint.x, toPoint.z;
stopDir.x,_,stopDir.z = localToWorld(turnInfo.targetNode, turnInfo.targetDeltaX - turnInfo.direction * (turnInfo.turnRadius + turnInfo.turnDiameter), 0, turnInfo.reverseOffset - turnInfo.turnRadius);
courseplay:generateTurnCircle(vehicle, center, startDir, stopDir, turnInfo.turnRadius, turnInfo.direction * -1);
--- Generate line between second and third turn circles
fromPoint.x, fromPoint.z = stopDir.x, stopDir.z
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, turnInfo.turnRadius * turnInfo.direction, 0, turnInfo.reverseOffset - turnInfo.turnRadius);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint);
--- Generate the third turn circles
center.x,_,center.z = localToWorld(turnInfo.targetNode, turnInfo.turnRadius * turnInfo.direction, 0, turnInfo.reverseOffset);
startDir.x, startDir.z = toPoint.x, toPoint.z;
stopDir.x,_,stopDir.z = localToWorld(turnInfo.targetNode, 0, 0, turnInfo.reverseOffset);
courseplay:generateTurnCircle(vehicle, center, startDir, stopDir, turnInfo.turnRadius, turnInfo.direction, true);
end;
--- Extra WP 2 - Reverse back to field edge
if not canTurnOnHeadland and not turnInfo.isHarvester and not turnInfo.noReverse and turnInfo.turnOnField then
-- Move a bit more forward
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, 0, 0, turnInfo.reverseOffset + directionNodeToTurnNodeLength + turnInfo.extraAlignLength + turnInfo.wpChangeDistance);
courseplay:generateTurnStraightPoints(vehicle, stopDir, toPoint, nil, nil, nil, true);
-- Reverse back
if turnInfo.reverseOffset - 3 > 0 then
fromPoint.x, _, fromPoint.z = localToWorld(turnInfo.targetNode, 0, 0, turnInfo.reverseOffset - 3);
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, 0, 0, 0);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint, true, true, turnInfo.frontMarker + directionNodeToTurnNodeLength + turnInfo.reverseWPChangeDistance);
else
posX, _, posZ = localToWorld(turnInfo.targetNode, 0, 0, 0);
local revPosX, _, revPosZ = localToWorld(turnInfo.targetNode, 0, 0, -(turnInfo.frontMarker + directionNodeToTurnNodeLength + turnInfo.reverseWPChangeDistance));
courseplay:addTurnTarget(vehicle, posX, posZ, true, true, revPosX, revPosZ);
end;
--- Extra WP 3 - Turn End
else
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, 0, 0, -turnInfo.reverseOffset + turnInfo.directionNodeToTurnNodeLength + 5);
courseplay:generateTurnStraightPoints(vehicle, stopDir, toPoint, nil, true);
end;
end;
function courseplay:generateTurnTypeOhmTurn(vehicle, turnInfo)
courseplay.debugLine(courseplay.DBG_TURN, 3);
courseplay:debug(string.format("%s:(Turn) Using Ohm Turn", nameNum(vehicle)), courseplay.DBG_TURN);
courseplay.debugLine(courseplay.DBG_TURN, 3);
local posX, posZ;
--- Get the Triangle sides
local centerOffset = abs(turnInfo.targetDeltaX) / 2;
local sideC = turnInfo.turnDiameter;
local sideB = centerOffset + turnInfo.turnRadius;
local centerHeight = square(sideC^2 - sideB^2);
--- Target is behind of us
local targetOffsetZ = 0;
if turnInfo.targetDeltaZ < 0 then
targetOffsetZ = abs(turnInfo.targetDeltaZ);
end;
if turnInfo.frontMarker > 0 then
targetOffsetZ = targetOffsetZ + (turnInfo.frontMarker * 1.5);
end;
--- Get the 3 circle center cordinate, startDir and stopDir
local center1, center2, center3, startDir, stopDir = {}, {}, {}, {}, {};
center1.x,_,center1.z = localToWorld(turnInfo.targetNode, (abs(turnInfo.targetDeltaX) + turnInfo.turnRadius) * turnInfo.direction, 0, turnInfo.zOffset - targetOffsetZ);
center2.x,_,center2.z = localToWorld(turnInfo.targetNode, centerOffset * turnInfo.direction, 0, turnInfo.zOffset - targetOffsetZ - centerHeight);
center3.x,_,center3.z = localToWorld(turnInfo.targetNode, -turnInfo.turnRadius * turnInfo.direction, 0, turnInfo.zOffset - targetOffsetZ);
startDir.x,_,startDir.z = localToWorld(turnInfo.targetNode, turnInfo.targetDeltaX, 0, turnInfo.zOffset - targetOffsetZ);
stopDir.x,_,stopDir.z = localToWorld(turnInfo.targetNode, 0, 0, turnInfo.zOffset - targetOffsetZ);
--- Generate the 3 turn circles
courseplay:generateTurnCircle(vehicle, center1, startDir, center2, turnInfo.turnRadius, (turnInfo.direction * -1));
courseplay:generateTurnCircle(vehicle, center2, center1, center3, turnInfo.turnRadius, turnInfo.direction);
courseplay:generateTurnCircle(vehicle, center3, center2, stopDir, turnInfo.turnRadius, (turnInfo.direction * -1), true);
--- Extra WP - Make strait points to field edge if needed
if turnInfo.frontMarker < 0 and turnInfo.zOffset - targetOffsetZ < 0 then
local toPoint = {}
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, 0, 0, -turnInfo.frontMarker - 3);
courseplay:generateTurnStraightPoints(vehicle, stopDir, toPoint);
end;
--- Extra WP - End Turn turnInfo.frontMarker
posX, _, posZ = localToWorld(turnInfo.targetNode, 0, 0, -turnInfo.zOffset + (turnInfo.frontMarker < 0 and -turnInfo.frontMarker or 0) + 5);
courseplay:addTurnTarget(vehicle, posX, posZ, true);
end;
function courseplay:generateTurnTypeQuestionmarkTurn(vehicle, turnInfo)
courseplay.debugLine(courseplay.DBG_TURN, 3);
courseplay:debug(string.format("%s:(Turn) Using Questionmark Turn", nameNum(vehicle)), courseplay.DBG_TURN);
courseplay.debugLine(courseplay.DBG_TURN, 3);
local posX, posZ;
local fromPoint, toPoint = {}, {};
local canTurnOnHeadland = false;
local center1, center2, startDir, stopDir = {}, {}, {}, {};
local isReverseingBaleLoader = turnInfo.reversingWorkTool and courseplay:isBaleLoader(turnInfo.reversingWorkTool)
--- Get the Triangle sides
local centerOffset = (turnInfo.targetDeltaX * turnInfo.direction) - turnInfo.turnRadius;
local sideC = turnInfo.turnDiameter;
local sideB = turnInfo.turnRadius + centerOffset;
local centerHeight = square(sideC^2 - sideB^2);
courseplay:debug(("%s:(Turn) centerOffset=%s, sideB=%s, sideC=%s, centerHeight=%s"):format(nameNum(vehicle), tostring(centerOffset), tostring(sideB), tostring(sideC), tostring(centerHeight)), courseplay.DBG_TURN);
--- Check if we can turn on the headlands
local spaceNeeded = 0;
if vehicle.cp.settings.oppositeTurnMode:is(true) or isReverseingBaleLoader then
spaceNeeded = -turnInfo.zOffset + centerHeight + turnInfo.turnRadius + turnInfo.halfVehicleWidth;
else
spaceNeeded = -turnInfo.zOffset + turnInfo.turnRadius + turnInfo.halfVehicleWidth;
end;
if spaceNeeded < turnInfo.headlandHeight then
canTurnOnHeadland = true;
end;
courseplay:debug(("%s:(Turn) canTurnOnHeadland=%s, headlandHeight=%.2fm, spaceNeeded=%.2fm"):format(nameNum(vehicle), tostring(canTurnOnHeadland), turnInfo.headlandHeight, spaceNeeded), courseplay.DBG_TURN);
--- Target is behind of us
local targetOffsetZ = 0;
if turnInfo.deltaZBetweenVehicleAndTarget < 0 then
if canTurnOnHeadland then
targetOffsetZ = abs(turnInfo.targetDeltaZ);
else
targetOffsetZ = turnInfo.zOffset + abs(turnInfo.targetDeltaZ);
end;
end;
--- Front marker is in front of tractor
local extraMoveBack = 0
-- This works fine as long as there's no implement with a work area in the back as well. We don't really handle that
-- case properly. In stage 0 we check for the backmarker to change to stage 1 so we'll be further ahead than with
-- a front implement only. So no need to move the circle back, actually it should be moved forward but I don't have
-- the motivation to change that, for now, just don't move back, this works most of the time.
if turnInfo.frontMarker > 0 and turnInfo.backMarker > 0 then
extraMoveBack = turnInfo.frontMarker;
end;
courseplay:debug(("%s:(Turn) targetOffsetZ=%s, extraMoveBack=%.2fm"):format(nameNum(vehicle), tostring(targetOffsetZ), extraMoveBack), courseplay.DBG_TURN);
--- Get the center height offset
local centerHeightOffset = -targetOffsetZ + turnInfo.reverseOffset + extraMoveBack;
if not turnInfo.haveHeadlands then
centerHeightOffset = centerHeightOffset + abs(turnInfo.targetDeltaZ * 0.75);
end;
--- Get the numLanes and onLaneNum, so we can switch to the right turn maneuver.
local width = vehicle.cp.courseWorkWidth * 0.5;
local doNormalTurn = true;
local widthNeeded = turnInfo.turnDiameter + turnInfo.halfVehicleWidth - vehicle.cp.courseWorkWidth;
if vehicle.cp.settings.oppositeTurnMode:is(true) then
width = turnInfo.onLaneNum * vehicle.cp.courseWorkWidth - (vehicle.cp.courseWorkWidth * 0.5);
doNormalTurn = (turnInfo.haveHeadlands and widthNeeded > (width + turnInfo.headlandHeight) or widthNeeded > width);
courseplay:debug(("%s:(Turn) doNormalTurn=%s, haveHeadlands=%s, %.1fm > %.1fm"):format(nameNum(vehicle), tostring(doNormalTurn), tostring(turnInfo.haveHeadlands), widthNeeded, (turnInfo.haveHeadlands and (width + turnInfo.headlandHeight) or width)), courseplay.DBG_TURN);
else
width = (turnInfo.numLanes - turnInfo.onLaneNum) * vehicle.cp.courseWorkWidth - (vehicle.cp.courseWorkWidth * 0.5);
doNormalTurn = (turnInfo.haveHeadlands and widthNeeded < (width + turnInfo.headlandHeight) or widthNeeded < width);
courseplay:debug(("%s:(Turn) doNormalTurn=%s, haveHeadlands=%s, %.1fm < %.1fm"):format(nameNum(vehicle), tostring(doNormalTurn), tostring(turnInfo.haveHeadlands), widthNeeded, (turnInfo.haveHeadlands and (width + turnInfo.headlandHeight) or width)), courseplay.DBG_TURN);
end;
--- Do the opposite direction turns for bale loaders, so we avoid bales in the normal turn direction
if doNormalTurn and isReverseingBaleLoader then
courseplay.debugVehicle(courseplay.DBG_TURN, vehicle, '(Turn) opposite direction for bale loaders to avoid bales')
doNormalTurn = false;
end;
if doNormalTurn then
----------------------------------------------------------
-- Question Mark Turn.
----------------------------------------------------------
--- If we cant turn on headland, then reverse back into the field to turn there.
--- This is to prevent vehicles to drive too much into fences and such
if not canTurnOnHeadland then
if turnInfo.targetDeltaZ + turnInfo.zOffset < centerHeightOffset then
-- Reverse back
fromPoint.x, _, fromPoint.z = localToWorld(turnInfo.targetNode, turnInfo.targetDeltaX, 0, centerHeightOffset + 3);
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, turnInfo.targetDeltaX, 0, centerHeightOffset + turnInfo.directionNodeToTurnNodeLength + turnInfo.reverseWPChangeDistance);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint, true, nil, turnInfo.reverseWPChangeDistance);
else
-- Move forward to the first turn circle
fromPoint.x, _, fromPoint.z = localToWorld(turnInfo.targetNode, turnInfo.targetDeltaX, 0, turnInfo.targetDeltaZ + turnInfo.zOffset);
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, turnInfo.targetDeltaX, 0, centerHeightOffset);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint);
end;
end;
--- Get the new zOffset
local newZOffset = centerHeight + centerHeightOffset;
courseplay:debug(("%s:(Turn) centerHeightOffset=%s, reverseOffset=%s, zOffset=%s, turnRadius=%s"):format(nameNum(vehicle), tostring(centerHeightOffset), tostring(turnInfo.reverseOffset), tostring(turnInfo.zOffset), tostring(turnInfo.turnRadius)), courseplay.DBG_TURN);
--- Get the 2 circle center cordinate
center1.x,_,center1.z = localToWorld(turnInfo.targetNode, centerOffset * turnInfo.direction, 0, centerHeightOffset);
center2.x,_,center2.z = localToWorld(turnInfo.targetNode, turnInfo.turnRadius * turnInfo.direction * -1, 0, newZOffset);
--- Generate first turn circle
startDir.x,_,startDir.z = localToWorld(turnInfo.targetNode, turnInfo.targetDeltaX, 0, centerHeightOffset);
courseplay:generateTurnCircle(vehicle, center1, startDir, center2, turnInfo.turnRadius, turnInfo.direction);
--- Generate second turn circle
stopDir.x,_,stopDir.z = localToWorld(turnInfo.targetNode, 0, 0, newZOffset);
courseplay:generateTurnCircle(vehicle, center2, center1, stopDir, turnInfo.turnRadius, (turnInfo.direction * -1), false);
--- If we have headlands, then see if we can skip the reversing back part.
if turnInfo.haveHeadlands and newZOffset < turnInfo.directionNodeToTurnNodeLength * 0.5 then
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, 0, 0, turnInfo.directionNodeToTurnNodeLength + turnInfo.wpChangeDistance + 6);
courseplay:generateTurnStraightPoints(vehicle, stopDir, toPoint, false, true);
else
--- Add extra length to the directionNodeToTurnNodeLength if there is an pivoted tool behind the tractor.
-- This is to prevent too sharp turning when reversing to the first reverse point.
local directionNodeToTurnNodeLength = turnInfo.directionNodeToTurnNodeLength;
if turnInfo.haveWheeledImplement and turnInfo.reversingWorkTool.cp.isPivot then
directionNodeToTurnNodeLength = directionNodeToTurnNodeLength * 1.25;
end;
--- Check if there is enough space to reverse back to the new lane start.
local fromDistance = newZOffset - 3;
local extraDistance = 0;
if fromDistance < 0 then
extraDistance = abs(fromDistance);
end;
--- Do we need to move extra back on the last reverse wp
if turnInfo.targetDeltaZ > 0 then
extraMoveBack = extraMoveBack + turnInfo.targetDeltaZ;
end;
--- Extra WP 1 - Move a bit more forward
fromPoint.x, _, fromPoint.z = localToWorld(turnInfo.targetNode, 0, 0, newZOffset);
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, 0, 0, newZOffset + 3);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint);
fromPoint.x, _, fromPoint.z = localToWorld(turnInfo.targetNode, 0, 0, newZOffset + 4);
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, 0, 0, newZOffset + directionNodeToTurnNodeLength + extraDistance + turnInfo.extraAlignLength + turnInfo.wpChangeDistance);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint, nil, nil, nil, true);
---newZOffset
if fromDistance > 0 then
--- Extra WP 2 - Reverse with End Turn
fromPoint.x, _, fromPoint.z = localToWorld(turnInfo.targetNode, 0, 0, fromDistance);
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, 0, 0, 0);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint, true, true, directionNodeToTurnNodeLength + extraMoveBack + turnInfo.reverseWPChangeDistance);
else
--- Extra WP 2 - Reverse with End Turn
posX, _, posZ = localToWorld(turnInfo.targetNode, 0, 0, 0);
local revPosX, _, revPosZ = localToWorld(turnInfo.targetNode, 0, 0, -(directionNodeToTurnNodeLength + extraMoveBack + turnInfo.reverseWPChangeDistance));
courseplay:addTurnTarget(vehicle, posX, posZ, true, true, revPosX, revPosZ);
end;
end;
else
----------------------------------------------------------
-- Reverse Question Mark Turn
----------------------------------------------------------
centerHeightOffset = centerHeightOffset + abs(turnInfo.targetDeltaZ * 0.25)
--- Target is behind of us
if turnInfo.targetDeltaZ < turnInfo.zOffset then
centerHeightOffset = centerHeightOffset - abs(turnInfo.targetDeltaZ * 0.75)
end;
--- Get the new zOffset.
local newZOffset = centerHeight + centerHeightOffset;
--- If we cant turn on headland, then reverse back into the field to turn there.
--- This is to prevent vehicles to drive too much into fences and such
if not canTurnOnHeadland then
--- Add the reverseOffset to centerHeigthOffset
--centerHeightOffset = centerHeightOffset + turnInfo.reverseOffset;
--- Recalculate the new zOffset since we need to reverse back.
newZOffset = centerHeight + centerHeightOffset;
--- Reverse back
fromPoint.x, _, fromPoint.z = localToWorld(turnInfo.targetNode, turnInfo.targetDeltaX, 0, turnInfo.targetDeltaZ + 3);
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, turnInfo.targetDeltaX, 0, newZOffset + turnInfo.directionNodeToTurnNodeLength + turnInfo.reverseWPChangeDistance);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint, true, nil, turnInfo.reverseWPChangeDistance);
end;
courseplay:debug(("%s:(Turn) centerHeightOffset=%s, reverseOffset=%s, zOffset=%s, turnRadius=%s"):format(nameNum(vehicle), tostring(centerHeightOffset), tostring(turnInfo.reverseOffset), tostring(turnInfo.zOffset), tostring(turnInfo.turnRadius)), courseplay.DBG_TURN);
--- Get the 2 circle center cordinate
center1.x,_,center1.z = localToWorld(turnInfo.targetNode, (abs(turnInfo.targetDeltaX) + turnInfo.turnRadius) * turnInfo.direction, 0, newZOffset);
center2.x,_,center2.z = localToWorld(turnInfo.targetNode, turnInfo.turnRadius * turnInfo.direction, 0, centerHeightOffset);
--- Generate first turn circle
startDir.x,_,startDir.z = localToWorld(turnInfo.targetNode, turnInfo.targetDeltaX, 0, newZOffset);
courseplay:generateTurnCircle(vehicle, center1, startDir, center2, turnInfo.turnRadius, turnInfo.direction * -1);
--- Generate second turn circle
stopDir.x,_,stopDir.z = localToWorld(turnInfo.targetNode, 0, 0, centerHeightOffset);
courseplay:generateTurnCircle(vehicle, center2, center1, stopDir, turnInfo.turnRadius, turnInfo.direction, true);
--- Check if there is enought space to reverse back to the new lane start.
local fromDistance = centerHeightOffset - 3;
--- If the last turn circle ends 3m behind the new lane start, we dont need to reverse back.
if fromDistance < -3 then
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, 0, 0, turnInfo.directionNodeToTurnNodeLength + turnInfo.wpChangeDistance + 6);
courseplay:generateTurnStraightPoints(vehicle, stopDir, toPoint, false, true);
--- The last turn circle is less than 3m behind the lane start, so we have to reverse back.
else
local extraDistance = 0;
if fromDistance < 3 then
extraDistance = abs(fromDistance - 3);
end;
--- Do we need to move extra back on the last reverse wp
if turnInfo.targetDeltaZ > 0 then
extraMoveBack = extraMoveBack + turnInfo.targetDeltaZ;
end;
--- Add extra length to the directionNodeToTurnNodeLength if there is an pivoted tool behind the tractor.
-- This is to prevent too sharp turning when reversing to the first reverse point.
local directionNodeToTurnNodeLength = turnInfo.directionNodeToTurnNodeLength * 1.5;
if turnInfo.haveWheeledImplement and turnInfo.reversingWorkTool.cp.isPivot then
directionNodeToTurnNodeLength = turnInfo.directionNodeToTurnNodeLength * 1.75;
end;
--- Extra WP 1 - Move a bit more forward
fromPoint.x, _, fromPoint.z = localToWorld(turnInfo.targetNode, 0, 0, centerHeightOffset);
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, 0, 0, centerHeightOffset + directionNodeToTurnNodeLength + extraDistance + turnInfo.extraAlignLength + turnInfo.wpChangeDistance);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint, nil, nil, nil, true);
if fromDistance >= 3 then
--- Extra WP 2 - Reverse with End Turn
fromPoint.x, _, fromPoint.z = localToWorld(turnInfo.targetNode, 0, 0, fromDistance);
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, 0, 0, 0);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint, true, true, directionNodeToTurnNodeLength + extraMoveBack + turnInfo.reverseWPChangeDistance);
else
--- Extra WP 2 - Reverse with End Turn
posX, _, posZ = localToWorld(turnInfo.targetNode, 0, 0, 0);
local revPosX, _, revPosZ = localToWorld(turnInfo.targetNode, 0, 0, -(directionNodeToTurnNodeLength + extraMoveBack + turnInfo.reverseWPChangeDistance));
courseplay:addTurnTarget(vehicle, posX, posZ, true, true, revPosX, revPosZ);
end;
end;
end;
end;
function courseplay:generateTurnTypeForward3PointTurn(vehicle, turnInfo)
courseplay.debugLine(courseplay.DBG_TURN, 3);
courseplay:debug(string.format("%s:(Turn) Using Forward 3 Point Turn", nameNum(vehicle)), courseplay.DBG_TURN);
courseplay.debugLine(courseplay.DBG_TURN, 3);
local posX, posZ;
local fromPoint, toPoint = {}, {};
--local canTurnOnHeadland = false;
local center1, center2, startDir, intersect1, intersect2, stopDir = {}, {}, {}, {}, {}, {};
local frontOffset = -1;
if turnInfo.frontMarker > 0 then
frontOffset = frontOffset - turnInfo.frontMarker;
end;
-- getAttachedImplementsAllowTurnBackward will return true for anything easy to reverse, that is has no towed implement,
-- like combines or tractors with implements mounted on the 3 point hitch. Those should make the same turn (fishtail or K-turn)
-- as combines do as it takes up a lot less space on the headland. Our calculation of how much space is needed is still off a bit
-- so you may have to turn off 'turn on field' for this to work for tractors.
if not (courseplay:isCombine(vehicle) or courseplay:isChopper(vehicle)) and
not AIVehicleUtil.getAttachedImplementsAllowTurnBackward(vehicle) then
local targetDeltaZ = turnInfo.targetDeltaZ;
if targetDeltaZ > 0 then
targetDeltaZ = 0;
end;
--- Get the numLanes and onLaneNum, so we can switch to the right turn maneuver.
local width = vehicle.cp.courseWorkWidth * 0.5;
local doNormalTurn = true;
local widthNeeded = turnInfo.turnDiameter + turnInfo.halfVehicleWidth - vehicle.cp.courseWorkWidth;
if vehicle.cp.settings.oppositeTurnMode:is(true) then
width = turnInfo.onLaneNum * vehicle.cp.courseWorkWidth - (vehicle.cp.courseWorkWidth * 0.5);
doNormalTurn = widthNeeded > width;
courseplay:debug(("%s:(Turn) doNormalTurn=%s, %.1fm > %.1fm"):format(nameNum(vehicle), tostring(doNormalTurn), widthNeeded, width), courseplay.DBG_TURN);
else
width = (turnInfo.numLanes - turnInfo.onLaneNum) * vehicle.cp.courseWorkWidth - (vehicle.cp.courseWorkWidth * 0.5);
doNormalTurn = widthNeeded < width;
courseplay:debug(("%s:(Turn) doNormalTurn=%s, %.1fm < %.1fm"):format(nameNum(vehicle), tostring(doNormalTurn), widthNeeded, width), courseplay.DBG_TURN);
end;
if not doNormalTurn then
--- We don't have space on the side we want to turn into, so we do the turn in opposite direction
turnInfo.direction = turnInfo.direction * -1;
end;
courseplay:debug(("%s:(Turn) centerOffset=%s, centerHeight=%s"):format(nameNum(vehicle), tostring(turnInfo.centerOffset), tostring(turnInfo.centerHeight)), courseplay.DBG_TURN);
--- Get the 2 circle center coordinate
center1.x,_,center1.z = localToWorld(turnInfo.targetNode, turnInfo.targetDeltaX - turnInfo.turnRadius * turnInfo.direction, 0, targetDeltaZ + turnInfo.zOffset + frontOffset);
center2.x,_,center2.z = localToWorld(turnInfo.targetNode, turnInfo.turnRadius * turnInfo.direction * -1, 0, targetDeltaZ + turnInfo.zOffset - turnInfo.centerHeight + frontOffset);
startDir.x,_,startDir.z = localToWorld(turnInfo.targetNode, turnInfo.targetDeltaX, 0, targetDeltaZ + turnInfo.zOffset + frontOffset);
--- Generate Strait point up to start point
if turnInfo.targetDeltaZ > 0 then
fromPoint.x, _, fromPoint.z = localToWorld(turnInfo.directionNode, 0, 0, 0);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, startDir);
end;
--- Generate first turn circle (Forward)
courseplay:generateTurnCircle(vehicle, center1, startDir, center2, turnInfo.turnRadius, turnInfo.direction, true);
--- Move a little bit more forward, so we can reverse properly
local dx, dz = courseplay:getPointDirection(center1, center2, false);
local rotationDeg = deg(MathUtil.getYRotationFromDirection(dx, dz));
rotationDeg = rotationDeg + (90 * turnInfo.direction);
dx, dz = MathUtil.getDirectionFromYRotation(rad(rotationDeg));
local wp = vehicle.cp.turnTargets[#vehicle.cp.turnTargets];
fromPoint.x = wp.posX;
fromPoint.z = wp.posZ;
toPoint.x = wp.posX + ((2 + turnInfo.wpChangeDistance) * dx);
toPoint.z = wp.posZ + ((2 + turnInfo.wpChangeDistance) * dz);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint);
--- Generate second turn circle (Reversing)
local zPosition = targetDeltaZ + turnInfo.zOffset - turnInfo.centerHeight + frontOffset;
stopDir.x,_,stopDir.z = localToWorld(turnInfo.targetNode, 0, 0, zPosition);
courseplay:generateTurnCircle(vehicle, center2, center1, stopDir, turnInfo.turnRadius, turnInfo.direction, true, true);
--- Move a bit further back
fromPoint.x, _, fromPoint.z = localToWorld(turnInfo.targetNode, 0, 0, zPosition - 2);
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, 0, 0, zPosition - (turnInfo.reverseWPChangeDistance * 1.5));
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint, true, nil, nil, true);
--- Move further back depending on the frontmarker
if turnInfo.frontMarker < zPosition then
fromPoint.x, _, fromPoint.z = localToWorld(turnInfo.targetNode, 0, 0, zPosition - (turnInfo.reverseWPChangeDistance * 2));
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, 0, 0, -turnInfo.frontMarker - (turnInfo.reverseWPChangeDistance * 2));
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint, true, nil, nil, true);
end;
--- Finish the turn
local x, z = toPoint.x, toPoint.z;
fromPoint.x = x;
fromPoint.z = z;
toPoint.x, _, toPoint.z = localToWorld(turnInfo.targetNode, 0, 0, turnInfo.directionNodeToTurnNodeLength - turnInfo.zOffset + 5);
courseplay:generateTurnStraightPoints(vehicle, stopDir, toPoint, false, true);
else
--- Get the 2 circle center coordinate
local center1ZOffset = turnInfo.targetDeltaZ + turnInfo.zOffset + frontOffset;
local center2ZOffset = frontOffset
center1.x,_,center1.z = localToWorld(turnInfo.targetNode, turnInfo.targetDeltaX - turnInfo.turnRadius * turnInfo.direction, 0, center1ZOffset);
center2.x,_,center2.z = localToWorld(turnInfo.targetNode, turnInfo.turnRadius * turnInfo.direction, 0, center2ZOffset);
--- Get the circle intersection points
intersect1.x, intersect1.z = center1.x, center1.z;
intersect2.x, intersect2.z = center2.x, center2.z;
intersect2, intersect1 = courseplay:getTurnCircleTangentIntersectionPoints(intersect2, intersect1, turnInfo.turnRadius, turnInfo.targetDeltaX > 0);
--- Generate first turn circle (Forward)
startDir.x,_,startDir.z = localToWorld(turnInfo.targetNode, turnInfo.targetDeltaX, 0, center1ZOffset);
courseplay:generateTurnCircle(vehicle, center1, startDir, intersect1, turnInfo.turnRadius, turnInfo.direction, true);
--- Move a little bit more forward, so we can reverse properly
local dx, dz = courseplay:getPointDirection(intersect2, intersect1, false);
toPoint.x = intersect1.x + (turnInfo.wpChangeDistance * dx);
toPoint.z = intersect1.z + (turnInfo.wpChangeDistance * dz);
courseplay:generateTurnStraightPoints(vehicle, intersect1, toPoint);
--- Reverse back to the second turn circle start point
fromPoint.x = intersect1.x - (2 * dx);
fromPoint.z = intersect1.z - (2 * dz);
toPoint.x = intersect2.x - ((2 + turnInfo.reverseWPChangeDistance) * dx);
toPoint.z = intersect2.z - ((2 + turnInfo.reverseWPChangeDistance) * dz);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint, true);
--- Generate second turn circle (Forward)
stopDir.x,_,stopDir.z = localToWorld(turnInfo.targetNode, 0, 0, center2ZOffset);
courseplay:generateTurnCircle(vehicle, center2, intersect2, stopDir, turnInfo.turnRadius, turnInfo.direction, false);
--- Finish the turn
toPoint.x,_,toPoint.z = localToWorld(turnInfo.targetNode, 0, 0, abs(turnInfo.frontMarker) + 5);
courseplay:generateTurnStraightPoints(vehicle, stopDir, toPoint, false, true);
end;
end;
function courseplay:generateTurnTypeReverse3PointTurn(vehicle, turnInfo)
courseplay.debugLine(courseplay.DBG_TURN, 3);
courseplay:debug(string.format("%s:(Turn) Using Reversing 3 Point Turn", nameNum(vehicle)), courseplay.DBG_TURN);
courseplay.debugLine(courseplay.DBG_TURN, 3);
local posX, posZ;
local fromPoint, toPoint = {}, {};
local center1, center2, startDir, stopDir = {}, {}, {}, {};
local targetDeltaZ = 0;
if turnInfo.targetDeltaZ > 0 then
targetDeltaZ = turnInfo.targetDeltaZ;
end;
--- Get the numLanes and onLaneNum, so we can switch to the right turn maneuver.
local width = vehicle.cp.courseWorkWidth * 0.5;
local doNormalTurn = true;
local widthNeeded = turnInfo.turnDiameter + turnInfo.halfVehicleWidth - vehicle.cp.courseWorkWidth;
-- TODO: even if getLaneInfo() was working correctly, this part only works for rectangular fields
if vehicle.cp.settings.oppositeTurnMode:is(true) then
width = turnInfo.onLaneNum * vehicle.cp.courseWorkWidth - (vehicle.cp.courseWorkWidth * 0.5);
doNormalTurn = widthNeeded > width;
courseplay:debug(("%s:(Turn) doNormalTurn=%s, %.1fm > %.1fm"):format(nameNum(vehicle), tostring(doNormalTurn), widthNeeded, width), courseplay.DBG_TURN);
else
width = (turnInfo.numLanes - turnInfo.onLaneNum) * vehicle.cp.courseWorkWidth - (vehicle.cp.courseWorkWidth * 0.5);
doNormalTurn = widthNeeded < width;
courseplay:debug(("%s:(Turn) doNormalTurn=%s, %.1fm < %.1fm"):format(nameNum(vehicle), tostring(doNormalTurn), widthNeeded, width), courseplay.DBG_TURN);
end;
if not doNormalTurn then
--- We don't have space on the side we want to turn into, so we do the turn in oposite direction
turnInfo.direction = turnInfo.direction * -1;
end;
--- Get the 2 circle center coordinate
center1.x,_,center1.z = localToWorld(turnInfo.targetNode, turnInfo.targetDeltaX - turnInfo.turnRadius * turnInfo.direction, 0, 1 + targetDeltaZ);
center2.x,_,center2.z = localToWorld(turnInfo.targetNode, turnInfo.turnRadius * turnInfo.direction * -1, 0, 1 + turnInfo.centerHeight + targetDeltaZ);
--- Generate first turn circle (Reversing)
startDir.x,_,startDir.z = localToWorld(turnInfo.targetNode, turnInfo.targetDeltaX, 0, 1 + targetDeltaZ);
courseplay:generateTurnCircle(vehicle, center1, startDir, center2, turnInfo.turnRadius, turnInfo.direction * -1, true, true);
--- Move a little bit more back, so we can align better when going forward
local dx, dz = courseplay:getPointDirection(center1, center2, false);
local rotationDeg = deg(MathUtil.getYRotationFromDirection(dx, dz));
rotationDeg = rotationDeg + (90 * turnInfo.direction);
dx, dz = MathUtil.getDirectionFromYRotation(rad(rotationDeg));
local wp = vehicle.cp.turnTargets[#vehicle.cp.turnTargets];
fromPoint.x = wp.posX;
fromPoint.z = wp.posZ;
toPoint.x = wp.posX - ((2 + turnInfo.reverseWPChangeDistance) * dx);
toPoint.z = wp.posZ - ((2 + turnInfo.reverseWPChangeDistance) * dz);
courseplay:generateTurnStraightPoints(vehicle, fromPoint, toPoint, true);
--- Generate second turn circle (Forward)
local zPosition = 1 + turnInfo.centerHeight + targetDeltaZ;