Ros2 for Unity create custom msg
Hi all,
I'm trying to add a custom msg to my ROS2 for Unity on Windows. From what I understand, I need to follow this. Everything is fine till I arrive at the point of modifying the custom_messages.repos
file in order to get my custom msg from get_repos.ps1
.
I don't know how to write this file in fact, it requires some URL and other parameters. But in my case, I don't have an online repo because as said here, I should put my custom msg pkg into src/ros2
sub-folder. Obviously every time I run the build.ps1
, I encountered some errors related to my msg.
After building ros2-for-Unity pkg and importing it into the project, I get this error in Unity: error CS1061: 'Teleop' does not contain a definition for 'ang' and no accessible extension method 'ang' accepting a first argument of type 'Teleop' could be found (are you missing a using directive or an assembly reference?)
Remember that ros2-for-unity is a plugin so I can't do ros2 topic list
from it. To see if the topic is active I need to launch the command from the normal ros2_ws
, and I can see my msg sent correctly.
This is my custom msg that builds in the normal ws:
std_msgs/Header header
# current position
geometry_msgs/Vector3[2] pose # meters
# current angles
geometry_msgs/Vector3[2] ang # rad
This is my Unity subscriber:
namespace ROS2 {
public class RCM2Omni : MonoBehaviour
{
private ROS2UnityComponent ros2Unity;
private ROS2Node ros2Node;
private ISubscription<my_msgs.msg.Teleop> Omni2;
private float[] transformations = new float[3];
bool moving = false;
void Start()
{
ros2Unity = GetComponent<ROS2UnityComponent>();
}
void Update()
{
if (ros2Node == null && ros2Unity.Ok())
{
ros2Node = ros2Unity.CreateNode("RCM2Omni");
Omni2 = ros2Node.CreateSubscription<my_msgs.msg.Teleop>(
"/Teleop", msg => { parse(msg); });
}
if (moving)
{
transform.Rotate(transformations[0], 0, 0);
moving = false;
}
}
public void parse(my_msgs.msg.Teleop msg)
{
transformations[0] = (float)msg.ang[1].x;
moving = true;
}
}
}
Has anyone already had this problem?
Is the line with
transformations[0] = (float)msg.ang[1].x;
in theparse(my_msgs.msg.Teleop msg)
correct? In the examples I can seeso maybe try
msg.Data.ang[1]
or similar?Even with
msg.Data
I receive: 'Teleop' does not contain a definition for 'Data' .. Instead, with onlymsg
, I get the result in the console, but it's only the msg object, and I don't know if it has the elements or how to access it.I think you can check it from ground up - I would start with this example: https://github.com/RobotecAI/ros2cs/b..., check if it works with
std_msgs.msg.String
, then add your custom message and check with this simplest possible ROS 2 Listener if your message is heard correctly. I don't see e.g.Ros2cs.Init();
in your code, that is there in the example listener code.