Offboard mode and mavlink message for this mode

I just start learning programming a few month ago and currently working on controling quadcopter. I use odroid XU4 as a companion board and connect with Pixhawk via /dev/ttyUSB0. Now, I confront with some problem and confusion. Could you please help me on these question.

  1. Follow the example from c_uart_interface_example (GitHub - mavlink/c_uart_interface_example: Simple MAVLink to UART interface example for *nix systems), I can arm/disarm, enable and disable offboard mode, takeoff using Local position (MAV_CMD_NAV_TAKEOFF_LOCAL). However, my program is unstable somehow. I have tested on my quadcopter for 10 times only two times is successfully takeoff, the less does arm then disarm. I don’t know what is the cause of this problem, i have searched through log file but i can’t find any log in the day i test command via companion board (it is in the same day)

  2. I follow the instruction on Offboard Control from Linux · PX4 Developer Guide for initial setup. What gonna happened if I switch to offboard mode from remote controller in the air without running a program on companion board, is it still in position mode until program is run and enable offboard mode (now I am always start from the ground).

  3. Is the commands that using in offboard mode is the same as guided mode? Ref. Copter Commands in Guided Mode — Dev documentation

  4. According to MAVLink message definitions (https://pixhawk.ethz.ch/mavlink/), Are all commands currently work on PX4 and where I can find a unit of parameters of some command such as lat, lon, and alt in MAV_CMD_NAV_TAKEOFF.

  5. If I keep sending the commands in sequence such as
    enable_offboard > arm > takeoff > add setpoint(to move copter) > add another setpoint > RTL > disarm > disable_offboard
    do I have to add delay between the command myself of px4/pixhawk will automatically process it in sequence.

  6. If I have to put delay(sleep/usleep) myself what is an optimal time for each command and how can I know when the copter reach the setpoint?. According to Mavlink message definition again, I can write setpoint via SET_POSITION_TARGET_LOCAL_NED and check current position via POSITION_TARGET_LOCAL_NED, can I check current position via LOCAL_POSITION_NED also?
    As I know so far, I can check arm stage via

int armed_state = api.current_messages.heartbeat.base_mode & MAV_MODE_FLAG_SAFETY_ARMED;
printf("Armed? %s \n", armed_state ? "Yes" : "No");
  1. if it automatically process in sequence, how can I delete the command or insert the commands between sent commands such as add new setpoint between first and second setpoint.

  2. What is _SW and _TH in this diagram https://dev.px4.io/en/concept/flight_modes.html

Some question might be the silliest question, but I really want to learn and start program it myself. Due to lacking of knowledge and experience in mechanic, programming(i just start c++ as my first programming language), and aerospace(how copter work), sometime I face with problem of understanding the problem and finding the specific keyword to search on the Internet so that I am struck on the start point for a few month. Hope you guys suggest me how to start and THANK YOU IN ADVANCE for any kind of help. :slight_smile:

After run around this board for sometime, for the second question i might have to use SET_POSITION_TARGET_LOCAL_NED instead of MAV_CMD_TAKEOFF_LOCAL by specified type_mask = (0x1000 | 0b110111000011) and type_mask = (0x2000 | 0b110111000011) for landing
Moreover, I might to need to set non Null speed in SET_POSITION_TARGET_LOCAL_NED for every action.

Please suggest if i understand it wrongly, i’ll test on the copter again tomorrow. However, is there any possible way to convert UDP (from jmavsim) to serial port. i am not successfully connect my c++ code to UDP for test, Although i can connect but nothing move in simulation.

ref.

  1. Offboard automatic TakeOff / Landing using MAV_CMD_LAND/TAKEOFF_LOCAL
  2. OFFBOARD takeoff, land, loiter via SET_POSITION_TARGET_LOCAL_NED
  3. PX4 Hidden Flight BitMasks

Hello,

First of all, before using it on a real copter, test it intensively with JMavSim (I’ve stayed on version 1.4.4 for now, don’t know if newer firmware version work with JMavSim). I’m using a pixhawk board for the HIL tests.

  1. Do you send an HB message faster than 1Hz (in my case i’m sending it a 3Hz). Also you need to use autopilot mavlink messages in order to know in which state your autopilot is. It is possible that some messages get corrupted hence droped. You should implement a way to repeat order if after some delay the autopilot is not in the expected state.

  2. If you switch to offboard and no companion is talking, the autopilot will reuse the previous mode set by user. That’s why it is recommended to enter Offboard mode once you are in position or hold mode.

3)You are switching of topics and use Ardupilot forum. Some information might be correct some might not apply to the PX4 firmware. The messages I use in order to do OFFBOARD are :
Order :
MAV_CMD_COMPONENT_ARM_DISARM
HEARTBEAT
MAV_CMD_NAV_GUIDED_ENABLE
SET_POSITION_TARGET_LOCAL_NED
MAV_CMD_SET_MESSAGE_INTERVAL

