A multi-agent-based unmanned aerial vehicle control code generation method and system

By leveraging a multi-agent architecture and a large language model, the problems of poor generalization and insufficient safety detection in UAV control code generation are solved. This enables the generation of safe and reliable UAV control codes from natural language instructions, ensuring the accuracy and security of the code.

CN122240116APending Publication Date: 2026-06-19BEIJING UNIV OF POSTS & TELECOMM

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
BEIJING UNIV OF POSTS & TELECOMM
Filing Date
2026-03-05
Publication Date
2026-06-19

AI Technical Summary

Technical Problem

Existing technologies for generating UAV control codes suffer from poor command generalization, forgetting of historical states, incorrect interface parameters, and a lack of effective safety detection mechanisms, making it difficult to meet the actual flight needs of ordinary users.

Method used

A multi-agent architecture is adopted, including an agent for extracting action intent and slots, an agent for generating code, and an agent for reviewing and detecting code security. Flight control code is generated and reviewed through a large language model, and the accuracy and security of the code are ensured by using preset code templates and security constraints.

🎯Benefits of technology

It enables the automated generation of safe and reliable UAV control codes from natural language commands, improving code accuracy and flight safety, lowering the operational threshold, and ensuring the syntactic correctness and flight safety of the generated codes.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122240116A_ABST
    Figure CN122240116A_ABST
Patent Text Reader

Abstract

This invention provides a method and system for generating UAV control code based on a multi-agent system, comprising: acquiring natural language commands and the initial motion state of the UAV, and establishing a state transition sequence; inputting the commands into a first agent, extracting action elements based on a large language model, calculating the current state by combining the previous state, generating a structured action sequence, and updating the state sequence; inputting the sequence into a second agent, guiding the generation of code to be reviewed based on a preset code template; inputting the code into a third agent, testing its syntax, and if successful, constructing an abstract syntax tree to extract flight parameters and comparing them with preset safety constraints; if both reviews pass, outputting the target code; otherwise, feeding back an error log to the second agent for iterative correction until successful or the maximum number of corrections is reached. This invention achieves automated generation of safe and reliable UAV control code from natural language through the collaboration of three agents, improving code accuracy and flight safety.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of code generation in computer software technology, and in particular to a method and system for generating control code for unmanned aerial vehicles based on multi-agent systems. Background Technology

[0002] In recent years, the low-altitude economy, as a key area of ​​strategic emerging industries, has increasingly become an important engine for promoting high-quality economic development in my country. Drones, as the core carrier of the low-altitude economy, have evolved from simple flying tools into intelligent aerial platforms integrating intelligent sensing and autonomous decision-making. However, for ordinary users, drone control involves complex operating procedures and requires a certain level of technical understanding. Although ecosystems represented by DJI, AirSim, and PX4 have begun to provide open application programming interfaces (APIs), greatly stimulating innovative applications of drones in various industries, writing control code according to specific needs remains an extremely difficult task for ordinary users without development experience.

[0003] Against this backdrop, the rise of large language models offers a new technological path to address the aforementioned challenges. Large language models possess natural language understanding and code generation capabilities, making it possible to translate natural language instructions into executable flight control code. However, existing technologies still have many limitations. For example, some existing technologies reduce code generation time by designing dedicated programming languages, but increase the learning cost for users; others design structured prompting frameworks to enhance reasoning capabilities, but lack review and inspection of the generated code; still others deeply bind code generation to specific modeling languages, forming fixed processing templates, resulting in insufficient generalization ability.

[0004] Overall, existing technologies either rely on specialized languages, increasing the learning burden, lack code review mechanisms, or are tightly coupled with specific modeling languages. While large language models possess basic capabilities, the training data lacks sufficient samples of UAV control code, resulting in generated code with issues such as poor instruction generalization, coordinate system confusion, forgetting of historical states, and incorrect interface parameters, making them difficult to apply in practice.

[0005] Therefore, there is an urgent need in this field for a method to generate drone control code that can balance flexibility and accuracy, has zero learning cost, and has a safety detection mechanism. Summary of the Invention

[0006] In view of this, embodiments of the present invention provide a method and system for generating UAV control codes based on multi-agent systems, in order to solve the technical problems of poor instruction generalization, forgetting of historical states, incorrect interface parameters, and lack of effective security detection mechanisms in the process of generating UAV control codes using large language models in the prior art.

[0007] One aspect of the present invention provides a method for generating unmanned aerial vehicle (UAV) control code based on multi-agent systems, comprising the following steps: Obtain natural language commands input by the user; Obtain the initial motion state of the UAV and establish a UAV state transition sequence containing the initial motion state; The natural language command is input to the first intelligent agent. The first intelligent agent extracts the action type, action numerical parameters, speed parameters, direction, and coordinate system from the natural language command based on a preset large language model, and calculates the current motion state based on the motion state of the previous moment stored in the UAV state transition sequence and the action elements. The motion state of the previous moment when the calculation is performed for the first time is the initial motion state. The action elements and the current motion state are combined into a structured action sequence, and the current motion state is added as a new state node to the UAV state transition sequence. The structured action sequence is input into the second intelligent agent, which, based on a preset code template, combines the parameters contained in the structured action sequence with the code template as prompt words and inputs them into the preset large language model to generate flight control code to be reviewed; The flight control code to be reviewed is input into a third intelligent agent. The third intelligent agent detects whether there are any syntax errors in the flight control code to be reviewed through trial operation. If there are syntax errors, an error log is generated directly. If there are no syntax errors, flight parameters are extracted from the flight control code to be reviewed by constructing an abstract syntax tree and the flight parameters are compared with preset safety constraints. If the flight control code to be reviewed has no syntax errors and the flight parameters meet the preset safety constraints, then the flight control code to be reviewed will be output as the target flight control code. Otherwise, an error log is generated, and the flight control code to be reviewed and the error log are re-entered into the second agent. The second agent corrects the flight control code to be reviewed based on the error log and regenerates a new flight control code to be reviewed, which is then re-entered into the third agent for review. This process continues until the review is passed or the preset number of corrections is reached, at which point the target flight control code is output or an error message is returned.

