-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageToROS.cs
159 lines (123 loc) · 4.73 KB
/
ImageToROS.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using UnityEngine;
using Unity.Robotics.ROSTCPConnector;
// using RosMessageTypes.UnityRoboticsDemo;
using RosMessageTypes.Sensor;
using RosMessageTypes.Std;
using RosMessageTypes.BuiltinInterfaces;
using System.Collections;
// credits
// https://forum.unity.com/threads/getrawtexturedata-method-resulting-in-flipped-image-in-rviz.1148723/
/// <summary>
///
/// </summary>
// [RequireComponent(typeof(ROSClockSubscriber))]
public class ImageToROS : MonoBehaviour
{
ROSConnection m_Ros;
public string imageTopic = "/XRCamera";
public string camInfoTopic = "/camera_info";
public string CompressedImageTopic = "/XRCamera_compressed";
public Camera target_camera;
public bool compressed = false;
public float pubMsgFrequency = 60f;
private float timeElapsed;
private RenderTexture renderTexture;
private RenderTexture lastTexture;
private Texture2D mainCameraTexture;
private Rect frame;
private int frame_width;
private int frame_height;
private const int isBigEndian = 0;
private uint image_step = 4;
TimeMsg lastTime;
// string topicName = "XRCamera";
// private ROSClockSubscriber clock;
private ImageMsg img_msg;
private CameraInfoMsg infoCamera;
private HeaderMsg header;
void Start()
{
// start the ROS connection
m_Ros = ROSConnection.GetOrCreateInstance();
// register topic name
// m_Ros.RegisterPublisher<RosMessageTypes.Sensor.ImageMsg>(topicName);
Debug.Log(m_Ros.RosIPAddress);
m_Ros.RegisterPublisher<ImageMsg>(imageTopic);
m_Ros.RegisterPublisher<CompressedImageMsg>(CompressedImageTopic);
m_Ros.RegisterPublisher<CameraInfoMsg>(camInfoTopic);
// if(m_Ros)
// {
// m_Ros.RegisterPublisher<ImageMsg>(imageTopic);
// m_Ros.RegisterPublisher<CompressedImageMsg>(CompressedImageTopic);
// m_Ros.RegisterPublisher<CameraInfoMsg>(camInfoTopic);
// // clock = GetComponent<ROSClockSubscriber>();
// }
// else
// {
// Debug.Log("No ros connection found.");
// }
// if (!target_camera)
// {
// // target_camera = Camera.main;
// Debug.Log("No camera found.");
// }
if (target_camera)
{
// the depth is the key! if 0 - nothing renders ...
// Debug.Log("I arriaved to line 81");
renderTexture = new RenderTexture(target_camera.pixelWidth, target_camera.pixelHeight, 8, UnityEngine.Experimental.Rendering.GraphicsFormat.R8G8B8A8_UNorm); //R8G8B8A8_UNorm
renderTexture.Create();
frame_width = renderTexture.width;
frame_height = renderTexture.height;
frame = new Rect(0, 0, frame_width, frame_height);
mainCameraTexture = new Texture2D(frame_width, frame_height, TextureFormat.RGBA32, false);
header = new HeaderMsg();
img_msg = new ImageMsg();
img_msg.width = (uint) frame_width;
img_msg.height = (uint) frame_height;
img_msg.step = image_step * (uint) frame_width;
img_msg.encoding = "rgba8";
// infoCamera = CameraInfoMsg. CameraInfoGenerator.ConstructCameraInfoMessage(target_camera, header);
}
else
{
Debug.Log("No camera found.");
}
}
private void Update()
{
if (target_camera)
{
timeElapsed += Time.deltaTime;
if (timeElapsed > (1 / pubMsgFrequency))
{
// header.stamp = clock._time;
// infoCamera.header = new RosMessageTypes.Std.HeaderMsg();
RosMessageTypes.Std.HeaderMsg header_m = new RosMessageTypes.Std.HeaderMsg();
header_m.frame_id = "XRCam";
// img_msg.header = header_m;//new RosMessageTypes.Std.HeaderMsg();
img_msg.data = get_frame_raw();
m_Ros.Publish(imageTopic, img_msg);
// m_Ros.Publish(camInfoTopic, infoCamera);
timeElapsed = 0;
}
}
else
{
Debug.Log("No camera found.");
}
}
private byte[] get_frame_raw()
{
target_camera.targetTexture = renderTexture;
lastTexture = RenderTexture.active;
RenderTexture.active = renderTexture;
// target_camera.Render();
target_camera.Render(); //WithShader(Shader.Find("Standard"));
mainCameraTexture.ReadPixels(frame, 0, 0);
mainCameraTexture.Apply();
target_camera.targetTexture = lastTexture;
target_camera.targetTexture = null;
return mainCameraTexture.GetRawTextureData();
}
}