Rosanswers logo

Hello, I'm Furuike.

I made a program that control turtlebot. But the program does not work although its publications, subscriptions, services and connections are the same as keyop (package : kobuki_keyop file name : keyop.launch).
Could you tell me what is the problem with my program?

This is my program.

 #include <ros/ros.h> 
 #include <geometry_msgs/Twist.h> 
 #include <kobuki_msgs/MotorPower.h>
 #define  UP            0x48    /* ↑ */ 
 #define  DOWN          0x50    /* ↓ */
 #define  LEFT          0x4D    /* → */ 
 #define  RIGHT         0x4B    /* ← */
int main(int argc, char** argv){ 
  ros::init(argc, argv, "key"); 
  ros::NodeHandle n1; 
  ros::NodeHandle n2; 
  ros::Publisher velocity_pub = n1.advertise<geometry_msgs::Twist>("/mobile_base/commands/velocity", 3);; 
  ros::Publisher motor_pub = n2.advertise<kobuki_msgs::MotorPower>("/mobile_base/commands/motor_power", 1); 
  int i; 
  while (ros::ok()) 
      geometry_msgs::Twist msg_velocity; 
      kobuki_msgs::MotorPower msg_motor; 
      char ss_velocity; 
      int ss_motor; 
    ss_motor = 1;
    msg_motor.state = ss_motor; 
    ss_motor = 0; 
    msg_motor.state = ss_motor; 
      if(ss_motor == 1){ 
    case UP :  msg_velocity.linear.x += 0.05;  break; 
    case DOWN : msg_velocity.linear.x -= 0.05; break; 
    case RIGHT : msg_velocity.angular.z += 0.33; break; 
    case LEFT : msg_velocity.angular.z -= 0.33; break; 
    return 0; 
  return 0; 

And this is the information.

Node [/key] 
 * /rosout [rosgraph_msgs/Log]
 * /mobile_base/commands/velocity [geometry_msgs/Twist] 
 * /mobile_base/commands/motor_power [kobuki_msgs/MotorPower] 
 * /clock [rosgraph_msgs/Clock] 
 * /key/set_logger_level 
 * /key/get_loggers 
contacting node http://IMI-T400s:60686/ ... 
Pid: 16016 
 * topic: /rosout 
    * to: /rosout 
    * direction: outbound 
    * transport: TCPROS 
 * topic: /mobile_base/commands/velocity 
    * to: /gazebo 
    * direction: outbound 
    * transport: TCPROS 
 * topic: /mobile_base/commands/motor_power
    * to: /gazebo 
    * direction: outbound 
    * transport: TCPROS 
 * topic: /clock 
    * to: /gazebo (http://IMI-T400s:48512/) 
    * direction: inbound 
    * transport: TCPROS 

Originally posted by Kaori Furuike on ROS Answers with karma: 21 on 2014-10-30

Post score: 0

Original comments

Comment by dornhege on 2014-10-30:
Can you post the code without any <br> symbols? Also what exactly does not work?

Comment by Andromeda on 2014-10-30:
if you do:

rostopic echo /mobile_base/commands/velocity
rostopic echo /mobile_base/commands/motor_power

do you get something on your console?

Comment by Kaori Furuike on 2014-10-30:
I'm sorry. I didn't notice that it start a new line without br if I choose preformatted text.

Comment by Kaori Furuike on 2014-10-30:
It return the well formatted reaction if I push two times when I do rostopic echo.


1 Answer 1


Rosanswers logo

Your code isn't well-formatted, so it's pretty difficult to read and understand, but my best guess is that you aren't doing the keyboard I/O properly. I suspect there's nothing wrong with the ROS parts of your code.

You can test if you have the I/O wrong by trying to use the q command to quit. If you push q by itself, I suspect nothing will happen. If you push q 2-3 times and then push enter, I suspect your program will exit.

The trouble here is that, by default, terminal input is line-buffered and "cooked", meaning that your program doesn't get any input until the user has entered an entire line and pushed enter, and that the terminal will strip or reinterpret nonprinting characters such as arrow keys and the control key.

There are lots of good resources for understanding non-blocking terminal input. You can either grab your favorite systems programming book, or consult Google and StackOverflow. This StackOverflow question seems to do a good job of describing the basics.

And don't forget to have your program put your terminal back the way was on exit. The terminal device is shared between all process that run in that shell, and programs like ssh will fail in strange and unusual ways if you leave the terminal in non-blocking mode. (The stty sane will also put it back to normal).

Originally posted by ahendrix with karma: 47576 on 2014-10-30

This answer was ACCEPTED on the original site

Post score: 2

Original comments

Comment by Kaori Furuike on 2014-10-30:
You're right. The program exit if I push q 2-3 times and then push enter.
I'll study about the terminal input. Thank you for your help. I will post the result after I rewrite the program.

Comment by Kaori Furuike on 2014-11-03:
Special thanks to you!! I corrected the part you pointed out (and the code of the arrow key) and finally succeeded.


Your Answer

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