[0008] In some embodiments of the present invention, the preset code template is pre-configured for each action type. The code template includes a general action description and a reference code snippet for implementing the action type. The reference code snippet includes a code example and corresponding comment text, which is used to explain the action and corresponding parameter meaning of each statement in the code example.

[0009] In some embodiments of the present invention, the third intelligent agent detects whether there are syntax errors in the flight control code to be reviewed through trial operation. Specifically, the third intelligent agent calls the subprocess module to execute the flight control code to be reviewed in a sandbox environment and captures abnormal information during the execution process. If abnormal information is captured, it is determined that there are syntax errors and an error log containing the abnormal information is generated.

[0010] In some embodiments of the present invention, before the third intelligent agent detects whether there are syntax errors in the flight control code to be reviewed through trial operation, it further includes performing regularization processing on the flight control code to be reviewed; the regularization processing includes: removing non-code information in the flight control code to be reviewed, and converting the flight control code to be reviewed into a clean string form of code.

[0011] In some embodiments of the present invention, the flight parameters include at least one of flight altitude and flight speed, and the preset safety constraints include at least one of maximum flight altitude, maximum flight speed, and no-fly zone boundary.

[0012] In some embodiments of the present invention, the method further includes: pre-configuring a global settings file, wherein the global settings file stores the interface information of the preset large language model, the preset code template, and the preset security constraints.

[0013] In some embodiments of the present invention, the preset large language model adopts a locally deployed large language model and / or a cloud-based large language model called via API; the large language model is a general-purpose large language model or a vertical domain large language model with natural language understanding and code generation capabilities.

[0014] On the other hand, the present invention also provides a multi-agent-based unmanned aerial vehicle control code generation system, including a processor, a memory, and a computer program or instructions stored in the memory, wherein the processor is used to execute the computer program or instructions, and when the computer program or instructions are executed, the system implements the steps of the method described in any of the above.

[0015] On the other hand, the present invention also provides a computer-readable storage medium having a computer program or instructions stored thereon, which, when executed by a processor, implement the steps of the method as described in any of the preceding claims.

[0016] On the other hand, the present invention also provides a computer program product, including a computer program or instructions that, when executed by a processor, implement the steps of any of the methods described above.

[0017] The present invention provides a method and system for generating UAV control codes based on a multi-agent system, comprising: acquiring a natural language command input by a user and the initial motion state of the UAV; establishing a UAV state transition sequence including the initial motion state; inputting the natural language command into a first agent; extracting action type, action numerical parameters, velocity parameters, direction, and coordinate system from the command as action elements based on a preset large language model; calculating the current motion state by combining the motion state of the previous moment stored in the state transition sequence; combining the action elements with the current motion state into a structured action sequence; and appending the current motion state as a new state node to the state transition sequence. The process involves three agents working together to automate the generation of safe and reliable UAV control code. A structured action sequence is input into a second agent, which generates flight control code based on a preset code template and encapsulates it into a dictionary-style code for review. This code is then input into a third agent, which performs a trial run to detect syntax errors. If errors are found, an error log is generated. If the code is grammatically correct, flight parameters are extracted by constructing an abstract syntax tree and compared with preset safety constraints. If the code passes both syntax and safety checks, it is output as the target flight control code. Otherwise, an error log is generated and fed back to the second agent along with the code for review for iterative correction until the code passes review or the preset number of corrections is reached. This invention achieves automated generation of safe and reliable UAV control code from natural language through the collaboration of three agents, effectively improving code accuracy and flight safety.

[0018] Additional advantages, objects, and features of the invention will be set forth in part in the description which follows, and will also become apparent in part to those skilled in the art upon studying the description, or may be learned by practice of the invention. The objects and other advantages of the invention can be realized and obtained by means of the structures specifically pointed out in the description and drawings.

[0019] Those skilled in the art will understand that the objectives and advantages achievable with the present invention are not limited to those specifically described above, and that the above and other objectives achievable with the present invention will become clearer from the following detailed description. Attached Figure Description

[0020] The accompanying drawings, which are provided to further illustrate the invention and form part of this application, are not intended to limit the scope of the invention.

[0021] Figure 1 This is a flowchart illustrating a method for generating UAV control code based on multiple agents according to an embodiment of the present invention.

[0022] Figure 2 This is a schematic diagram of the method flow in the multi-agent-based unmanned aerial vehicle control code generation method according to an embodiment of the present invention.

[0023] Figure 3This is a flowchart of the action intent and slot extraction agent in the multi-agent-based UAV control code generation method according to an embodiment of the present invention.

[0024] Figure 4 This is a flowchart of the code generation agent in the multi-agent-based UAV control code generation method according to an embodiment of the present invention.

