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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5b0434
 
e134085
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Drone Control Center - Real Hardware</title>
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
    <style>
        :root {
            --bg-dark: #0f1720;
            --panel-dark: #1f2937;
            --accent: #f59e0b;
            --accent-light: #ffb020;
            --text-light: #f8fafc;
            --text-gray: #94a3b8;
            --danger: #ef4444;
            --success: #10b981;
            --warning: #f59e0b;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background-color: var(--bg-dark);
            color: var(--text-light);
            overflow: hidden;
            height: 100vh;
        }

        .container {
            display: grid;
            grid-template-rows: auto 1fr;
            height: 100vh;
        }

        /* Header Styles */
        .header {
            background-color: var(--panel-dark);
            padding: 12px 24px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid #334155;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
            flex-shrink: 0;
        }

        .app-title {
            font-size: 1.5rem;
            font-weight: 600;
            color: var(--accent);
        }

        .connection-status {
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .serial-indicator {
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .led {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background-color: var(--danger);
            box-shadow: 0 0 4px rgba(239, 68, 68, 0.5);
            transition: all 0.3s ease;
        }

        .led.connected {
            background-color: var(--success);
            box-shadow: 0 0 6px rgba(16, 185, 129, 0.7);
        }

        .led.connecting {
            background-color: var(--warning);
            box-shadow: 0 0 6px rgba(245, 158, 11, 0.7);
        }

        /* Main Content Layout */
        .main-content {
            display: grid;
            grid-template-columns: 1fr 380px;
            height: 100%;
            overflow: hidden;
        }

        /* Map Section */
        .map-section {
            position: relative;
            overflow: hidden;
        }

        #map {
            height: 100%;
            width: 100%;
        }

        .map-controls {
            position: absolute;
            top: 16px;
            right: 16px;
            z-index: 1000;
            display: flex;
            flex-direction: column;
            gap: 8px;
        }

        .map-btn {
            background-color: var(--panel-dark);
            color: var(--text-light);
            border: 1px solid #334155;
            border-radius: 4px;
            padding: 8px 16px;
            cursor: pointer;
            transition: all 0.2s ease;
            font-size: 0.9rem;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
        }

        .map-btn:hover {
            background-color: #374151;
        }

        .map-btn.active {
            background-color: var(--accent);
            color: var(--bg-dark);
            font-weight: 600;
        }

        .map-btn:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }

        /* Sidebar Styles - PERBAIKAN UTAMA DI SINI */
        .sidebar {
            background-color: var(--panel-dark);
            border-left: 1px solid #334155;
            display: flex;
            flex-direction: column;
            overflow: hidden;
            height: 100%;
        }

        .sidebar-content {
            display: flex;
            flex-direction: column;
            height: 100%;
            overflow-y: auto; /* Scroll untuk seluruh konten sidebar */
        }

        .sidebar-section {
            padding: 16px;
            border-bottom: 1px solid #334155;
            flex-shrink: 0; /* Mencegah section menyusut */
        }

        .sidebar-section-title {
            font-size: 1rem;
            font-weight: 600;
            margin-bottom: 12px;
            color: var(--accent);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        /* Connection Controls */
        .connection-controls {
            display: flex;
            flex-direction: column;
            gap: 12px;
        }

        .connection-row {
            display: flex;
            gap: 8px;
            align-items: center;
        }

        .connection-select {
            flex: 1;
            background-color: #0f1720;
            border: 1px solid #334155;
            border-radius: 4px;
            color: var(--text-light);
            padding: 6px 8px;
            font-family: monospace;
        }

        .btn {
            background-color: var(--accent);
            color: var(--bg-dark);
            border: none;
            border-radius: 4px;
            padding: 8px 16px;
            cursor: pointer;
            transition: all 0.2s ease;
            font-weight: 600;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
        }

        .btn:hover {
            background-color: var(--accent-light);
        }

        .btn:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }

        .btn.secondary {
            background-color: #374151;
            color: var(--text-light);
        }

        .btn.secondary:hover {
            background-color: #475569;
        }

        .btn.danger {
            background-color: var(--danger);
            color: white;
        }

        /* HUD Display */
        .hud-display {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 12px;
        }

        .hud-item {
            background-color: #0f1720;
            border-radius: 4px;
            padding: 12px;
            text-align: center;
            border: 1px solid #334155;
        }

        .hud-value {
            font-size: 1.2rem;
            font-weight: 600;
            font-family: monospace;
            margin-top: 4px;
            color: var(--accent);
        }

        .hud-label {
            font-size: 0.8rem;
            color: var(--text-gray);
        }

        /* Target Bar */
        .target-bar {
            display: flex;
            flex-direction: column;
            gap: 8px;
        }

        .target-coords {
            display: flex;
            flex-direction: column;
            gap: 4px;
            margin-bottom: 8px;
        }

        .coord-row {
            display: flex;
            justify-content: space-between;
            font-family: monospace;
            font-size: 0.9rem;
        }

        .coord-label {
            color: var(--text-gray);
        }

        .distance-info {
            background-color: #0f1720;
            border-radius: 4px;
            padding: 8px;
            text-align: center;
            border: 1px solid #334155;
            margin-bottom: 8px;
        }

        .distance-value {
            font-size: 1.1rem;
            font-weight: 600;
            color: var(--accent);
            font-family: monospace;
        }

        /* Tuning Controls */
        .tuning-controls {
            display: flex;
            flex-direction: column;
            gap: 16px;
        }

        .slider-container {
            display: flex;
            flex-direction: column;
            gap: 8px;
        }

        .slider-label {
            display: flex;
            justify-content: space-between;
            font-size: 0.9rem;
        }

        .slider-value {
            font-family: monospace;
            color: var(--accent);
        }

        .slider {
            -webkit-appearance: none;
            width: 100%;
            height: 6px;
            background: #0f1720;
            border-radius: 3px;
            outline: none;
        }

        .slider::-webkit-slider-thumb {
            -webkit-appearance: none;
            width: 16px;
            height: 16px;
            border-radius: 50%;
            background: var(--accent);
            cursor: pointer;
        }

        .slider::-moz-range-thumb {
            width: 16px;
            height: 16px;
            border-radius: 50%;
            background: var(--accent);
            cursor: pointer;
            border: none;
        }

        /* Log Display - PERBAIKAN SCROLL LOG */
        .log-section {
            flex: 1;
            display: flex;
            flex-direction: column;
            min-height: 200px; /* Minimum height untuk log */
        }

        .log-container {
            background-color: #0f1720;
            border-radius: 4px;
            padding: 12px;
            flex: 1;
            overflow-y: auto;
            border: 1px solid #334155;
            font-family: monospace;
            font-size: 0.8rem;
            min-height: 150px;
        }

        .log-entry {
            margin-bottom: 4px;
            padding: 4px;
            border-radius: 2px;
            word-break: break-word;
        }

        .log-entry.telemetry {
            color: var(--success);
        }

        .log-entry.command {
            color: var(--accent);
        }

        .log-entry.ack {
            color: #60a5fa;
        }

        .log-entry.error {
            color: var(--danger);
        }

        .log-entry.info {
            color: var(--text-gray);
        }

        .log-timestamp {
            color: #6b7280;
            margin-right: 8px;
        }

        /* Responsive Design */
        @media (max-width: 1024px) {
            .main-content {
                grid-template-columns: 1fr 320px;
            }
        }

        @media (max-width: 768px) {
            .main-content {
                grid-template-columns: 1fr;
                grid-template-rows: 1fr auto;
            }
            
            .sidebar {
                max-height: 50vh; /* Tinggi lebih besar di mobile */
                border-left: none;
                border-top: 1px solid #334155;
            }
            
            .sidebar-content {
                max-height: 100%;
            }
        }

        /* Custom markers */
        .drone-marker {
            background: transparent !important;
            border: none !important;
        }

        .target-marker {
            background: transparent !important;
            border: none !important;
        }

        /* Scrollbar styling */
        .sidebar-content::-webkit-scrollbar {
            width: 8px;
        }

        .sidebar-content::-webkit-scrollbar-track {
            background: #1f2937;
        }

        .sidebar-content::-webkit-scrollbar-thumb {
            background: #4b5563;
            border-radius: 4px;
        }

        .sidebar-content::-webkit-scrollbar-thumb:hover {
            background: #6b7280;
        }

        .log-container::-webkit-scrollbar {
            width: 6px;
        }

        .log-container::-webkit-scrollbar-track {
            background: #0f1720;
        }

        .log-container::-webkit-scrollbar-thumb {
            background: #374151;
            border-radius: 3px;
        }
    </style>
