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

How to pass service parameters in ROSLib.js

asked 2022-11-24 05:55:35 -0500

bhomaidan gravatar image

updated 2023-06-18 10:05:12 -0500

lucasw gravatar image

According to the ROSLib.js documentation I did this class

import { Ros, Param, Message, Topic, Service, ServiceRequest } from "roslib";

export default class ROSInterface {
  constructor() {
    this.ros = new Ros({
      url: "ws://localhost:9090",
    });
  }
  createService(service_name, service_type) {
    const service = new Service({
      ros: this.ros,
      name: service_name,
      serviceType: service_type,
    });

    return service;
  }

  callService(service_name, service_type, params) {
    const service = this.createService(service_name, service_type);
    const request = new ServiceRequest(params);
    service.callService(request, function (result) {
      return result;
    });
  }
}

But I'm confused how can I pass a complex parameters to my service!

My Service looks like this:

geometry_msgs/Point pick
geometry_msgs/Point place
std_msgs/UInt16 speed
std_msgs/Bool arm

I have tried let params = {pick:{x:0.1,y:0.2,z:0.3}, place:{x:-0.1,y:-0.2,z:-0.3},speed: 200, arm: 1} and it didn't work! can you please tell me how can I form my params variabile properly to make my service works well? thanks in advance.

edit retag flag offensive close merge delete

Comments

Can you add service_name and service_type to this? What are you passing

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2022-11-24 06:26:16 -0500 )edit

@RanjitKathiriya that is straight forward const service_name = "/pick_place"; const service_type = "my_interface/PickPlace";

bhomaidan gravatar image bhomaidan  ( 2022-11-24 06:33:39 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2022-11-30 00:10:44 -0500

bhomaidan gravatar image

The solution is:

  1. Simpler ROS Message representation:

    geometry_msgs/Point pick
    geometry_msgs/Point place
    int16 speed
    bool arm
    ---
    bool success
    
  2. Function Name different from callService to remove ambiguity, for example: callService_:

     import { Ros, Param, Message, Topic, Service, ServiceRequest } from "roslib";
    
     export default class ROSInterface {
    
       constructor() {
    
         this.ros = new Ros({
           url: "ws://localhost:9090",
         });
       }
    
       createService=(service_name, service_type) =>{
         let service = new Service({
           ros: this.ros,
           name: service_name,
           serviceType: service_type,
         });
         return service;
       }
    
      requestService=(params) =>{
         let request = new ServiceRequest(params);
         return request;
       }
    
    
       callService_ = (service_name, service_type, params) => {
         const srv = this.createService(service_name, service_type);
         const req = this.requestService(params);
         srv.callService(
           req,
           (result) => {
             console.log(result);
           },
           (error) => {
             console.log(error);
           }
         );
        }
     }
    
  3. Finally the parameters can be passed as object with children objects:

    const params_ = {
    pick: {x: 1, y: 2, z: 3},
    place: {x: -1, y: -2, z: -3},
    speed: 10,
    arm: false,
    };
    ROSInterface.callService_(service_name, service_type, params_);
    
edit flag offensive delete link more
0

answered 2022-11-24 06:44:38 -0500

Ranjit Kathiriya gravatar image

updated 2022-11-25 08:56:53 -0500

Hello,

Try this code snippet, I think it will work on your case,

  var customSrvClient = new ROSLIB.Service({
    ros : ros,
    name : '/pick_place',
    serviceType : 'my_interface/PickPlace'
  });


  var request = new ROSLIB.ServiceRequest({
    pick : {
      x : 0.1,
      y : 0.2,
      z : 0.3
    },
    place : {
      x : -0.1,
      y : -0.2,
      z : -0.3
    },
    speed: { data: 200 },
    arm: { data: 'false' }
  });

  customSrvClient.callService(request, function(result) {
    console.log('Your LOG -> Action performed');
  });
edit flag offensive delete link more

Comments

@RanjitKathiriya. it didn't work!

bhomaidan gravatar image bhomaidan  ( 2022-11-24 06:56:34 -0500 )edit

Are you getting any errors or checking your log by intercepting elements from your browser?

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2022-11-24 08:01:08 -0500 )edit

@RanjitKathiriya No Errors

bhomaidan gravatar image bhomaidan  ( 2022-11-24 08:20:15 -0500 )edit

Have you tested executing the service from your terminal?

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2022-11-24 08:22:06 -0500 )edit

@RanjitKathiriya I'm not sure how can I pass theses arguments in the terminal? $ rosservice call pick_place ??

bhomaidan gravatar image bhomaidan  ( 2022-11-24 08:43:08 -0500 )edit

First confirm that service is available by rossrv show.

Then after You can test by using:

rosservice call /pick_place "pick:{x:0.1,y:0.2,z:0.3}, place:{x:-0.1,y:-0.2,z:-0.3}, speed: 200, arm: 1"

You can use tab to auto-complete.

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2022-11-24 08:48:16 -0500 )edit

@RanjitKathiriya calling the service via the tap autocomplete from the terminal called and executed the ros service correctly rosservice call pick_place "pick: x: 0.0 y: 0.0 z: 0.0 place: x: 0.0 y: 0.0 z: 0.0 speed: data: 0 arm: data: false"

bhomaidan gravatar image bhomaidan  ( 2022-11-25 08:27:18 -0500 )edit

Hello,

You forgot to add speed: data: 0 arm: data: false

speed: { data: 200 }

Try this, I think it will work, Thanks

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2022-11-25 08:57:43 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-11-24 05:55:35 -0500

Seen: 229 times

Last updated: Nov 30 '22