[0025] Figure 5 This is a flowchart of the code review and security detection agent in the multi-agent-based UAV control code generation method according to an embodiment of the present invention. Detailed Implementation

[0026] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the embodiments and accompanying drawings. Here, the illustrative embodiments and descriptions of this invention are used to explain the invention, but are not intended to limit the invention.

[0027] It should also be noted that, in order to avoid obscuring the invention with unnecessary details, only the structures and / or processing steps closely related to the solution according to the invention are shown in the accompanying drawings, while other details that are not closely related to the invention are omitted.

[0028] With the rapid development of the low-altitude economy, drones have been widely used in various industries. However, for ordinary users, writing drone control code according to their own needs remains an extremely difficult task. The rise of large language models has provided a new technical path for the conversion of natural language instructions into executable code; however, existing technologies still have many limitations. Some studies reduce code generation time by designing dedicated programming languages, but this increases the learning cost for users. Other methods enhance reasoning ability through structured prompting frameworks, but lack review and detection of the generated code. In addition, existing patented technologies are often deeply bound to specific modeling languages, resulting in insufficient generalization ability. Overall, in the application of large language models in the field of drone control, insufficient training data samples lead to problems such as poor instruction generalization, coordinate system confusion, forgetting of historical states, and incorrect interface parameters in the generated code. Furthermore, the lack of effective safety detection mechanisms makes it difficult to truly meet actual flight requirements.

[0029] In view of this, one aspect of the present invention provides a method for generating unmanned aerial vehicle (UAV) control codes based on multi-agent systems, such as... Figure 1 As shown, the process includes the following steps S101 to S107: S101: Obtain natural language instructions input by the user.

[0030] S102: Obtain the initial motion state of the UAV and establish a UAV state transition sequence containing the initial motion state.

[0031] S103: Input the natural language command into the first intelligent agent. The first intelligent agent extracts the action type, action numerical parameters, speed parameters, direction, and coordinate system from the natural language command based on the preset large language model. It calculates the current motion state based on the motion state and action elements stored in the UAV state transition sequence. The motion state at the first calculation is the initial motion state. The action elements and the current motion state are combined into a structured action sequence, and the current motion state is added as a new state node to the UAV state transition sequence.

[0032] S104: Input the structured action sequence into the second intelligent agent. Based on the preset code template, the second intelligent agent combines the parameters contained in the structured action sequence with the code template as prompt words and inputs them into the preset large language model to generate flight control code to be reviewed.

[0033] S105: Input the flight control code to be reviewed into the third intelligent agent. The third intelligent agent checks whether there are any syntax errors in the flight control code to be reviewed through trial operation. If there are syntax errors, an error log is generated directly. If there are no syntax errors, the flight parameters are extracted from the flight control code to be reviewed by constructing an abstract syntax tree and the flight parameters are compared with the preset safety constraints.

[0034] S106: If the flight control code to be reviewed has no syntax errors and the flight parameters meet the preset safety constraints, then the flight control code to be reviewed will be output as the target flight control code.

[0035] S107: Otherwise, generate an error log and re-enter the flight control code to be reviewed and the error log into the second agent. The second agent corrects the flight control code to be reviewed based on the error log and regenerates a new flight control code to be reviewed, which is then re-entered into the third agent for review. This process continues until the review is passed or the preset number of corrections is reached, at which point the target flight control code is output or an error message is returned.

[0036] In step S101, the natural language instructions input by the user are obtained.

[0037] Specifically, the user inputs natural language commands through the human-computer interaction interface. These commands, in string form, describe the flight mission the user expects the drone to perform. The user interaction layer receives the natural language command and passes it to the multi-agent layer for processing. This method directly obtains the user's original natural language input, eliminating the need for programming knowledge or learning specific command formats. This lowers the barrier to drone operation, allowing even non-professional users to easily control the drone and perform complex flight missions. For example, the user's input natural language command could be: "Take off, ascend to a height of 10 meters at a speed of 1 meter per second. Proceed 5 meters, then turn due west and proceed 10 meters at a speed of 1 meter per second. Then land."

[0038] In step S102, the initial motion state of the UAV is obtained, and a UAV state transition sequence containing the initial motion state is established.

[0039] Specifically, before executing action parsing, the first agent first acquires the initial motion state of the UAV before the start of the current task. This initial motion state describes the initial spatial pose of the UAV in the global coordinate system, typically including the x-axis position, y-axis position, z-axis position, and yaw angle. Based on this initial motion state, the first agent initializes a UAV state transition sequence. This sequence records the state changes after each action is executed during flight, and stores the initial motion state as the first state node of the sequence. The establishment of this sequence provides accurate historical data for tracking the continuous motion state of the UAV and calculating the new state after each action in subsequent steps, thereby ensuring that the state transitions between actions are coherent and the parameter calculations are accurate under multi-step continuous commands. For example, the initial motion state of the UAV can be specifically represented as follows: These correspond to the initial x-position, y-position, z-position, and initial yaw angle in the global coordinate system, respectively.

[0040] In some embodiments, the initial motion state is obtained by reading a status data packet provided by the UAV flight control system, or is set by the user through manual configuration.

[0041] In step S103, the user interaction layer transmits the acquired natural language instructions to the first intelligent agent. The first intelligent agent calls a preset large language model to perform natural language understanding on the instruction text, identifies and extracts multiple actions that constitute the flight mission from the instructions, and parses five core action elements for each action: action type, action numerical parameters, speed parameters, direction, and coordinate system.