</head>
<body>
    <div class="container">
        <header class="header">
            <div class="app-title">DRONE CONTROL CENTER - REAL HARDWARE</div>
            <div class="connection-status">
                <div class="serial-indicator">
                    <div class="led" id="connection-led"></div>
                    <span id="connection-status">Disconnected</span>
                </div>
            </div>
        </header>

        <main class="main-content">
            <section class="map-section">
                <div id="map"></div>
                <div class="map-controls">
                    <button id="follow-drone-btn" class="map-btn">Follow Drone</button>
                    <button id="select-target-btn" class="map-btn">Pilih Target</button>
                    <button id="clear-path-btn" class="map-btn">Clear Path</button>
                </div>
            </section>

            <aside class="sidebar">
                <div class="sidebar-content">
                    <div class="sidebar-section">
                        <div class="sidebar-section-title">
                            <span>KONEKSI SERIAL</span>
                        </div>
                        <div class="connection-controls">
                            <div class="connection-row">
                                <select id="port-select" class="connection-select">
                                    <option value="">Pilih Port Serial</option>
                                </select>
                                <button id="refresh-ports-btn" class="btn secondary">⟳</button>
                            </div>
                            <div class="connection-row">
                                <select id="baud-select" class="connection-select">
                                    <option value="9600">9600</option>
                                    <option value="19200">19200</option>
                                    <option value="38400">38400</option>
                                    <option value="57600">57600</option>
                                    <option value="115200" selected>115200</option>
                                </select>
                                <button id="connect-btn" class="btn">Connect</button>
                                <button id="disconnect-btn" class="btn danger" disabled>Disconnect</button>
                            </div>
                        </div>
                    </div>
                    <div class="sidebar-section">
                        <div class="sidebar-section-title">
                            <span>HUD DRONE</span>
                        </div>
                        <div class="hud-display">
                            <div style="grid-column: span 2; height: 200px; background: #0f1720; border-radius: 4px; overflow: hidden; border: 1px solid #334155;">
                                <canvas id="drone-canvas" style="width: 100%; height: 100%;"></canvas>
                            </div>
                            <div class="hud-item">
                                <div class="hud-label">POSISI</div>
                                <div class="hud-value" id="position-value">-,-</div>
                            </div>
                            <div class="hud-item">
                                <div class="hud-label">ALTITUDE</div>
                                <div class="hud-value" id="altitude-value">- m</div>
                            </div>
                            <div class="hud-item">
                                <div class="hud-label">YAW</div>
                                <div class="hud-value" id="yaw-value">-°</div>
                            </div>
                            <div class="hud-item">
                                <div class="hud-label">PITCH</div>
                                <div class="hud-value" id="pitch-value">-°</div>
                            </div>
                            <div class="hud-item">
                                <div class="hud-label">ROLL</div>
                                <div class="hud-value" id="roll-value">-°</div>
                            </div>
                        </div>
                    </div>
