Robotics StackExchange | Archived questions

How to instantaneously send the values of a trajectory using the ruckig trajectory generation library

I am using ROS2 with C++ along the Ruckig Library to generate a trajectory to simulate a robotic arm with CoppliaSim. I want to publish to a topic every set of motor values generated at each iteration.

There is a Ruckig control loop that generates joint values, you can find the code below, I want to break the while loop to send every value computed at once (not wait till the end of the loop to send them all) I tried to replace the while statement with an if statement so that the publisher sends value progressively. But the ruckig library gave me errors

Can anyone suggest how I can break the while loop? Thank you!

here is the code:

 using namespace std::chrono_literals;
 using namespace ruckig;
 using Transform = geometry_msgs::msg::Transform;
 using milliseconds= std::chrono::duration <int, std::milli>;
 class URTrajectory : public rclcpp::Node 
 {
 public:
     URTrajectory() : Node("ur_trajectory") {
         cmd_publisher_ = create_publisher<Transform>(
             "/joint_intermediate_positions", 1000);   ////publishes the positions to coppelia

         using namespace std::chrono_literals;
         run_timer_ = create_wall_timer(2000ms, [this] { this->timer_callback(); });
}

void timer_callback() {
    // Create instances: the Ruckig OTG as well as input and output parameters
    Ruckig<3> otg {0.1};  // control cycle
    InputParameter<3> input;
    OutputParameter<3> output;

    // Set input parameters
    input.current_position = {0.0, 0.0, 0.0};
    input.current_velocity = {0.0, 0.0, 0.0};
    input.current_acceleration = {0.0, 0.0, 0.0};

    input.target_position = {3.14/2, 3.14/4, 3.14/3};
    input.target_velocity = {0.0, 0.0, 0.0};
    input.target_acceleration = {0.0, 0.0, 0.0};

    input.max_velocity = {2.09, 2.09, 3.14};
    input.max_acceleration = {5.2, 5.2, 5.2};
    input.max_jerk = {1, 1, 1};

    // Generate the trajectory within the control loop
    RCLCPP_INFO(this->get_logger(), "Publishing: t | p1 | p2 | p3 ");

 //// HERE IS THE LOOP THAT I CANNOT BREAK //////
    while (otg.update(input, output) == Result::Working) {
        auto& p = output.new_position

        RCLCPP_INFO(this->get_logger(), "Publishing: p1: %f, p2: %f, p3: %f", 
                                        p[0], 
                                        p[1], 
                                        p[2]);

        auto cmd_ = geometry_msgs::msg::Transform();
        cmd_.translation.x = p[0];      //joint1
        cmd_.translation.y = p[1];      //joint2
        cmd_.translation.z = p[2];      //joint3


        cmd_publisher_->publish(cmd_);


        output.pass_to_input(input);

    }

    std::cout << "Trajectory duration: " << output.trajectory.get_duration() << " [s]." << std::endl;
    rclcpp::shutdown();
}

 private:

rclcpp::Publisher<Transform>::SharedPtr cmd_publisher_;
rclcpp::TimerBase::SharedPtr run_timer_;

 };

 int main(int argc, char* argv[]) {
     rclcpp::init(argc, argv);

     auto ur_trajectory = std::make_shared<URTrajectory>();

     rclcpp::spin(ur_trajectory);

     rclcpp::shutdown();

     return 0;
  }

Asked by food_is_awesome on 2022-06-10 04:43:24 UTC

Comments

Answers