[0042] After extracting the motion elements, the first agent reads the previous motion state from the UAV's state transition sequence. For the first action in the sequence, the previous motion state is the initial motion state stored in step S102. The first agent combines the read previous motion state with the current motion elements and calculates the current motion state after executing the current action through kinematic calculations. This state specifically includes the UAV's x-axis position, y-axis position, z-axis position, and yaw angle in the global coordinate system.

[0043] The first intelligent agent combines the action elements of the current action with the calculated current motion state to form a complete structured action record, creating a structured action sequence. Simultaneously, this current motion state is appended as a new state node to the UAV's state transition sequence to update the flight trajectory and provide accurate historical state data for calculating subsequent actions.

[0044] Through the above processing, the first intelligent agent transforms the fuzzy natural language instructions into precise and structured action sequences, and achieves explicit tracking of the continuous motion state of the UAV by maintaining the state transition sequence. This effectively avoids the problem of historical state forgetting that is prone to occur when large language models process multi-step instructions, and ensures that the input data on which subsequent code generation depends is accurate.

[0045] In some embodiments, the first intelligent agent obtains five elements—action type, action numerical parameters, velocity parameters, direction, and coordinate system—through intent understanding and slot extraction, and calculates the current motion state by combining these with the motion state from the previous moment. Specifically, this includes the UAV's x-axis position, y-axis position, z-axis position, and yaw angle in the global coordinate system. The four parameters included in the motion state fully characterize the UAV's position and orientation in three-dimensional space, providing an accurate spatial reference for subsequent action parameter calculations.

[0046] In some embodiments, the five elements extracted by the first intelligent agent from natural language commands—action type, action numerical parameters, velocity parameters, direction, and coordinate system—are the core foundation for converting fuzzy natural language into precise control commands. The action type defines the basic behavior pattern of the UAV; the action numerical parameters specify the exact value of the action; the velocity parameters determine the speed of the action; the direction indicates the orientation of the action; and the coordinate system determines the motion reference. These five elements together constitute a complete semantic description of each action, providing accurate input for subsequent code generation. By explicitly extracting the coordinate system element, this method can accurately distinguish whether the motion direction in the command is based on the aircraft coordinate system or the global coordinate system, effectively avoiding flight direction errors caused by coordinate system confusion and ensuring that the UAV executes actions as expected by the user. Through this structured element extraction method, this approach can decompose arbitrarily complex natural language commands into a series of standardized action units, thereby ensuring accurate understanding of the user's intent.

[0047] In some embodiments, the default large language model is a locally deployed large language model and / or a cloud-based large language model accessed via API. The large language model is a general-purpose large language model or a vertical domain large language model with natural language understanding and code generation capabilities.

[0048] Large language models deployed locally include the Llama 4 series released by Meta, the Qwen 3.5 series released by Alibaba, the DeepSeek-V3 released by DeepSeek, the Mistral Large 3 released by Mistral AI, or the Nemotron 3 series released by NVIDIA. Large language models in the cloud include the GPT-5 series released by OpenAI, the Claude 4 series released by Anthropic, or the Gemini 3 series released by Google DeepMind.

[0049] In step S104, the first agent transmits the generated structured action sequence to the second agent. Upon receiving the structured action sequence, the second agent, for each action in the sequence, retrieves the corresponding code template from a pre-set code template library. The code template contains the code framework required to implement the action, a general action description, and reference code snippets, providing clear generation guidance for the large language model.

[0050] The second agent combines numerical parameters, velocity parameters, direction, coordinate system, and current position and yaw angle from the structured action sequence with corresponding code templates, inputting these as prompts into the large language model. Based on the semantic guidance of the templates, the large language model integrates the parameter information into the code framework, generating specific and complete flight control codes that meet the parameter requirements.

[0051] By using a template-driven code generation approach, this method transforms the code generation task from "autonomous creation by a large language model" to "template-based guided generation," effectively avoiding problems such as API call errors, parameter type mismatches, and function name errors that may occur due to the lack of domain knowledge in large language models, thereby improving the accuracy and standardization of the generated code.

[0052] In some embodiments, a pre-configured code template is provided for each action type. The code template includes a general action description and a reference code snippet that implements the action type. The reference code snippet includes a code example and corresponding comment text, which explains the action of each statement in the code example and the meaning of the corresponding parameters. This template structure provides clear contextual guidance for the large language model to understand the meaning of parameters and generate code that meets the requirements, further improving the accuracy of code generation.

[0053] In step S105, the second agent transmits the generated flight control code to be reviewed to the third agent. The third agent first performs a syntax correctness check on the received flight control code. This check is implemented through a trial run: the third agent executes the flight control code in an isolated sandbox environment and captures any exceptions generated during execution. If an exception occurs during execution, the code is determined to have a syntax error, and the third agent immediately generates an error log containing detailed information such as the exception type, location, and description.

[0054] If the flight control code to be reviewed executes successfully in the sandbox environment without any exceptions, the third agent further performs flight safety checks. The third agent parses the flight control code into an abstract syntax tree (AST), and extracts the flight parameters set in the code by traversing the function call nodes related to movement control within the AST, specifically key values ​​such as flight altitude and speed. Subsequently, the third agent compares these extracted flight parameters one by one with preset safety constraints.

