# PR simulator in hardware, new module #11400

**URL:** <https://discuss.px4.io/t/pr-simulator-in-hardware-new-module-11400/9631>\
**Category:** Uncategorized\
**Created:** [February 15, 2019, 5:05pm UTC](https://discuss.px4.io/t/pr-simulator-in-hardware-new-module-11400/9631 "2019-02-15T17:05:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Romain\_Chiappinelli](https://discuss.px4.io/user_avatar/discuss.px4.io/romain_chiappinelli/32/6089_2.png) [@Romain\_Chiappinelli](https://discuss.px4.io/u/Romain_Chiappinelli)\
**Post date:** [February 15, 2019, 5:05pm UTC](https://discuss.px4.io/t/pr-simulator-in-hardware-new-module-11400/9631/1 "2019-02-15T17:05:11Z")

</div>

Hi everyone,

I would like to do a follow up on the Pull Request I made few days ago

> <https://github.com/PX4/PX4-Autopilot/pull/11400>
>
> new module added that allows to run a simulator for quadrotor directly on the ha…rdware autopilot.
> For more documentation visit 
> https://github.com/romain-chiap/PX4\_SIH\_QuadX
> 
> Please use \[PX4 Discuss\](http://discuss.px4.io/) or \[Slack\](http://slack.px4.io/) to align on pull requests if necessary.
> 
> \*\*Test data / coverage\*\*
> https://review.px4.io/plot\_app?log=4a5b5b89-7231-4e7e-a989-f16e71aa9966
> 
> \*\*Describe problem solved by the proposed pull request\*\*
> Timing delays due to the bidirectional connection from the hardware to the desktop computer are an issue in the HITL and affect the flight performance. Furthermore, a user willing to modify the quadrotor parameters (mass, inertia, maximum thrust, ...) needs to dig into JMAVSim or Gazebo to do the modification.
> 
> \*\*Describe your preferred solution\*\*
> The proposed solution is to add a new module, fully compatible with PX4, called Simulator In Hardware (SIH). It implements the equations of motion of a quadrotor along with thruster force and torque, a simple drag model is also implemented. The sih module subscribes to actuator\_outputs to get directly the outputs of the mixer. It also reconstructs noisy sensor data and publish them. The idea is to include the state estimator in the loop, and the controller of course.
> 
> The user can modify the drag coefficient, inertia matrix and mass of the vehicle directly from QGC parameters. 
> 
> This module is fully compatible with the current PX4 Firmware. The following flight modes have been tested:
> \- Manual
> \- Altitude
> \- Accro
> \- Mission
> \- Failsafe (RC signal lost)
> 
> \*\*Describe possible alternatives\*\*
> The other alternatives are to use the SITL or current HITL
> 
> \*\*Additional context\*\*
> This module was developed to display the quadrotor trajectory in FlightGear, to have a real time visual.
> This module outputs the quad position and orientation on a serial port. The idea is to transform those serial data into UDP to communicate the position/attitude to FlightGear for the display. A Java application SerialToUDP.jar is also provided at the following link along with a simple quadrotor model for FlightGear
> https://github.com/romain-chiap/PX4\_SIH\_QuadX

I added a module called sih (for simulator in hardware). This is an alternative to SITL and HITL for quadrotors where the simulator is executed in the microcontroller.

Did anyone had a chance to try it ? any comments ?

Thanks ,

Romain

---

<div class="post-metadata">

**Author:** ![Romain\_Chiappinelli](https://discuss.px4.io/user_avatar/discuss.px4.io/romain_chiappinelli/32/6089_2.png) [@Romain\_Chiappinelli](https://discuss.px4.io/u/Romain_Chiappinelli)\
**Post date:** [March 1, 2019, 3:52pm UTC](https://discuss.px4.io/t/pr-simulator-in-hardware-new-module-11400/9631/2 "2019-03-01T15:52:41Z")

</div>

@JulianOes

I would like to know if it is possible to use jMAVSim as a visualization for a quadrotor.  
The idea would be to disable the simulation engine, read the MAVlink message HIL\_STATE\_QUATERNION sent by the autopilot hardware and display the states in the 3D visualization.

The PX4 team proposed me to use jMAVSim for the visualization of the quadrotor trajectory (I am currently using FlightGear).

Do you have an idea of an easy way to implement that in jMAVSim?

I imagine:

1. adding an argument to start jMAVSim without the simulation engine, let’s say ‘-nse’. That will involve a modification of the main function in Simulator.java
2. for the MAVlink, I guess I should create a new class that inherited from MAVLinkSystem and use it instead of MAVLinkHILSystem. This will be used to read HIL\_STATE\_QUATERNION and should send nothing.
3. To keep the same code structure, I guess I would just put the simulator in pause and update the visual with the states from HIL\_STATE\_QUATERNION. Unfortunately, it seems the Visualizer3D is linked with the physical World object (itself containing the physical vehicle). Is it possible to separate the physical and visual objects?

Any other things I should be aware of before starting?

Thanks,

Romain

---

<div class="post-metadata">

**Author:** ![Romain\_Chiappinelli](https://discuss.px4.io/user_avatar/discuss.px4.io/romain_chiappinelli/32/6089_2.png) [@Romain\_Chiappinelli](https://discuss.px4.io/u/Romain_Chiappinelli)\
**Post date:** [March 6, 2019, 4:50pm UTC](https://discuss.px4.io/t/pr-simulator-in-hardware-new-module-11400/9631/3 "2019-03-06T16:50:51Z")

</div>

@david_s5

Can you send me a link for an example of code of the semaphore to get a real time calback that can be itself interrupted?

Thanks,

Romain

---

<div class="post-metadata">

**Author:** ![david\_s5](https://discuss.px4.io/user_avatar/discuss.px4.io/david_s5/32/4160_2.png) [@david\_s5](https://discuss.px4.io/u/david_s5)\
**Post date:** [March 6, 2019, 5:15pm UTC](https://discuss.px4.io/t/pr-simulator-in-hardware-new-module-11400/9631/4 "2019-03-06T17:15:03Z")

</div>

@Romain_Chiappinelli

I am not 100% clear on what it is you wanted to do, so I am not sure if the examples are germain.

Here is one example:[https://github.com/PX4/Firmware/pull/10602](https://github.com/PX4/Firmware/pull/10602) and [https://github.com/PX4/Firmware/pull/11571](https://github.com/PX4/Firmware/pull/11571)

The term call back is overloaded. An HRT call back in on an interrupt thread. A HPwork call back is on a high priority thread and it would not be a good idea to block it as well.

Interrupts and HP work should be “Short service” “long service” should be done on properly prioritized threads.

In that case a thread waits on a semaphore sem\_wait() and the HRT call back posts to that semaphore sem\_post(). The down side is resources need to be allocated to the task. If the memory is available this is not an issue.

---

<div class="post-metadata">

**Author:** ![Romain\_Chiappinelli](https://discuss.px4.io/user_avatar/discuss.px4.io/romain_chiappinelli/32/6089_2.png) [@Romain\_Chiappinelli](https://discuss.px4.io/u/Romain_Chiappinelli)\
**Post date:** [March 6, 2019, 5:44pm UTC](https://discuss.px4.io/t/pr-simulator-in-hardware-new-module-11400/9631/5 "2019-03-06T17:44:09Z")

</div>

Thank you for this info, I will explore the options.

EDITED:

I finally implemented it using hrt\_call\_every() and px4\_sem\_wait()

```
void Sih::run()
{
    px4_sem_init(&_data_semaphore, 0, 0);

    hrt_call_every(&_timer_call, LOOP_INTERVAL, LOOP_INTERVAL, timer_callback, &_data_semaphore);

    while (!should_exit())
    {
        px4_sem_wait(&_data_semaphore); // periodic real time wakeup

        inner_loop(); // main execution function
    }

hrt_cancel(&_timer_call); // close the periodic timer interruption
}

// timer_callback() is used as a real time callback to post the semaphore
void Sih::timer_callback(void *sem)
{
    px4_sem_post((px4_sem_t *)sem);
}

```

It works super well, thanks 😃

- sampling time [us]: 3999 avg, 3937 min, 4059 max, \<3us rms error
- the module sih uses 10.7% of the CPU time (on auav-x2.1 board), and the idle is at 50%

---

<div class="post-metadata">

**Author:** ![JulianOes](https://discuss.px4.io/user_avatar/discuss.px4.io/julianoes/32/30_2.png) [@JulianOes](https://discuss.px4.io/u/JulianOes)\
**Post date:** [March 12, 2019, 3:52pm UTC](https://discuss.px4.io/t/pr-simulator-in-hardware-new-module-11400/9631/6 "2019-03-12T15:52:30Z")

</div>

The suggestions on how to change jMAVSim sound ok. I don’t know the details myself but if you can add your visualization only feature without too crazy changes and disabled by default, I think it’s worthwhile to get it merged.

---

<div class="post-metadata">

**Author:** ![Romain\_Chiappinelli](https://discuss.px4.io/user_avatar/discuss.px4.io/romain_chiappinelli/32/6089_2.png) [@Romain\_Chiappinelli](https://discuss.px4.io/u/Romain_Chiappinelli)\
**Post date:** [March 20, 2019, 5:04pm UTC](https://discuss.px4.io/t/pr-simulator-in-hardware-new-module-11400/9631/7 "2019-03-20T17:04:52Z")

</div>

I implemented something which is working but the rendering is jerky.

I used KinematicObject.setPosition() and KinematicObject.setRotation() to update the position and orientation of the vehicle whenever HIL\_STATE\_QUATERNION (i.e. the groundtruth) is received from the autopilot. The code is available [here](https://github.com/romain-chiap/jMAVSim/commit/8281545e35e00f0d0f98aa51e36706e44a9c17d9#diff-3b1970ef867eb7917007e31d42f70f9cR70).

It looked like an aliasing problem, so I tried with the flags -aa and -no-aa to enable/disable the antialiasing. But both attempts resulted in a jerky display.

Any other flag I can modify for the display?  
Any other idea?

---

<div class="post-metadata">

**Author:** ![ChraebiPK](https://discuss.px4.io/letter_avatar_proxy/v4/letter/c/a5b964/32.png) [@ChraebiPK](https://discuss.px4.io/u/ChraebiPK)\
**Post date:** [May 3, 2019, 11:51am UTC](https://discuss.px4.io/t/pr-simulator-in-hardware-new-module-11400/9631/8 "2019-05-03T11:51:50Z")

</div>

Hi Romain,  
Your concept is really nice. Could you please explain me, why px4\_fmu-v2 is not supported? Is the hardware too slow or any other reason?

Thank you and kind regards  
Chraebi

---

<div class="post-metadata">

**Author:** ![Romain\_Chiappinelli](https://discuss.px4.io/user_avatar/discuss.px4.io/romain_chiappinelli/32/6089_2.png) [@Romain\_Chiappinelli](https://discuss.px4.io/u/Romain_Chiappinelli)\
**Post date:** [May 6, 2019, 1:21pm UTC](https://discuss.px4.io/t/pr-simulator-in-hardware-new-module-11400/9631/9 "2019-05-06T13:21:15Z")

</div>

Hi,

As far as I understand, excluding the simulator-in-hardware from px4\_fmu-v2 is due to hardware limitations. Maybe @dagar can give more details as [he advised me](https://github.com/PX4/Firmware/pull/11400#issuecomment-461141204) to do so.

Best,

Romain

---

<div class="post-metadata">

**Author:** ![JulianOes](https://discuss.px4.io/user_avatar/discuss.px4.io/julianoes/32/30_2.png) [@JulianOes](https://discuss.px4.io/u/JulianOes)\
**Post date:** [May 6, 2019, 1:56pm UTC](https://discuss.px4.io/t/pr-simulator-in-hardware-new-module-11400/9631/10 "2019-05-06T13:56:19Z")

</div>

`px4_fmu-v2` has a [silicon errata](https://docs.px4.io/en/flight_controller/silicon_errata.html). Therefore, we can only safely use 1 MB of 2 MB flash space and are quite limited in what can be included in the binary.

---

<div class="post-metadata">

**Author:** ![ChraebiPK](https://discuss.px4.io/letter_avatar_proxy/v4/letter/c/a5b964/32.png) [@ChraebiPK](https://discuss.px4.io/u/ChraebiPK)\
**Post date:** [June 1, 2019, 10:07am UTC](https://discuss.px4.io/t/pr-simulator-in-hardware-new-module-11400/9631/11 "2019-06-01T10:07:08Z")

</div>

Thank you for your reply.

If someone else needs to use the SIH simulator for a multirotor helicopter on px4-fmu-v2 you can simply:

-Exclude the fixedwing attitude and position controller  
-add in cmake file the SIH module  
-and build

For me it works well

Kind regards
