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

Need help with writing c++ code for NXT

asked 2012-11-29 13:22:16 -0500

robof83 gravatar image

updated 2014-01-28 17:14:26 -0500

ngrennan gravatar image

I have an NXT robot that I've been (unsuccessfully trying to control)for the past few months. I went through all of the on-site basic tutorials, as well as the NXT tutorials. Unfortunately, I still feel as though I have very little idea as to what's actually going on. The NXT tutorials cover the robot description, but they stop there, and don't actually go on to cover programming the robot.

I've been trying to write c++ code to make a simple talker/listener for each of the sensors, but at this point it feels like I'm somewhat stabbing in the dark. I'm trying to follow the simple publisher/subscriber example in the tutorials, but it's just too different and doesn't really apply to what I'm trying to do. Currently I have this written

#include <ros/ros.h>
#include <nxt_msgs/Contact.h>

int main(int argc, char **argv)
    ros::init(argc, argv, "touch_sensor_talker");

    ros::NodeHandle n;
    ros::Publisher pub = n.advertise<nxt_msgs::Contact>("touch_sensor", 1);
    ros::Rate loop_rate(10);

    nxt_msgs::Contact c;
    c = Contact();
    test =



    return 0;

but it doesn't work. I've looked into the Contact.h file, but it's fairly difficult to understand what's going on in there. How do I create a Contact object and then publish that onto the touch_sensor topic? How then would I create a simple subscriber/listener?

I haven't really decided on an ultimate goal as of now; my short term goal is to just get simple talker/listeners up and running for the touch sensor, color sensor, and distance sensor.

I have been able to simulate automated movement by publishing onto the cmd_vel topic, but I did that by modifying per-existing nxt_teleop.cpp code, and not by writing it from scratch.

edit retag flag offensive close merge delete


I'm a bit puzzled as to why you'd want to write a 'talker' for the sensors: ROS doesn't run 'on' the robot, all the ros_nxt pkgs do is let you communicate with the brick, which runs some proxies and transmits sensor data to the PC (via bluetooth or usb cable). Your PC runs ROS, not the brick.

ipso gravatar image ipso  ( 2012-12-02 20:43:24 -0500 )edit

have you got you code working? can you share it?

Harry gravatar image Harry  ( 2013-01-23 04:01:40 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2012-11-30 10:56:57 -0500

allenh1 gravatar image

As far as I can tell, is an integer. I recommend you try: = 1;

and publish it. If that doesn't work, let's pick at it to see what it is. Try:

int test =; 
ROS_INFO("Contact value: %i", test);

and paste the output for us.

Another thing to consider: Most NXT launch files should provide the sensor data for you. Most would write a listener, then a publisher for motor commands.

edit flag offensive delete link more

Question Tools


Asked: 2012-11-29 13:22:16 -0500

Seen: 495 times

Last updated: Nov 30 '12