-
Notifications
You must be signed in to change notification settings - Fork 41
Open
Description
Hi. Thanks very much for putting together this awesome library to connect Unity 3D to ROS. I have enjoyed using it to connect SLAM algorithms to Unity 3D. I had a quick question regarding publishing images. I'm trying to publish an image to ROS bridge every 2 seconds.
Does this use case feel feasible? Any help you can offer would be appreciated.
ROS is currently mentioning that it's missing an op code.
`
using UnityEngine;
using System;
using System.Collections;
using ROSBridgeLib.sensor_msgs;
using ROSBridgeLib.std_msgs;
using ROSBridgeLib;
using System.Text;
public class VideoTexture : MonoBehaviour {
// Use this for initialization
ROSBridgeWebSocketConnection ros;
WebCamTexture webcamTexture;
int count;
DateTime lastFrame;
DateTime camStart;
void Start () {
webcamTexture = new WebCamTexture();
Renderer renderer = GetComponent<Renderer>();
renderer.material.mainTexture = webcamTexture;
webcamTexture.Play();
ros = new ROSBridgeWebSocketConnection ("ws://192.168.2.12", 9090);
ros.AddPublisher(typeof(ImagePublisher));
ros.Connect ();
count = 0;
lastFrame = DateTime.Now;
camStart = DateTime.Now;
}
void OnApplicationQuit() {
if(ros!=null)
ros.Disconnect ();
}
// Update is called once per frame
void Update () {
//get jpeg of current frame ..
Texture2D snap = new Texture2D(webcamTexture.width, webcamTexture.height);
snap.SetPixels(webcamTexture.GetPixels());
snap.Apply();
byte[] data = snap.EncodeToJPG();
string picString = Convert.ToBase64String(data);
//picString = picString.Replace("data:image/jpeg;base64,", "");
// set format
string format = "jpeg";
//How do you get a header message???
// public TimeMsg(int secs, int nsecs)
var now = DateTime.Now;
var timeSpan = now - lastFrame;
var timeSinceStart = now - camStart;
if(timeSpan.Seconds > 2)
{
var timeMessage = new TimeMsg(timeSinceStart.Seconds, timeSinceStart.Milliseconds);
// public HeaderMsg(int seq, TimeMsg stamp, string frame_id)
var headerMessage = new HeaderMsg(count, timeMessage, "camera");
//CompressedImageMsg(HeaderMsg header, string format, byte[] data)
byte[] array = Encoding.ASCII.GetBytes(picString);
var compressedImageMsg = new CompressedImageMsg(headerMessage, format, array);
ros.Publish(ImagePublisher.GetMessageTopic(), compressedImageMsg);
lastFrame = now;
count++;
}
ros.Render ();
}
}
`
Metadata
Metadata
Assignees
Labels
No labels