Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

joint state publisher

Discussion in 'Robotics' started by abdurrahimsemiz22, Nov 15, 2021.

  1. abdurrahimsemiz22

    abdurrahimsemiz22

    Joined:
    Oct 4, 2021
    Posts:
    1
    Hello everyone,

    my question is how can i create a joint state subscriber or publisher like ros# script ? ı want to control a robot arm via rviz with moviet.

    ı upload a photo of ros# script for subs to joints.

    thanks. joint_publisher_script.png
     
  2. LaurieUnity

    LaurieUnity

    Unity Technologies

    Joined:
    Oct 23, 2020
    Posts:
    77
  3. medinafb01

    medinafb01

    Joined:
    Feb 1, 2021
    Posts:
    14
    Hi, I am also working with the same robot.
    I have created this. It works perfectly.
    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Robotics.ROSTCPConnector;
    3. using SensorUnity = RosMessageTypes.Sensor.JointStateMsg;
    4. using System.Collections;
    5.  
    6. public class JointStateSub : MonoBehaviour
    7. {
    8.     [SerializeField] private string rosTopic = "joint_states";
    9.     [SerializeField] private ROSConnection ROS;
    10.     [SerializeField] private ArticulationBody[] robotJoints = new ArticulationBody[9];
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         ROS = ROSConnection.GetOrCreateInstance();
    15.         ROS.Subscribe<SensorUnity>(rosTopic, GetJointPositions);
    16.     }
    17.  
    18.     private void GetJointPositions(SensorUnity sensorMsg)
    19.     {
    20.         StartCoroutine(SetJointValues(sensorMsg));
    21.     }
    22.     IEnumerator SetJointValues(SensorUnity message)
    23.     {
    24.         for (int i = 0; i < message.name.Length; i++)
    25.         {
    26.             var joint1XDrive = robotJoints[i].xDrive;
    27.             joint1XDrive.target = (float)(message.position[i]) * Mathf.Rad2Deg;
    28.             robotJoints[i].xDrive = joint1XDrive;
    29.             Debug.Log(joint1XDrive.target);
    30.         }
    31.  
    32.         yield return new WaitForSeconds(0.5f);
    33.     }
    34.  
    35.     public void UnSub()
    36.     {
    37.         ROS.Unsubscribe(rosTopic);
    38.     }
    39. }
     
    petpetpeter likes this.
  4. vermahrithik10

    vermahrithik10

    Joined:
    Jan 10, 2022
    Posts:
    12
    @medinafb01 Do you know how to use joint velocity controller?

    At present, I have seen unity only has position controller.
    If anyone knows how to setup joint velocity controller than please let me know.
     
    Last edited: Jan 20, 2022
  5. medinafb01

    medinafb01

    Joined:
    Feb 1, 2021
    Posts:
    14
  6. MHaefs

    MHaefs

    Joined:
    Apr 24, 2020
    Posts:
    1
    @medinafb01 @vermahrithik10 Hello, you two. You basically did the same work i'm doing now and maybe one of you can help me.
    First of all, @medinafb01 thanks for your script, it works really well and i haven't found anyone who did the same thing besides you. I just have one question. Do you know any way to specify how often the joint states position gets updated in unity or something like that?
    For better context: i'm simulating the panda robot on the hololens 2 and i try to use the robotics hub. Since there is really no good documentation, i searched ages for a way to subscribe to the jointstates topic. Then i found your script. I have to say i don't understand it completly, for example: does the Subscribe function you call in Start reguarly call the GetJointPositions function or just once? Before i found your script i thought, that i probably have to update the joint position reguarly in the update function or something like that.
    But back to my main problem. Your Script works fine, but on the hololens, the robots movement looks like it is only moving with 15-20 fps, like really jittery. I don't know how to fix that, in Ros i already specified a 60 fps rate for sending the joint states. Do you have any idea how to fix that? Thanks in advance and if you need any more information, do not hesitate to ask!