[0055] Through the aforementioned dual detection strategy, this method ensures that the final output flight control code not only has syntactic correctness, but also that the flight commands it contains are strictly limited within the preset safety boundaries, effectively preventing flight accidents caused by code logic errors or improper parameter settings.

[0056] In some embodiments, before detecting syntax errors through trial runs, the third agent further includes performing regularization processing on the flight control code to be reviewed. Regularization processing includes removing non-code information from the flight control code to be reviewed and converting the flight control code to be reviewed into clean string-like code. This non-code information typically includes Markdown tags appended to the output of large language models, model identity prefixes, dialogue role identifiers, etc. Removing these ensures the accuracy of the trial run detection.

[0057] In some embodiments, the third agent detects syntax errors through trial operation by calling the subprocess module to execute the flight control code to be reviewed in the sandbox environment and capturing abnormal information during the execution process; if abnormal information is captured, it is determined that there is a syntax error and an error log containing abnormal information is generated.

[0058] In some embodiments, flight parameters include at least one of flight altitude and flight speed, and preset safety constraints include at least one of maximum flight altitude, maximum flight speed, and no-fly zone boundaries. By setting multi-dimensional safety constraints, this method can adapt to the flight safety management needs in different scenarios.

[0059] In some embodiments, the third-party agent achieves precise extraction and safety verification of flight parameters through Abstract Syntax Tree (AST) analysis. Specifically, after parsing the flight control code to be verified into an AST, the third-party agent traverses the function call nodes related to movement control within the tree structure to locate specific parameter positions and extract key values ​​such as flight altitude and speed. The advantage of AST analysis lies in its ability to accurately identify the meaning of parameters at the syntactic structure level of the code, avoiding potential misjudgments that may occur from simple regular expression matching of code strings. The extracted flight parameters are compared item by item with preset safety constraints; only when all parameters meet the safety requirements can the code pass the verification. This AST-based safety verification mechanism enables the method to detect potential safety hazards such as speeding, exceeding altitude limits, and entering no-fly zones before code execution, providing reliable safety assurance for UAV flights.

[0060] In step S106, the third agent makes a comprehensive judgment on the dual detection results of the flight control code to be reviewed.

[0061] If the third agent determines that the flight control code to be reviewed does not produce any anomalies during trial operation in the sandbox environment, it means that there are no syntax errors; at the same time, all flight parameters extracted from the code abstract syntax tree meet the preset safety constraints, indicating that the flight altitude does not exceed the maximum flight altitude, the flight speed does not exceed the maximum flight speed, and the no-fly zone boundary has not been entered, then the third agent confirms that the code is a safe and usable flight control code.

[0062] The third agent marks the code as the target flight control code and passes it to the output interface. The output interface encapsulates the target flight control code according to a preset format and outputs it to the user as a code file. The output code file can be used directly for UAV flight control without any modification or secondary verification by the user.

[0063] This step ensures that the flight control code delivered to the user has undergone rigorous syntactic correctness and flight safety verification, enabling it to function correctly and comply with preset safe flight specifications, thus providing users with a reliable and secure drone control code generation service.

[0064] In step S107, if the third agent detects a syntax error in the flight control code to be reviewed in step S105, or if the syntax is correct but the extracted flight parameters do not meet the preset safety constraints, then the iterative correction process is triggered, specifically including the following sub-steps S1071~S1074: S1071: Generate an error log.

[0065] The third agent generates an error log, which records in detail the specific reasons why the code failed the review. If it is a syntax error, the error log includes information such as the exception type, exception location, and exception description; if it is a security constraint violation, the error log includes the type of security constraint violated, the specific parameter value extracted, and the allowed security threshold range.

[0066] S1072: Feedback error message.

[0067] The third agent will send the generated error logs, along with the pending flight control codes that failed the review, back to the second agent.

[0068] S1073: Analysis and correction of code.

[0069] After receiving the flight control code to be reviewed and the corresponding error log, the second agent analyzes and corrects the code based on the indications in the error log. If the error log indicates a syntax error, the second agent locates the error in the code and adjusts the code structure or corrects the API call method according to the error type; if the error log indicates that the parameters exceed safety constraints, the second agent identifies the corresponding parameter setting location and adjusts the parameter value to the range allowed by the preset safety constraints.

[0070] S1074: Regenerate and submit for review.

[0071] After the correction is completed, the second agent regenerates the new flight control code to be reviewed and inputs it again into the third agent for review.

[0072] Repeat steps S1071 to S1074 until the review is approved or the preset maximum number of corrections is reached, then output the target flight control code or return an error message.

[0073] Through the aforementioned iterative correction mechanism, this method forms a closed-loop code quality optimization process, which can automatically correct common code errors and parameter out-of-bounds issues without manual intervention, significantly improving the final success rate of code generation. Simultaneously, by setting an upper limit on the number of corrections, it avoids infinite loops caused by uncorrectable errors, ensuring the method's execution efficiency.

[0074] In some embodiments, the error log contains information about the error type, error location, and the violated preset safety constraints. When the second agent corrects the flight control code to be reviewed based on the error log, it locates the code problem according to the error type and error location, and adjusts the corresponding parameters according to the violated preset safety constraints.

[0075] In some embodiments, if the review still fails after reaching the preset maximum number of correction attempts, an error message is returned, which includes the final generated error log and the reason for the correction failure.

