Use joint_states to publish messages to control the robotic arm - ROS robotic arm study notes (2)

To realize the movement of the manipulator without using moveit, the current idea is to add information about joints to the topic joint_states, or to publish topic messages on the terminal. Of course, you can also use the built-in joint_states_publisher GUI plug-in. But after some tests in the past few days, I found that all three methods will have problems. For example, if you run the node or move the slider on the GUI, it turns out that the model in rviz does not move. This problem has been very tangled, I don't know if it is rviz problem.

A topic message published on the terminal

 rostopic pub -1 /joint_states sensor_msgs/JointState '{header: auto, name: ['joint1', 'joint2', 'joint3'], position: [1, 0.5418, -1], velocity: [], effort: []}'

Below is the corresponding code.

#include<iostream>
#include <string>
#include <ros/ros.h>
#include <sensor_msgs/JointState.h>
#include <robot_state_publisher/robot_state_publisher.h>
using namespace std;

int main(int argc, char** argv)
 {
    ros::init(argc, argv, "state_1_publisher"); //节点的名称
    ros::NodeHandle n;
    ros::Publisher joint_pub = n.advertise<sensor_msgs::JointState>("joint_states", 1);    //设置一个发布者,将消息发布出去,发布到相应的节点中去
    ros::Rate loop_rate(10);    //这个设置的太大,效果很不好,目前觉得为10最好了
    const double degree = M_PI/180;
    const double radius = 2;
    int i=-69;

    // robot state
    double angle= 0;
    // message declarations
    sensor_msgs::JointState joint_state;
  //  for(int j=0;j<6;++j)
   // {
        //joint_state.velocity.push_back(1);
        //joint_state.effort.push_back(200);
    //}



    while (ros::ok()) {
        //update joint_state
        joint_state.header.stamp = ros::Time::now();
        joint_state.name.resize(6);
        joint_state.position.resize(6);
    joint_state.name[0]="joint1";
        joint_state.position[0] = i*degree;
    joint_state.name[1] ="joint2";
        joint_state.position[1] = i*degree;
    joint_state.name[2] ="joint3";
        joint_state.position[2] = i*degree;
        joint_state.name[3] ="joint4";
        joint_state.position[3] = i*degree;
    joint_state.name[4] ="joint5";
        joint_state.position[4] = i*degree;
    joint_state.name[5] ="joint6";
        joint_state.position[5] = i*degree;
        if(i<=70)
            i++;
        else
            i=-69; 

        //send the joint state and transform
        joint_pub.publish(joint_state);

        // This will adjust as needed per iteration
        loop_rate.sleep();
    }

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324421419&siteId=291194637