<div class="sidebar-section">
                        <div class="sidebar-section-title">
                            <span>TARGET</span>
                        </div>
                        <div class="target-bar">
                            <div class="target-coords">
                                <div class="coord-row">
                                    <span class="coord-label">Latitude:</span>
                                    <span id="target-lat">-</span>
                                </div>
                                <div class="coord-row">
                                    <span class="coord-label">Longitude:</span>
                                    <span id="target-lon">-</span>
                                </div>
                                <div class="coord-row">
                                    <span class="coord-label">Altitude:</span>
                                    <span id="target-alt">-</span>
                                </div>
                            </div>
                            <div class="distance-info">
                                <div class="hud-label">JARAK KE TARGET</div>
                                <div class="distance-value" id="distance-value">-</div>
                            </div>
                            <button id="send-target-btn" class="btn" disabled>Kirim Target ke Drone</button>
                        </div>
                    </div>

                    <div class="sidebar-section">
                        <div class="sidebar-section-title">
                            <span>TUNING PID</span>
                        </div>
                        <div class="tuning-controls">
                            <div class="slider-container">
                                <div class="slider-label">
                                    <span>Kp (Proporsional)</span>
                                    <span class="slider-value" id="kp-value">1.0</span>
                                </div>
                                <input type="range" min="0" max="5" step="0.1" value="1.0" class="slider" id="kp-slider">
                            </div>
                            <div class="slider-container">
                                <div class="slider-label">
                                    <span>Ki (Integral)</span>
                                    <span class="slider-value" id="ki-value">0.1</span>
                                </div>
                                <input type="range" min="0" max="2" step="0.01" value="0.1" class="slider" id="ki-slider">
                            </div>
                            <div class="slider-container">
                                <div class="slider-label">
                                    <span>Kd (Derivative)</span>
                                    <span class="slider-value" id="kd-value">0.5</span>
                                </div>
                                <input type="range" min="0" max="5" step="0.1" value="0.5" class="slider" id="kd-slider">
                            </div>
                            <button id="send-tuning-btn" class="btn" disabled>Kirim Tuning</button>
                        </div>
                    </div>

                    <div class="sidebar-section log-section">
                        <div class="sidebar-section-title">
                            <span>LOG</span>
                            <button id="clear-log-btn" class="btn secondary" style="padding: 4px 8px; font-size: 0.8rem;">Clear</button>
                        </div>
                        <div class="log-container" id="log-container">
                            <!-- Log entries will be added here -->
                        </div>
                    </div>
                </div>
            </aside>
        </main>
    </div>
    <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
    <script type="importmap">
    {
      "imports": {
        "three": "https://unpkg.com/three@0.158.0/build/three.module.js",
        "GLTFLoader": "https://unpkg.com/three@0.158.0/examples/jsm/loaders/GLTFLoader.js"
      }
    }
    </script>