[0076] It should be noted that before executing steps S101 to S107, the method also includes a step of pre-configuring a global settings file. The global settings file stores the interface information of a preset large language model, preset code templates, and preset security constraints. Specifically, the interface information of the preset large language model is used by the first agent to call the corresponding large language model service in step S103; the preset code template is used by the second agent to call in step S104 to generate flight control code to be reviewed; and the preset security constraints are used by the third agent as a benchmark for flight parameter comparison in step S105. Through the unified configuration of the global settings file, this method can flexibly adapt to different programming languages, UAV control libraries, large language model services, and security policies, improving the system's configurability and scalability.

[0077] In some embodiments, the global settings file also includes parameters such as the target programming language, target UAV control library functions, output file path, and security detection enable flag.

[0078] On the other hand, the present invention also provides a multi-agent-based unmanned aerial vehicle control code generation system, including a processor, a memory, and a computer program or instructions stored in the memory. The processor is used to execute the computer program or instructions, and when the computer program or instructions are executed, the system implements the steps of any of the above methods.

[0079] On the other hand, the present invention also provides a computer-readable storage medium having a computer program or instructions stored thereon, which, when executed by a processor, implement the steps of any of the methods described above.

[0080] On the other hand, the present invention also provides a computer program product, including a computer program or instructions that, when executed by a processor, implement the steps of any of the above methods.

[0081] The present invention will now be described with reference to a specific embodiment: This embodiment will elaborate on the complete technical solution of the multi-agent-based UAV control code generation method proposed in this invention, specifically including the overall architecture of the method flow, the functional responsibilities and execution details of each agent, and the data transfer relationships between each step. The specific technical solution followed in this embodiment is as follows: 1. Overall architecture of the methodology This invention decomposes the task into three sub-steps, each handled by a distinct intelligent agent. These three agents collaborate to convert user-input natural language commands into generated code files: an agent for action intent and slot extraction, a code generation agent, and a code review and security detection agent. This design adheres to the principle of "high cohesion and low coupling," facilitating subsequent business maintenance and functional expansion.

[0082] The action intent and slot extraction agent is responsible for parsing the semantic intent of the user input, identifying the core action, and extracting key information slots. This agent also possesses context awareness, maintaining a state transition sequence for the drone to address the problem of forgetting historical states. The code generation agent receives the rule-based output from the action intent and slot extraction agent, combines it with a preset code template, and inputs it as prompts into the large language model. The large language model then generates flight control code that meets the parameter requirements based on the semantic guidance of the template, ensuring a clear and controllable process and resolving issues such as API call errors and incorrect parameter input. The code review and security detection agent is responsible for performing syntax checks on the generated code and extracting relevant parameters such as speed and altitude based on the AST (Abstract Syntax Tree) to ensure grammatical correctness and flight safety. If the code fails the review, the specific issues are fed back to the generation agent for iterative optimization.

[0083] In this method, each agent undertakes its own functional task, breaking down a complex request into multiple stages in a streaming manner. Each stage is handled by a dedicated agent, which then passes the result to the next stage. The method flow is as follows: Figure 2 As shown, the specific text description is as follows: 1) The user interaction layer receives natural language instructions in string form sent by the user and passes them to the multi-agent layer for action intent and slot extraction agents; 2) Action Intent and Slot Extraction: After receiving natural language instructions, the intelligent agent performs intent recognition and slot extraction, and generates a state transition sequence based on the UAV's position and state. 3) The action intent and slot extraction agent will extract the results according to the structure rules required by the agent and output them, and pass the results to the code generation agent of the multi-agent layer; 4) After receiving the rule-based action sequence, the code generation agent generates code that meets the requirements of the specified language and library functions according to the predefined template, and then passes the generated code to the code review and security detection agent of the multi-agent layer. 5) The code review and security detection intelligent agent reviews the generated code, including checking both syntax correctness and functional security. If the review is approved, the code will be sent to the user interaction layer; If the review fails, the generated error code log and the error code will be fed as input to the code generation agent for correction, and the corrected result will be reviewed again. 6) The user interaction layer encapsulates the approved code into a code file and outputs it to the user.

[0084] 2. Functional responsibilities and execution details of each agent 1) Action Intent and Slot Extraction Agent Action intent and slot extraction agent execution process as follows Figure 3 As shown, steps S1 to S5 are included: S1: Input natural language commands.

[0085] S2: Intent Recognition and Slot Extraction: Performs natural language understanding and action decomposition on the input natural language commands, and identifies the action intent and slot, including five major elements: action type, action numerical parameters, speed parameters, direction, and coordinate system.

[0086] S3: State Sequence Maintenance: Calculate the next state based on the previous state and the extracted actions, and record the current aircraft state information (sixth element) after each action. This element includes the global position in the x direction, the global position in the y direction, the global position in the z direction, and the aircraft yaw angle.

[0087] S4: Regularized Output: Outputs a six-element sequence transformed from the user's natural language according to a specified format. The format is {number of steps} – {action type}: {action numerical parameters} {velocity parameters, which can be null} {direction} {coordinate system, two possible values: global or fuselage}. ({global position in x direction}, {global position in y direction}, {global position in z direction}, {aircraft yaw angle}).

[0088] S5: Output the six-element sequence.

[0089] 2) Code-generating intelligent agent The code generation intelligent agent process execution flow is as follows: Figure 4 As shown, steps S6 to S9 are as follows: S6: Input the six-element sequence.

[0090] S7: Template Matching: Constructs code templates based on user-specified language and library functions, including a general description of the action and specific code examples (including specific use case code and the meaning of corresponding parameters).

