# Cannot control quad in JMAVSim using actuator\_controls. Reference - Writing your first application, PX4 User Guide

**URL:** <https://discuss.px4.io/t/cannot-control-quad-in-jmavsim-using-actuator-controls-reference-writing-your-first-application-px4-user-guide/30059>\
**Category:** PX4 Autopilot\
**Created:** [December 11, 2022, 7:56pm UTC](https://discuss.px4.io/t/cannot-control-quad-in-jmavsim-using-actuator-controls-reference-writing-your-first-application-px4-user-guide/30059 "2022-12-11T19:56:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![A7sus4](https://discuss.px4.io/user_avatar/discuss.px4.io/a7sus4/32/11958_2.png) [@A7sus4](https://discuss.px4.io/u/A7sus4)\
**Post date:** [December 11, 2022, 7:56pm UTC](https://discuss.px4.io/t/cannot-control-quad-in-jmavsim-using-actuator-controls-reference-writing-your-first-application-px4-user-guide/30059/1 "2022-12-11T19:56:58Z")

</div>

**Objective**

In the example here([First Application Tutorial (Hello Sky) | PX4 Guide (main)](https://docs.px4.io/main/en/modules/hello_sky.html)), they’ve written a code in C that reads data from a topic and prints it. I want to write a similar C program that will instruct the drone in JMAVSim to fly a certain way(give pitch, yaw, roll, throttle commands).

**How did I get to this point?**

I followed the Writing your first application tutorial from here([https://dev.px4.io/v1.11\_noredirect/en/apps/hello\_sky.html](https://dev.px4.io/v1.11_noredirect/en/apps/hello_sky.html))  
I found an example here([https://github.com/PX4/PX4-Autopilot/blob/main/src/examples/hwtest/hwtest.c](https://github.com/PX4/PX4-Autopilot/blob/main/src/examples/hwtest/hwtest.c)) and modified it.

**I have done the following before uploading code**

I’ve disabled lockstep in sitl.cmake in boards/px4/sit  
I’ve commented the line mc\_rate\_control start in PX4-Autopilot/ROMFS/px4fmu\_common/init.d to disable the default application that controls the quad.

**Moreover, this is the code that I’m running in px4\_simple\_app to control the quad:**

```auto
struct actuator_controls_s actuators;
memset(&actuators, 0, sizeof(actuators));
orb_advert_t actuator_pub_ptr = orb_advertise(ORB_ID(actuator_controls_0), &actuators);

struct actuator_armed_s arm;
memset(&arm, 0, sizeof(arm));

arm.timestamp = hrt_absolute_time();
arm.ready_to_arm = true;
arm.armed = true;
orb_advert_t arm_pub_ptr = orb_advertise(ORB_ID(actuator_armed), &arm);
orb_publish(ORB_ID(actuator_armed), arm_pub_ptr, &arm);

hrt_abstime stime;

int count = 0;

while (count != 36) {
    stime = hrt_absolute_time();

    while (hrt_absolute_time() - stime < 1000000) {
        actuators.control[0]=0.5f;
        actuators.control[1]=0.3f;
        actuators.control[2]=0.3f;
        actuators.control[3]=0.9f;

        actuators.timestamp = hrt_absolute_time();
        orb_publish(ORB_ID(actuator_controls_0), actuator_pub_ptr, &actuators);
        usleep(10000);
    }

    warnx("count %i", count);
    count++;

```

**Problem**

There are no errors, but I don’t see the quad flying or change yaw/pitch angles. What should I do? Am i missing something basic?

---

<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:** [December 13, 2022, 12:58pm UTC](https://discuss.px4.io/t/cannot-control-quad-in-jmavsim-using-actuator-controls-reference-writing-your-first-application-px4-user-guide/30059/2 "2022-12-13T12:58:31Z")

</div>

@A7sus4 Did you try arming the vehicle?

I am not sure what the expectation of sending arming commands as you are doing in your application.

There are a lot of things missing from your implementation that ensures safely operating the vehicle. If you want to have a reference, you can look at what the `mc_rate_control`, which you have commented out to check what your application is missing.

---

<div class="post-metadata">

**Author:** ![A7sus4](https://discuss.px4.io/user_avatar/discuss.px4.io/a7sus4/32/11958_2.png) [@A7sus4](https://discuss.px4.io/u/A7sus4)\
**Post date:** [December 15, 2022, 5:46am UTC](https://discuss.px4.io/t/cannot-control-quad-in-jmavsim-using-actuator-controls-reference-writing-your-first-application-px4-user-guide/30059/3 "2022-12-15T05:46:07Z")

</div>

Hey Jaeyoung-Lim. Thank you for your reply.

Yes I did. In a modified application, I armed my vehicle by publishing to vehicle\_control\_mode and then publishing to vehicle\_attitude\_setpoint. However, I still did not get any results.I did commander stop and ran my code. The Attitude\_setpoints revert back to some default value after every second iteration. I wasn’t really concerned with the safe operating of the vehicle as I just wanted to get some actuator output and see it move around.  
Moreover, here’s a link to my modified code- [px4\_simple\_app/px4\_simple\_app.c at fbfbe794c865811cdd8571702d2cffe15cbf2490 · wickedticket/px4\_simple\_app · GitHub](https://github.com/wickedticket/px4_simple_app/blob/fbfbe794c865811cdd8571702d2cffe15cbf2490/px4_simple_app/px4_simple_app.c)  
I’ll check out mc\_rate\_control.

---

<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:** [December 15, 2022, 8:02am UTC](https://discuss.px4.io/t/cannot-control-quad-in-jmavsim-using-actuator-controls-reference-writing-your-first-application-px4-user-guide/30059/4 "2022-12-15T08:02:06Z")

</div>

@A7sus4 Why would you stop commander to run your code? Then this means that you need to handle what commander was doing in your code, which I believe the code is not doing and therefore not working

---

<div class="post-metadata">

**Author:** ![A7sus4](https://discuss.px4.io/user_avatar/discuss.px4.io/a7sus4/32/11958_2.png) [@A7sus4](https://discuss.px4.io/u/A7sus4)\
**Post date:** [December 15, 2022, 8:07am UTC](https://discuss.px4.io/t/cannot-control-quad-in-jmavsim-using-actuator-controls-reference-writing-your-first-application-px4-user-guide/30059/5 "2022-12-15T08:07:53Z")

</div>

I tried running the code with commander start too.  
here’s the output with commander stop

 ![Screenshot from 2022-12-15 10-59-24](https://discuss.px4.io/uploads/default/original/2X/3/36dacfe7547536d1d53e3668899c315817340aac.png)

here’s the output with commander start

 ![Screenshot from 2022-12-15 11-05-31](https://discuss.px4.io/uploads/default/original/2X/c/c72c98df63335cb16f72f1c2891b86767ac262cc.png)

Commander start is updating the vehicle\_control\_status values after every iteration.

---

<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:** [December 15, 2022, 9:09am UTC](https://discuss.px4.io/t/cannot-control-quad-in-jmavsim-using-actuator-controls-reference-writing-your-first-application-px4-user-guide/30059/6 "2022-12-15T09:09:37Z")

</div>

@A7sus4 I was not implying that you should run your code together with commander.

What I meant is that there are a lot of states that are being set for safety in commander, and that you need to handle these if you want to replace commander.

If you are just interested in implementing a controller, you can look at what information the mc\_rate\_controller is using, and just replace the `mc_rate_controller` with your implementation

---

<div class="post-metadata">

**Author:** ![A7sus4](https://discuss.px4.io/user_avatar/discuss.px4.io/a7sus4/32/11958_2.png) [@A7sus4](https://discuss.px4.io/u/A7sus4)\
**Post date:** [December 19, 2022, 6:31am UTC](https://discuss.px4.io/t/cannot-control-quad-in-jmavsim-using-actuator-controls-reference-writing-your-first-application-px4-user-guide/30059/7 "2022-12-19T06:31:07Z")

</div>

hey @Jaeyoung-Lim . I followed your advice and went through MulticopterRateControl.cpp in src/modules/mc\_rate\_control thoroughly . I also decided to make some changes in it. Changes in yaw, pitch or roll are getting reflected in jmavsim only when I do commander takeoff.  
For example, when I run make px4\_sitl\_default jmavsim, nothing happens. When i do commander takeoff, the quad takes off, but doesn’t really fly away as I’d expect it to. Commands that come under commander takeoff are probably interfering with my commands.  
How do I make sure that the quad runs MulticopterRateControl.cpp with only the upthrust and yaw pitch roll values set by me?
