0
$\begingroup$

Rosanswers logo

Hi,

How do I code the function toRawMessage when I implement geometry_msgs.Vector3 ?

Currently the structure of my class is as shown below. Is there any documentation.

public class Vector3_self implements geometry_msgs.Vector3 {

public double x,y,z;

@Override
public RawMessage toRawMessage() {
    
       // TODO Auto-generated method stub
    return null;
}

@Override
public double getX() {
    
    
    // TODO Auto-generated method stub
    return x;
}

But when I try to publish an Imu value using this class, I get the null exception error:

Code:

protected void loop() throws InterruptedException {

  sensor_msgs.Imu imu_accel = publisher.newMessage();
    
      
     accel_values.setX( 5.0 ); // [ where accel_value is  defined as public  Vector3_selg accel_values = new Vector3_self() ];  
     accel_values.setY( 6.0 );
     accel_values.setZ( 7.0 );
         
     imu_accel.setLinearAcceleration(accel_values);
     publisher.publish(imu_accel);


     sequenceNumber += 10;
     Thread.sleep(1000);
  }

Error:

Exception in thread "pool-1-thread-6" java.lang.NullPointerException at org.ros.internal.message.ValueField.getSerializedSize(ValueField.java:76) at org.ros.internal.message.MessageImpl.getSerializedSize(MessageImpl.java:452) at org.ros.internal.message.MessageImpl.serialize(MessageImpl.java:459) at org.ros.internal.message.DefaultMessageSerializer.serialize(DefaultMessageSerializer.java:30) at org.ros.internal.message.DefaultMessageSerializer.serialize(DefaultMessageSerializer.java:26) at org.ros.internal.transport.OutgoingMessageQueue.writeMessageToChannel(OutgoingMessageQueue.java:79) at org.ros.internal.transport.OutgoingMessageQueue.access$100(OutgoingMessageQueue.java:38) at org.ros.internal.transport.OutgoingMessageQueue$Writer.loop(OutgoingMessageQueue.java:56) at org.ros.concurrent.CancellableLoop.run(CancellableLoop.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:679)

Thank you.


Originally posted by ss_robotics on ROS Answers with karma: 75 on 2012-09-04

Post score: 0

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

Why are you trying to manually implement a message? Everything is generated automatically and you simply need to use a message factory to get an instance. See the rosjava docs: http://docs.rosjava.googlecode.com/hg/rosjava_core/html/index.html


Originally posted by damonkohler with karma: 3838 on 2012-09-12

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by Pickman on 2016-08-06:
This links now leads to a missing page. Where could I find the new documentation?

$\endgroup$
0
$\begingroup$

Rosanswers logo

I also first tried to implement a geometry_msgs.Vector3, which seemed an intuitive solution at the time. And implementing a Vector3 led me to wonder about implementing a RawMessage. And that led me here, which provided enough of a hint to create the following solution for rosjava 0.1.6.

/*
 * Initialization within onStart()
 */ 

String pubTopic = "/turtle1/cmd_vel";
String pubType = Twist._TYPE;
final Publisher<Twist> publisher =
    connectedNode.newPublisher(pubTopic, pubType);

/*
 * To create and publish each message (e.g., inside a loop)
 */

Twist cmd = publisher.newMessage();

Vector3 linear = cmd.getLinear();
linear.setX(1);
linear.setY(0);
linear.setZ(0);

Vector3 angular = cmd.getAngular();
angular.setX(0);
angular.setY(0);
angular.setZ(1);

publisher.publish(cmd);

Originally posted by bradknox with karma: 101 on 2014-02-17

This answer was NOT ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.