Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. 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