Communication problem between MATLAB and Unity using ROS Toolbox.
18 views (last 30 days)
Show older comments
ROSToolbox is used to communicate between MATLAB(R2023b) and Unity(2022.3.10f1). However, when sending data from MATLAB to Unity, data is not sent for about two seconds from the start of communication.
When I check, it seems that the data is sent from MATLAB to Unity, but Unity side is not receiving the data.
Please let me know if you can solve this problem.
I am attaching the MATLAB and Unity code for your reference.
MATLAB code
%Create a ROS subscriber
poseSub = rossubscriber('/pose','geometry_msgs/PoseArray');
%Send multiple numerical data
arrayPub = rospublisher("/array_data", "std_msgs/Float32MultiArray");
for i = 1:3000
poseArray = receive(poseSub);
position = poseArray.Poses(1).Position;
posx = poseArray.Poses(1).Position.X;
posy = poseArray.Poses(1).Position.Y;
posz = poseArray.Poses(1).Position.Z;
controlSignal = posx*2;
number = 0;
numericData = controlSignal
% Summarize data as an array
dataArray = single([controlSignal, number]);
% Create ROS message
arrayMsg = rosmessage('std_msgs/Float32MultiArray');
arrayMsg.Data = dataArray;
% Send a message
send(arrayPub, arrayMsg);
end
Unity code (C#)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Robotics.ROSTCPConnector;
using RosMessageTypes.Geometry;
using RosMessageTypes.Std;
using Unity.Robotics.ROSTCPConnector.ROSGeometry;
public class RosSubPub : MonoBehaviour
{
//Publish related
ROSConnection ros;
public string topicName = "pose";
//Sbscribe related
public float num;
public float input_num;
// Start is called before the first frame update
void Start()
{
//Publish related
ros = ROSConnection.GetOrCreateInstance();
ros.RegisterPublisher<PoseArrayMsg>(topicName);
//Sbscribe related to receive multiple pieces of data
ros.Subscribe<Float32MultiArrayMsg>("array_data",NumericDataReceived);//数値データ複数取得
}
//sbscribe related (receive multiple data)
void NumericDataReceived(Float32MultiArrayMsg numericArrayMessage)
{
float[] receivedData = numericArrayMessage.data;
if (receivedData != null && receivedData.Length >= 2)
{
num = receivedData[0];
float receivedNumber = receivedData[1];
}
}
}
0 Comments
Answers (0)
See Also
Categories
Find more on Publishers and Subscribers in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!