Send mavlink messages to Pixhawk using arduino

I am working on a hobby project. It involves a Quadcopter, uses pixhawk flight controller. I want to send mavlink messages using arduino to pixhawk. I followed following tutorials.
http://qgroundcontrol.org/dev/mavlink_arduino_integration_tutorial

I compiled my sketch with arduino 1.8.1 GitHub - arduino/Arduino: Arduino IDE 1.x

Got following error.
Arduino: 1.8.1 (Windows 10), Board: "Arduino/Genuino Uno"

In file included from E:\Dw\arduino-mavlink-master\ArduinoMAVLink\ArduinoMAVLink.ino:2:0:

E:\Dw\ardsrc\Arduino\build\windows\work\libraries\FastSerial/FastSerial.h:47:3: error: #error Must include FastSerial.h before the Arduino serial driver is defined.

error Must include FastSerial.h before the Arduino serial driver is defined.

^
exit status 1
Error compiling for board Arduino/Genuino Uno.

Please direct me to the problem area, Is it arduino sketch and libraries(mavlink and fastserial) or Arduino IDE.

The problem is that project was written 4 years ago and there have been some changes in the Arduino IDE since then that broke it. I have made some changes that should get it working but I don't own the hardware to test it. I'm hoping that you can try it out and if it works then I'll submit a pull request to the author of the sketch so that the fixes will be made available to all.

The fixed project is here:

Installation instructions:

  • Download https://github.com/per1234/arduino-mavlink/archive/ide-compatibility-updates.zip
  • Unzip the downloaded file(arduino-mavlink-ide-compatibility-updates.zip).
  • Copy the two folders in arduino-mavlink-ide-compatibility-updates\libraries to {sketchbook folder}\libraries. The location of your sketchbook folder is set at File > Preferences > Sketchbook location in the Arduino IDE
  • Delete {Arduino IDE installation folder}\libraries\FastSerial and {Arduino IDE installation folder}\libraries\mavlink. You should never install libraries to the Arduino IDE installation folder.
  • Restart the Arduino IDE if it's running.
  • File > Open > arduino-mavlink-ide-compatibility-updates\ArduinoMAVLink\ArduinoMAVLink.ino > Open

The fixes I made should retain backwards compatibility with older versions of the Arduino IDE(tested with 1.0.6) while also making it compatible with the latest version.

Please let me know if this works/doesn't work for you.

pert:
Please let me know if this works/doesn't work for you.

I am able to compile and upload it on Arduino successfully. My connection setup is following>

  1. Arduino board is connected to laptop using USB cable.-- com3
  2. ski telemetry radio(ground) is connected to laptop -- com8
  3. ski telemetry radio(air) is connected to pixhawk on TELEM1
  4. i am using Qgroundcontrol as GCS.

Further I want to send position data (ATT_POS_MOCAP) to pixhawk. Could you please tell me how this connection between com3 and 8 is going to work and how can I check in Qgroundcontrol that my mavalink packet is received by pixhawk. If this setup is not correct/efficient then please tell me guide how can I achieve the same objective.

First you need to verify that each part of the system is working correctly. Do this:

  • Connect the Arduino running the ArduinoMAVLink program to your computer's USB.
  • In the Arduino IDE select the COM port of the Arduino from the Tools > Port menu.
  • Tools > Serial Monitor
  • Select 57600 from the baud menu in the lower right corner of the Serial Monitor.
  • If the Arduino is sending mavlink packets then you should see them in the Serial Monitor. This tells you that everything is working correctly with that part of the system.
  • Ideally you would repeat this process with the ski telemetry radio(ground) on COM8, only this time you would be manually sending packets and verifying that they were received by the pixhawk. I'm not sure how difficult that would be.

Let me know if the Arduino is correctly sending the MAVlink packets and I'll submit the pull request with my changes to the author of ArduinoMAVLink.

As for the connection between COM3 and COM8, I don't have any experience with any of what you're doing. I'd need more information on how your system works.

