Creating a seperate package for offboard control example from px4_ros_com package

I am trying to make a separate package for the example offboard_control.cpp from px4_ros_com package. In my workspace I have px4_msgs package and my offboard package. During build, i get this error

aamir@aamir-HP:~/dev_ws$ colcon build
Starting >>> px4_msgs
Finished <<< px4_msgs [12.6s]                     
Starting >>> offboard_standalone
--- stderr: offboard_standalone                              
/home/aamir/dev_ws/src/offboard_standalone/src/offboard_control.cpp:47:10: fatal error: px4_msgs/msg/timesync.hpp: No such file or directory
 #include <px4_msgs/msg/timesync.hpp>
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/offboard_control.dir/src/offboard_control.cpp.o] Error 1
make[1]: *** [CMakeFiles/offboard_control.dir/all] Error 2
make: *** [all] Error 2
---
Failed   <<< offboard_standalone [0.13s, exited with code 2]

Summary: 1 package finished [13.2s]
  1 package failed: offboard_standalone
  1 package had stderr output: offboard_standalone

here is the cmake file, i have tried to keep it as simple as possible

cmake_minimum_required(VERSION 3.5)
project(offboard_standalone)

# Default to C99
if(NOT CMAKE_C_STANDARD)
  set(CMAKE_C_STANDARD 99)
endif()

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(px4_msgs REQUIRED)

add_executable(offboard_control src/offboard_control.cpp)
ament_target_dependencies(offboard_control rclcpp px4_msgs)

install(TARGETS
  offboard_control
  DESTINATION lib/${PROJECT_NAME})
   
ament_package()

I am pretty sure it has something to do with my cmake file, i have tried to keep it similar to the px4_ros_com package.

Any help would be appreciated.

I have given absolute path for timesync.hpp. and now i get these errors after some warnings in during colcon build.

/home/aamir/dev_ws/src/offboard_standalone/src/offboard_control.cpp: In member function ‘void OffboardControl::publish_offboard_control_mode() const’:
/home/aamir/dev_ws/src/offboard_standalone/src/offboard_control.cpp:157:6: error: ‘using OffboardControlMode = struct px4_msgs::msg::OffboardControlMode_<std::allocator<void> > {aka struct px4_msgs::msg::OffboardControlMode_<std::allocator<void> >}’ has no member named ‘position’
  msg.position = true;
      ^~~~~~~~
/home/aamir/dev_ws/src/offboard_standalone/src/offboard_control.cpp:158:6: error: ‘using OffboardControlMode = struct px4_msgs::msg::OffboardControlMode_<std::allocator<void> > {aka struct px4_msgs::msg::OffboardControlMode_<std::allocator<void> >}’ has no member named ‘velocity’
  msg.velocity = false;
      ^~~~~~~~
/home/aamir/dev_ws/src/offboard_standalone/src/offboard_control.cpp:159:6: error: ‘using OffboardControlMode = struct px4_msgs::msg::OffboardControlMode_<std::allocator<void> > {aka struct px4_msgs::msg::OffboardControlMode_<std::allocator<void> >}’ has no member named ‘acceleration’
  msg.acceleration = false;
      ^~~~~~~~~~~~
/home/aamir/dev_ws/src/offboard_standalone/src/offboard_control.cpp:160:6: error: ‘using OffboardControlMode = struct px4_msgs::msg::OffboardControlMode_<std::allocator<void> > {aka struct px4_msgs::msg::OffboardControlMode_<std::allocator<void> >}’ has no member named ‘attitude’; did you mean ‘ignore_attitude’?
  msg.attitude = false;
      ^~~~~~~~
      ignore_attitude
/home/aamir/dev_ws/src/offboard_standalone/src/offboard_control.cpp:161:6: error: ‘using OffboardControlMode = struct px4_msgs::msg::OffboardControlMode_<std::allocator<void> > {aka struct px4_msgs::msg::OffboardControlMode_<std::allocator<void> >}’ has no member named ‘body_rate’
  msg.body_rate = false;
      ^~~~~~~~~
