Ask Your Question
0

Publishing on a topic using rosserial windows?

asked 2018-03-03 12:22:13 -0500

sdcguy gravatar image

updated 2018-03-04 01:19:56 -0500

gvdhoorn gravatar image

Hi, So I have a ROS running on my Linux machine and I am following the tutorial given here: http://wiki.ros.org/rosserial_windows... The code given in the tutorial looks like this:

// rosserial_hello_world.cpp : Example of sending command velocities from Windows using rosserial
//
#include "stdafx.h"
#include <string>
#include <stdio.h>
#include "ros.h"
#include <geometry_msgs/Twist.h>
#include <windows.h>

using std::string;

int _tmain (int argc, _TCHAR * argv[])
{
  ros::NodeHandle nh;
  char *ros_master = "1.2.3.4";

  printf ("Connecting to server at %s\n", ros_master);
  nh.initNode (ros_master);

  printf ("Advertising cmd_vel message\n");
  geometry_msgs::Twist twist_msg;
  ros::Publisher cmd_vel_pub ("cmd_vel", &twist_msg);
  nh.advertise (cmd_vel_pub);

  printf ("Go robot go!\n");
  while (1)
  {
    twist_msg.linear.x = 5.1;
    twist_msg.linear.y = 0;
    twist_msg.linear.z = 0;
    twist_msg.angular.x = 0;
    twist_msg.angular.y = 0;
    twist_msg.angular.z = -1.8;
    cmd_vel_pub.publish (&twist_msg);

    nh.spinOnce ();
    Sleep (100);
  }

  printf ("All done!\n");
  return 0;
}

I want to publish a message type Float32 on a topic name "/truevision/throttle_cmd" what changes do I need to make? This is what my code looks like:

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <stdio.h>
#include "ros.h"
#include <std_msgs/Float32.h>
#include <windows.h>


using std::string;



int main(int argc, _TCHAR * argv[])
{
    ros::NodeHandle nh;
    char *ros_master = "172.17.194.162";

    printf("Connecting to server at %s\n", ros_master);
    nh.initNode (ros_master);
    printf("Advertising message\n");
    std_msgs::Float32 a;
    ros::Publisher cmd("/truevision/throttle_cmd", &a);
    nh.advertise(cmd);

    printf("Go Car!\n");

    while (1)
    {
        a = 1.0;
        cmd.publish(a);
        nh.spinOnce();
        Sleep(100);
    }

    printf("All done\n");
    return 0;
}

THis is giving me the following errors:

(So apparently 1.0 is not of type float32, how do I change that?)

Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0349   no operator "=" matches these operands  ConsoleApplication1 c:\Users\kaela\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 32  

Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0413   no suitable conversion function from "std_msgs::Float32" to "const ros::Msg *" exists   ConsoleApplication1 c:\Users\kaela\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 33

Thank you.

edit retag flag offensive close merge delete

Comments

1

Can you clarify how this is related to #q284292?

gvdhoorn gravatar image gvdhoorn  ( 2018-03-03 13:42:33 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-03-04 01:26:41 -0500

gvdhoorn gravatar image

See the documentation for std_msgs/Float32:

Raw Message Definition

float32 data

std_msgs/Float32 is not just an alias for a double or float. It's a message structure, with a member. You cannot assign a scalar (such as 1.0) to a structure in C (or in most languages actually).

The value should be assigned to the data member. The following should work:

a.data = 1.0;

Note also that the error message Visual Studio gives you is rather descriptive:

E0349   no operator "=" matches these operands [..] ConsoleApplication1.cpp 32
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-03-03 12:22:13 -0500

Seen: 210 times

Last updated: Mar 04 '18