PX4 Host target in simulink with Simulink Model

Hello, does anyone familiar with the design of flight controller in simulink and test it with SIL, with simulink model?
I started to test the examples in simulink and they works very well.I build my controller in simulink and i tested it in a normal simulation in simulink and it works well. But when i try to perform SIL, Hosting PX4 on my pc and as a target The model in simulink, the controller isn’t able to control the drone anymore.
Now, i know that, normal simulation in simulink is like an ideal simulation, and SIL is more realistic. So my doubs are that;

  • is just a matter of tuning better the controller.
  • or there are some tips, ( for example some keys point like Sample time, ecc…) to perform
    better this task.

For clarity, i copied the settings parameters for the simulation by the example in simulink.

Thank in advance

Hi,

If your controller works in simulink environment fine then it should behave the same in the SIL which means you don’t need to tune for a better controller. In addition, Simulink SIL has “Lockstep” logic so your controller and aircraft model wait eachother. Since, I don’t see your models, I can suggest you some advices. Firstly, make sure that you publish “HIL_SENSOR”, “HIL_GPS”, “HEARTBEAT”, “HIL_STATE” messages corretly on your model side. Also, you need to check that you get correct actuator commands from “HIL_ACTUATOR_CONTROL” mavlink message.

Thanks for you relpy.

Regarding the Actuator command from “HIL_ACTUATOR_CONTROL”, i did some tests and i notice that i miss some commands, i mean is like that the comunication remains stuck:

The controller send in 1 CH, via PWM Block for PX4 Autopilots [1500 1600 1700 1600]
The model recevied via “HIL_ACTUATOR_CONTROL”, mavlink message [1500 1500 1500 1600]

Is like i miss some data, but i don’t know why. I took the block for the communication port (XCP/IP) directly from the matlab example. I set also the step size at 0.04.

Hi again, sorry for the late reply I’ve been too busy.

The reason why you get different pwm values might be the cmake configuration. Simulink has “px4_sitl_default”. You can check that on Hardware>Hardware Settings>Hardware Implemantation page. px4_sitl_default configuration uses the “iris” drone(Firmware\ROMFS\px4fmu_common\init.d\airframes>10016_3dr_iris) mixer which is quad_w. You can find the mixer in Firmware\ROMFS\px4fmu_common\mixers. So your pwm values might be changed according to this mixer. Now, there are 2 options that you can choose. First, you can edit the mixer file of the Iris. Second, you can change the vehicle model, by edit the sitl_run.sh file which is located Firmware\Tools path.

Good luck,

Bilal

1 Like

Thanks again Bilal, dont’t worry and sorry for my late reply.
I didn’t undestrand very well what you mean sorry. Because In my mind the comunication is straight forward between the controller and simulator, also i have already my mixer.
So there is a further step, between the PWM block and HIL_ACTUATOR_CONTROL, that i miss?

By the way, we have tried to rebuild the controller, and use the simulator of the matlab example. Now even SIL works, but only if the step size is set 0.004, (that value was chosen almost randomly) . I tried with 0.008 like the example but didn’t work.
Have you an idea how to set it, with some criteria? I know that controller and the simulator works in a looked stepsize, as you said, but In my mind the simulator should go faster than the controller in order to simulate better the “continous world”.

Sorry for all questions but this is my first approach into a more complex simulation.
Thanks in advance.