# Setpoint\_attitude/attitude quaternion transformation

**URL:** <https://discuss.px4.io/t/setpoint-attitude-attitude-quaternion-transformation/15081>\
**Category:** PX4 Autopilot\
**Created:** [February 8, 2020, 12:35am UTC](https://discuss.px4.io/t/setpoint-attitude-attitude-quaternion-transformation/15081 "2020-02-08T00:35:10Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haijie\_Zhang](https://discuss.px4.io/user_avatar/discuss.px4.io/haijie_zhang/32/4448_2.png) [@Haijie\_Zhang](https://discuss.px4.io/u/Haijie_Zhang)\
**Post date:** [February 8, 2020, 12:35am UTC](https://discuss.px4.io/t/setpoint-attitude-attitude-quaternion-transformation/15081/1 "2020-02-08T00:35:10Z")

</div>

Hello,  
I am trying to implement a closed loop hover control using setpoint\_attitude/attitude (I know I can directly use set\_point/position/local, but this is not what I want). After the get the control command from feedback and goal position, I can get desired roll pitch, yaw and thrust. However, when transformation the roll pitch yaw into quaternion so that px4 can accept the signal. I am confused what transformation should I use? I googled it seems for different transformation sequence or method, the equation to get the quaterniion is different. And in tf2::Quarternion, there are several methods, ie, setRPY, setEulerZYX. I am wondering which one should I use. (I did directly use the roll pitch yaw to control Crazyflie before, but for px4 seems I need to transform angles to quaternion).  
Thanks for your time!

---

<div class="post-metadata">

**Author:** ![Jaeyoung-Lim](https://discuss.px4.io/user_avatar/discuss.px4.io/jaeyoung-lim/32/10829_2.png) [@Jaeyoung-Lim](https://discuss.px4.io/u/Jaeyoung-Lim)\
**Post date:** [February 8, 2020, 5:06pm UTC](https://discuss.px4.io/t/setpoint-attitude-attitude-quaternion-transformation/15081/2 "2020-02-08T17:06:04Z")

</div>

@Haijie_Zhang You can use my personal project as reference. [https://github.com/Jaeyoung-Lim/mavros\_controllers](https://github.com/Jaeyoung-Lim/mavros_controllers) This sends the body angular rates and thrust as a command. Most of the transformation is implemented inside `geometric_controller`

---

<div class="post-metadata">

**Author:** ![Haijie\_Zhang](https://discuss.px4.io/user_avatar/discuss.px4.io/haijie_zhang/32/4448_2.png) [@Haijie\_Zhang](https://discuss.px4.io/u/Haijie_Zhang)\
**Post date:** [February 9, 2020, 10:20pm UTC](https://discuss.px4.io/t/setpoint-attitude-attitude-quaternion-transformation/15081/3 "2020-02-09T22:20:13Z")

</div>

Hi Jaeyoung,  
I have several more questions.

1. Is there a reason you use angular rates instead of desired roll pitch yaw to do this?

2. I saw there is a function `quaternion acc2quaternion(acceleration, yaw)`. Does that mean after I get aceleration from my own PID controller, I directly use directly use this function to get the desired quaternion angle? Then I can pub this quaternion angle to `setpoin_attitude/attitude `?

3. If question 2 is correct, what should I use for `cmd_att.pose.position.x y z` as shown in your post? [https://404warehouse.net/2015/12/20/autopilot-offboard-control-using-mavros-package-on-ros/](https://404warehouse.net/2015/12/20/autopilot-offboard-control-using-mavros-package-on-ros/)  
My understanding is that px4 wouldn’t use these three position values since it is the attitude control, only concentrate on the attitude values. Is it correct?

4. From your answer this post, [https://discuss.px4.io/t/trajectory-following-via-acceleration-setpoints-which-controller-to-use/7106](https://discuss.px4.io/t/trajectory-following-via-acceleration-setpoints-which-controller-to-use/7106), it seems I can directly use three desired accelerations to control the quad with `setpoint_accel/accel`. Is it correct? (Actually my desired attitude and thrust is calculated from desired accelerations based on the flatness equation). If yes, it would simplify my work.

Thanks for your time.

---

<div class="post-metadata">

**Author:** ![Jaeyoung-Lim](https://discuss.px4.io/user_avatar/discuss.px4.io/jaeyoung-lim/32/10829_2.png) [@Jaeyoung-Lim](https://discuss.px4.io/u/Jaeyoung-Lim)\
**Post date:** [February 10, 2020, 8:52am UTC](https://discuss.px4.io/t/setpoint-attitude-attitude-quaternion-transformation/15081/4 "2020-02-10T08:52:24Z")

</div>

1. Heading is one of the states that are inaccurate and hard to obtain on real vehicles. Also it is something that does not need to be too accurate for trajectory tracking. Using rates make the planner be more resilient for these kind of uncertainties

2. Yes

3. You can set the type mask depending on what setpoint you want to send

4. I am not sure what the status is with acceleration setpoints. It would depend on your implementation how reliable it is since you can’t input jerk to the system

---

<div class="post-metadata">

**Author:** ![Haijie\_Zhang](https://discuss.px4.io/user_avatar/discuss.px4.io/haijie_zhang/32/4448_2.png) [@Haijie\_Zhang](https://discuss.px4.io/u/Haijie_Zhang)\
**Post date:** [February 11, 2020, 12:51am UTC](https://discuss.px4.io/t/setpoint-attitude-attitude-quaternion-transformation/15081/5 "2020-02-11T00:51:49Z")

</div>

Hi Jaeyoung-Lim,  
Sorry that I have more questions came up.

1. I tried to implement the setpoint\_accel controller and it seems difficult to tune. I searched and found the acceleration control is not closed on px4. I guess that is the reason why there is almost no project based on this control method.

2. When using `setpoing_raw/attitude`, according to your code `pubRateCommands` , you set the `msgs.type_mask = 128`, which means you only need angular rates and thrust to do the control. I understand this together with the paper you mentioned. But when I want to only use angle setpoints and thrust to control this. It seems impossible becase the `msgs.type_mask` can only be set to `1, 2 or 4`. I cann’t block all the angular rates at the same time. So sending attitude\_setpoint and thrust is impossible. Is my understanding corret?

3. If use `setpoint_attitude/attitude` and `setpoint_attitude/thrust` to control it, I have to cope with the `pose.positoin.x, y ,z`, since there is no such `msgs.type_mask` in `PoseStamped`. Which means I have to send position, attitude and thrust to control it. Is it correct? Or I can just leave the `position.x, y z` blank?

Because I don’t want position feedback used in my real MAV project(I will use images to calculate the desired attitudes), the only way is to send `angular rates` and `thrust`. Also, I need to change the controller implementation using images based on the paper you mentioned.

---

<div class="post-metadata">

**Author:** ![Jaeyoung-Lim](https://discuss.px4.io/user_avatar/discuss.px4.io/jaeyoung-lim/32/10829_2.png) [@Jaeyoung-Lim](https://discuss.px4.io/u/Jaeyoung-Lim)\
**Post date:** [February 11, 2020, 9:36am UTC](https://discuss.px4.io/t/setpoint-attitude-attitude-quaternion-transformation/15081/6 "2020-02-11T09:36:14Z")

</div>

2, 3 those are both bitmasks you can mask whatever you want

---

<div class="post-metadata">

**Author:** ![Haijie\_Zhang](https://discuss.px4.io/user_avatar/discuss.px4.io/haijie_zhang/32/4448_2.png) [@Haijie\_Zhang](https://discuss.px4.io/u/Haijie_Zhang)\
**Post date:** [February 11, 2020, 11:05pm UTC](https://discuss.px4.io/t/setpoint-attitude-attitude-quaternion-transformation/15081/7 "2020-02-11T23:05:46Z")

</div>

I didn’t figure out how to set the `type_mask` correctly. It should be something like `type_mask = IGNORE_ROOL_RATE | IGNORE_PITCH_RATE | IGNORE_YAW_RATE`

Thanks for your explanation.

---

<div class="post-metadata">

**Author:** ![Jaeyoung-Lim](https://discuss.px4.io/user_avatar/discuss.px4.io/jaeyoung-lim/32/10829_2.png) [@Jaeyoung-Lim](https://discuss.px4.io/u/Jaeyoung-Lim)\
**Post date:** [February 12, 2020, 8:54am UTC](https://discuss.px4.io/t/setpoint-attitude-attitude-quaternion-transformation/15081/8 "2020-02-12T08:54:35Z")

</div>

@Haijie_Zhang [https://github.com/mavlink/mavros/blob/master/mavros/src/plugins/setpoint\_trajectory.cpp#L157](https://github.com/mavlink/mavros/blob/master/mavros/src/plugins/setpoint_trajectory.cpp#L157)

---

<div class="post-metadata">

**Author:** ![px4\_robot](https://discuss.px4.io/user_avatar/discuss.px4.io/px4_robot/32/14999_2.png) [@px4\_robot](https://discuss.px4.io/u/px4_robot)\
**Post date:** [August 26, 2024, 5:20pm UTC](https://discuss.px4.io/t/setpoint-attitude-attitude-quaternion-transformation/15081/9 "2024-08-26T17:20:06Z")

</div>

Is there a way to implement this mask using offboard mode in ROS? I don’t quite understand how to mix the position and attitude control with the mask. From what I understand you can select _either_ position or attitude mode in Offboard mode, but can’t do both [Offboard Mode (Generic/All Frames) | PX4 Guide (main)](https://docs.px4.io/main/en/flight_modes/offboard#ros-2-messages) . Is there way to do the masking you described with ROS2 offboard?

---

<div class="post-metadata">

**Author:** ![Jaeyoung-Lim](https://discuss.px4.io/user_avatar/discuss.px4.io/jaeyoung-lim/32/10829_2.png) [@Jaeyoung-Lim](https://discuss.px4.io/u/Jaeyoung-Lim)\
**Post date:** [August 26, 2024, 7:05pm UTC](https://discuss.px4.io/t/setpoint-attitude-attitude-quaternion-transformation/15081/10 "2024-08-26T19:05:35Z")

</div>

@px4_robot You cannot mix position and attitude, since they are not independently controllable.

---

<div class="post-metadata">

**Author:** ![px4\_robot](https://discuss.px4.io/user_avatar/discuss.px4.io/px4_robot/32/14999_2.png) [@px4\_robot](https://discuss.px4.io/u/px4_robot)\
**Post date:** [August 26, 2024, 7:34pm UTC](https://discuss.px4.io/t/setpoint-attitude-attitude-quaternion-transformation/15081/11 "2024-08-26T19:34:07Z")

</div>

@Jaeyoung-Lim . Thanks for your reply. Can you tell me if my understanding is correct for these two points?

(1) You cannot mix position and attitude in offboard mode

(2) You can send a setpoint for attitude with a setpoint for altitude (z) with mavsdk (and mavros?) if you use the correct type\_mask?

> [@Setting the type mask for setpoints](https://discuss.px4.io/t/setting-the-type-mask-for-setpoints/11717/2):
>
> Hi, I think that’s tricky because for position or velocity control we use a different message [SET\_POSITION\_TARGET\_LOCAL\_NED](https://mavlink.io/en/messages/common.html#SET_POSITION_TARGET_LOCAL_NED) than for attitude control where we use the message [SET\_ATTITUDE\_TARGET](https://mavlink.io/en/messages/common.html#SET_ATTITUDE_TARGET). In theory you could be sending both messages with most fields set as ignored except the altitude in the first message and the attitude in the second. If I was you I would try that in SITL and see what happens, or alternatively look at the position controller to try to figure out how it deals with that. …

After reading your comment from Feb 2020 and the above comment I got hope for point #2. Am I understanding your post and the linked discussion correctly?

---

<div class="post-metadata">

**Author:** ![Jaeyoung-Lim](https://discuss.px4.io/user_avatar/discuss.px4.io/jaeyoung-lim/32/10829_2.png) [@Jaeyoung-Lim](https://discuss.px4.io/u/Jaeyoung-Lim)\
**Post date:** [August 27, 2024, 4:23am UTC](https://discuss.px4.io/t/setpoint-attitude-attitude-quaternion-transformation/15081/12 "2024-08-27T04:23:47Z")

</div>

@px4_robot Nope you cannot send altitude and attitude at the same time. Position and attitude are two different controllers and you cannot independently control both at the same time.
