Newbie MAVLink issue

552 views
Skip to first unread message

Colin Tan

unread,
Jun 22, 2015, 3:34:21 AM6/22/15
to drones-...@googlegroups.com
Hi all,

I'm writing a program to control APM 2.6 (running Arducopter 3.2.1) via the telemetry port. I can receive heartbeat packets from the APM, but whatever MAVLink packets I send to the APM simply get echoed back to me. See attached screenshot for what I mean.

Even heartbeat packets from my program to the APM are being echoed back to me. My system has system ID of 255, while the APM has system ID of 1. You can also see the parameter list request (message ID 21) and data stream request (message ID 66) being echoed back to me, and ignored by the APM. I've tried this with several APM 2.5 and 2.6 modules, all exact same result. I've also tried it with motors armed, same result.

This code is running on an Arduino Mega 2560. The 2560 is connected via UART1 to the APM's telemetry port.

This is my code on the Arduino Mega for sending out the heartbeat, parameter list and data stream request packets to the APM:

void send(mavlink_message_t *msg)
{
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
uint16_t len=mavlink_msg_to_send_buffer(buf, msg);
for(uint16_t i=0; i<len; i++)
Serial1.write(buf[i]);
}


void CArdGCS::requestDataStream(uint8_t target_system, uint8_t target_component, uint8_t req_stream_id, uint16_t req_message_rate, uint8_t start_stop)
{
mavlink_message_t msg;
mavlink_msg_request_data_stream_pack(255, 0, &msg, received_sysid, received_sysid, MAV_DATA_STREAM_RAW_SENSORS, 1, 1);
send(&msg);
}

// Request parameter list
void CArdGCS::requestParamList(uint8_t targetSystem, uint8_t targetComponent)
{
mavlink_message_t msg;
mavlink_msg_param_request_list_pack(255, 1, &msg, 1, 1);
send(&msg);
}


This is the code for processMessage, which gets called whenever a character comes in from Serial1:

void CArdGCS::processMessage(mavlink_message_t msg)
{
// Empty
#ifdef MYDEBUG
char buffer[64];
#endif
switch(msg.msgid)
{
mavlink_heartbeat_t heartbeat;
case MAVLINK_MSG_ID_HEARTBEAT:
mavlink_msg_heartbeat_decode(&msg, &heartbeat);

#ifdef MYDEBUG
sprintf(buffer, "Heartbeat: Status: %d from system %d component %d\n",  heartbeat.system_status, msg.sysid, msg.compid);
received_sysid=msg.sysid;
received_compid=msg.compid;
Serial.print(buffer);
#endif
break;
case MAVLINK_MSG_ID_STATUSTEXT:

mavlink_statustext_t statustext;
mavlink_msg_statustext_decode(&msg, &statustext);
#ifdef MYDEBUG
sprintf(buffer, "Status: Severity %d Text %s\n", statustext.severity, statustext.text);
Serial.print(buffer);
#endif
break;
default:
#ifdef MYDEBUG
sprintf(buffer, "Unknown message id: %d from system %d component %d\n", msg.msgid, msg.sysid, msg.compid);
Serial.print(buffer);
#endif
break;
}
}

Finally this is the event loop that polls for characters from Serial1, parses it and calls processMessage:

// The event loop. We never exit from this.
void CArdGCS::eventLoop()
{
char ch;
mavlink_message_t msg;
mavlink_status_t status;

while(1)
{
                if(Serial1.available())
{
                        ch=Serial1.read();
if(mavlink_frame_char(_stationNum, ch, &msg, &status)!=MAVLINK_FRAMING_INCOMPLETE)
processMessage(msg);
}
}
}

I've been stuck on this for days.. I would really appreciate any help!

Many thanks!
Colin.

Chris Anderson

unread,
Jun 22, 2015, 6:11:56 AM6/22/15
to drones-discuss

If you switch to a Raspberry Pi or Beaglebone Black,  you can use DroneKit for this,  which makes the whole MAVlink interface very easy.

--
You received this message because you are subscribed to the Google Groups "drones-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drones-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Colin Tan

unread,
Jun 22, 2015, 10:00:35 AM6/22/15
to drones-...@googlegroups.com
Hi all,

I've managed to fix the echo problem by connecting GND from the telemetry port to GND on the Arduino Mega. However APM still ignores my packets. As far as I can tell my code is ok, even tried code from https://github.com/sgofferj/uxv-cs/blob/master/arduino/UxV_CS/MAVLink.ino but it doesn't work. The APM just ignores everything.

I would really appreciate help here. It's an important project and this issue is driving me crazy. :(

Colin.

Colin Tan

unread,
Jun 22, 2015, 10:00:44 AM6/22/15
to drones-...@googlegroups.com
Thanks Chris! I need to use the Arduino Mega for various (mostly budgetary) reasons. I have a huge bunch of Megas lying around. :)

In any case I seem to have fixed the problem, much thanks to Pedro Santos's reply here:


The issue was that the rates for sr0 and sr1 weren't set. To do this, connect to the APM on Mission Planner, click on "Config/Tuning", then choose "Complete Parameter Tree" on the left, locate the sr0 and sr1 groups, click on the + to expand, then update the parameters as per the posting above.

Now I have a huge stream of sensor data from the APM. :)

Hopefully this helps other people working on similar ideas.