Is Qgroundcontrol connected to the ski telemetry radio(ground) via COM8 and using it to send MAVlink packets to the pixhawk via ski telemetry radio(air)?

pert:
Let me know if the Arduino is correctly sending the MAVlink packets and I'll submit the pull request with my changes to the author of ArduinoMAVLink.

I uploaded the cketch on arduino and checked in serial monitor. it keeps printing 'p' there. TX led on arduino board blinks during this process. So yes Arduino sends the MAVLink packet. Could you tell me what is the significance of letter 'p' here for heart_beat packet.

pert:
Is Qgroundcontrol connected to the ski telemetry radio(ground) via COM8 and using it to send MAVlink packets to the pixhawk via ski telemetry radio(air)?

Yes Qgroundcontrol is connected to pixhawk using ski telemetry radio(ground-air) via COM8 . I want to send mavlink packets using arduino to pxhawk via ski telemetry radio. In Qgroundcontrol I am able to access all the information of pixhawk.

rbohara:
Could you tell me what is the significance of letter 'p' here for heart_beat packet.

I have no knowledge of the MAVlink protocol. I've never heard of it or before your comment on GitHub. Do you have the Serial Monitor baud rate menu set to 57600?

rbohara:
Yes Qgroundcontrol is connected to pixhawk using ski telemetry radio(ground-air) via COM8 . I want to send mavlink packets using arduino to pxhawk via ski telemetry radio. In Qgroundcontrol I am able to access all the information of pixhawk.

You can't make multiple connections to the same serial port. This means you can't connect Qgroundcontrol and the Arduino separately to the ski telemetry radio(ground) at the same time. So you'd need to figure out how to make Qgroundcontrol receive the packets from the Arduino and then send them on to the pixhawk via the ski telemetry radio(ground).

@rbohara, any progress on this?

rbohara:
I uploaded the cketch on arduino and checked in serial monitor. it keeps printing 'p' there. TX led on arduino board blinks during this process. So yes Arduino sends the MAVLink packet. Could you tell me what is the significance of letter 'p' here for heart_beat packet.

Yes Qgroundcontrol is connected to pixhawk using ski telemetry radio(ground-air) via COM8 . I want to send mavlink packets using arduino to pxhawk via ski telemetry radio. In Qgroundcontrol I am able to access all the information of pixhawk.

Have you had any success using arduino-mavlink rbohara?

pert:
I have no knowledge of the MAVlink protocol. I've never heard of it or before your comment on GitHub. Do you have the Serial Monitor baud rate menu set to 57600?

yes I have set the baud rate to 57600

pert:
Have you had any success using arduino-mavlink rbohara?

I am sorry that I am replying so late. Still I have not figured out what is going on. No success yet. I have hit a wall. Could you give reference of someone who has done a drone project for indoor purpose.

rbohara:
I am sorry that I am replying so late.

It's OK, I just want to submit a pull request to the original arduino-mavlink repository with my fixes but I'd like confirmation that it's working first.

rbohara:
Still I have not figured out what is going on. No success yet. I have hit a wall.

If you provide more information on what you've tried and why it didn't work someone might be able to help.

rbohara:
Could you give reference of someone who has done a drone project for indoor purpose.

This might be helpful: http://forum.arduino.cc/index.php?topic=382592.0

pert:
It's OK, I just want to submit a pull request to the original arduino-mavlink repository with my fixes but I'd like confirmation that it's working first.
If you provide more information on what you've tried and why it didn't work someone might be able to help.
This might be helpful: Arduino Mega ADK interfacing with Pixhawk autopilot - Project Guidance - Arduino Forum

I am using the similar sketch to send heart_beat mavlink packet to pixhawk.

#include <FastSerial.h>
#include <Arduino.h>
#include <mavlink_bridge_header.h>
#include <mavlink.h>
FastSerialPort0(Serial);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
}


