PX4 / PX4-Autopilot

PX4 Autopilot Software
https://px4.io
BSD 3-Clause "New" or "Revised" License
8.6k stars 13.56k forks source link

PX4-JSBSim SITL HIL_ACTUATOR_CONTROLS is not generating #21661

Closed JeongseokHyun closed 1 year ago

JeongseokHyun commented 1 year ago

Hi, Now, I try to edit the JSBSim code to add electric propulsion performance analysis and make SITL with PX4 using jsbsim-bridge.

I use the jsbsim v1.1.0 and PX4-Autopilot v1.13.0 with custom VTOL configuration.

I did not edit the communication part in JSBSim and PX4. But, When I add custom code in JSBSim, and try to run with PX4, The SITL is connected. Also, HIL_ACTUATOR_CONTROLS is not generating from PX4. To check it, I checked to send and run function in the simulator_mavelink.cpp code. The receiving part in the run function is always working with sending part in send function. The request_hil_state_quaternion function in the run function and the send_heartbeat function in send function also work at the initial time. But, only the send_controls function is not working in the while loop in send function because the pret value is zero. the So, I think actuator output is not generating in PX4. If someone know this problem, then, can I know the pret variables mean and the problem in this situation???

File location: home\PX4-Autopilot\src\modules\simulator\simulator_mavlink.cpp

void Simulator::send()
{
#ifdef __PX4_DARWIN
    pthread_setname_np("sim_send");
#else
    pthread_setname_np(pthread_self(), "sim_send");
#endif

    // Subscribe to topics.
    // Only subscribe to the first actuator_outputs to fill a single HIL_ACTUATOR_CONTROLS.
    if (_use_dynamic_mixing) {
        _actuator_outputs_sub = orb_subscribe_multi(ORB_ID(actuator_outputs_sim), 0);

    } else {
        _actuator_outputs_sub = orb_subscribe_multi(ORB_ID(actuator_outputs), 0);
    }

    // Before starting, we ought to send a heartbeat to initiate the SITL
    // simulator to start sending sensor data which will set the time and
    // get everything rolling.
    // Without this, we get stuck at px4_poll which waits for a time update.
    send_heartbeat();

    px4_pollfd_struct_t fds_actuator_outputs[1] = {};
    fds_actuator_outputs[0].fd = _actuator_outputs_sub;
    fds_actuator_outputs[0].events = POLLIN;

    while (true) {

        // Wait for up to 100ms for data.
        int pret = px4_poll(&fds_actuator_outputs[0], 1, 100);
        PX4_INFO("send pret : %d", pret);

        if (pret == 0) {
            // Timed out, try again.
            continue;
        }

        if (pret < 0) {
            PX4_ERR("poll error %s", strerror(errno));
            continue;
        }

        if (fds_actuator_outputs[0].revents & POLLIN) {
            // Got new data to read, update all topics.
            parameters_update(false);
            check_failure_injections();
            _vehicle_status_sub.update(&_vehicle_status);

            // Wait for other modules, such as logger or ekf2
            px4_lockstep_wait_for_components();

            send_controls();
        }
    }

    orb_unsubscribe(_actuator_outputs_sub);
}
Jaeyoung-Lim commented 1 year ago

@JeongseokHyun Not sure what exactly you are asking.

Are you implying that there is something wrong with the implementation? What custom changes did you make? Did it work without your custom changes?

JSBSim and px4 SITL instance runs on a lockstep, which means for each actuator signal there needs to be a sensor message returning to the SITL instance (see https://docs.px4.io/main/en/simulation/)

I am not sure what the custom changes you have made are, but I would suggest to compare your implementation with the implementation that works and find out where the problem might be.

Jaeyoung-Lim commented 1 year ago

@JeongseokHyun Was this resolved? If so, could you share insights on how you resolved your problem?

JeongseokHyun commented 1 year ago

@Jaeyoung-Lim It is my mistake. :D To run a custom airframe with a custom configuration using PX4-JSBSim SITL, I copied a custom airframe from windows. After then, I tried to run PX4-JSBSim SITL. PX4 couldn't find the airframe file. To resolve it, I ignored a line in rCS file (https://discuss.px4.io/t/error-when-simulating-a-new-airframe/31919/6). I think that was the reason. PX4 couldn't send the HIL_ACTUATOR_CONTROLS message.

After I recovered the line in rCS file, I also created custom airframe in ubuntu again. Then it was resolved. I don't know the exact reason, but PX4 didn't seem to recognize the Custom Airframe that Windows copied.

Thank you

Jaeyoung-Lim commented 1 year ago

@JeongseokHyun Thanks for the update

No idea how px4 works with windows, so not sure if I can comment on that side