<script>
        // Inisialisasi variabel global
        let map;
        let droneMarker;
        let targetMarker;
        let pathPolyline;
        let pathCoordinates = [];
        let isSelectingTarget = false;
        let isFollowingDrone = false;
        let currentPosition = null;
        let targetPosition = null;
        let serialPort = null;
        let reader = null;
        let writer = null;
        let isConnected = false;
        let logEntries = [];
        let readBuffer = '';

        // Inisialisasi peta
        function initMap() {
            // Default position (Bandung, Indonesia)
            const defaultPosition = [-6.914744, 107.609810];
            
            map = L.map('map').setView(defaultPosition, 15);
            
            L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
            }).addTo(map);
            
            // Buat ikon kustom untuk drone
            const droneIcon = L.divIcon({
                className: 'drone-marker',
                html: `
                    <div style="
                        position: relative;
                        width: 30px;
                        height: 30px;
                    ">
                        <div style="
                            position: absolute;
                            top: 50%;
                            left: 50%;
                            transform: translate(-50%, -50%);
                            width: 24px;
                            height: 24px;
                            background: #f59e0b;
                            border-radius: 50%;
                            border: 3px solid white;
                            box-shadow: 0 0 10px rgba(0,0,0,0.5);
                        "></div>
                        <div style="
                            position: absolute;
                            top: 50%;
                            left: 50%;
                            transform: translate(-50%, -50%) rotate(45deg);
                            width: 0;
                            height: 0;
                            border-left: 8px solid transparent;
                            border-right: 8px solid transparent;
                            border-bottom: 12px solid #f59e0b;
                        "></div>
                    </div>
                `,
                iconSize: [30, 30],
                iconAnchor: [15, 15]
            });
            
            // Tambahkan marker drone awal
            droneMarker = L.marker(defaultPosition, { 
                icon: droneIcon,
                zIndexOffset: 1000 
            }).addTo(map);
            
            // Inisialisasi polyline untuk jejak lintasan
            pathPolyline = L.polyline([], { 
                color: '#f59e0b', 
                weight: 3,
                opacity: 0.7
            }).addTo(map);
            
            // Event listener untuk kontrol peta
            document.getElementById('follow-drone-btn').addEventListener('click', toggleFollowDrone);
            document.getElementById('select-target-btn').addEventListener('click', toggleSelectTarget);
            document.getElementById('clear-path-btn').addEventListener('click', clearPath);
            
            // Event listener untuk klik peta saat mode pemilihan target aktif
            map.on('click', function(e) {
                if (isSelectingTarget) {
                    setTarget(e.latlng.lat, e.latlng.lng);
                }
            });
        }

        // Toggle mode mengikuti drone
        function toggleFollowDrone() {
            const btn = document.getElementById('follow-drone-btn');
            isFollowingDrone = !isFollowingDrone;
            
            if (isFollowingDrone && currentPosition) {
                btn.classList.add('active');
                map.setView([currentPosition.lat, currentPosition.lng], map.getZoom());
                addLogEntry('info', 'Map following drone');
            } else {
                btn.classList.remove('active');
                addLogEntry('info', 'Map following disabled');
            }
        }

        // Toggle mode pemilihan target
        function toggleSelectTarget() {
            const btn = document.getElementById('select-target-btn');
            isSelectingTarget = !isSelectingTarget;
            
            if (isSelectingTarget) {
                btn.classList.add('active');
                btn.textContent = 'Klik pada peta untuk set target';
                addLogEntry('info', 'Target selection mode active');
            } else {
                btn.classList.remove('active');
                btn.textContent = 'Pilih Target';
                addLogEntry('info', 'Target selection cancelled');
            }
        }

        // Set target pada peta
        function setTarget(lat, lng, alt = 25) {
            targetPosition = { lat, lng, alt };
            
            // Hapus marker target lama jika ada
            if (targetMarker) {
                map.removeLayer(targetMarker);
            }
            
            // Buat ikon kustom untuk target
            const targetIcon = L.divIcon({
                className: 'target-marker',
                html: `
                    <div style="
                        position: relative;
                        width: 24px;
                        height: 24px;
                    ">
                        <div style="
                            position: absolute;
                            top: 50%;
                            left: 50%;
                            transform: translate(-50%, -50%);
                            width: 20px;
                            height: 20px;
                            background: #ef4444;
                            border-radius: 50%;
                            border: 3px solid white;
                            box-shadow: 0 0 10px rgba(239,68,68,0.7);
                        "></div>
                        <div style="
                            position: absolute;
                            top: 50%;
                            left: 50%;
                            transform: translate(-50%, -50%);
                            width: 30px;
                            height: 30px;
                            border: 2px solid #ef4444;
                            border-radius: 50%;
                            animation: pulse 1.5s infinite;
                        "></div>
                    </div>
                    <style>
                        @keyframes pulse {
                            0% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
                            100% { transform: translate(-50%, -50%) scale(2); opacity: 0; }
                        }
                    </style>
                `,
                iconSize: [30, 30],
                iconAnchor: [15, 15]
            });
            
            // Tambahkan marker target baru
            targetMarker = L.marker([lat, lng], { 
                icon: targetIcon,
                zIndexOffset: 900 
            }).addTo(map);
            
            // Update UI dengan koordinat target
            document.getElementById('target-lat').textContent = lat.toFixed(6);
            document.getElementById('target-lon').textContent = lng.toFixed(6);
            document.getElementById('target-alt').textContent = alt.toFixed(1) + ' m';
            
            // Aktifkan tombol kirim target
            document.getElementById('send-target-btn').disabled = false;
            
            // Hitung dan tampilkan jarak jika ada posisi drone
            if (currentPosition) {
                updateDistance();
            }
            
            // Keluar dari mode pemilihan target
            isSelectingTarget = false;
            document.getElementById('select-target-btn').classList.remove('active');
            document.getElementById('select-target-btn').textContent = 'Pilih Target';
            
            addLogEntry('info', `Target set to: ${lat.toFixed(6)}, ${lng.toFixed(6)}, ${alt.toFixed(1)}m`);
        }

        // Kirim target ke drone
        async function sendTargetToDrone() {
            if (!targetPosition || !isConnected) return;
            
            const targetData = {
                cmd: "set_target",
                lat: targetPosition.lat,
                lon: targetPosition.lng,
                alt: targetPosition.alt
            };
            
            const jsonString = JSON.stringify(targetData) + '\n';
            
            try {
                await writeToSerial(jsonString);
                addLogEntry('command', `Sent target: ${jsonString.trim()}`);
            } catch (error) {
                addLogEntry('error', `Failed to send target: ${error.message}`);
            }
        }
        // Three.js variables
        let droneScene, droneCamera, droneRenderer, droneModel;
        let currentAttitude = { yaw: 0, pitch: 0, roll: 0 };
        let targetAttitude = { yaw: 0, pitch: 0, roll: 0 };

        // Initialize 3D drone visualization
        function initDroneVisualization() {
            const canvas = document.getElementById('drone-canvas');
            
            // Scene setup
            droneScene = new THREE.Scene();
            droneScene.background = new THREE.Color(0x0f1720);
            
            // Camera setup
            droneCamera = new THREE.PerspectiveCamera(50, canvas.clientWidth / canvas.clientHeight, 0.1, 1000);
            droneCamera.position.set(0, 3, 6);
            
            // Renderer setup
            droneRenderer = new THREE.WebGLRenderer({ canvas, antialias: true });
            droneRenderer.setSize(canvas.clientWidth, canvas.clientHeight);
            
            // Lighting
            const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444, 0.6);
            droneScene.add(hemiLight);
            
            const dirLight = new THREE.DirectionalLight(0xffffff, 0.9);
            dirLight.position.set(5, 10, 7);
            droneScene.add(dirLight);
            
            // Grid helper
            const grid = new THREE.GridHelper(20, 20, 0x334155, 0x1e293b);
            grid.position.y = -1;
            droneScene.add(grid);
            
            // Load drone model
            const loader = new THREE.GLTFLoader();
            loader.load('Flying drone_.glb', (gltf) => {
                droneModel = gltf.scene;
                droneModel.scale.set(0.8, 0.8, 0.8);
                droneScene.add(droneModel);
                animateDrone();
            }, undefined, (error) => {
                console.error('Error loading drone model:', error);
                // Fallback simple drone if model fails to load
                const geo = new THREE.BoxGeometry(1, 0.2, 1.5);
                const mat = new THREE.MeshStandardMaterial({ color: 0x1e40af });
                droneModel = new THREE.Mesh(geo, mat);
                droneScene.add(droneModel);
                animateDrone();
            });
            
            // Handle window resize
            window.addEventListener('resize', () => {
                droneCamera.aspect = canvas.clientWidth / canvas.clientHeight;
                droneCamera.updateProjectionMatrix();
                droneRenderer.setSize(canvas.clientWidth, canvas.clientHeight);
            });
        }
        
        // Animate drone model
        function animateDrone() {
            requestAnimationFrame(animateDrone);
            
            // Smooth attitude changes
            currentAttitude.yaw += (targetAttitude.yaw - currentAttitude.yaw) * 0.1;
            currentAttitude.pitch += (targetAttitude.pitch - currentAttitude.pitch) * 0.1;
            currentAttitude.roll += (targetAttitude.roll - currentAttitude.roll) * 0.1;
            
            if (droneModel) {
                droneModel.rotation.set(
                    THREE.MathUtils.degToRad(currentAttitude.pitch),
                    THREE.MathUtils.degToRad(currentAttitude.yaw),
                    THREE.MathUtils.degToRad(currentAttitude.roll)
                );
            }
            
            droneRenderer.render(droneScene, droneCamera);
        }
        
        // Update posisi drone
        function updateDronePosition(lat, lng, alt, yaw, pitch, roll) {
            currentPosition = { lat, lng, alt };
            
            // Update marker drone
            droneMarker.setLatLng([lat, lng]);
            
            // Update path
            pathCoordinates.push([lat, lng]);
            pathPolyline.setLatLngs(pathCoordinates);
            
            // Update HUD
            document.getElementById('position-value').textContent = `${lat.toFixed(4)}, ${lng.toFixed(4)}`;
            document.getElementById('altitude-value').textContent = `${alt.toFixed(1)} m`;
            document.getElementById('yaw-value').textContent = `${yaw.toFixed(1)}°`;
            document.getElementById('pitch-value').textContent = `${pitch.toFixed(1)}°`;
            document.getElementById('roll-value').textContent = `${roll.toFixed(1)}°`;
            
            // Update 3D model attitude
            targetAttitude = { yaw, pitch, roll };
            
            // Update jarak jika ada target
            if (targetPosition) {
                updateDistance();
            }
            
            // Ikuti drone jika mode aktif
            if (isFollowingDrone) {
                map.setView([lat, lng], map.getZoom());
            }
        }