make[2]: *** [CMakeFiles/offboard_control.dir/src/offboard_control.cpp.o] Error 1
make[1]: *** [CMakeFiles/offboard_control.dir/all] Error 2
make: *** [all] Error 2

these fields are there, this example works within px4_ros_com package. Why do these error are there? I still think it has something to do with cmake.
has anyone used px4_msgs separate from px4_ros_com package?

Maybe post all your code. It is difficult to guess what you could have done wrong without actually seeing it all.
Did you for instance include
#include <px4_msgs/msg/offboard_control_mode.hpp>

hi,
thanks for replying

I am using this offboard_control.cpp.

The problem i think is that the cmake does not correctly refer to the files that exist in the same workspace, but in different packages.

here is the code, its the same as the example provided in the px4_ros_com package, but i have made the timesync path absolute.

/****************************************************************************
 *
 * Copyright 2020 PX4 Development Team. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/**
 * @brief Offboard control example
 * @file offboard_control.cpp
 * @addtogroup examples
 * @author Mickey Cowden <info@cowden.tech>
 * @author Nuno Marques <nuno.marques@dronesolutions.io>
 * The TrajectorySetpoint message and the OFFBOARD mode in general are under an ongoing update.
 * Please refer to PR: https://github.com/PX4/PX4-Autopilot/pull/16739 for more info. 
 * As per PR: https://github.com/PX4/PX4-Autopilot/pull/17094, the format
 * of the TrajectorySetpoint message shall change.
 */

#include <px4_msgs/msg/offboard_control_mode.hpp>
#include <px4_msgs/msg/trajectory_setpoint.hpp>
#include "/home/aamir/dev_ws/install/px4_msgs/include/px4_msgs/msg/timesync.hpp"
#include <px4_msgs/msg/vehicle_command.hpp>
#include <px4_msgs/msg/vehicle_control_mode.hpp>

#include <rclcpp/rclcpp.hpp>
#include <stdint.h>

#include <chrono>
#include <iostream>

using namespace std::chrono;
using namespace std::chrono_literals;
using namespace px4_msgs::msg;

class OffboardControl : public rclcpp::Node {
public:
	OffboardControl() : Node("offboard_control") {
#ifdef ROS_DEFAULT_API
		offboard_control_mode_publisher_ =
			this->create_publisher<OffboardControlMode>("OffboardControlMode_PubSubTopic", 10);
		trajectory_setpoint_publisher_ =
			this->create_publisher<TrajectorySetpoint>("TrajectorySetpoint_PubSubTopic", 10);
		vehicle_command_publisher_ =
			this->create_publisher<VehicleCommand>("VehicleCommand_PubSubTopic", 10);
#else
		offboard_control_mode_publisher_ =
			this->create_publisher<OffboardControlMode>("OffboardControlMode_PubSubTopic");
		trajectory_setpoint_publisher_ =
		 	this->create_publisher<TrajectorySetpoint>("TrajectorySetpoint_PubSubTopic");
		vehicle_command_publisher_ =
			this->create_publisher<VehicleCommand>("VehicleCommand_PubSubTopic");
#endif

		// get common timestamp
		timesync_sub_ =
			this->create_subscription<px4_msgs::msg::Timesync>("Timesync_PubSubTopic", 10,
				[this](const px4_msgs::msg::Timesync::UniquePtr msg) {
					timestamp_.store(msg->timestamp);
				});

		offboard_setpoint_counter_ = 0;

		auto timer_callback = [this]() -> void {

			if (offboard_setpoint_counter_ == 10) {
				// Change to Offboard mode after 10 setpoints
				this->publish_vehicle_command(VehicleCommand::VEHICLE_CMD_DO_SET_MODE, 1, 6);

				// Arm the vehicle
				this->arm();
			}

            		// offboard_control_mode needs to be paired with trajectory_setpoint
			publish_offboard_control_mode();
			publish_trajectory_setpoint();

           		 // stop the counter after reaching 11
			if (offboard_setpoint_counter_ < 11) {
				offboard_setpoint_counter_++;
			}
		};
		timer_ = this->create_wall_timer(100ms, timer_callback);
	}

