diff --git a/Chat/index.html b/Chat/index.html index 8c5c5dbf..0c3b68e0 100644 --- a/Chat/index.html +++ b/Chat/index.html @@ -106,6 +106,12 @@

Chat

+ + +
@@ -125,10 +131,14 @@

Chat

// MAVLink connection related functions let connect_button = document.getElementById("mavlink-connect-button"); let is_connected = false; // connection state - let mavlink_ws = null; // websocket object + // let mavlink_ws = null; // websocket object + let mavlink_sysid = 254; // system id let mavlink_compid = MAVLink20Processor.MAV_COMP_ID_MISSIONPLANNER; // component id - let MAVLink = new MAVLink20Processor(null, mavlink_sysid, mavlink_compid); + // let MAVLink = new MAVLink20Processor(null, mavlink_sysid, mavlink_compid); + + window.mavlink_ws = null; // Make them globally accessible + window.MAVLink = new MAVLink20Processor(null, mavlink_sysid, mavlink_compid); // toggle connection state (called by Connect/Disconnect button) function mavlink_toggle_connect() { @@ -281,12 +291,115 @@

Chat

// attach event listener to the arm button document.getElementById("mavlink-arm-button").addEventListener("click", mavlink_arm_vehicle); + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // switch to guided mode +function mavlink_switch_to_guided() { + if (mavlink_ws == null || mavlink_ws.readyState !== WebSocket.OPEN) { + alert("Please connect to the vehicle first"); + return; + } + + // Use command_long message for mode change (command 176: MAV_CMD_DO_SET_MODE) + let command_long_msg = new mavlink20.messages.command_long( + 1, // Target system ID + 1, // Target component ID + 176, // MAV_CMD_DO_SET_MODE + 1, // Confirmation + 1, // Param1: set to 1 for MAV_MODE_FLAG_CUSTOM_MODE_ENABLED + 4, // Param2: 4 is GUIDED mode for Copter + 0, 0, 0, 0, 0 // Unused parameters + ); + + const buffer = MAVLink.send(command_long_msg); + const uint = new Uint8Array(buffer); + + // send the message as an ArrayBuffer over the WebSocket + if (mavlink_ws && mavlink_ws.readyState === WebSocket.OPEN) { + mavlink_ws.send(uint); + add_text_to_debug("Sent GUIDED mode command via MAVLink"); + } else { + alert("WebSocket is not open. Cannot send GUIDED mode command."); + } +} +// attach event listener to the guided mode button +document.getElementById("mavlink-guided-button").addEventListener("click", mavlink_switch_to_guided); +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // add text to debug text box function add_text_to_debug(text) { let debugOutput = document.getElementById("debugOutput"); debugOutput.value += text + "\n"; debugOutput.scrollTop = debugOutput.scrollHeight; } + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // Global variable to store wakeup timers + const wakeup_timers = []; + + // Then implement set_wakeup_timer + function set_wakeup_timer(args) { + // Parse arguments if needed + if (typeof args === 'string') { + try { + args = JSON.parse(args); + } catch (e) { + return "Error: Invalid arguments format"; + } + } + + // Check required arguments + const seconds = args?.seconds; + if (seconds === undefined || seconds < 0) { + return "Error: seconds not specified or invalid"; + } + + const message = args?.message; + if (!message) { + return "Error: message not specified"; + } + + // Create a unique ID for the timer + const timer_id = Date.now().toString() + Math.random().toString(36).substr(2, 5); + + // Calculate expiry time + const expiry_time = new Date(Date.now() + seconds * 1000); + + // Create timer object + const timer = { + id: timer_id, + seconds: seconds, + message: message, + created_time: new Date(), + expiry_time: expiry_time + }; + + // Add to wakeup schedule + wakeup_timers.push(timer); + + // Set the JavaScript timeout + setTimeout(() => { + // Add message to chat + const chatBox = document.getElementById("chatBox"); + const messageDiv = document.createElement("div"); + messageDiv.className = "user-text"; + messageDiv.textContent = "WAKEUP:" + message; + chatBox.appendChild(messageDiv); + chatBox.scrollTop = chatBox.scrollHeight; + + add_text_to_debug("Wakeup timer triggered: " + message); + + // Remove from wakeup schedule + const index = wakeup_timers.findIndex(t => t.id === timer_id); + if (index !== -1) { + wakeup_timers.splice(index, 1); + } + }, seconds * 1000); + + return "Wakeup timer set for " + seconds + " seconds from now with message: " + message; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////