// Hitung jarak menggunakan rumus Haversine
        function calculateDistance(lat1, lon1, lat2, lon2) {
            const R = 6371000; // Radius bumi dalam meter
            const dLat = (lat2 - lat1) * Math.PI / 180;
            const dLon = (lon2 - lon1) * Math.PI / 180;
            const a = 
                Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * 
                Math.sin(dLon/2) * Math.sin(dLon/2);
            const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
            return R * c;
        }

        // Update tampilan jarak
        function updateDistance() {
            if (!targetPosition || !currentPosition) return;
            
            const distance = calculateDistance(
                currentPosition.lat, currentPosition.lng,
                targetPosition.lat, targetPosition.lng
            );
            
            document.getElementById('distance-value').textContent = 
                distance < 1000 ? `${distance.toFixed(1)} m` : `${(distance/1000).toFixed(2)} km`;
        }

        // Hapus jejak lintasan
        function clearPath() {
            pathCoordinates = [];
            pathPolyline.setLatLngs([]);
            addLogEntry('info', 'Flight path cleared');
        }

        // ==================== SERIAL COMMUNICATION ====================

        // Cek dukungan Web Serial API
        function checkSerialSupport() {
            if (!navigator.serial) {
                addLogEntry('error', 'Web Serial API tidak didukung di browser ini. Gunakan Chrome/Edge versi terbaru.');
                document.getElementById('connect-btn').disabled = true;
                return false;
            }
            return true;
        }

        // Koneksi ke port serial
        async function connectSerial() {
            if (!checkSerialSupport()) return;

            try {
                addLogEntry('info', 'Meminta akses port serial...');
                
                // Minta izin pengguna untuk mengakses port serial
                serialPort = await navigator.serial.requestPort();
                
                const baudRate = parseInt(document.getElementById('baud-select').value);
                addLogEntry('info', `Membuka port dengan baud rate ${baudRate}...`);
                
                await serialPort.open({ 
                    baudRate: baudRate,
                    dataBits: 8,
                    stopBits: 1,
                    parity: 'none'
                });
                
                // Setup writer dan reader
                writer = serialPort.writable.getWriter();
                reader = serialPort.readable.getReader();
                
                // Update status koneksi
                isConnected = true;
                updateConnectionStatus();
                
                // Kirim handshake
                await writeToSerial("BEGIN\n");
                addLogEntry('info', 'Handshake sent: BEGIN');
                
                // Mulai membaca data dari serial
                readSerialData();
                
            } catch (error) {
                addLogEntry('error', `Koneksi gagal: ${error.message}`);
            }
        }

        // Membaca data dari serial
        async function readSerialData() {
            try {
                while (isConnected && reader) {
                    const { value, done } = await reader.read();
                    
                    if (done) {
                        reader.releaseLock();
                        break;
                    }
                    
                    // Decode data yang diterima
                    const text = new TextDecoder().decode(value);
                    readBuffer += text;
                    
                    // Process complete lines
                    const lines = readBuffer.split('\n');
                    readBuffer = lines.pop() || ''; // Keep incomplete line in buffer
                    
                    for (const line of lines) {
                        if (line.trim()) {
                            processIncomingData(line.trim());
                        }
                    }
                }
            } catch (error) {
                if (isConnected) {
                    addLogEntry('error', `Error membaca data serial: ${error.message}`);
                }
            }
        }

        // Memproses data yang masuk dari Arduino
        function processIncomingData(data) {
            try {
                const jsonData = JSON.parse(data);
                
                if (jsonData.type === "telemetry") {
                    // Update posisi drone dengan data real dari Arduino
                    updateDronePosition(
                        jsonData.lat, 
                        jsonData.lon, 
                        jsonData.alt || 0, 
                        jsonData.yaw || 0, 
                        jsonData.pitch || 0, 
                        jsonData.roll || 0
                    );
                    addLogEntry('telemetry', `Position: ${jsonData.lat.toFixed(6)}, ${jsonData.lon.toFixed(6)}, Alt: ${jsonData.alt?.toFixed(1) || 0}m`);
                    
                } else if (jsonData.type === "ack") {
                    addLogEntry('ack', `ACK received: ${jsonData.cmd} - ${jsonData.status}`);
                    
                } else if (jsonData.type === "error") {
                    addLogEntry('error', `Drone error: ${jsonData.message}`);
                    
                } else if (jsonData.type === "status") {
                    addLogEntry('info', `Drone status: ${jsonData.message}`);
                    
                } else {
                    addLogEntry('info', `Unknown data: ${data}`);
                }
                
            } catch (e) {
                // Jika bukan JSON, tampilkan sebagai raw data
                if (data.length > 0) {
                    addLogEntry('info', `Raw: ${data}`);
                }
            }
        }

        // Menulis data ke serial
        async function writeToSerial(data) {
            if (!writer) {
                throw new Error('Serial writer tidak tersedia');
            }
            
            const encoder = new TextEncoder();
            await writer.write(encoder.encode(data));
        }

        // Putuskan koneksi serial
        async function disconnectSerial() {
            try {
                if (reader) {
                    await reader.cancel();
                    reader = null;
                }
                
                if (writer) {
                    writer.releaseLock();
                    writer = null;
                }
                
                if (serialPort) {
                    await serialPort.close();
                    serialPort = null;
                }
                
                isConnected = false;
                updateConnectionStatus();
                addLogEntry('info', 'Serial disconnected');
                
            } catch (error) {
                addLogEntry('error', `Error saat disconnect: ${error.message}`);
            }
        }

        // Update status koneksi di UI
        function updateConnectionStatus() {
            const led = document.getElementById('connection-led');
            const status = document.getElementById('connection-status');
            const connectBtn = document.getElementById('connect-btn');
            const disconnectBtn = document.getElementById('disconnect-btn');
            const sendTuningBtn = document.getElementById('send-tuning-btn');
            
            if (isConnected) {
                led.className = 'led connected';
                status.textContent = 'Connected';
                connectBtn.disabled = true;
                disconnectBtn.disabled = false;
                sendTuningBtn.disabled = false;
            } else {
                led.className = 'led';
                status.textContent = 'Disconnected';
                connectBtn.disabled = false;
                disconnectBtn.disabled = true;
                sendTuningBtn.disabled = true;
            }
        }

        // Kirim parameter tuning PID ke Arduino
        async function sendTuningParameters() {
            if (!isConnected) return;
            
            const kp = parseFloat(document.getElementById('kp-value').textContent);
            const ki = parseFloat(document.getElementById('ki-value').textContent);
            const kd = parseFloat(document.getElementById('kd-value').textContent);
            
            const tuningData = {
                cmd: "set_pid",
                kp: kp,
                ki: ki,
                kd: kd
            };
            
            const jsonString = JSON.stringify(tuningData) + '\n';
            
            try {
                await writeToSerial(jsonString);
                addLogEntry('command', `Sent PID tuning: Kp=${kp}, Ki=${ki}, Kd=${kd}`);
            } catch (error) {
                addLogEntry('error', `Failed to send tuning: ${error.message}`);
            }
        }

        // Tambah entri log
        function addLogEntry(type, message) {
            const timestamp = new Date().toLocaleTimeString();
            const logEntry = document.createElement('div');
            logEntry.className = `log-entry ${type}`;
            logEntry.innerHTML = `<span class="log-timestamp">[${timestamp}]</span> ${message}`;
            
            const logContainer = document.getElementById('log-container');
            logContainer.appendChild(logEntry);
            logContainer.scrollTop = logContainer.scrollHeight;
            
            // Simpan entri log (maks 200 entri)
            logEntries.push({ type, message, timestamp });
            if (logEntries.length > 200) {
                logEntries.shift();
            }
        }

        // Hapus log
        function clearLog() {
            document.getElementById('log-container').innerHTML = '';
            logEntries = [];
            addLogEntry('info', 'Log cleared');
        }

        // ==================== INITIALIZATION ====================
        // Inisialisasi saat halaman dimuat
        document.addEventListener('DOMContentLoaded', function() {
            initMap();
            initDroneVisualization();
// Event listeners untuk kontrol serial
            document.getElementById('connect-btn').addEventListener('click', connectSerial);
            document.getElementById('disconnect-btn').addEventListener('click', disconnectSerial);
            document.getElementById('send-target-btn').addEventListener('click', sendTargetToDrone);
            document.getElementById('send-tuning-btn').addEventListener('click', sendTuningParameters);
            document.getElementById('clear-log-btn').addEventListener('click', clearLog);
            
            // Event listeners untuk slider PID
            document.getElementById('kp-slider').addEventListener('input', function() {
                document.getElementById('kp-value').textContent = this.value;
            });
            
            document.getElementById('ki-slider').addEventListener('input', function() {
                document.getElementById('ki-value').textContent = this.value;
            });
            
            document.getElementById('kd-slider').addEventListener('input', function() {
                document.getElementById('kd-value').textContent = this.value;
            });
            
            // Cek dukungan Web Serial
            checkSerialSupport();
            
            // Tambah beberapa log awal
            addLogEntry('info', 'Drone Control Center initialized - REAL HARDWARE MODE');
            addLogEntry('info', 'Ready to connect to Arduino via Web Serial API');
            addLogEntry('info', 'Ensure Arduino is connected and running compatible firmware');
        });
    </script>
</body>
</html>