mavlink / c_uart_interface_example

Simple MAVLink to UART interface example for *nix systems
264 stars 257 forks source link

Switch custom mode and base mode #24

Closed ptit-tom closed 6 years ago

ptit-tom commented 7 years ago

Hi everyone

I'm working on offboard control of multicopter using c_uart_interface, pixhawk and mavlink. It might seem useless to know how to switch custom and base mode's autopilot for me but I'm curious and I don't understand the way to do it.

I saw https://dev.px4.io/en/concept/flight_modes.html , px4_custom_mode.h, MAV_MODE, MAV_MODE_FLAG, commander_state.msg in firmware but it muddled up to me the spirit !

What the difference between custom mode, base mode, main mode, main state and how use it with mavlink ?

Thanks you in advance, Thomas

MHageH commented 7 years ago

In this part code, you can see clearly how the interface sends the offboard command to the pixhawk.

I believe you can mod it the way you like to switch between modes. Check the MAVLink specification for further information. (MAV_CMD_DO_SET_MODE and MAV_MODE Enum).

On another note, in what circumstances are you trying to implement the Interface?

P.S : If I recall correctly, for the offboard mode, you should start to stream something first before activating it otherwise it will just turn off automatically on the pixhawk's end.

ptit-tom commented 7 years ago

Thank you for your response MHageH !

I have already seen the code's part you showed me and it's not really what I mean. However, I don't have RC to control my MAV, so switch mode witch RC isn't a solution for me ! ahah

I'm in internship and my aim is to control MAV with companion computer for 3D mapping. I see in this website (https://dev.px4.io/en/concept/flight_modes.html) and in the file px4_custom_mode.h of PX4's firmware, modes don't correspond. For example, there is a sub_mode called AUTO_TAKEOFF apparently but I don't how switch to this mode. For the moment, I use MAV_CMD_NAV_TAKEOFF but my MAV doesn't move when I use it in HITL whereas a message appears :"takeoff detected"

Did you mean REQUEST_DATA_STREAM msg ?

PS : I have read your report one month ago and your code. It's not really the same method but still useful.

MHageH commented 7 years ago

I had a similar internship when I modified the interface (didn't have a RC neither). Did you try to use SITL ? I've nearly made all of my flights in simulation first then tested on ground later on.

As for the mods, I simply neglected them. I assumed that the drone will be controlled by the companion computer all the time, so I made a custom "takeoff" and hovering, with offboard mode on since the start. It's a bit dangerous this way, but I made sure that the drone will drop in safe mode if anything goes wrong. Unfortunately, I am no longer in position of the materials to test this for myself.

Think about these points :

If neither of these works, a simple search indicates that PX4_CUSTOM_SUB_MODE_AUTO_TAKEOFF is passed over from mavlink_messages.cpp to commander.cpp (main app) and is treated at line 773 (case vehicle_command_s::VEHICLE_CMD_DO_SET_MODE).

I think you should check param2 and 3 of MAV_CMD_DO_SET_MODE and send it in.

ptit-tom commented 7 years ago

Hi MHageH !

Yes, I use it since 2 weeks ago and more exactly HITL. In fact, I don't really understand the difference between HITL and SITL. Thanks, I do all of my commands in offboard mode now and it's work well, thanks to you !

About take off command, I have re-written my parameters : mavlink_command_long_t takeoff_cmd; /* Takeoff from ground / hand

For PX4_CUSTOM_SUB_MODE_AUTO_TAKEOFF, I 've already tried to use MAV_CMD_DO_SET_MODE but I didn't find which rights param1 and param2 to set for change in mode I wish. In px4_custom_mode.h , only one MAIN_MODE and one SUB_MODE get values... Do you think I have to set values for others modes to use it ?

MHageH commented 7 years ago

Hello again, Good thing it works well now. As for HITL, it is basically a simulation where you use the real hardware (pixhawk) to fly the drone in simulation (with qgroundcontrol), whereas in SITL it is solely based on software, where you can see a virtual drone flying.

Try to specify param2 as PX4_CUSTOM_MAIN_MODE_AUTO and param3 as PX4_CUSTOM_SUB_MODE_AUTO_TAKEOFF with the MAV_CMD_DO_SET_MODE command as described with the PX4 Custom modes. As param1, send MAV_MODE_AUTO_ARMED (described in MAV_MODE enum in MAVlink reference).

I think the sub_modes are only defined for the AUTO_MOD values. Try to send only the PX4_CUSTOM_MAIN_MODE for other modes (not the sub ones), and see if it works.

Your approach is rather interesting. I can implement this later on if you can test it with my modification of the interface to rather command the drone in proper mods, rather than all the time in offboard mode.

ptit-tom commented 7 years ago

Ok, thanks for the information. I use HITL but I check the result with jmavsim and not with qgroundcontrol, actually.

I 've checked for MAV_CMD_DO_SET_MODE and I 've seen little "problem". In c_uart_interface directory, the command got 2 params. In mavlink website, this same command got 3 params...

I will try with main mode only like you said me. I shall come back to you when it's done. Yes, if you want I can test it for you with my material. But for the moment, I only got Pixhawk and jmavsim.

MHageH commented 7 years ago

I actually went through this when I was developing my version of the interface. C_uart_interface isn't compatible with the latest MAVLink library (was testing it on jmavsim and the real drone back then), that's why I downloaded that version (commit: 7d0aa0205ef8bd13bf855b21d3ebe054dc9109ef) and fixed it in my mod.

You are rather right, I didn't notice that at first. If you look at the MAVLink version used 3 years ago (actually used as a submodule in C_UART_Interface), you'll see that MAV_CMD_DO_SET_MODE doesn't have a CUSTOM_SUB_MODE, whereas for the current version, it is defined. I think that the MAVLink reference is automatically generated with the latest compilation of MAVLink library.

It is actually very similar with SET_MODE command before it was deprecated.

I'm afraid that we are not capable to set custom submods with this version. Current PX4 firmware do understand it, but there's no way to send it.

Perhaps the only solution would be MAV_CMD_NAV_TAKEOFF as you've done before.

Update me with your latest findings.

ptit-tom commented 7 years ago

Hi MHageH !

I've found something interesting about offboard's submode. Look at this, there are hidden mode linked with 3 mavlink's message : set_position_target_local_ned, set_attitude_target and set_actuator_control_target For more details, look at mavlink_receiver.cpp.

About PX4_CUSTOM mode, It did'nt work so I've give it up.