Mavlink communication between Arduino UNO and Pixhawk Mini

#1

I am currently using PX4 1.8.2 stable version for my Pixhawk Mini. I am able to read the data transmitted from Pixhawk Mini to Arduino Uno but sending command from Arduino Uno to Pixhawk Mini seems does not work for me. This causes me the problem to change from Loiter mode to Offboard mode using the RC. I just constantly getting the message, “Offboard Rejected”. My code is attached below. Can anyone take a quick look what is happening because I believe that the Pixhawk MINI cannot receive the target setpoints from Arduino Uno and this causes the “Offboard Rejected” issues. I try to debug for many days but no luck for me. I am coming here for some suggestion if anyone knows what happens.
///////////////////////THIS IS MY CODE//////////////////////////////////
#include <SoftwareSerial.h>
SoftwareSerial pxSerial(9,10); // RX, TX
#include “mavlink.h”

// Mavlink variables

int target_system_id = 1;
int companion_id =20; // Arduino UNO id (I set it randomly from 1-255 except 1 since Pixhawk using 1)
int component_id = 1; // Can be 0 or 1? I just set 1.
int target_component_id = 1;

void setup() {
Serial.begin(57600);
pxSerial.begin(57600);

}
void loop() {
mavlink_message_t msg;
for(int x = 0; x<100; x++)
{
send_HB(msg);
delay(100);
Serial.println("Sending HeartBeat");
}
while(true)
{
//Serial.print("Time:");
//Serial.println(current_time/1000);
previous_time = current_time;

float velxx = 1.5;
set_Position(msg,velxx);
delay(100); // Setpoints sent run in 10Hz.

}

}
void send_HB(mavlink_message_t &msgg){
int sysid = 20; ///< ID for my companion board
int compid = 1 ; ///< The component sending the message, either 0 or 1
int type = MAV_TYPE_QUADROTOR; ///< This system is an quadrotor

uint8_t autopilot_type = MAV_AUTOPILOT_PX4; //< PX4 firmware
uint8_t system_mode = MAV_MODE_MANUAL_ARMED; ///< Booting up
uint32_t custom_mode = 0; ///< Custom mode, 0 since not using this
uint8_t system_state = MAV_STATE_STANDBY; ///< System ready for flight
// Initialize the required buffers
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];

// Pack the message
mavlink_msg_heartbeat_pack(sysid,compid, &msgg, 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);
pxSerial.write(buf,len); // Write the message through the serial port I sent
}
void set_Position(mavlink_message_t &msg, float velx)
{
mavlink_set_position_target_local_ned_t sp;

sp.type_mask= 0b110111110111; // Only velocity_x is set for testing.
sp.coordinate_frame = MAV_FRAME_LOCAL_NED;
sp.vx =velx ; // unit: m/s
sp.target_system = target_system_id; // Target_sysid = 1 since I am sending command to pixhawk
sp.target_component = target_component_id;// Target_compid = 0 or 1, but I use 1.
mavlink_msg_set_position_target_local_ned_encode(companion_id,component_id,&msg,&sp);
send_Message(msg); // passs the msg to send_Message in order to write message to pixhawk via serial port
Serial.print("Velocity x: ");
Serial.println(sp.vx);

}
#2

Ok, first thing, if you could edit your post and put the code inside ``` that would make it a lot easier to read.

Another thing is to check the type mask against what is done in the Dronecode SDK, check this:

#3

@JulianOes

Thanks for your reply. Appreciate it so much! I did solve the problem I have. The arduino was sending command.
I have couple of questions which I hope to get answered by you if possible.

  1. I am now working for state machine. I am trying to output the vehicle mode (Stabilized,Manual,Loiter,Offboard and etc) and vehicle arming status (armed,disarmed) but I could not find any mavlink msg function which does that. Do you know which function I can use to do that?

  2. How do you define the system id and component id for the Arduino board? Can the system id and component id be any number between 1-255 except 1 since 1 is used by Pixhawk?

-zimin

#4
  1. Check this code: https://github.com/Dronecode/DronecodeSDK/blob/ac0c1fde587c22411bdca84fdc19cb955a59e052/plugins/telemetry/telemetry_impl.cpp#L475-L482

  2. Yes, system ID you can choose anything but 0, 1. And component ID you can choose something that fits according to: https://mavlink.io/en/messages/common.html#MAV_COMPONENT

#5

@JulianOes,

Thanks for your help. I am sorry that I did not reply immediately what I have accomplished. Anyway, everything works fine for me right now and thank you for your time and efforts for helping me.

PS: You can close this discussion post if needed.

Regards,
Zi Min Er

1 Like
#6

Great, thanks for the feedback.