[0091] S8: Guided Code Generation: Combines the parameter information in the six-element sequence with the corresponding code template, and inputs it as prompt words into the large language model. The large language model understands the meaning of the parameters and generates an initial version of UAV control code that meets the parameter requirements.

[0092] S9: Encapsulation Output: The generated initial version of the UAV control code is encapsulated into a dictionary type according to a preset format. The dictionary contains a version number, code content, status, and description field. The encapsulated dictionary is then output as the flight control code to be reviewed.

[0093] 3) Code review and security detection intelligent agent The execution process of the code review and security detection agent is as follows: Figure 5 As shown, steps S10 to S16 are included: S10: Input control code.

[0094] S11: Regularization: Regularize the code provided by the large language model to remove identity information and irrelevant format information from the large model dialogue and convert it into string code.

[0095] S12: Is the code runnable? Use the subprocess module to run the code in the sandbox environment. If an error occurs, it means that there is a syntax error in the code. Output the error log. If it runs normally, proceed to the next step of the check.

[0096] S13: Constructing the AST abstract tree: Parsing the code into an AST object facilitates subsequent parameter analysis and extraction.

[0097] S14: Extract height and speed parameters: Access the AST node of the motion control related functions to extract the specific values ​​of speed, height and other related parameters.

[0098] S15: Does it comply with flight safety restrictions?: Compare the flight safety-related settings in the global settings file with the extracted parameter values. If it does not comply with safety requirements, an error log will be output; otherwise, a correct code will be output.

[0099] S16: Output the judgment result.

[0100] The present invention will now be described with reference to another specific embodiment: To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to another specific embodiment and the appendix.

[0101] 1. Method-related variables Table 1 Global Setting File Variables Table Table 2 Multi-agent cooperation variables table 2. Specific Implementation Process The following example illustrates the specific implementation process of this invention. Taking the example of a user needing to generate control code using the Python language's Airsim library functions to perform the following sequence: "Take off, ascend to a height of 10 meters at a speed of 1 meter per second. Proceed 5 meters, then turn due west and proceed 10 meters at a speed of 1 meter per second. Then land."

[0102] 1) Global file settings Configure the global configuration file according to user needs and the global settings file variable table. The specific values ​​are as follows: 2) User input, multi-agent collaborative work The user inputs the string "Take off, ascend to a height of 10 meters at a speed of 1 meter per second. Proceed 10 meters, then turn due west and proceed 10 meters at a speed of 1 meter per second. Then land." The AI ​​agent uses natural language understanding to extract the action intent and slots, specifying the reference coordinate system for each action as either the global coordinate system or the aircraft's body coordinate system. Simultaneously, it records the drone's flight status and outputs a six-element sequence containing coordinate system information. This ensures the correct extraction of subsequent function parameters and avoids forgetting historical states and coordinate system confusion. The output six-element sequence is as follows: After receiving the six-element series, the code generation agent generates code according to the "seed_code" template code in the global settings file, and encapsulates the generated code into a "code_output" dictionary type, representing the generated version v0 code. This code has not yet been checked. The generation result is as follows: The "code_output" is input into the code review and security detection agent. First, regularization is used to remove irrelevant text such as Markdown format and large model character information. Then, the code is checked for runnability using the subprocess module. The passed code is then used by the ast module to build an abstract tree. Height and velocity-related parameters are extracted from the movement function based on the parameter position, generating a height list and a velocity list, as shown below: Based on the extracted list of height and speed, and the constraint values ​​of the globally configured file variable `security_constraints`, an error log is generated and encapsulated as a dictionary type "code_output" before being passed to the code generation agent. The generated result is as follows: The code generation agent modifies the code based on the current code and the description in the "description" field of the "code_output" dictionary, outputs the modified code and encapsulates it into a "code_output" dictionary type and passes it to the code generation agent. If the check passes, the regularized code is passed to the user interaction layer.

[0103] 3) The user receives the output code file. The code is output to the specified location according to the globally set file variable output_file_path, resulting in the final generated code file.

[0104] In summary, the UAV control code generation method and system based on multi-agent systems provided by this invention includes: acquiring user-input natural language commands and the initial motion state of the UAV; establishing a UAV state transition sequence containing the initial motion state; inputting the natural language commands into a first agent; extracting action type, action numerical parameters, speed parameters, direction, and coordinate system from the commands as action elements based on a preset large language model; calculating the current motion state by combining the motion state of the previous moment stored in the state transition sequence; combining the action elements with the current motion state into a structured action sequence; and appending the current motion state as a new state node to the state transition sequence. The process involves several steps: First, a structured action sequence is input into a second agent, which generates flight control code based on a preset code template. This code is then encapsulated as a dictionary-style flight control code awaiting review. Next, a third agent is input into the flight control code awaiting review. A trial run detects syntax errors. If errors are found, an error log is generated. If the syntax is correct, an abstract syntax tree is constructed to extract flight parameters, which are then compared with preset safety constraints. If the code passes both syntax and safety checks, it is output as the target flight control code. Otherwise, an error log is generated, and the code awaiting review is fed back to the second agent for iterative correction until the code passes review or the preset number of corrections is reached. This invention achieves automated generation of safe and reliable UAV control code from natural language through the collaboration of three agents, effectively improving code accuracy and flight safety.

