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

[solved] How to publish multiple messages from single topic?

asked 2018-09-26 03:44:41 -0500

updated 2018-09-27 00:28:48 -0500

hai,

I have a problem in my code. i have to publish multiple messages from single topic, to display 2 voltages which is data type of Float64.

#include <ros.h>
#include <std_msgs/Float64.h>


ros::NodeHandle nh;

float voltage1 = 23.44, voltage2 = 74.43;


std_msgs::Float64 test;
ros::Publisher p("my_topic", &test);

void setup()
{

  nh.initNode();
  nh.advertise(p);

}

void loop()
{
  test.data = voltage1;
  test.data = voltage2;
  p.publish( &test );
  nh.spinOnce();
  delay(10);
}

when i did this, i only getting

voltage2

value.how can i publish both?

melvin@melvin-HP-Notebook:~$ rosrun rosserial_arduino serial_node.py /dev/ttyACM0
[INFO] [1537962733.845694]: ROS Serial Python Node
[INFO] [1537962733.851536]: Connecting to /dev/ttyACM0 at 57600 baud
[INFO] [1537962735.962522]: Requesting topics...
[INFO] [1537962740.982019]: Packet Failed :  Failed to read msg data
[INFO] [1537962740.983027]: expected msg length is 84
[WARN] [1537962740.984163]: Last read step: data
[WARN] [1537962740.985069]: Run loop error: Serial Port read failure: Returned short (expected 84 bytes, received 83 instead).
[INFO] [1537962740.985887]: Requesting topics...

I got this error while connecting through serial_node after add this line test.layout.dim_length = 2;

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-09-26 04:44:58 -0500

Hamid Didari gravatar image

updated 2018-09-27 00:42:00 -0500

hi

you must to use Float32MultiArray

#include <ros.h>
#include "std_msgs/Float64MultiArray.h"


ros::NodeHandle nh;

float voltage1 = 23.44, voltage2 = 74.43;

std_msgs::Float64MultiArray test;

ros::Publisher p("my_topic", &test);

void setup()
{

  nh.initNode();
  nh.advertise(p);
  test.data = (float*)malloc(sizeof(float) *2);
  test.data_length =2;

}

void loop()
{
  test.data[0]= voltage1;
  test.data[1] = voltage2;
  p.publish( &test );
  nh.spinOnce();
  delay(10);
}

if you want to publish single float via topic change this part of code:

void loop()
{
  test.data = voltage1;
  p.publish( &test );// add this line
  test.data = voltage2;
  p.publish( &test );
  nh.spinOnce();
  delay(10);
}
edit flag offensive delete link more

Comments

Hi Hamid, i got this error

exit status 1
request for member 'at' in 'test.std_msgs::Float64MultiArray::data', which is of non-class type 'std_msgs::Float64MultiArray::_data_type* {aka float*}'
sudo_melvinyesudas gravatar image sudo_melvinyesudas  ( 2018-09-26 04:50:24 -0500 )edit

change test.data.at(0) to test.data[0] and see the err is gone?

Hamid Didari gravatar image Hamid Didari  ( 2018-09-26 04:57:55 -0500 )edit

No Hamid. same error.

sudo_melvinyesudas gravatar image sudo_melvinyesudas  ( 2018-09-26 05:04:03 -0500 )edit

did you chang both line :

test.data.at(0) = voltage1;
test.data.at(1) = voltage2;

to:

test.data[0] = voltage1;
test.data[1] = voltage2;
Hamid Didari gravatar image Hamid Didari  ( 2018-09-26 05:08:39 -0500 )edit

yes Hamid

exit status 1
request for member 'resize' in 'test.std_msgs::Float64MultiArray::data', which is of non-class type 'std_msgs::Float64MultiArray::_data_type* {aka float*}'

same error.

sudo_melvinyesudas gravatar image sudo_melvinyesudas  ( 2018-09-26 05:17:05 -0500 )edit

i update the code in answer It will work , some syntax different in Arduino and c++

Hamid Didari gravatar image Hamid Didari  ( 2018-09-26 05:21:21 -0500 )edit

first and second err not the same ;)

Hamid Didari gravatar image Hamid Didari  ( 2018-09-26 05:23:49 -0500 )edit

oh sorry. my bad. The code executed without error and uploaded. but how to get those particular values?when i did rostopic echo /my_topic it returns

layout: 
  dim: []
  data_offset: 0
data: []

The voltage not showing.How to subscribe those voltage1 and 2 separately?

sudo_melvinyesudas gravatar image sudo_melvinyesudas  ( 2018-09-26 05:43:58 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2018-09-26 03:44:41 -0500

Seen: 3,671 times

Last updated: Sep 27 '18