0
$\begingroup$

Rosanswers logo

Hello all i have a real robot. using ROS. i have a problem with (Joint State Publisher)


i understand that i should write my own (joint state publisher node). i read the related tutorials but i do not get how to convert encoders' messages to joint state message. i need steps or tutorial for doing that.

thanks in advance.


edit

i test the code in

my code becomes as follows

#!/usr/bin/env python
# Lucas Walter
# make a joint exactly what the command wants it to be- this only works
# for position control.

import rospy

from sensor_msgs.msg import JointState
from std_msgs.msg import Int64


class CommandToJointState:
    def __init__(self):
        self.joint_name = rospy.get_param("lwheel",True)
        self.joint_state = JointState()
        self.joint_state.name.append(self.joint_name)
        self.joint_state.position.append(0.0)
        self.joint_state.velocity.append(0.0)
        self.joint_pub = rospy.Publisher("joint_states", JointState, queue_size=1)
        self.command_sub = rospy.Subscriber("lwheel", Int64,
                                            self.command_callback, queue_size=1)


    def command_callback(self, msg):
        self.joint_state.position[0] = msg.data
        self.joint_state.header.stamp = rospy.Time.now()
        self.joint_pub.publish(self.joint_state)

if __name__ == '__main__':
    rospy.init_node('diffdrive_controller')
    diffdrive_controller = CommandToJointState()
rospy.spin()

but i get error as in the image attachedimage description

i am not familiar with python. all my codes are in C++


Originally posted by marawy_alsakaf on ROS Answers with karma: 101 on 2017-11-21

Post score: 0


Original comments

Comment by lucasw on 2017-11-24:
In get_param the first argument is the name of the parameter, not the value of it- the second value is the value, you probably want get_param("joint_name", "lwheel")

Comment by jayess on 2017-11-24:
@marawy_alsakaf: can you please post a copy and paste of the error instead of a screen shot? Text is searchable while images are not.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

This is a very simple example in python (https://github.com/lucasw/carbot/blob/master/carbot_control/scripts/command_to_joint_state.py) :

import rospy  
from sensor_msgs.msg import JointState
from std_msgs.msg import Float64


class CommandToJointState:
    def __init__(self):
        self.joint_name = rospy.get_param("~joint_name")
        self.joint_state = JointState()
        self.joint_state.name.append(self.joint_name)
        self.joint_state.position.append(0.0)
        self.joint_state.velocity.append(0.0)
        self.joint_pub = rospy.Publisher("joint_states", JointState, queue_size=1)
        self.command_sub = rospy.Subscriber("command", Float64,
                                            self.command_callback, queue_size=1)

    def command_callback(self, msg):
        self.joint_state.position[0] = msg.data
        self.joint_state.header.stamp = rospy.Time.now()
        self.joint_pub.publish(self.joint_state)

if __name__ == '__main__':
    rospy.init_node('command_to_joint_state')
    command_to_joint_state = CommandToJointState()
    rospy.spin()

If for example you were publishing your encoder as a Float64 (replace the 'command' topic with that encoder topic), you would change the following line to have a scale factor that converts the input encoder value to radians:

self.joint_state.position[0] = msg.data

Originally posted by lucasw with karma: 8729 on 2017-11-22

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by Mekateng on 2017-11-23:
Hi Mr. @lucasw I have a 2d laser scanner.I converted from 2d laserscan to 3d pointcloud using servo.But I want to this using dc motor with encoder.I write an arduino and python code. In my arduino code, joint_state is subscribing,In python code, angle is publishing.I guess I have to do the opposite

Comment by Mekateng on 2017-11-23:
Mr. @lucasw My codes in here https://github.com/mekateng/ros-arduino-communication-with-encoder Could you look at it and fix the error I made? I'm very new in ros. I'm very happy if you help me.

Comment by lucasw on 2017-11-23:
Does the code not run? If there is an error message post it here, otherwise describe what the problem is.

Comment by Mekateng on 2017-11-23:
The code is running but the encoder angle does not match the angle from the ROS exactly. so, for example there is an object in my lab ,but it looks like three on rviz in the attachment https://hizliresim.com/4G3VkQ There is a pairing problem. @lucasw Did you control my codes? is there any wrong?

Comment by lucasw on 2017-11-23:
There is a scale factor error somewhere in your conversion math between the radians angle and the encoder value. The easiest/laziest thing to do is to multiply the angle by 3 (or if not 3 exactly, experimentally adjust the fudge factor until it comes out right- maybe it is actually pi?).

Comment by lucasw on 2017-11-23:
And then later track down in your math where the error is introduced and fix it properly there.

Comment by Mekateng on 2017-11-23:
I am sorry but I really do not understand the line I'm going to fix:/ Can you write lines? are you talking about the code in automate.py or the arduino code? @lucasw

Comment by marawy_alsakaf on 2017-11-24:
@lucasw , i saw the example that you post. i edited it to fit my requirements. but i get an error i will edit the post to provide the image of error.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.