[0105] Those skilled in the art will understand that the exemplary components, systems, and methods described in conjunction with the embodiments disclosed herein can be implemented in hardware, software, or a combination of both. Whether implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this invention. When implemented in hardware, it can be, for example, electronic circuits, application-specific integrated circuits (ASICs), appropriate firmware, plug-ins, function cards, etc. When implemented in software, the elements of this invention are programs or code segments used to perform the desired tasks. The programs or code segments can be stored in a machine-readable medium or transmitted over a transmission medium or communication link via data signals carried in a carrier wave.

[0106] It should be clarified that the present invention is not limited to the specific configurations and processes described above and shown in the figures. For the sake of brevity, detailed descriptions of known methods are omitted here. In the above embodiments, several specific steps are described and shown as examples. However, the method process of the present invention is not limited to the specific steps described and shown. Those skilled in the art can make various changes, modifications, and additions, or change the order of steps, after understanding the spirit of the present invention.

[0107] In this invention, features described and / or illustrated for one embodiment may be used in the same or similar manner in one or more other embodiments, and / or combined with or in place of features of other embodiments.

[0108] The above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, various modifications and variations of the embodiments of the present invention are possible. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.

Claims

1. A method for generating unmanned aerial vehicle (UAV) control codes based on multi-agent systems, characterized in that, The method includes the following steps: Obtain natural language commands input by the user; Obtain the initial motion state of the UAV and establish a UAV state transition sequence containing the initial motion state; The natural language command is input to the first intelligent agent. The first intelligent agent extracts the action type, action numerical parameters, speed parameters, direction, and coordinate system from the natural language command based on a preset large language model, and calculates the current motion state based on the motion state of the previous moment stored in the UAV state transition sequence and the action elements. The motion state of the previous moment when the calculation is performed for the first time is the initial motion state. The action elements and the current motion state are combined into a structured action sequence, and the current motion state is added as a new state node to the UAV state transition sequence. The structured action sequence is input into the second intelligent agent, which, based on a preset code template, combines the parameters contained in the structured action sequence with the code template as prompt words and inputs them into the preset large language model to generate flight control code to be reviewed; The flight control code to be reviewed is input into a third intelligent agent. The third intelligent agent detects whether there are any syntax errors in the flight control code to be reviewed through trial operation. If there are syntax errors, an error log is generated directly. If there are no syntax errors, flight parameters are extracted from the flight control code to be reviewed by constructing an abstract syntax tree and the flight parameters are compared with preset safety constraints. If the flight control code to be reviewed has no syntax errors and the flight parameters meet the preset safety constraints, then the flight control code to be reviewed will be output as the target flight control code. Otherwise, an error log is generated, and the flight control code to be reviewed and the error log are re-entered into the second agent. The second agent corrects the flight control code to be reviewed based on the error log and regenerates a new flight control code to be reviewed, which is then re-entered into the third agent for review. This process continues until the review is passed or the preset number of corrections is reached, at which point the target flight control code is output or an error message is returned.

2. The method for generating UAV control code based on multi-agent technology according to claim 1, characterized in that, The preset code template is pre-configured for each action type. The code template includes a general action description and a reference code snippet for implementing the action type. The reference code snippet includes a code example and corresponding comment text. The comment text is used to explain the action and corresponding parameter meaning of each statement in the code example.

3. The method for generating UAV control code based on multi-agent technology according to claim 1, characterized in that, The third agent detects whether there are syntax errors in the flight control code to be reviewed through trial operation. Specifically, the third agent calls the subprocess module to execute the flight control code to be reviewed in a sandbox environment and captures abnormal information during the execution process. If abnormal information is captured, it is determined that there are syntax errors and an error log containing the abnormal information is generated.

4. The method for generating UAV control code based on multi-agent technology according to claim 1, characterized in that, Before the third intelligent agent detects whether there are syntax errors in the flight control code to be reviewed through trial operation, it also includes performing regularization processing on the flight control code to be reviewed. The regularization process includes: removing non-code information from the flight control code to be reviewed and converting the flight control code to be reviewed into a clean string-like code.

5. The method for generating UAV control code based on multi-agent technology according to claim 1, characterized in that, The flight parameters include at least one of flight altitude and flight speed, and the preset safety constraints include at least one of maximum flight altitude, maximum flight speed, and no-fly zone boundaries.

6. The method for generating UAV control code based on multi-agent technology according to claim 1, characterized in that, The method further includes: pre-configuring a global settings file, which stores the interface information of the preset large language model, the preset code template, and the preset security constraints.

7. The method for generating UAV control code based on multi-agent technology according to claim 1, characterized in that, The preset large language model adopts a locally deployed large language model and / or a cloud-based large language model called via API; the large language model is a general-purpose large language model or a vertical domain large language model with natural language understanding and code generation capabilities.

8. A multi-agent-based unmanned aerial vehicle (UAV) control code generation system, comprising a processor, a memory, and computer programs or instructions stored in the memory, characterized in that, The processor is configured to execute the computer program or instructions, and when the computer program or instructions are executed, the system implements the steps of the method as described in any one of claims 1 to 7.

9. A computer-readable storage medium having a computer program or instructions stored thereon, characterized in that, When the computer program or instructions are executed by a processor, they implement the steps of the method as described in any one of claims 1 to 7.

10. A computer program product, comprising a computer program or instructions, characterized in that, When the computer program or instructions are executed by a processor, they implement the steps of the method according to any one of claims 1 to 7.