### Install pybind11 example module Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/lib/libmedia_codec/README.md Clone the repository recursively to include the pybind11 submodule, then install using pip. This command invokes CMake to build the module. ```bash git clone --recursive https://github.com/pybind/cmake_example.git pip install ./cmake_example ``` -------------------------------- ### Standard header and namespace setup Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/basics.md Common boilerplate code assumed for most pybind11 examples. ```cpp #include namespace py = pybind11; ``` -------------------------------- ### Install pybind11 configuration files Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/CMakeLists.txt Handles the installation of headers, CMake config files, and target exports. ```cmake if (PYBIND11_INSTALL) install(DIRECTORY ${PYBIND11_INCLUDE_DIR}/pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) # GNUInstallDirs "DATADIR" wrong here; CMake search path wants "share". set(PYBIND11_CMAKECONFIG_INSTALL_DIR "share/cmake/${PROJECT_NAME}" CACHE STRING "install path for pybind11Config.cmake") configure_package_config_file(tools/${PROJECT_NAME}Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) # Remove CMAKE_SIZEOF_VOID_P from ConfigVersion.cmake since the library does # not depend on architecture specific settings or libraries. set(_PYBIND11_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P}) unset(CMAKE_SIZEOF_VOID_P) write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake VERSION ${${PROJECT_NAME}_VERSION} COMPATIBILITY AnyNewerVersion) set(CMAKE_SIZEOF_VOID_P ${_PYBIND11_CMAKE_SIZEOF_VOID_P}) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake tools/FindPythonLibsNew.cmake tools/pybind11Tools.cmake DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) if(NOT (CMAKE_VERSION VERSION_LESS 3.0)) if(NOT PYBIND11_EXPORT_NAME) set(PYBIND11_EXPORT_NAME "${PROJECT_NAME}Targets") endif() install(TARGETS pybind11 module embed EXPORT "${PYBIND11_EXPORT_NAME}") if(PYBIND11_MASTER_PROJECT) install(EXPORT "${PYBIND11_EXPORT_NAME}" NAMESPACE "${PROJECT_NAME}::" DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) endif() endif() endif() ``` -------------------------------- ### Full Example: Initialize Robot and Log to File Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/ubuntu/src/pybind11/docs/python_sdk/log.md This example demonstrates how to enable logging to a file, initialize a RoboMaster robot, and print its version. Ensure the SDK is configured correctly for your network environment. ```python import robomaster from robomaster if __name__ == '__main__': robomaster.enable_logging_to_file() # 如果本地IP 自动获取不正确,手动指定本地IP地址 # robomaster.config.LOCAL_IP_STR = "192.168.2.20" ep_robot = robot.Robot() # 指定连接方式为AP 直连模式 ep_robot.initialize(conn_type='rndis') version = ep_robot.get_version() print("Robot version: {0}".format(version)) ep_robot.close() ``` -------------------------------- ### Set Drone LED Module (Full Example) Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/lib/libmedia_codec/pybind11/docs/python_sdk/multi_robot_drone_example.md A complete example demonstrating how to set drone LED colors, including setting all to white and then setting individual colors using a dictionary. Requires importing time. ```python import time from multi_robomaster import multi_robot def base_action_1(robot_group): robot_group.set_led(255, 255, 255) time.sleep(2) robot_group.set_led(command_dict={1: [255, 0, 0], 2: [255, 255, 0]}) if __name__ == '__main__': # get drone sn by run the expamles of /15_multi_robot/multi_drone/01_scan_ip.py robot_sn_list = ["0TQZH79ED00H56", "0TQZH79ED00H89"] multi_drone = multi_robot.MultiDrone() multi_drone.initialize(robot_num=2) multi_drone.number_id_by_sn([1, robot_sn_list[0]], [2, robot_sn_list[1]]) multi_drone_group1 = multi_drone.build_group([1, 2]) multi_drone.run([multi_drone_group1, base_action_1]) multi_drone.close() ``` -------------------------------- ### Install Pybind11 Manually Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/compiling.md Steps to build and install pybind11 locally. This is necessary for find_package to locate it. ```bash cd pybind11 mkdir build cd build cmake .. make install ``` -------------------------------- ### Create Mock Install Target Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/lib/libmedia_codec/pybind11/tests/test_cmake_build/CMakeLists.txt Defines a custom target 'mock_install' that simulates an installation process using CMake's install script. This is used when the 'PYBIND11_INSTALL' option is enabled. ```cmake add_custom_target(mock_install ${CMAKE_COMMAND} "-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/mock_install" -P "${PROJECT_BINARY_DIR}/cmake_install.cmake" ) ``` -------------------------------- ### Install RoboMaster Python SDK (Windows) Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/docs/source/python_sdk/installs.md Use this command to install the RoboMaster SDK on Windows. Run the command prompt as an administrator. ```default pip install robomaster ``` -------------------------------- ### Mock Installation Target and Tests Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/tests/test_cmake_build/CMakeLists.txt If `PYBIND11_INSTALL` is enabled, this section defines a 'mock_install' target to simulate installation and then adds build test targets for installed configurations, including 'installed_function', 'installed_target', and conditionally 'installed_embed'. ```cmake if(PYBIND11_INSTALL) add_custom_target(mock_install ${CMAKE_COMMAND} "-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/mock_install" -P "${PROJECT_BINARY_DIR}/cmake_install.cmake" ) pybind11_add_build_test(installed_function INSTALL) pybind11_add_build_test(installed_target INSTALL) if(NOT ${PYTHON_MODULE_EXTENSION} MATCHES "pypy") pybind11_add_build_test(installed_embed INSTALL) endif() endif() ``` -------------------------------- ### Query Drone SN and Battery (Full Example) Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/lib/libmedia_codec/pybind11/docs/python_sdk/multi_robot_drone_example.md A complete example demonstrating how to query drone SN and battery information. Ensure drone initialization and grouping are done prior to running this task. ```python from multi_robomaster import multi_robot def basic_task(robot_group): robot_group.get_sn() robot_group.get_battery() if __name__ == '__main__': # get drone sn by run the expamles of /15_multi_robot/multi_drone/01_scan_ip.py robot_sn_list = ["0TQZH79ED00H56", "0TQZH79ED00H89"] sn_list = [] battery_list = [] drone_num = 2 multi_drone = multi_robot.MultiDrone() multi_drone.initialize(robot_num=2) multi_drone.number_id_by_sn([0, robot_sn_list[0]], [1, robot_sn_list[1]]) tello_group = multi_drone.build_group([0, 1]) multi_drone.run([tello_group, basic_task]) multi_drone.close() ``` -------------------------------- ### Example Program Output Log Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/ubuntu/src/pybind11/docs/text_sdk/multi_ctrl.md This log shows the communication between the host and multiple robots during the execution of the example script. It includes connection attempts, command acknowledgments, and gimbal movement commands. ```text 192.168.1.103 connecting... 192.168.1.103:command seq 1 192.168.1.103:ok seq 1 192.168.1.103:robot mode free seq 2 192.168.1.103:ok seq 2 192.168.1.117 connecting... 192.168.1.117:command seq 1 192.168.1.117:ok seq 1 192.168.1.117:robot mode free seq 2 192.168.1.117:ok seq 2 192.168.1.103:gimbal moveto p 0 y 0 vp 90 vy 90 wait_for_complete false seq 3 192.168.1.103:ok seq 3 192.168.1.117:gimbal moveto p 0 y 0 vp 90 vy 90 wait_for_complete false seq 3 192.168.1.117:ok seq 3 ``` -------------------------------- ### Post-Release Development Setup Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/lib/libmedia_codec/pybind11/docs/release.rst Commands to push changes after incrementing the version for development. ```bash git add ``` ```bash git commit ``` ```bash git push ``` -------------------------------- ### Configure installation directory Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/CMakeLists.txt Sets the installation path for headers based on the Python include directory if enabled. ```cmake option (USE_PYTHON_INCLUDE_DIR "Install pybind11 headers in Python include directory instead of default installation prefix" OFF) if (USE_PYTHON_INCLUDE_DIR) file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS}) endif() ``` -------------------------------- ### Install RoboMaster Python SDK with Tsinghua Mirror (Windows) Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/docs/source/python_sdk/installs.md If facing network issues during installation on Windows, try this command to use the Tsinghua mirror. ```default pip install -i https://pypi.tuna.tsinghua.edu.cn/simple robomaster ``` -------------------------------- ### Create Library with pybind11 Embed and Install Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/ubuntu/src/pybind11/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt Defines a library target 'test_embed_lib' from 'embed.cpp', links it with pybind11 embed, and configures installation rules for the library and its export files. This allows the library to be installed and used by other projects. ```cmake # Test custom export group -- PYBIND11_EXPORT_NAME add_library(test_embed_lib ../embed.cpp) target_link_libraries(test_embed_lib PRIVATE pybind11::embed) install(TARGETS test_embed_lib EXPORT test_export ARCHIVE DESTINATION bin LIBRARY DESTINATION lib RUNTIME DESTINATION lib) install(EXPORT test_export DESTINATION lib/cmake/test_export/test_export-Targets.cmake) ``` -------------------------------- ### RoboMaster SDK Example: Logger Configuration and File Logging Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/lib/libmedia_codec/pybind11/docs/python_sdk/log.md A complete example demonstrating how to enable logging to a file and initialize the robot connection. This script will generate a log file in the program's directory upon execution. ```python import robomaster from robomaster import robot if __name__ == '__main__': robomaster.enable_logging_to_file() # 如果本地IP 自动获取不正确,手动指定本地IP地址 # robomaster.config.LOCAL_IP_STR = "192.168.2.20" ep_robot = robot.Robot() # 指定连接方式为AP 直连模式 ep_robot.initialize(conn_type='rndis') version = ep_robot.get_version() print("Robot version: {0}".format(version)) ep_robot.close() ``` -------------------------------- ### Install PySerial for UART Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/text_sdk/connection.md Command to install the required serial communication library. ```default pip install pyserial ``` -------------------------------- ### Install H.264 Decoder on Windows (Python 3.6) Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/README.md Install the H.264 decoder wheel for Python 3.6 on a 64-bit Windows system using pip. ```bash pip install libh264decoder-0.0.1-cp36-cp36m-win_amd64.whl ``` -------------------------------- ### Create a complete binding module Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/basics.md Full example of a C++ file including the function and the PYBIND11_MODULE macro. ```cpp #include int add(int i, int j) { return i + j; } PYBIND11_MODULE(example, m) { m.doc() = "pybind11 example plugin"; // optional module docstring m.def("add", &add, "A function which adds two numbers"); } ``` -------------------------------- ### Install Opus Decoder on Windows (Python 3.6) Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/README.md Install the Opus decoder wheel for Python 3.6 on a 64-bit Windows system using pip. ```bash pip install opus_decoder-0.0.1-cp36-cp36m-win_amd64.whl ``` -------------------------------- ### Configure USB Connection Script Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/text_sdk/connection.md Example Python script for initializing a socket connection to the robot over USB. ```python # -*- encoding: utf-8 -*- # 测试环境: Python 3.6 版本 import socket import sys # USB 模式下,机器人默认 IP 地址为 192.168.42.2, 控制命令端口号为 40923 host = "192.168.42.2" port = 40923 # other code ``` -------------------------------- ### Applying Policies to Properties Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/ubuntu/src/pybind11/docs/advanced/functions.md Examples of how to apply return value policies to class properties. ```APIDOC ## Applying Policies to Properties ### Description Policies can be applied to properties using `def_property`. You can apply a policy to both getter and setter, or target specific functions using `py::cpp_function`. ### Example: Terse Syntax ```cpp class_(m, "MyClass") .def_property("data", &MyClass::getData, &MyClass::setData, py::return_value_policy::copy); ``` ### Example: Targeted Syntax ```cpp class_(m, "MyClass") .def_property("data", py::cpp_function(&MyClass::getData, py::return_value_policy::copy), py::cpp_function(&MyClass::setData) ); ``` ``` -------------------------------- ### Start Camera Video Streaming and Capture Frames Source: https://context7.com/dji-sdk/robomaster-sdk/llms.txt Start video streaming from the robot's camera and capture frames for processing using OpenCV. Frames can be read with a specified timeout and strategy. Ensure video streaming is stopped and the robot connection is closed afterwards. ```python from robomaster import robot from robomaster import camera import time import cv2 ep_robot = robot.Robot() ep_robot.initialize(conn_type="sta") ep_camera = ep_robot.camera # Start video stream with display (resolution: 360p, 540p, or 720p) ep_camera.start_video_stream(display=True, resolution=camera.STREAM_720P) # Read frames for processing for i in range(100): # Read frame as OpenCV image (numpy array) img = ep_camera.read_cv2_image(timeout=3, strategy="newest") if img is not None: # Process the image with OpenCV gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.imshow("Processed", gray) cv2.waitKey(1) cv2.destroyAllWindows() ep_camera.stop_video_stream() ep_robot.close() ``` -------------------------------- ### Import and Use pybind11 Module in Python Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/lib/libmedia_codec/pybind11/docs/basics.md This interactive Python session demonstrates how to import the compiled 'example' module and call the exposed 'add' function. ```pycon >>> import example >>> example.add(1, 2) 3L >>> ``` -------------------------------- ### Console Output Log Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/docs/source/text_sdk/multi_ctrl.md Example of the terminal output showing the connection sequence and command acknowledgments between the host and the robots. ```text 192.168.1.103 connecting... 192.168.1.103:command seq 1 192.168.1.103:ok seq 1 192.168.1.103:robot mode free seq 2 192.168.1.103:ok seq 2 192.168.1.117 connecting... 192.168.1.117:command seq 1 192.168.1.117:ok seq 1 192.168.1.117:robot mode free seq 2 192.168.1.117:ok seq 2 192.168.1.103:gimbal moveto p 0 y 0 vp 90 vy 90 wait_for_complete false seq 3 192.168.1.103:ok seq 3 192.168.1.117:gimbal moveto p 0 y 0 vp 90 vy 90 wait_for_complete false seq 3 192.168.1.117:ok seq 3 ``` -------------------------------- ### Verify Modularized Bindings in Python Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/faq.md Example usage of the modularized binding code within a Python interpreter. ```pycon >>> import example >>> example.add(1, 2) 3 >>> example.sub(1, 1) 0 ``` -------------------------------- ### Build H.264 and Opus Decoders on Ubuntu Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/README.md Execute this script to build the H.264 and Opus decoder libraries on an Ubuntu system. Ensure all dependencies are installed prior to running. ```bash chmod +x ./build.sh ./build.sh ``` -------------------------------- ### Import multi_robot module Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/lib/libmedia_codec/pybind11/docs/python_sdk/multi_robot_drone_example.md Import the multi_robot module from the installed multi_robomaster package to begin controlling multiple drones. ```python from multi_robomaster import multi_robot ``` -------------------------------- ### Building with Setuptools Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/lib/libmedia_codec/pybind11/docs/compiling.md Instructions for building Python projects using setuptools, suitable for projects intended for PyPI. ```APIDOC ## Building with setuptools For projects on PyPI, building with setuptools is the way to go. Sylvain Corlay has kindly provided an example project which shows how to set up everything, including automatic generation of documentation using Sphinx. Please refer to the [[python_example]](#python-example) repository. ``` -------------------------------- ### Complete Audio Recording Routine Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/ubuntu/src/pybind11/docs/python_sdk/beginner_ep.md A full script demonstrating robot initialization, audio recording, and resource cleanup. ```python from robomaster import robot if __name__ == '__main__': ep_robot = robot.Robot() ep_robot.initialize(conn_type="rndis") ep_camera = ep_robot.camera ep_camera.record_audio(save_file="output.wav", seconds=5, sample_rate=16000) ep_robot.close() ``` -------------------------------- ### Find Pybind11 Package Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/compiling.md Use find_package to detect an installed pybind11. This requires pybind11 to be correctly installed on the system. ```cmake cmake_minimum_required(VERSION 2.8.12) project(example) find_package(pybind11 REQUIRED) pybind11_add_module(example example.cpp) ``` -------------------------------- ### Python Usage Example for Custom Type Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/advanced/cast/custom.md Demonstrates how a Python class with an `__int__` method can be used with the custom C++ type caster. Ensure the `example` module is correctly set up. ```python class A: def __int__(self): return 123 from example import print print(A()) ``` -------------------------------- ### Enter SDK Mode Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/ubuntu/src/pybind11/docs/text_sdk/intro.md To enable SDK control, send the 'command' command to the robot. A successful response is 'ok'. This must be done before sending other SDK control commands. ```Shell >>> please input SDK cmd: command ok ``` -------------------------------- ### Basic CMake Build Configuration Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/ubuntu/src/pybind11/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt Sets up the minimum CMake version, project name, and enables pybind11 for embedding. This is the foundational configuration for building the SDK. ```cmake cmake_minimum_required(VERSION 3.0) project(test_subdirectory_embed CXX) set(PYBIND11_INSTALL ON CACHE BOOL "") set(PYBIND11_EXPORT_NAME test_export) add_subdirectory(${PYBIND11_PROJECT_DIR} pybind11) ``` -------------------------------- ### Initialize Eigen Matrix from buffer Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/advanced/pycpp/numpy.md Demonstrates creating a custom constructor that accepts a py::buffer to initialize an Eigen matrix. ```cpp /* Bind MatrixXd (or some other Eigen type) to Python */ typedef Eigen::MatrixXd Matrix; typedef Matrix::Scalar Scalar; constexpr bool rowMajor = Matrix::Flags & Eigen::RowMajorBit; py::class_(m, "Matrix", py::buffer_protocol()) .def("__init__", [](Matrix &m, py::buffer b) { typedef Eigen::Stride Strides; /* Request a buffer descriptor from Python */ py::buffer_info info = b.request(); /* Some sanity checks ... */ if (info.format != py::format_descriptor::format()) throw std::runtime_error("Incompatible format: expected a double array!"); if (info.ndim != 2) throw std::runtime_error("Incompatible buffer dimension!"); auto strides = Strides( info.strides[rowMajor ? 0 : 1] / (py::ssize_t)sizeof(Scalar), info.strides[rowMajor ? 1 : 0] / (py::ssize_t)sizeof(Scalar)); auto map = Eigen::Map( static_cast(info.ptr), info.shape[0], info.shape[1], strides); new (&m) Matrix(map); }); ``` -------------------------------- ### Import and use the module in Python Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/basics.md Interactive session demonstrating how to import and call the bound function. ```pycon $ python Python 2.7.10 (default, Aug 22 2015, 20:33:39) [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import example >>> example.add(1, 2) 3L >>> ``` -------------------------------- ### Pybind11 Deprecation Warning Example Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/upgrade.md This is an example of a runtime warning that may appear during module initialization when using deprecated pybind11 features, such as old-style placement-new constructors or pickling functions. The warning is typically only visible in debug builds. ```none pybind11-bound class 'mymodule.Foo' is using an old-style placement-new '__init__' which has been deprecated. See the upgrade guide in pybind11's docs. ``` -------------------------------- ### GET /sensor_adapter_ctrl/check_condition Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/python/sensor_adapter.md Checks if a specific sensor adapter condition is currently met. ```APIDOC ## GET /sensor_adapter_ctrl/check_condition ### Description 判断传感器转接模块相应端口引脚脉冲是否为(高/低/跳变)。 ### Parameters - **condition** (object) - Required - 传感器转接模块相应端口引脚的触发条件对象 ### Response - **result** (bool) - 满足条件时返回真,否则返回假 ``` -------------------------------- ### CMake Project Setup and Pybind11 Module Build Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/lib/libmedia_codec/pybind11/tests/test_cmake_build/subdirectory_function/CMakeLists.txt Configures the minimum CMake version, names the project, adds the Pybind11 subdirectory, and builds a Python extension module named 'test_cmake_build' from '../main.cpp'. ```cmake cmake_minimum_required(VERSION 2.8.12) project(test_subdirectory_module CXX) add_subdirectory(${PYBIND11_PROJECT_DIR} pybind11) pybind11_add_module(test_cmake_build THIN_LTO ../main.cpp) ``` -------------------------------- ### Initialize and Connect to Robot Source: https://context7.com/dji-sdk/robomaster-sdk/llms.txt Connect to the robot using different network modes and retrieve basic device information. ```python from robomaster import robot # Initialize robot with WiFi direct connection (AP mode) ep_robot = robot.Robot() ep_robot.initialize(conn_type="ap") # Get robot information version = ep_robot.get_version() sn = ep_robot.get_sn() print(f"Robot Version: {version}, SN: {sn}") # Set robot work mode: 'free', 'gimbal_lead', or 'chassis_lead' ep_robot.set_robot_mode(mode=robot.GIMBAL_LEAD) # Close connection when done ep_robot.close() ``` ```python from robomaster import robot # Station mode - robot and computer on same network ep_robot = robot.Robot() ep_robot.initialize(conn_type="sta") # Or connect via USB RNDIS # ep_robot.initialize(conn_type="rndis") # Perform operations... print(f"Connected to robot at {ep_robot.ip}") ep_robot.close() ``` -------------------------------- ### Execute Python Dictionary Print Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/advanced/functions.md Example usage of the exported print_dict function in Python. ```pycon >>> print_dict({'foo': 123, 'bar': 'hello'}) key=foo, value=123 key=bar, value=hello ``` -------------------------------- ### Armor Plate Sensitivity Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/lib/libmedia_codec/pybind11/docs/text_sdk/protocol_api.md Get the sensitivity of the armor plate's hit detection. ```APIDOC ## GET /sdk/armor/sensitivity ### Description Gets the sensitivity of the armor plate's hit detection. ### Method GET ### Endpoint /sdk/armor/sensitivity ### Parameters None ### Response #### Success Response (200) - **sensitivity** (float) - The current armor plate hit detection sensitivity. ### Response Example ```json { "sensitivity": 5.0 } ``` ``` -------------------------------- ### GET /sensor_adapter_ctrl/pulse_period Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/python/sensor_adapter.md Retrieves the pulse duration for a specific port on the sensor adapter module. ```APIDOC ## GET /sensor_adapter_ctrl/pulse_period ### Description 获取传感器转接模块相应端口引脚的脉冲持续时间。 ### Parameters #### Query Parameters - **board_id** (int) - Required - 传感器转接模块编号,范围为[1:6] - **port_num** (uint8) - Required - 传感器转接模块上的端口号,范围为[1:2] ### Response - **duration** (uint32) - 传感器转接模块相应端口引脚的脉冲持续时间,精确度为 1 ms ### Response Example { "duration": 100 } ``` -------------------------------- ### Release and Deployment Commands Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/lib/libmedia_codec/pybind11/docs/release.rst Commands used to tag a release and upload distributions to PyPI. ```bash git tag -a vX.Y.Z -m 'vX.Y.Z release' ``` ```bash git push ``` ```bash git push --tags ``` ```bash python setup.py sdist upload ``` ```bash python setup.py bdist_wheel upload ``` -------------------------------- ### 执行多机动作命令 Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/ubuntu/src/pybind11/docs/python_sdk/multi_robot_drone_example.md 使用 run 方法执行定义好的动作函数,支持多组动作并行执行。 ```default multi_drone.run([multi_drone_group1, base_action_1]) ``` ```default def base_action_1(robot_group): robot_group.get_sn() robot_group.get_battery() ``` ```default multi_drone.run([multi_drone_group1, base_action_1], [multi_drone_group2, base_action_2]) ``` -------------------------------- ### GET /sensor_adapter_ctrl/adc Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/python/sensor_adapter.md Retrieves the ADC value from a specific port on the sensor adapter module. ```APIDOC ## GET /sensor_adapter_ctrl/adc ### Description 获取传感器转接模块相应端口模拟引脚的 ADC 值。 ### Parameters #### Query Parameters - **board_id** (int) - Required - 传感器转接模块编号,范围为[1:6] - **port_num** (uint8) - Required - 传感器转接模块上的端口号,范围为[1:2] - **wait_for_complete** (bool) - Optional - 是否等待执行完成,默认为 True ### Response - **value** (uint16) - 传感器转接模块相应端口模拟引脚的 ADC 值,范围为[0:1023] ### Response Example { "value": 512 } ``` -------------------------------- ### Verify Pytest Installation Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/ubuntu/src/pybind11/tests/CMakeLists.txt Checks for the presence of pytest and ensures it meets the minimum version requirement. ```cmake if(NOT PYBIND11_PYTEST_FOUND) execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import pytest; print(pytest.__version__)" RESULT_VARIABLE pytest_not_found OUTPUT_VARIABLE pytest_version ERROR_QUIET) if(pytest_not_found) message(FATAL_ERROR "Running the tests requires pytest. Please install it manually" " (try: ${PYTHON_EXECUTABLE} -m pip install pytest)") elseif(pytest_version VERSION_LESS 3.0) message(FATAL_ERROR "Running the tests requires pytest >= 3.0. Found: ${pytest_version}" "Please update it (try: ${PYTHON_EXECUTABLE} -m pip install -U pytest)") endif() set(PYBIND11_PYTEST_FOUND TRUE CACHE INTERNAL "") endif() ``` -------------------------------- ### Override virtual methods in Python Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/advanced/classes.md Example of extending the C++ Animal class within Python. ```pycon >>> from example import * >>> d = Dog() >>> call_go(d) u'woof! woof! woof! ' >>> class Cat(Animal): ... def go(self, n_times): ... return "meow! " * n_times ... >>> c = Cat() >>> call_go(c) u'meow! meow! meow! ' ``` -------------------------------- ### UART 明文 SDK 通信测试 Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/docs/source/extension_module/uart.md 通过串口发送 command; 指令以验证明文 SDK 是否解析成功,成功时应收到 ok 响应。 ```text command; ``` -------------------------------- ### Pass character literals from Python Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/advanced/cast/strings.md Examples of passing characters to bound C++ functions from Python. ```python >>> example.pass_char('A') 'A' ``` -------------------------------- ### SDK 模式控制指令交互 Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/docs/source/text_sdk/intro.md 展示了通过 SDK 交互界面进入模式、执行动作及退出的指令序列。 ```default >>> please input SDK cmd: command ok ``` ```default >>> please input SDK cmd: blaster fire ok ``` ```default >>> please input SDK cmd: quit ok ``` -------------------------------- ### Pickle a C++ Object in Python Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/advanced/classes.md Example of using the pickle module to serialize a bound C++ object. ```python try: import cPickle as pickle # Use cPickle on Python 2.7 except ImportError: import pickle p = Pickleable("test_value") p.setExtra(15) data = pickle.dumps(p, 2) ``` -------------------------------- ### Call bound functions with keyword arguments Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/basics.md Demonstrates calling the bound function from Python using keyword arguments. ```pycon >>> import example >>> example.add(i=1, j=2) 3L ``` -------------------------------- ### Define a C++ Pet struct Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/classes.md A simple C++ struct definition to be used for Python binding examples. ```cpp struct Pet { Pet(const std::string &name) : name(name) { } void setName(const std::string &name_) { name = name_; } const std::string &getName() const { return name; } std::string name; }; ``` -------------------------------- ### Apply Return Value Policy to Properties Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/advanced/functions.md Demonstrates applying a return value policy to a property getter using the concise syntax. ```cpp class_(m, "MyClass") .def_property("data", &MyClass::getData, &MyClass::setData, py::return_value_policy::copy); ``` -------------------------------- ### 设置无人机LED模块 Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/ubuntu/src/pybind11/docs/python_sdk/multi_robot_drone_example.md 通过 set_led 接口控制无人机灯光,支持统一设置或通过字典为不同飞机设置不同颜色。 ```python def base_action_1(robot_group): robot_group.set_led(255, 255, 255) time.sleep(2) robot_group.set_led(command_dict={1: [255, 0, 0], 2: [255, 255, 0]}) ``` ```python import time from multi_robomaster import multi_robot def base_action_1(robot_group): robot_group.set_led(255, 255, 255) time.sleep(2) robot_group.set_led(command_dict={1: [255, 0, 0], 2: [255, 255, 0]}) if __name__ == '__main__': # get drone sn by run the expamles of /15_multi_robot/multi_drone/01_scan_ip.py robot_sn_list = ["0TQZH79ED00H56", "0TQZH79ED00H89"] multi_drone = multi_robot.MultiDrone() multi_drone.initialize(robot_num=2) multi_drone.number_id_by_sn([1, robot_sn_list[0]], [2, robot_sn_list[1]]) multi_drone_group1 = multi_drone.build_group([1, 2]) multi_drone.run([multi_drone_group1, base_action_1]) multi_drone.close() ``` -------------------------------- ### Incorrect binding example Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/advanced/classes.md Demonstrates an incorrect approach where the trampoline method is bound instead of the base class method. ```cpp py::class_(m, "Animal"); .def(py::init<>()) .def("go", &PyAnimal::go); /* <--- THIS IS WRONG, use &Animal::go */ ``` -------------------------------- ### Use keyword arguments in Python and C++ Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/lib/libmedia_codec/pybind11/docs/advanced/pycpp/object.md Demonstrates keyword argument syntax in Python compared to the C++ literal _a syntax. ```python def f(number, say, to): ... # function code f(1234, say="hello", to=some_instance) # keyword call in Python ``` ```cpp using namespace pybind11::literals; // to bring in the `_a` literal f(1234, "say"_a="hello", "to"_a=some_instance); // keyword call in C++ ``` -------------------------------- ### Define inheritance in a single module Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/advanced/misc.md Basic example of defining a base class and a derived class within the same module. ```cpp py::class_ pet(m, "Pet"); pet.def(py::init()) .def_readwrite("name", &Pet::name); py::class_(m, "Dog", pet /* <- specify parent */) .def(py::init()) .def("bark", &Dog::bark); ``` -------------------------------- ### Inspect function signatures in Python Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/basics.md Use the help function to view the generated function signatures and documentation. ```pycon >>> help(example) .... FUNCTIONS add(...) Signature : (i: int, j: int) -> int A function which adds two numbers ``` -------------------------------- ### Bind a C++ class to Python Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/classes.md Basic pybind11 module setup to expose a C++ class and its methods to Python. ```cpp #include namespace py = pybind11; PYBIND11_MODULE(example, m) { py::class_(m, "Pet") .def(py::init()) .def("setName", &Pet::setName) .def("getName", &Pet::getName); } ``` -------------------------------- ### Preferred Module Entry Point: PYBIND11_MODULE Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/changelog.md Use PYBIND11_MODULE as the preferred macro for creating module entry points. The older PYBIND11_PLUGIN macro is deprecated. ```cpp // new PYBIND11_MODULE(example, m) { m.def("add", [](int a, int b) { return a + b; }); } // old PYBIND11_PLUGIN(example) { py::module m("example"); m.def("add", [](int a, int b) { return a + b; }); return m.ptr(); } ``` -------------------------------- ### Define C++ Class for Benchmarking Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/lib/libmedia_codec/pybind11/docs/benchmark.md Example of a C++ class definition with multiple methods, used in the benchmark to generate bindings. ```cpp class cl034 { public: cl279 *fn_000(cl084 *, cl057 *, cl065 *, cl042 *); cl025 *fn_001(cl098 *, cl262 *, cl414 *, cl121 *); cl085 *fn_002(cl445 *, cl297 *, cl145 *, cl421 *); cl470 *fn_003(cl200 *, cl323 *, cl332 *, cl492 *); }; ``` -------------------------------- ### Compile test cases on Windows Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/src/pybind11/docs/basics.md Commands to build and run tests using CMake on Windows. ```batch mkdir build cd build cmake .. cmake --build . --config Release --target check ``` -------------------------------- ### Define pybind11 class bindings Source: https://github.com/dji-sdk/robomaster-sdk/blob/master/examples/plaintext_sample_code/RoboMasterEP/stream/decoder/ubuntu/src/pybind11/docs/benchmark.md Example of binding a C++ class with multiple methods using the pybind11 module macro. ```cpp ... class cl034 { public: cl279 *fn_000(cl084 *, cl057 *, cl065 *, cl042 *); cl025 *fn_001(cl098 *, cl262 *, cl414 *, cl121 *); cl085 *fn_002(cl445 *, cl297 *, cl145 *, cl421 *); cl470 *fn_003(cl200 *, cl323 *, cl332 *, cl492 *); }; ... PYBIND11_MODULE(example, m) { ... py::class_(m, "cl034") .def("fn_000", &cl034::fn_000) .def("fn_001", &cl034::fn_001) .def("fn_002", &cl034::fn_002) .def("fn_003", &cl034::fn_003) ... } ```