Arduino Mega ADK interfacing with Pixhawk autopilot

What i am trying to achieve is to have the Mega transmit mavlink commands to the TELEM1 port of the Pixhawk autopilot. Using the Mega as a ground station.

I have a code that appears to be working.
Inspecting the packets that i am sending with qGroundControl i get the following output :


(some of the params on heartbeat packet have been changed since the picture was taken).

I have connected the Mega to TELEM1 of Pixhawk as shown in the picture bellow

What i am trying to do with the resistors is a voltage divider because the Pixhawk is 3.3v for TX, RX but the Mega outputs 5V.

I have measured the voltage of TX0 on the Mega and it’s 3.2 - 3.3 V with the circuit i have above.
But when sending data i do not see any significant drop to the voltage, it will drop to something like 3V from 3.2V. So i believe i am doing something wrong. How can the Pixhawk know when we have a logical 0 or 1 if the difference is only 0.2 Volts?

I am using the following code to test the connection.

//include the mavlink library
#include "mavlink.h"
//Baudrate
#define bRate 115200


void setup() {
  
  Serial.begin(bRate);
}

void loop() {

 command_heartbeat();
 command_waypoint();
}

/************************************************************
* @brief Sends a heartbeat message every second.
* @param NONE
* @return void
*************************************************************/

void command_heartbeat() {

  //< ID 1 for this system
  int sysid = 1;                   
  //< The component sending the message.
  int compid = MAV_COMP_ID_MISSIONPLANNER;    
  
  // Define the system type, in this case ground control station
  uint8_t system_type = MAV_TYPE_GCS;
  uint8_t autopilot_type = MAV_AUTOPILOT_INVALID;
  
  uint8_t system_mode = 0; 
  uint32_t custom_mode = 0;                
  uint8_t system_state = 0;
  
  // Initialize the required buffers
  mavlink_message_t msg;
  uint8_t buf[MAVLINK_MAX_PACKET_LEN];
  
  // Pack the message
  mavlink_msg_heartbeat_pack(sysid,compid, &msg, system_type, autopilot_type, system_mode, custom_mode, system_state);
  
  // Copy the message to the send buffer
  uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
  
  // Send the message 
  delay(1000);
  Serial.write(buf, len);
}

/************************************************************
* @brief Sends a waypoint command
* @param NONE
* @return void
*************************************************************/

void command_waypoint() {

  //TARGET DRONE
  uint8_t _target_system = 1; // Target drone id
  uint8_t _target_component = 0; // Target component, 0 = all

  uint16_t seq = 0; // Sequence is always set to 0
  uint8_t frame = MAV_FRAME_GLOBAL; // Set target frame to global default
  uint16_t command = MAV_CMD_NAV_WAYPOINT; // Specific command for PX4 
  uint8_t current = 2; // Guided mode waypoint
  uint8_t autocontinue = 0; // Always 0
  float param1 = 0; // Loiter time
  float param2 = 1; // Acceptable range from target - radius in meters
  float param3 = 0; // Pass through waypoint
  float param4 = 0; // Desired yaw angle
  float x = 52.464217; // Latitude - degrees
  float y = -1.280222; // Longitude - degrees
  float z = 200; // Altitude - meters

  // Initialize the required buffers
  mavlink_message_t msg;
  uint8_t buf[MAVLINK_MAX_PACKET_LEN];

  // Pack the message
  mavlink_msg_mission_item_pack(1,1, &msg, _target_system, _target_component, seq, frame, command, current, autocontinue, param1, param2, param3, param4, x, y, z);

  // Copy the message to the send buffer
  uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
  // Send the message (.write sends as bytes) 
  delay(1000);
  Serial.write(buf, len);
}

Running this code i do not see any changes to the Pixhawk, the rotors do not start to spin, reading the waypoints from the pixhawk using mission planner does not show the waypoint i am trying to send.

I am unsure as to what the drone id should be.

Both the Pixhawk and the Mega are powered by their usb connected to a computer.

Hi,
Did you solve your problem?
if so, could you tell me what was wrong, because i have the same problem, but i can do everything but sending waypoints, if you didnt get it working till now, write me an email, so we can discuss this problem please

email: Jonas.woerner@online.de

  • Jonas

CodingGhost:
Hi,
Did you solve your problem?
if so, could you tell me what was wrong, because i have the same problem, but i can do everything but sending waypoints, if you didnt get it working till now, write me an email, so we can discuss this problem please

email: Jonas.woerner@online.de

  • Jonas

I have emailed you.

I will also post the solution here for everyone, when i figure it out.

Hi,

Im working on the same project and i have the same problems can you pls post the solution.

Marouen.

Hi,
i have finished my project, it works and does not have any known bugs.
Heres the link to my github repo: GitHub - CodingGhost/MAFM: Mavlink advanced Follow me Program, which converts mnormal degree coordinates into the gauss krüger system and back, to get a point 10Mfrom the drone, so it can film you from a distance.
If you have any questions email me.

  • Jonas