Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem importing custom ROS msgs

Discussion in 'Robotics' started by vipulsareen660, Oct 19, 2023.

  1. vipulsareen660

    vipulsareen660

    Joined:
    May 15, 2023
    Posts:
    2
    i have imported and built the message package through ros unity tcp and it was fine . I established the unity ros connection it was working fine i created a publisher in ros which is first subscribing the data from the px4 then publishing it to the different node .I created the subscription node in unity to subscribe the data but when i play the scene its shows the "error =
    SysCommand.subscribe - Unknown message class 'px4_msgs/SensorCombined'
    UnityEngine.Debug:LogError (object)
    Unity.Robotics.ROSTCPConnector.ROSConnection:ReceiveSysCommand (string,string) (at ./Library/PackageCache/com.unity.robotics.ros-tcp-connector@3288e188a2/Runtime/TcpConnector/ROSConnection.cs:681)
    Unity.Robotics.ROSTCPConnector.ROSConnection:Update () (at ./Library/PackageCache/com.unity.robotics.ros-tcp-connector@3288e188a2/Runtime/TcpConnector/ROSConnection.cs:588)"
    I am attaching both publisher and subscriber file .Can someone help me to solve this problem



    subscriber node which I have made in unity:


    using UnityEngine;


    using Unity.Robotics.ROSTCPConnector;
    using RosPos = RosMessageTypes.Px4.SensorCombinedMsg;

    public class RosSub : MonoBehaviour
    {
    public GameObject cube;
    void Start()
    {
    ROSConnection.GetOrCreateInstance().Subscribe<RosPos>("data", PosChange);
    }

    void PosChange(RosPos posMessage)
    {
    // Handle received sensor data here
    float gyroX = posMessage.gyro_rad[0];
    float gyroY = posMessage.gyro_rad[1];
    float gyroZ = posMessage.gyro_rad[2];

    // Example: Rotate the object based on gyro data
    transform.Rotate(new Vector3(gyroX, gyroY, gyroZ) * Time.deltaTime);
    }
    }



    subscribe and publisher node created in ros which is subscribing the px4 data and publishing that :



    #include <rclcpp/rclcpp.hpp>
    #include <px4_msgs/msg/sensor_combined.hpp>

    /**
    * @brief Sensor Combined uORB topic data callback
    */
    class SensorCombinedListener : public rclcpp::Node
    {
    public:
    explicit SensorCombinedListener() : Node("sensor_combined_listener")
    {
    rmw_qos_profile_t qos_profile = rmw_qos_profile_sensor_data;
    auto qos = rclcpp::QoS(rclcpp::QoSInitialization(qos_profile.history, 5), qos_profile);

    subscription_ = this->create_subscription<px4_msgs::msg::SensorCombined>("/fmu/out/sensor_combined", qos,
    [this](const px4_msgs::msg::SensorCombined::UniquePtr msg) {
    std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
    std::cout << "RECEIVED SENSOR COMBINED DATA" << std::endl;
    std::cout << "=============================" << std::endl;
    std::cout << "ts: " << msg->timestamp << std::endl;
    std::cout << "gyro_rad[0]: " << msg->gyro_rad[0] << std::endl;
    std::cout << "gyro_rad[1]: " << msg->gyro_rad[1] << std::endl;
    std::cout << "gyro_rad[2]: " << msg->gyro_rad[2] << std::endl;
    std::cout << "gyro_integral_dt: " << msg->gyro_integral_dt << std::endl;
    std::cout << "accelerometer_timestamp_relative: " << msg->accelerometer_timestamp_relative << std::endl;
    std::cout << "accelerometer_m_s2[0]: " << msg->accelerometer_m_s2[0] << std::endl;
    std::cout << "accelerometer_m_s2[1]: " << msg->accelerometer_m_s2[1] << std::endl;
    std::cout << "accelerometer_m_s2[2]: " << msg->accelerometer_m_s2[2] << std::endl;
    std::cout << "accelerometer_integral_dt: " << msg->accelerometer_integral_dt << std::endl;
    });
    }

    private:
    rclcpp::Subscription<px4_msgs::msg::SensorCombined>::SharedPtr subscription_;

    };

    int main(int argc, char *argv[])
    {
    std::cout << "Starting sensor_combined listener node..." << std::endl;
    setvbuf(stdout, NULL, _IONBF, BUFSIZ);
    rclcpp::init(argc, argv);
    rclcpp::spin(std::make_shared<SensorCombinedListener>());

    rclcpp::shutdown();
    return 0;
    }



    I have imported the same message package which comes with px4 so that i can publish the px4 data to ros2