void loop() {
  // put your main code here, to run repeatedly:
  mavlink_message_t msg;
  mavlink_system.sysid=1; // System ID, 1-255
  mavlink_system.compid=0; // Component/Subsystem ID, 1-255
  uint8_t buff[MAVLINK_MAX_PACKET_LEN];
  int type = MAV_TYPE_QUADROTOR;   ///< This system is quarotor
    uint8_t autopilot_type = MAV_AUTOPILOT_PIXHAWK;
 
    uint8_t base_mode = MAV_MODE_GUIDED_DISARMED; ///<88
    uint32_t custom_mode = 1;                 ///< Custom mode, can be defined by user/adopter
    uint8_t system_state = MAV_STATE_STANDBY; ///< System ready for flight
 mavlink_msg_heartbeat_pack(mavlink_system.sysid, mavlink_system.compid, &msg , type , autopilot_type , base_mode , custom_mode , system_state );
 uint16_t len=mavlink_msg_to_send_buffer(buff, &msg);
 Serial.write(buff, len);
 delay(500);
 comm_receive();
}

void comm_receive() {
  mavlink_message_t msg;
  mavlink_status_t status;
  uint8_t c;
  
  if(Serial.available()) 
  {
    while((c=Serial.read())!=-1)
    {
     if(mavlink_parse_char(MAVLINK_COMM_0, c, &msg, &status))
        Serial.println(msg.msgid);
     }
      delay(500);
    }
  return;
}

But here is a difference, in the thread you shared arduino is directly connected to pixhawk on TELEM1. In my case as I earlier pointed out I want to send packets over radio telemetry.

  1. I have connected ski telemetry radio(Air) on pixhawk -- TELEM 1
  2. ski telemetry radio(Ground) is connected to laptop --- COM8
  3. Qgroundcontrol is connected to pixhawk using ski radio.
  4. Arduino is connected to laptop -- COM3
  5. Serial connection between qgroundcontrol and COM3

On arduino I can see TX and RX leds are blinking continuously but there is no change in Mavlink inspector in Qgroundcontrol for Heartbeat packet.

I have successfully programmed a Teensy3.2 to send Mavlink hearbeat packets. I would like to know how to send other - non heartbeat packets. For example: I would like to change the MODE from AUTO to GUIDED or STABILIZE and back. I would also like to change the direction of movement.

I have read some documents that say to use QGroundControl to generate packets, but my version of QGroundControl doesn't give me the option to do that.

I would also like for the TEENSY to decode Mavlink messages coming in on its Rx port. I have no idea how to do that.

Could someone please help me get started?

Hi...

You have to connect the telemetry to arduino and connect it to qground in this case you can send commands form both Qground and Arduino or you have to use 2 telemetry PTMP.

I'd still love to get confirmation that my fixed version of arduino-mavlink is working. If anyone gives it a try, please let me know what your results are.

Details here: Send mavlink messages to Pixhawk using arduino - #2 by pert - Programming Questions - Arduino Forum

I did go ahead and submit the pull request for the fixes, but it has not been merged:

I can't get the latest code to do anything. I'm using a TEENSY 3.2 which has 4 hardware serial ports - all of which have no problem running at 57.6KBaud, so I don't need to use FASTSERIAL.
I'm a hardware engineer, so everything is connected properly.

The Pixhawk is connected to Serial 1, and my PC is connected to the Teensy's USB port (Serial 0).

The Teensy is receiving data from the Pixhawk (as proven when I added a line to the program which echos characters to my PC set up as a terminal). I see the 0xFE... strings.

But the bytes are not decoded. I would expect to see something being returned. But I see nothing.

My goal is to use the TEENSY to decode such things as MODE, battery voltage, etc, and have them show up on my PC, and also send commands (MODE, move right, move up, move left, etc.) from my PC either as words or command numbers.

I don't have a clue how to proceed.

I'm getting a bit more now. I added a line that prints out the var msg.msid

It prints out

0x00 0x48 0x42 0x20 and repeats. Is that correct?