Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ros2+px4 rover offboard mode #24398

Open
samirertt opened this issue Feb 22, 2025 · 0 comments
Open

Ros2+px4 rover offboard mode #24398

samirertt opened this issue Feb 22, 2025 · 0 comments

Comments

@samirertt
Copy link

Describe the bug

so am trying to use the rover r1 and trying to use it in offboard mode first i tried using px4_msgs::msg::TrajectorySetpoint but this msg is not for rover i think its for copter so i started using px4_msgs::msg::VehicleThrustSetpoint + px4_msgs::msg::VehicleTorqueSetpoint to be able to move the rover but am not able to move the rover.
my code is this what am i doing wrong ?

To Reproduce

i have used this example

`/****************************************************************************
*

  • 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.
    1. 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.
    1. 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.

****************************************************************************/

/**

#include <px4_msgs/msg/offboard_control_mode.hpp>
#include <px4_msgs/msg/trajectory_setpoint.hpp>
#include <px4_msgs/msg/vehicle_thrust_setpoint.hpp>
#include <px4_msgs/msg/vehicle_torque_setpoint.hpp>
#include <px4_msgs/msg/vehicle_command.hpp>
#include <px4_msgs/msg/vehicle_control_mode.hpp>
#include <rclcpp/rclcpp.hpp>
#include <stdint.h>

#include
#include

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

class OffboardControl : public rclcpp::Node
{
public:
OffboardControl() : Node("offboard_control")
{

	offboard_control_mode_publisher_ = this->create_publisher<OffboardControlMode>("/fmu/in/offboard_control_mode", 10);
	vehicle_thrust_setpoint_publisher_ = this->create_publisher<VehicleThrustSetpoint>("/fmu/in/vehicle_thrust_setpoint", 10);
	vehicle_torque_setpoint_publisher_ = this->create_publisher<VehicleTorqueSetpoint>("/fmu/in/vehicle_torque_setpoint", 10);
	vehicle_command_publisher_ = this->create_publisher<VehicleCommand>("/fmu/in/vehicle_command", 10);

	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_vehicle_thrust_setpoint();
		publish_vehicle_torque_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();
void disarm();

private:
rclcpp::TimerBase::SharedPtr timer_;

rclcpp::Publisher<OffboardControlMode>::SharedPtr offboard_control_mode_publisher_;
rclcpp::Publisher<VehicleCommand>::SharedPtr vehicle_command_publisher_;
rclcpp::Publisher<VehicleThrustSetpoint>::SharedPtr vehicle_thrust_setpoint_publisher_;
rclcpp::Publisher<VehicleTorqueSetpoint>::SharedPtr vehicle_torque_setpoint_publisher_;



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();

//void publish_trajectory_setpoint();
void publish_vehicle_thrust_setpoint();
void publish_vehicle_torque_setpoint();
void publish_vehicle_command(uint16_t command, float param1 = 0.0, float param2 = 0.0);

};

/**

  • @brief Send a command to Arm the vehicle
    */
    void OffboardControl::arm()
    {
    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()
    {
    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()
{
OffboardControlMode msg{};
msg.position = false;
msg.velocity = false;
msg.acceleration = true;
msg.attitude = false;
msg.body_rate = false;
msg.timestamp = this->get_clock()->now().nanoseconds() / 1000;
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()
{
TrajectorySetpoint msg{};
msg.position = {5.0, 0.0, 0.0}; // Move 5m forward, 3m right, stay on ground
msg.yaw = 1.57; // Face right (90 degrees)
msg.timestamp = this->get_clock()->now().nanoseconds() / 1000;
trajectory_setpoint_publisher_->publish(msg);
}
*/

void OffboardControl::publish_vehicle_thrust_setpoint()
{
VehicleThrustSetpoint msg{};
msg.xyz = {1.0, 0.0, 0.0};
msg.timestamp = this->get_clock()->now().nanoseconds() / 1000;
vehicle_thrust_setpoint_publisher_->publish(msg);
}

void OffboardControl::publish_vehicle_torque_setpoint()
{
VehicleTorqueSetpoint msg{};
msg.xyz = {0.0, 0.0, 1.0};
msg.timestamp = this->get_clock()->now().nanoseconds() / 1000;
vehicle_torque_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)
    {
    VehicleCommand msg{};
    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;
    msg.timestamp = this->get_clock()->now().nanoseconds() / 1000;
    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());

rclcpp::shutdown();
return 0;

}`

Expected behavior

any movement

Screenshot / Media

No response

Flight Log

No response

Software Version

latest version of px4 16beta

Flight controller

No response

Vehicle type

Rover

How are the different components wired up (including port information)

No response

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant