如何通过移动API/SDK读取N3产品Guidance系统的超声波数据
Hey there! Let’s tackle your two questions about pulling ultrasonic sensor data from the DJI Guidance system—whether via mobile API or for an N3 product using the mobile SDK. I’ve messed around with these setups before, so here’s a straightforward breakdown to get you up and running:
First, let’s cover the core workflow for accessing ultrasonic data through DJI’s mobile API. This applies to most DJI products paired with the Guidance module:
Prerequisites first
- Make sure you’re using the latest stable DJI Mobile SDK (v4.x or newer—Guidance support became reliable around this version).
- Your mobile device is connected to the DJI product (via USB OTG, WiFi, or Bluetooth).
- You’ve configured your app with a valid DJI App Key, and added all required permissions (e.g.,
ACCESS_FINE_LOCATION,INTERNETfor Android; corresponding privacy entries inInfo.plistfor iOS).
Initialize the DJI SDK
Start by registering the SDK when your app launches—this is the first step to establish communication with the product. Here’s a quick Android example:DJISDKManager.getInstance().registerApp(getApplicationContext(), new DJISDKManager.SDKManagerCallback() { @Override public void onRegister(DJIError error) { if (error == DJIError.ERROR_NONE) { // SDK registered successfully—start connecting to the product DJISDKManager.getInstance().startConnectionToProduct(); } else { // Handle registration failures (check App Key or network) Log.e("DJISDK", "Registration failed: " + error.getDescription()); } } @Override public void onProductDisconnect() { // Handle product disconnection } @Override public void onProductConnect(DJIProduct product) { // Product connected—proceed to access Guidance setupGuidanceUltrasonicListener(product); } // Other required callback methods... });Access the Guidance Manager
Once the product is connected, grab theDJIGuidanceManagerinstance from the product object. This is your gateway to sensor data:private void setupGuidanceUltrasonicListener(DJIProduct product) { if (product instanceof DJIAircraft) { DJIAircraft aircraft = (DJIAircraft) product; DJIGuidanceManager guidanceManager = aircraft.getGuidanceManager(); if (guidanceManager != null) { // Enable Guidance if it's not already active guidanceManager.setGuidanceMode(DJIGuidanceMode.NORMAL, new DJICompletionCallback() { @Override public void onResult(DJIError error) { if (error == DJIError.ERROR_NONE) { // Guidance is active—subscribe to ultrasonic data subscribeToUltrasonicData(guidanceManager); } } }); } } }Subscribe to Ultrasonic Data
Use thesetUltrasonicDataListenerto receive real-time ultrasonic readings. The data includes distances from multiple directions (front, back, left, right, etc., depending on your Guidance model):private void subscribeToUltrasonicData(DJIGuidanceManager guidanceManager) { guidanceManager.setUltrasonicDataListener(new DJIGuidanceManager.UltrasonicDataListener() { @Override public void onUltrasonicDataReceived(DJIGuidanceUltrasonicData ultrasonicData) { // Extract data for each direction float frontDistance = ultrasonicData.getFrontDistance(); // Units: cm float backDistance = ultrasonicData.getBackDistance(); float leftDistance = ultrasonicData.getLeftDistance(); float rightDistance = ultrasonicData.getRightDistance(); // Use the data as needed (log, display, trigger actions) Log.d("GuidanceUltrasonic", String.format("Front: %.1fcm, Back: %.1fcm", frontDistance, backDistance)); } }); }Quick Tips
- Ensure the Guidance module is powered on and not in sleep mode.
- Check the DJI Assistant 2 tool to confirm ultrasonic sensors are enabled for your product.
The N3 is a standalone flight controller, so the workflow is similar but has a few critical hardware/configuration steps to note:
Verify Hardware Connection
The N3 must be connected to the Guidance module via a CAN bus cable. Double-check the wiring (follow DJI’s official N3-Guidance connection diagram) to ensure proper communication between the two systems.Firmware Compatibility
Update both the N3 flight controller and Guidance module to the latest compatible firmware using DJI Assistant 2. Mismatched firmware is a common cause of communication failures.Configure Guidance in DJI Assistant 2
Before using the mobile SDK, open DJI Assistant 2, connect your N3, and:- Enable the ultrasonic sensors in the Guidance settings.
- Set the data output frequency to match your app’s needs (e.g., 10Hz, 20Hz).
- Confirm the Guidance module is set to work with the N3 flight controller.
Mobile SDK Workflow
The code for accessing ultrasonic data is identical to the first section! Once your mobile device is connected to the N3 (via USB OTG or a paired WiFi module), initialize the SDK, grab theDJIGuidanceManager, and subscribe to the ultrasonic data listener as shown earlier.Troubleshooting N3-Specific Issues
- If the SDK can’t detect the Guidance module, check the CAN bus connection and firmware versions again.
- Ensure the N3 is in a powered-on state (not just connected to USB) when testing.
内容的提问来源于stack exchange,提问作者Gino Farisano

