# Offboard api with timecontrol

**URL:** <https://discuss.px4.io/t/offboard-api-with-timecontrol/42882>\
**Category:** PX4 Autopilot\
**Created:** [December 15, 2024, 2:17am UTC](https://discuss.px4.io/t/offboard-api-with-timecontrol/42882 "2024-12-15T02:17:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![duguguang](https://discuss.px4.io/user_avatar/discuss.px4.io/duguguang/32/18046_2.png) [@duguguang](https://discuss.px4.io/u/duguguang)\
**Post date:** [December 15, 2024, 2:17am UTC](https://discuss.px4.io/t/offboard-api-with-timecontrol/42882/1 "2024-12-15T02:17:35Z")

</div>

Is the any good idea to achieve accurate time contorl with offboard api, like goTo(x,y,z,time), or other operation like takeoff(height, time), land(height,time). now i use the velocity, and i use the distance divide the time. but i do need to check the goal is touched, and this alway have error. I am inspired by the crazyflie with high level contorl. can i move it to the controller ?

```
  if (command.getCommandId() == CommandId::Takeoff) {
    const TakeOffCommand& takeOffCmd = static_cast<const TakeOffCommand&>(command);

    if (takeOffCmd.params.height <= 0 || takeOffCmd.params.duration <= 0) {
        m_logger.warning("Ignore command : Invalid takeoff parameters: height and duration must be positive.");
        return;
     }

    auto takeoff_speed = takeOffCmd.params.height / takeOffCmd.params.duration;

    auto get_speed_result = action.get_maximum_speed();
    if (get_speed_result.first == Action::Result::Success)
    {
        action.set_maximum_speed(std::min(takeoff_speed, get_speed_result.second));
    }
    action.set_takeoff_altitude(takeOffCmd.params.height);

    const Action::Result takeoff_result = action.takeoff();
    if (takeoff_result != Action::Result::Success) {

        std::stringstream sstr;
        sstr << "Takeoff failed: " << takeoff_result;
        m_logger.error(sstr.str());
        return;
    }
{
    // check position reached
    auto in_air_promise = std::promise<void>{};
    auto in_air_future = in_air_promise.get_future();
    Telemetry::PositionVelocityNedHandle handle = telemetry.subscribe_position_velocity_ned(
        [&telemetry, &in_air_promise, &handle, takeOffCmd](Telemetry::PositionVelocityNed position_velocity_ned) {
            if (fabs(position_velocity_ned.position.down_m + takeOffCmd.params.height) < 0.05)
            {
                std::cout << "Taking off has finished\n.";
                telemetry.unsubscribe_position_velocity_ned(handle);
                in_air_promise.set_value();
            }
        });
    in_air_future.wait_for(std::chrono::duration<float>(DeltaSeconds));
    if (in_air_future.wait_for(std::chrono::duration<float>(takeOffCmd.params.duration)) == std::future_status::timeout) {
        std::cerr << "Takeoff timed out.\n";
        return;
    }
}
// or just sleep
//sleep_for(std::chrono::duration<float>(takeOffCmd.params.duration));

```

---

<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:** [December 17, 2024, 2:00am UTC](https://discuss.px4.io/t/offboard-api-with-timecontrol/42882/2 "2024-12-17T02:00:42Z")

</div>

You need to “close the loop”, so basically write a position controller to get to the right position. Or alternatively, you can just use the position offboard interface instead of the velocity one. Does that make sense?
