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

how to send a message?

asked 2014-10-30 09:16:42 -0600

Kaori Furuike gravatar image

updated 2014-10-30 22:05:49 -0600

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; 
      if(getchar()=='e'){
    ss_motor = 1;
    msg_motor.state = ss_motor; 
    motor_pub.publish(msg_motor); 
      } 
      if(getchar()=='d'){ 
    ss_motor = 0; 
    msg_motor.state = ss_motor; 
    msg_velocity.linear.x=0; 
    msg_velocity.angular.x=0; 
    msg_velocity.linear.y=0; 
    msg_velocity.angular.y=0; 
    msg_velocity.linear.z=0; 
    msg_velocity.angular.z=0; 
    motor_pub.publish(msg_motor); 
    velocity_pub.publish(msg_velocity); 
      }  
      if(ss_motor == 1){ 
    switch(getchar()){ 
    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; 
    } 
    velocity_pub.publish(msg_velocity); 
      } 
      if(getchar()=='q'){ 
    return 0; 
      } 
      ros::spinOnce(); 
    } 
  return 0; 
}

And this is the information.

Node [/key] 
Publications: 
 * /rosout [rosgraph_msgs/Log]
 * /mobile_base/commands/velocity [geometry_msgs/Twist] 
 * /mobile_base/commands/motor_power [kobuki_msgs/MotorPower] 
Subscriptions:  
 * /clock [rosgraph_msgs/Clock] 
Services:  
 * /key/set_logger_level 
 * /key/get_loggers 
contacting node http://IMI-T400s:60686/ ... 
Pid: 16016 
Connections: 
 * 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
edit retag flag offensive close merge delete

Comments

Can you post the code without any <br> symbols? Also what exactly does not work?

dornhege gravatar image dornhege  ( 2014-10-30 09:24:38 -0600 )edit

if you do:

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

do you get something on your console?

Andromeda gravatar image Andromeda  ( 2014-10-30 11:29:11 -0600 )edit

I'm sorry. I didn't notice that it start a new line without br if I choose preformatted text.

Kaori Furuike gravatar image Kaori Furuike  ( 2014-10-30 22:11:21 -0600 )edit

It return the well formatted reaction if I push two times when I do rostopic echo.

Kaori Furuike gravatar image Kaori Furuike  ( 2014-10-30 22:18:02 -0600 )edit

1 Answer

Sort by » oldest newest most voted
2

answered 2014-10-30 11:52:17 -0600

ahendrix gravatar image

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).

edit flag offensive delete link more

Comments

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.

Kaori Furuike gravatar image Kaori Furuike  ( 2014-10-30 22:31:33 -0600 )edit

Special thanks to you!! I corrected the part you pointed out (and the code of the arrow key) and finally succeeded.

Kaori Furuike gravatar image Kaori Furuike  ( 2014-11-03 08:59:10 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2014-10-30 09:16:42 -0600

Seen: 2,587 times

Last updated: Oct 30 '14