	void arm() const;
	void disarm() const;

private:
	rclcpp::TimerBase::SharedPtr timer_;

	rclcpp::Publisher<OffboardControlMode>::SharedPtr offboard_control_mode_publisher_;
	rclcpp::Publisher<TrajectorySetpoint>::SharedPtr trajectory_setpoint_publisher_;
	rclcpp::Publisher<VehicleCommand>::SharedPtr vehicle_command_publisher_;
	rclcpp::Subscription<px4_msgs::msg::Timesync>::SharedPtr timesync_sub_;

	std::atomic<uint64_t> timestamp_;   //!< common synced timestamped

	uint64_t offboard_setpoint_counter_;   //!< counter for the number of setpoints sent

	void publish_offboard_control_mode() const;
	void publish_trajectory_setpoint() const;
	void publish_vehicle_command(uint16_t command, float param1 = 0.0,
				     float param2 = 0.0) const;
};

/**
 * @brief Send a command to Arm the vehicle
 */
void OffboardControl::arm() const {
	publish_vehicle_command(VehicleCommand::VEHICLE_CMD_COMPONENT_ARM_DISARM, 1.0);

	RCLCPP_INFO(this->get_logger(), "Arm command send");
}

/**
 * @brief Send a command to Disarm the vehicle
 */
void OffboardControl::disarm() const {
	publish_vehicle_command(VehicleCommand::VEHICLE_CMD_COMPONENT_ARM_DISARM, 0.0);

	RCLCPP_INFO(this->get_logger(), "Disarm command send");
}

/**
 * @brief Publish the offboard control mode.
 *        For this example, only position and altitude controls are active.
 */
void OffboardControl::publish_offboard_control_mode() const {
	OffboardControlMode msg{};
	msg.timestamp = timestamp_.load();
	msg.position.data = true;
	msg.velocity.data = false;
	msg.acceleration.data = false;
	msg.attitude.data = false;
	msg.body_rate.data = false;

	offboard_control_mode_publisher_->publish(msg);
}


/**
 * @brief Publish a trajectory setpoint
 *        For this example, it sends a trajectory setpoint to make the
 *        vehicle hover at 5 meters with a yaw angle of 180 degrees.
 */
void OffboardControl::publish_trajectory_setpoint() const {
	TrajectorySetpoint msg{};
	msg.timestamp = timestamp_.load();
	msg.x = 0.0;
	msg.y = 0.0;
	msg.z = -5.0;
	msg.yaw = -3.14; // [-PI:PI]

	trajectory_setpoint_publisher_->publish(msg);
}

/**
 * @brief Publish vehicle commands
 * @param command   Command code (matches VehicleCommand and MAVLink MAV_CMD codes)
 * @param param1    Command parameter 1
 * @param param2    Command parameter 2
 */
void OffboardControl::publish_vehicle_command(uint16_t command, float param1,
					      float param2) const {
	VehicleCommand msg{};
	msg.timestamp = timestamp_.load();
	msg.param1 = param1;
	msg.param2 = param2;
	msg.command = command;
	msg.target_system = 1;
	msg.target_component = 1;
	msg.source_system = 1;
	msg.source_component = 1;
	msg.from_external = true;

	vehicle_command_publisher_->publish(msg);
}

int main(int argc, char* argv[]) {
	std::cout << "Starting offboard control node..." << std::endl;
	setvbuf(stdout, NULL, _IONBF, BUFSIZ);
	rclcpp::init(argc, argv);
	rclcpp::spin(std::make_shared<OffboardControl>());

	rclcpp::shutdown();
	return 0;
}