Read Mavlink Pixhawk-Arduino

997 views
Skip to first unread message

Luis AVC

unread,
Apr 4, 2016, 2:31:56 PM4/4/16
to MAVLink
Hi Guys.

I been trying to connect a Pixhawk with and arduino using Mavlink

In Pixhawk im using firmware 3.3 quadcoprter. And im using the Serial4/5 Port.

In the Arduino, is an Arduino Mega, and im ussing the TX1 and RX1, while the TX and RX are ussed to see the information in the PC.

For now I understend some of the way Mavlink work, but I not sure if im asking for the Heartbeat in the correct way and how im supose to call the variables trow mavlink. 


//#include <FastSerial.h>
#include "C:/Users/luis.Valtierra/Documents/ardupilot/libraries/mavlink/include/mavlink.h"         // Mavlink interface

// FastSerialPort0(Serial);

void setup() {
Serial1.begin(57600);
 Serial.begin(57600);
}

void loop() { 
// Define the system type (see mavlink_types.h for list of possible types) 
int system_type = MAV_QUADROTOR;
int autopilot_type = MAV_AUTOPILOT_GENERIC;
// Initialize the required buffers 
mavlink_message_t msg; 
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
// Pack the message
// mavlink_message_heartbeat_pack(system id, component id, message container, system type, MAV_AUTOPILOT_GENERIC)
mavlink_msg_heartbeat_pack(100, 200, &msg, system_type, autopilot_type);
// Copy the message to send buffer 
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
// Send the message (.write sends as bytes) 
Serial1.write(buf, len);
comm_receive();
}

void comm_receive() { 
mavlink_message_t msg; 
mavlink_status_t status;
//receive data over serial 
while(Serial1.available() > 0) { 
uint8_t c = Serial1.read();
//try to get a new message 
if(mavlink_parse_char(MAVLINK_COMM_0, c, &msg, &status)) { 
// Handle message
  switch(msg.msgid) {
       case MAVLINK_MSG_ID_SET_MODE: {
        // set mode

       }
       break;
       case MAVLINK_MSG_ID_ACTION:
// EXECUTE ACTION
break;
default:
//Do nothing
break;
}
// And get the next one
}
}


The code work, but im not getting inside the receive switch, most probable becase the program doesnt know how to send a proper heartbeat. 

Hope someone could help me.

Luis AVC

unread,
Apr 4, 2016, 5:17:24 PM4/4/16
to MAVLink
UPDATE

I check the group and found som advices. Now I get sometingh from Serial1.read(); Looks like the HeartBeat, but I dont know how to handle it...

// Arduino MAVLink test code.
   Serial.println(c);
//try to get a new message 
if(mavlink_parse_char(MAVLINK_COMM_0, c, &msg, &status)) { 
Serial.println("Enter"); 
// Handle message
  switch(msg.msgid) {
       case MAVLINK_MSG_ID_SET_MODE: {
      Serial.println("set mode");
        // set mode
             }
       break;
       case MAVLINK_MSG_ID_ACTION:
            Serial.println("ID action");

Luis AVC

unread,
Apr 4, 2016, 5:50:31 PM4/4/16
to MAVLink

john console

unread,
Feb 16, 2017, 7:45:59 PM2/16/17
to MAVLink
hi,

i have a arduino mkr1000, would it be possible to make a version that will listen to a udp connection? THANKS
Reply all
Reply to author
Forward
0 new messages