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

publish json roslib

asked 2023-01-17 09:53:45 -0500

bhomaidan gravatar image

updated 2023-01-17 11:04:31 -0500

I have a dictionary and I tried to JSON.stringify() to turn it into a string which looks like this: {"p_01_08":{},"p_01_09":{},"p_01_10":{},"p_01_11":{},"p_01_12":{},"p_01_13":{},"p_01_14":{},"p_02_07":{},"p_02_08":{},"p_02_09":{},"p_02_10":{},"p_02_11":{},"p_02_12":{},"p_02_13":{},"p_02_14":{},"p_02_15":{},"p_03_07":{},"p_03_08":{},"p_03_09":{},"p_03_10":{},"p_03_11":{},"p_03_12":{},"p_03_13":{},"p_03_14":{},"p_03_15":{},"p_04_07":{},"p_04_08":{},"p_04_09":{},"p_04_10":{},"p_04_11":{},"p_04_12":{},"p_04_13":{},"p_04_14":{},"p_04_15":{},"p_05_07":{},"p_05_08":{},"p_05_09":{},"p_05_10":{},"p_05_11":{},"p_05_12":{},"p_05_13":{},"p_05_14":{},"p_05_15":{},"p_06_07":{},"p_06_08":{},"p_06_09":{},"p_06_10":{},"p_06_11":{},"p_06_12":{},"p_06_13":{},"p_06_14":{},"p_06_15":{},"p_07_08":{},"p_07_09":{},"p_07_10":{},"p_07_11":{},"p_07_12":{},"p_07_13":{},"p_07_14":{}}

Then I tried to use roslib.js to publish that Dictionary string:

import {store} from './store.js';
import { Ros, Message, Topic } from "roslib";

export default class ROSInterface {
  constructor() {
    this.ros = new Ros({
      url: "ws://localhost:9090",
    });
  }

  createMessage = (msg_data) => {
    const msg = new Message(msg_data);
    return msg;
  }

  createTopic = (topic_name, msg_type) => {
    const topic_ = new Topic({
      ros: this.ros,
      name: topic_name,
      messageType: msg_type,
    });
    return topic_;
  }

init=()=> {
  this.model_topic = this.createTopic('/model', 'std_msgs/String');
  this.lego_map_topic = this.createTopic('/lego_map', 'std_msgs/String');
  this.rate = 10; // 10hz
}

publish=()=> {
  this.interval = setInterval(this.publish_topics, 1000/this.rate)
}

publish_topics=()=> {
  // model
  console.log(JSON.stringify(store.model));
  let model_msg_ = this.createMessage(JSON.stringify(store.model));
  this.model_topic.publish(model_msg_);
  // lego_map
  let lego_map_msg_ = this.createMessage(JSON.stringify(store.lego_map));
  this.model_topic.publish(lego_map_msg_);
}

stopPublish=()=> {
  clearInterval(this.interval);
}
}

But Whenever I use the publish() method I get the following error on the rosbridge server terminal roslaunch rosbridge_server rosbridge_websocket.launch: publish: Message type std_msgs/String does not have a field 0, Can you please tell me how can I advertise and publish the topic correctly? thanks in advance.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-01-20 23:05:44 -0500

miura gravatar image

Perhaps there is a flaw in the way the message was created. You may be successful if you try the following std_msgs/String has the string in a field called data, not the string itself.

// const msg = new Message(msg_data);
const msg = new Message({ data: msg_data});
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2023-01-17 09:53:45 -0500

Seen: 244 times

Last updated: Jan 20 '23