ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

Here you are an example. I've tested it and it works.

public class Talker implements NodeMain {

  @Override
  public GraphName getDefaultNodeName() {
    return new GraphName("rosjava_tutorial_pubsub/talker");
  }

  @Override
  public void onStart(final Node node) {
    final Publisher<org.ros.message.sensor_msgs.image> publisher =
        node.newPublisher("chatter", "sensor_msgs/Image");
    // This CancellableLoop will be canceled automatically when the Node shuts
    // down.
    node.executeCancellableLoop(new CancellableLoop() {


      @Override
      protected void setup() {

      }

      @Override
      protected void loop() throws InterruptedException {
        org.ros.message.sensor_msgs.Image str = new org.ros.message.sensor_msgs.Image();
        publisher.publish(str);
        Thread.sleep(1000);
      }
    });
  }

  @Override
  public void onShutdown(Node node) {
  }

  @Override
  public void onShutdownComplete(Node node) {
  }
}

Of course you have to make sure that your javabootstrap.jar contains the sensor_msg/Image class. If not, here you can find how to do it. And you also should understand how to fill all the fields of the message. There are many documentation about this. You can start from here:

$ rosmsg show sensor_msgs/Image 
Header header
  uint32 seq
  time stamp
  string frame_id
uint32 height
uint32 width
string encoding
uint8 is_bigendian
uint32 step
uint8[] data

Here you are an example. I've tested it and it works.

public class Talker implements NodeMain {

  @Override
  public GraphName getDefaultNodeName() {
    return new GraphName("rosjava_tutorial_pubsub/talker");
  }

  @Override
  public void onStart(final Node node) {
    final Publisher<org.ros.message.sensor_msgs.image> publisher =
        node.newPublisher("chatter", "sensor_msgs/Image");
    // This CancellableLoop will be canceled automatically when the Node shuts
    // down.
    node.executeCancellableLoop(new CancellableLoop() {


      @Override
      protected void setup() {

      }

      @Override
      protected void loop() throws InterruptedException {
        org.ros.message.sensor_msgs.Image str = new org.ros.message.sensor_msgs.Image();
        publisher.publish(str);
        Thread.sleep(1000);
      }
    });
  }

  @Override
  public void onShutdown(Node node) {
  }

  @Override
  public void onShutdownComplete(Node node) {
  }
}

Of course you have to make sure that your javabootstrap.jar contains the sensor_msg/Image class. If not, here you can find a tutorial about how to do you can add it. And you You also should understand how to fill all the fields of the sensor_msgs/Image message. There are many documentation about this. You can start from here:

$ rosmsg show sensor_msgs/Image 
Header header
  uint32 seq
  time stamp
  string frame_id
uint32 height
uint32 width
string encoding
uint8 is_bigendian
uint32 step
uint8[] data

Here you are an example. I've tested it and it works.

public class Talker implements NodeMain {

  @Override
  public GraphName getDefaultNodeName() {
    return new GraphName("rosjava_tutorial_pubsub/talker");
  }

  @Override
  public void onStart(final Node node) {
    final Publisher<org.ros.message.sensor_msgs.image> publisher =
        node.newPublisher("chatter", "sensor_msgs/Image");
    // This CancellableLoop will be canceled automatically when the Node shuts
    // down.
    node.executeCancellableLoop(new CancellableLoop() {


      @Override
      protected void setup() {

      }

      @Override
      protected void loop() throws InterruptedException {
        org.ros.message.sensor_msgs.Image str = new org.ros.message.sensor_msgs.Image();
        publisher.publish(str);
        Thread.sleep(1000);
      }
    });
  }

  @Override
  public void onShutdown(Node node) {
  }

  @Override
  public void onShutdownComplete(Node node) {
  }
}

Of course you have to make sure that your javabootstrap.jar contains the sensor_msg/Image class. If not, here you can find a tutorial about how you can add it. You also should understand how to fill all the fields of the sensor_msgs/Image message. There are many documentation about this. You can start from here:

$ rosmsg show sensor_msgs/Image 
Header header
  uint32 seq
  time stamp
  string frame_id
uint32 height
uint32 width
string encoding
uint8 is_bigendian
uint32 step
uint8[] data

Some time ago I did a code in c++ that may be useful for you (I may test it tomorrow on rosjava):

    image_msg->header.stamp = ros::Time::now();
    image_msg->height = dwa_data->config.resolution_height;
    image_msg->width = dwa_data->config.resolution_width;
    image_msg->encoding = "rgb8"; //sensor_msgs::image_encodings::MONO16;
    image_msg->step = image_msg->width * 3;
    image_msg->data.resize(image_msg->height * image_msg->step);

    for (int i = 0; i<dwa_data->velocity_distance_map.size() ; i++)
    {
      //ROS_INFO("index %d",i);
      int offset = 3 * i;
      ...
        image_msg->data[offset] = 255;
        image_msg->data[offset + 1] = 0;
        image_msg->data[offset + 2] = i%255;
      ...
    }

Here you are an example. I've tested it and it works.

public class Talker implements NodeMain {

  @Override
  public GraphName getDefaultNodeName() {
    return new GraphName("rosjava_tutorial_pubsub/talker");
  }

  @Override
  public void onStart(final Node node) {
    final Publisher<org.ros.message.sensor_msgs.image> publisher =
        node.newPublisher("chatter", "sensor_msgs/Image");
    // This CancellableLoop will be canceled automatically when the Node shuts
    // down.
    node.executeCancellableLoop(new CancellableLoop() {


      @Override
      protected void setup() {

      }

      @Override
      protected void loop() throws InterruptedException {
        org.ros.message.sensor_msgs.Image str = new org.ros.message.sensor_msgs.Image();
        publisher.publish(str);
        Thread.sleep(1000);
      }
    });
  }

  @Override
  public void onShutdown(Node node) {
  }

  @Override
  public void onShutdownComplete(Node node) {
  }
}

Of course you have to make sure that your javabootstrap.jar contains the sensor_msg/Image class. If not, here you can find a tutorial about how you can add it. You also should understand how to fill all the fields of the sensor_msgs/Image message. There are many documentation about this. You can start from here:

$ rosmsg show sensor_msgs/Image 
Header header
  uint32 seq
  time stamp
  string frame_id
uint32 height
uint32 width
string encoding
uint8 is_bigendian
uint32 step
uint8[] data

Some time ago I did a code in c++ that may be useful for you (I may test it tomorrow on rosjava):

    image_msg->header.stamp = ros::Time::now();
    image_msg->height = dwa_data->config.resolution_height;
    image_msg->width = dwa_data->config.resolution_width;
    image_msg->encoding = "rgb8"; //sensor_msgs::image_encodings::MONO16;
    image_msg->step = image_msg->width * 3;
    image_msg->data.resize(image_msg->height * image_msg->step);

    for (int i = 0; i<dwa_data->velocity_distance_map.size() i<dwa_data->config.resolution_width*dwa_data->config.resolution_height ; i++)
    {
      //ROS_INFO("index %d",i);
      int offset = 3 * i;
      ...
        image_msg->data[offset] = 255;
        image_msg->data[offset + 1] = 0;
        image_msg->data[offset + 2] = i%255;
      ...
    }