mavlink: Unable to get any kind of data from UDP connection
Hi, I have an issue trying to get data with UDP connection to an arducopter >= 3.7.
I’m following these steps:
- create mavlink connection
- start to send CGS Heartbeat
- request Data Stream
- get messages from mavlinkConnection
I set up some wrapper and functions but these steps can be simplified in this way.
—create mavlink connection
Socket socket = new Socket(host, port, false);
socket.setSoTimeout(10000);
MavlinkConnection connection = MavlinkConnection.builder(socket.getInputStream(), socket.getOutputStream())
.dialect(MavAutopilot.MAV_AUTOPILOT_GENERIC, new StandardDialect())
.dialect(MavAutopilot.MAV_AUTOPILOT_ARDUPILOTMEGA, newArdupilotmegaDialect()).build();
- start to send CGS Heartbeat (sent every second)
Heartbeat msg = Heartbeat.builder()
.type(MavType.MAV_TYPE_GCS)
.autopilot(MavAutopilot.MAV_AUTOPILOT_INVALID)
.systemStatus(MavState.MAV_STATE_UNINIT)
.mavlinkVersion(3)
.build();
try {
connection.send2(255, 1, msg);
} catch (IOException e) {
Logger.logException(TAG, e);
}
- request Data Stream
int heartbeatStreamId = 0;
int batteryStatusStreamId = 147;
CommandLong hbCmd = commandLongBuilder.command(MavCmd.MAV_CMD_SET_MESSAGE_INTERVAL).param1(heartbeatStreamId).param2(0).targetSystem(0).targetComponent(0).build();
CommandLong batteryCmd = commandLongBuilder.command(MavCmd.MAV_CMD_SET_MESSAGE_INTERVAL).param1(heartbeatStreamId).param2(0).targetSystem(0).targetComponent(0).build();
connection.send2(255, 1, hbCmd);
connection.send2(255, 1, batteryCmd);
- get messages from mavlinkConnection
while (mavlinkConnection != null) {
MavlinkMessage message = null;
try {
message = mavlinkConnection.next();
......
} catch (Throwable e) {
message = null;
......
}
}
I never get any kind of message from this connection, I always get timeout. What am I doing wrong?
Is there any issue in the create of mavlink connection? Do I have to do something else to start the mavlink connection in addition to sending the heartbeat and requesting the data stream?
Regarding the connection, I know that this kind of socket constructor is deprecated but I don’t know any other way to create a mavlink connection with a UDP socket. Can you suggest a better one?
Many thanks in advance
About this issue
- Original URL
- State: open
- Created 5 years ago
- Comments: 26 (6 by maintainers)
In terms of reading there should not be an issue. Try calling
udpOut.flush()right afterconnection.send, so that packets are sent out immediately, perhaps that is preventing you from seeing traffic.On Mon, Apr 19, 2021, 18:19 OctavianIonel @.***> wrote:
This is a Java issue but I suppose a ready-made solution would be nice to have because of the friction it creates in terms of starting to use
dronefleet/mavlinkwith existing UDP-based tools like mavproxy. Perhaps this can be implemented in a separate library.Here’s a snippet that might help for now. I only tested this lightly, but it may help with direction:
Here’s a simple relay example (writes whatever it receives to the remote endpoint):
Hi there.
Socketuses TCP, so it’s likely failing to communicate with your UDP destination. To make UDP connections you will have to look intoDatagramSocket. Since DatagramSocket doesn’t provide input/output streams, you will also need to make these forMavlinkConnectionto consume. I suggest usingPipedInputStreamandPipedOutputStream.Good luck!