Colin Tan

unread,
Jun 22, 2015, 10:00:50 AM6/22/15
to drones-...@googlegroups.com
I take back what I said; setting those parameters only make APM send you the data at regular intervals. 

APM still doesn't listen to commands I send to it. >.<

If anyone knows what's happening, I really need the help.

Many thanks,
Colin.


On Monday, 22 June 2015 18:11:56 UTC+8, chris wrote:

Bill Bonney

unread,
Jun 22, 2015, 10:22:12 PM6/22/15
to drones-...@googlegroups.com
What commands are you sending it. I cannot see an issue in your code (other than the request data streams. but you say you fixed that) Though you don't have a receive method to know you have received a parameter back.

You are using 'Serial1', what library are you using, and what buffer size do you have? 

Colin Tan

unread,
Jun 22, 2015, 10:31:40 PM6/22/15
to drones-...@googlegroups.com
Thanks Bill for running through my code. Serial1 is a standard Arduino serial library for the Mega, not sure of the buffer size though. I am getting data streams but it's being sent automatically by the APM because of how I've configured the sr0 and sr1 parameters, rather than because of my data stream commands.

My main() calls requestParameterList and requestDataStream, and then calls eventLoop, which is an infinite loop that checks Serial1 for data and sends it to mavlink_frame_char for parsing. When mavlink_frame_char reports that a frame has been fully parsed, eventLoop calls handleMessage to process the msg packet.

I'm doing everything textbook and seem to be the only person with this issue. :(

Colin.

Bill Bonney

unread,
Jun 23, 2015, 12:57:19 AM6/23/15
to drones-...@googlegroups.com
Hi,

I’d suggest turning the SR1 and SR0 off, and then work on getting requestDataStreams to work (if you have that working, the rest is easy).

The steps are 
1) Have your code detect the HB message from the APM
2) Have your code send a requestDataStreamMessage
3) check for the extra messages requested being sent.

this call looks wrong 

mavlink_msg_request_data_stream_pack(255, 0, &msg, received_sysid, received_sysid, MAV_DATA_STREAM_RAW_SENSORS, 1, 1);

the prototype is
 static inline uint16_t mavlink_msg_request_data_stream_pack(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg,
      uint8_t target_system, uint8_t target_component, uint8_t req_stream_id, uint16_t req_message_rate, uint8_t start_stop)
That would be 
   mavlink_msg_request_data_stream_pack(255, 1, &msg, 1, 1, MAV_DATA_STREAM_RAW_SENSORS, 1, 1);
since your received_sysid should be ok as it should be the value of 1. is it? you only set received_sysid=msg.sysid; in your infinite loop. if may just be some random value when you request the datasteram

Try sending the rqeuestdatastream packet 3 times but with a delay between each send. 

Connect the output of your arduino to another arduino in serial pass through mode and enable the serial monitor, do you see any data?

have you set the baud rate to 57600? I assume you are connecting on a telemetry port.Are you using software serial? Have you disabled the flow control?

Without seeing all the code it’s hard to see what is wrong.

Hope those questions help :)

Bill



Colin Tan

unread,
Jun 23, 2015, 3:40:55 AM6/23/15
to drones-...@googlegroups.com
Thanks Bill! These are really helpful tips. I'll try them out now and report back. :)

Many thanks again!

Colin.

Colin Tan

unread,
Jun 23, 2015, 4:40:12 AM6/23/15
to drones-...@googlegroups.com
Hi all,

tl;dr: Packets were ignored because of bad CRC generated by a corrupted MAVLink header files.

The problem has been resolved! 

It was a bad CRC issue that made the APM ignore all my packets.

I had followed the steps for the Arduino MAVLink tutorial on qgroundcontrol.org. The example code comes with MAVLink 0.9. Somehow while upgrading to MAVLink 1.0 I accidentally corrupted the l library and it become a Frankenstein Monster of 0.99 and 1.0 parts. The result is that the packets were encoded with bad CRCs (Did the CRC algorithm change between versions?)

Bill, your suggestion to do a passthrough inspired me to hook up my Arduino Mega to another Mega via UART 1 (Serial 1), then reconfigure my code to send/receive MAVLink packets via UART 0 (Serial). I would then send/receive  MAVLink packets on UART0, and write debugging messages to UART1, which is then picked up by the other Mega and printed onto the screen. I then fired up Mission Planner and connected to the first Mega, and that's where I saw it:

On the console it said "MAVLink bad CRC".

I rm -rf the mavlink directory, did a git clone from the original MAVLink repository, regenerated the headers, and voila! Problem fixed. :) APM now responds to my packets. :D

Many many thanks Bill for taking the time to go through my code. Your tips saved me days of debugging!

For anyone interested in using Mission Planner to debug MAVLink:

i) Start Mission Planner
ii) Click HELP on the top menu bar
iii) Way down on the bottom, click the checkbox that says "Show Console Window (restart)"
iv) Restart Mission Planner
v) Click "Config/Tuning" on the top menu bar
vi) Click "Planner" on the left menu bar.
vii) Go way down to the bottom again, click the checkbox that says "Mavlink Message Debug"

MAVLink messages will now appear on the console window. You might need to ALT-TAB to see it.


Thanks!
Colin.
Reply all
Reply to author
Forward
0 new messages