MAVSDK-Java
MAVSDK-Java copied to clipboard
MAV_CMD_DO_SET_MODE (176) .Base on io.dronefleet.mavlink PC JAVA
Base on maven in io.dronefleet.mavlink,version 1.1.9
Now I user the AcFly , I am attempting to switch the mode using the MAV_CMD_DO_SET_MODE method, as mentioned in my documentation. It mentions the "hold position mode" with the following code:
public void setUavMode(String sn, Integer mode) throws IOException {
float modeValue = 0;
UavDataCacheManager cacheManager = UavDataCacheManager.getInstance();
switch (mode) {
case 1:
// Guided mode
modeValue = 0x60000;
break;
case 2:
// Hold position mode
modeValue = 0x30000;
break;
case 3:
// Auto mode
modeValue = 0x4000000;
break;
default:
break;
}
boolean exists = cacheManager.hasUavData(sn);
if (exists) {
Map<String, Object> uavData = cacheManager.getUavDataBySn(sn);
Object cachedModeNum = uavData.get("modeNum");
if (cachedModeNum instanceof Float && (Float) cachedModeNum == modeValue) {
return;
}
}
sendCommand(
sn,
MavCmd.MAV_CMD_DO_SET_MODE,
1,
modeValue,
0, 0, 0, 0, 0
);
}
Here are the respective mode commands for switching between modes:
Hold position mode:
COMMAND_LONG {target_system: 1, target_component: 1, command: 176, confirmation: 0, param1: 1, param2: 0x30000, param3: 0.0, param4: 0, param5: 0.0, param6: 0.0, param7: 0.0}
Altitude hold mode:
COMMAND_LONG {target_system: 1, target_component: 1, command: 176, confirmation: 0, param1: 1, param2: 0x20000, param3: 0.0, param4: 0, param5: 0.0, param6: 0.0, param7: 0.0}
Mission mode:
COMMAND_LONG {target_system: 1, target_component: 1, command: 176, confirmation: 0, param1: 1, param2: 0x4000000, param3: 0.0, param4: 0, param5: 0.0, param6: 0.0, param7: 0.0}
Return to home mode:
COMMAND_LONG {target_system: 1, target_component: 1, command: 176, confirmation: 0, param1: 1, param2: 0x5000000, param3: 0.0, param4: 0, param5: 0.0, param6: 0.0, param7: 0.0}
However, when I attempt to switch the mode, I get an error. Here are the logs:
2025-06-06 10:23:46.966 - [MQTT Call: re4d3f6a7cc42f0aac8015a1d68913fecd][] INFO com.yuefei.uav.mavlink.service.MavlinkService - The mode value to be switched is 393216.0
2025-06-06 10:23:46.967 - [MQTT Call: re4d3f6a7cc42f0aac8015a1d68913fecd][] INFO com.yuefei.uav.mavlink.service.MavlinkService - The sent command is CommandLong{targetSystem=6, targetComponent=1, command=EnumValue{value=176, entry=MAV_CMD_DO_SET_MODE}, confirmation=0, param1=1.0, param2=393216.0, param3=0.0, param4=0.0, param5=0.0, param6=0.0, param7=0.0}
2025-06-06 10:23:47.010 - [Thread-8][] ERROR com.yuefei.uav.mavlink.service.MavlinkService - Command rejected: MAV_RESULT_DENIED, FROM: MAV_CMD_DO_SET_MODE
2025-06-06 10:23:47.417 - [Thread-8][] INFO com.yuefei.uav.mavlink.service.MavlinkService - baseMode value is EnumValue{value=209, entry=null}
2025-06-06 10:23:47.420 - [Thread-8][] INFO com.yuefei.uav.mavlink.service.MavlinkService - customMode value is 196608
The question is whether the issue is because I'm passing a value that is not in the 0x60000 hexadecimal format. Currently, I am unable to perform the mode switch operation. How should I proceed?
I am confused. This is the repository of MAVSDK-Java, not io.dronefleet.mavlink, right?
I am confused. This is the repository of MAVSDK-Java, not io.dronefleet.mavlink, right?
yes,I know,I want to slove the problem,At least they are all implemented in Java and MavLink. At present, I have encountered problems in switching modes. I refer to the code format of MAV-SDK-JAVA here.
public void setUavMode(String sn, Integer mode) throws IOException {
float modeValue = 0;
UavDataCacheManager cacheManager = UavDataCacheManager.getInstance();
switch (mode) {
case 1:
// Guided mode
modeValue = 0x60000;
break;
case 2:
// Hold position mode
modeValue = 0x30000;
break;
case 3:
// Auto mode
modeValue = 0x4000000;
break;
default:
break;
}
boolean exists = cacheManager.hasUavData(sn);
if (exists) {
Map<String, Object> uavData = cacheManager.getUavDataBySn(sn);
Object cachedModeNum = uavData.get("modeNum");
if (cachedModeNum instanceof Float && (Float) cachedModeNum == modeValue) {
return;
}
}
sendCommand(
sn,
MavCmd.MAV_CMD_DO_SET_MODE,
1,
modeValue,
0, 0, 0, 0, 0
);
}
private void sendCommand(
String sn,
MavCmd command,
float param1, float param2, float param3, float param4,
float param5, float param6, float param7) throws Exception {
CommandLong cmd = new CommandLong.Builder()
.command(command)
.targetSystem(getMavlinkUav(sn).getTargetSystem().getTargetSystem()) // 飞控系统ID
.targetComponent(getMavlinkUav(sn).getTargetSystem().getTargetComponent()) // 组件ID
.param1(param1)
.param2(param2)
.param3(param3)
.param4(param4)
.param5(param5)
.param6(param6)
.param7(param7)
.build();
MavlinkConnection connection = getMavlinkUav(sn).getMavlinkConnection();
logger.info("发送的指令为{}",cmd);
connection.send1(
properties.getSystemId(),
properties.getComponentId(),
cmd);
}
I want to change the uav mode,I now want to switch the drone mode, and this mode is also switched according to the document such as
Hold position mode:
COMMAND_LONG {target_system: 1, target_component: 1, command: 176, confirmation: 0, param1: 1, param2: 0x30000, param3: 0.0, param4: 0, param5: 0.0, param6: 0.0, param7: 0.0}
Altitude hold mode:
COMMAND_LONG {target_system: 1, target_component: 1, command: 176, confirmation: 0, param1: 1, param2: 0x20000, param3: 0.0, param4: 0, param5: 0.0, param6: 0.0, param7: 0.0}
Mission mode:
COMMAND_LONG {target_system: 1, target_component: 1, command: 176, confirmation: 0, param1: 1, param2: 0x4000000, param3: 0.0, param4: 0, param5: 0.0, param6: 0.0, param7: 0.0}
Return to home mode:
COMMAND_LONG {target_system: 1, target_component: 1, command: 176, confirmation: 0, param1: 1, param2: 0x5000000, param3: 0.0, param4: 0, param5: 0.0, param6: 0.0, param7: 0.0}
At present, the take-off and pointing flight modes have been completed. Are there other points that need to be paid attention to when switching modes, such as some prerequisites and transmission issues, etc.? I look forward to your reply. Thank you.
But the code you're showing is not using MAVSDK-Java, is it?
But the code you're showing is not using MAVSDK-Java, is it?
This is a low-level method,At present, I just want to know if I need to switch a mode, are there any other conditions or points that I need to pay attention to?My switching method here is consistent with MAVSDK.The bottom layer is to call cmd_long's MAV_CMD_DO_SET_MODE (176)
Maybe somebody else can help, but I personally won't be doing custom MAVLink debugging here. If this is something that you can do with MAVSDK but that fails with your custom handling, then you can use the MAVSDK implementation as a reference to understand how you should do it.
If this is something that should be supported with MAVSDK but that doesn't work, then you can file a bug and explain how to reproduce it with MAVSDK 👍.