Status :
MAVLINK_MSG_ID_HEARTBEAT
MAVLINK_MSG_ID_LOCAL_POSITION_NED
MAVLINK_MSG_ID_GLOBAL_POSITION_INT
MAVLINK_MSG_ID_ATTITUDE
MAVLINK_MSG_ID_EXTENDED_SYS_STATE
MAVLINK_MSG_ID_SYS_STATUS
MAVLINK_MSG_ID_HOME_POSITION
MAVLINK_MSG_ID_GPS_RAW_INT

  1. Using only the set of messages above will allow you to move everywhere you need to

  2. As stated in 1), some messages might get dropped. The delay is a ‘fast and dumy’ way of sending order (it is usefull as an example of how it works and for quick tests) but if you want something more robust, you need to use the statuses reported by Autopilot. In the end you’ll have a kind of state machine.

  3. POSITION_TARGET_LOCAL_NED will indicate to you it has received your order and intend to go there. MAVLINK_MSG_ID_LOCAL_POSITION_NED can indeed be used in conjonction with MAVLINK_MSG_ID_ATTITUDE to know whether it has completed the requested movement.

  4. I think you refer to the way it is done in Ardupilot documentation. I do not know whether it is working with PX4. In my way of understanding it, as you have a companion, it is his job to wait until the UAV has reached the position before giving the next setpoint. (in my case I keep sending the desired position every 500ms, and once it has reached it, give a new one)

  5. Not sure but i think SW stand for switch and TH for threshold.

For your last question, you indeed need to set some non null speed otherwise there might be undesired behavior.

Last advice : each time you change your code in the companion, double check it on JMAVSIM even if you don’t think you have affected something vital; I’ve have crashed many virtual drone in JMavSim

If you have a spare pixhawk, you can configure it to HIL Quadcopter frame. This way you can connect your companion to the pixhawk serial port and perform Hardware In the Loop. I did not used JMavSim with UDP

Hope it helps

1 Like

Thank you so much for your answers and suggestion, I have no success on HITL simulator as described in https://dev.px4.io/en/simulation/hitl.html. for the command in an example on the website

./Tools/jmavsim_run.sh -q -d /dev/ttyACM0 -b 921600 -r 250

reply me an error “Invalid option: -r” and when I take a look on jmavsim_run.sh, there is no such an option r, if i understand it correctly.

#! /bin/bash

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR/jMAVSim"

udp_port=14560
extra_args=
baudrate=921600
device=
while getopts ":b:d:p:q" opt; do
	case $opt in
		b)
			baudrate=$OPTARG
			;;
		d)
			device="$OPTARG"
			;;
		p)
			udp_port=$OPTARG
			;;
		q)
			extra_args="$extra_args -qgc"
			;;
		\?)
			echo "Invalid option: -$OPTARG" >&2
			exit 1
			;;
	esac
done

However, if I remove " -r 250" at the end it work, i also try with a full command from this website https://uav-lab.org/2016/08/31/px4-research-log-8-hardware-in-the-loop-hitl-simulation-using-jmavsim-on-ubuntu-14-04/ which is

java -Djava.ext.dirs= -cp lib/*:out/production/jmavsim.jar me.drton.jmavsim.Simulator -serial /dev/ttyACM0 921600 -qgc

However, both commands, i end up with purple blink led after start simulator (before is blue) as in this link and I am not able to control the simulated copter to takeoff from the ground (sometime flipping, and sometime sliding on the floor) by RC in Stabilize mode, and also can’t switch to Altitude and Position modes. There is no error or waning message in qgroundcontrol also. Do you (or anyone) know how to fix this problem?

I clone Firmware project from master branch and use remote control (calibrated in qgc already) instead of joystick (do i need to connect joystick also?)

Hello,

I don’t whether it is related but what is the version of PX4 you’re using for HITL ?
If it is higher than 1.4.4, I know there was a bug related to the use by default of EKF instead of LPE. I still didn’t take time to upload my HITL to a newer version so I won’t be able to help.

1 Like

Finally it work! have to update to the lastest version of the Firmware version and qgroundcontrol version to install lastest firmware to Pixhawk.

@tdo Thank you so much for helping me. Now, I can fly it in simulator via SET_POSITION_TARGET_LOCAL_NED.

Hy guys !

Do you use c_uart_interface too ? I try to command my drone with this soft since 4 months ago but nothing work like I want…

Thomas

Yes, I am implementing on it. It work fine for me for offboard mode

Thanks for your answer Rasp !

I got exactly the same configuration as you and I work to enable offboard on a drone but impossible to understand how to use this c++ script. In reality, I only have a pixhawk to test in HITL with jmavsim and sometimes it’s work sometimes nothing happens especially since the last version of PX4 flight stack release. I have begin 4 months ago and nothing work, I’m a bit discouraged.

Is it possible to work together and exchange our scripts ? Because I don’t know what I could forget. I can send my

Therefore, I have found few protocols in mavlink website like companion computer protocol but it seems like nobody use it. So, is it deprecated ?

Sorry for replying you late, don’t be discourage! you are not the only one who confront with these problem. I spend 4 months to got a first simple flight on my drone starting from zero knowledge of programming. Now i am starting to use Github, somehow, we can work together. :slight_smile:

1 Like

Hello,
Did you make the drone land with type_mask = (0x2000 | 0b110111000011) ? Is it different from setting the target z position of SET_POSITION_TARGET_LOCAL_NED message to zero? When I tried these two methods in JMAVSIM, both methods can not make the drone land without hovering near the ground. Did you meet this similar phenomenon.