-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend.js
228 lines (200 loc) · 8.05 KB
/
frontend.js
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
const rosApiUrl = 'http://127.0.0.1:5000'; // Replace with your ROS API endpoint
async function sendCommand(throttle, steering) {
const command = {
throttle: throttle,
steering: steering
};
try {
const response = await axios.post(`${rosApiUrl}/ros2_command`, command);
if (response.status === 200) {
console.log('Command sent successfully:', command);
} else {
console.error('Failed to send command:', command);
}
} catch (error) {
console.error('Error sending command:', error);
}
}
let lastBatteryValue = null;
async function fetchBatteryData() {
try {
const response = await axios.get(`${rosApiUrl}/battery`);
if (response.status === 200) {
const batteryValue = parseFloat(response.data.battery); // Ensure the battery value is a float
console.log(`Battery value: ${batteryValue}`); // Debug print
let displayValue = 0;
if (batteryValue > 100) {
displayValue = 100;
} else if (batteryValue < 0) {
displayValue = 0;
} else {
displayValue = batteryValue;
}
if (lastBatteryValue !== null && displayValue - lastBatteryValue > 3) {
console.warn(`Ignoring battery value: ${displayValue}, using last value: ${lastBatteryValue}`);
displayValue = lastBatteryValue;
} else {
lastBatteryValue = displayValue;
}
document.getElementById('batteryValue').textContent = displayValue.toFixed(2); // Display with 2 decimal places
} else {
console.error('Failed to fetch battery data');
}
} catch (error) {
console.error('Error fetching battery data:', error);
}
}
async function initMap() {
// Initialize the map
const map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 35.77195358276367, lng: -78.67390441894531 },
zoom: 14,
});
// Create an InfoWindow
const infoWindow = new google.maps.InfoWindow();
// Create a draggable marker
const draggableMarker = new google.maps.Marker({
position: { lat: 35.77195358276367, lng: -78.67390441894531 },
map: map,
draggable: true,
title: "This marker is draggable."
});
// Add an event listener for the drag end event
draggableMarker.addListener('dragend', (event) => {
const position = draggableMarker.getPosition();
infoWindow.close();
infoWindow.setContent(`Pin dropped at: ${position.lat()}, ${position.lng()}`);
infoWindow.open(map, draggableMarker);
});
}
document.addEventListener('DOMContentLoaded', () => {
const leftButton = document.getElementById('left');
const rightButton = document.getElementById('right');
const throttleSlider = document.getElementById('throttle');
const speedValue = document.getElementById('speedValue');
let steeringValue = 0;
let throttleInterval;
// Function to handle steering
const handleSteering = (direction) => {
console.log(`Steering: ${direction}`);
if (direction === 'Left') {
leftButton.style.backgroundColor = 'red';
rightButton.style.backgroundColor = '';
steeringValue = -100;
} else if (direction === 'Right') {
rightButton.style.backgroundColor = 'red';
leftButton.style.backgroundColor = '';
steeringValue = 100;
} else if (direction === 'halfRight') {
rightButton.style.backgroundColor = 'orange';
leftButton.style.backgroundColor = '';
steeringValue = 50;
} else if (direction === 'halfLeft') {
leftButton.style.backgroundColor = 'orange';
rightButton.style.backgroundColor = '';
steeringValue = -50;
}
sendCommand(parseInt(throttleSlider.value), steeringValue);
};
// Function to reset button colors and steering value
const resetSteering = () => {
leftButton.style.backgroundColor = '';
rightButton.style.backgroundColor = '';
steeringValue = 0;
sendCommand(parseInt(throttleSlider.value), steeringValue);
};
// Function to gradually increase throttle
const increaseThrottle = () => {
clearInterval(throttleInterval);
let throttleValue = parseInt(throttleSlider.value + 10);
throttleInterval = setInterval(() => {
if (throttleValue < 20) {
throttleValue += 1.5;
throttleSlider.value = throttleValue;
speedValue.textContent = throttleValue;
sendCommand(throttleValue, steeringValue);
} else {
clearInterval(throttleInterval);
}
}, 25);
};
// Function to gradually decrease throttle
const decreaseThrottle = () => {
clearInterval(throttleInterval);
let throttleValue = parseInt(throttleSlider.value - 10);
throttleInterval = setInterval(() => {
if (throttleValue > -20) {
throttleValue -= 1.5;
throttleSlider.value = throttleValue;
speedValue.textContent = throttleValue;
sendCommand(throttleValue, steeringValue);
} else {
clearInterval(throttleInterval);
}
}, 25);
};
// Fetch battery data every 5 seconds
setInterval(fetchBatteryData, 250);
// Event listeners for buttons
leftButton.addEventListener('mousedown', () => handleSteering('Left'));
rightButton.addEventListener('mousedown', () => handleSteering('Right'));
leftButton.addEventListener('mouseup', resetSteering);
rightButton.addEventListener('mouseup', resetSteering);
leftButton.addEventListener('mouseleave', resetSteering);
rightButton.addEventListener('mouseleave', resetSteering);
// Event listener for key presses
document.addEventListener('keydown', (event) => {
if (event.key === 'ArrowLeft') {
handleSteering('Left');
} else if (event.key === 'ArrowRight') {
handleSteering('Right');
} else if (event.key === 'a') {
handleSteering('halfLeft');
} else if (event.key === 'd') {
handleSteering('halfRight');
} else if (event.key === 'w') {
increaseThrottle();
} else if (event.key === 's') {
decreaseThrottle();
}
});
document.addEventListener('keyup', (event) => {
if (event.key === 'ArrowLeft' || event.key === 'ArrowRight' || event.key === 'a' || event.key === 'd') {
resetSteering();
} else if (event.key === 'w' || event.key === 's') {
clearInterval(throttleInterval);
resetSlider();
}
});
// Prevent arrow keys from affecting the slider
throttleSlider.addEventListener('keydown', (event) => {
if (event.key === 'ArrowUp' || event.key === 'ArrowDown' || event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
event.preventDefault();
}
});
// Event listener for slider input
throttleSlider.addEventListener('input', (event) => {
const value = event.target.value;
speedValue.textContent = value;
console.log(`Throttle: ${value}`);
sendCommand(parseInt(value), steeringValue);
});
// Reset the slider to zero when mouse is released
const resetSlider = () => {
throttleSlider.value = 0;
speedValue.textContent = 0;
console.log('Throttle reset to 0');
sendCommand(0, steeringValue);
};
// Add mouseup event listener to the document
document.addEventListener('mouseup', (event) => {
if (event.target !== throttleSlider) {
resetSlider();
}
});
// Add mouseleave event listener to the slider
throttleSlider.addEventListener('mouseleave', resetSlider);
// Initialize the slider to start in the middle
throttleSlider.value = 0;
speedValue.textContent = 0;
});