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

Datatype issue while calling setBool service from JS client

asked 2023-02-09 04:44:45 -0600

bhomaidan gravatar image

I 'm using this service server in Python:

#! /usr/bin/env python3
import rospy
from std_srvs.srv import SetBool, SetBoolResponse

class StateController:
    def __init__(self):
        rospy.init_node("state_controller", anonymous=True)
        rospy.Service("validate_vision_info", SetBool, self.validVision)
        rospy.spin()

    def validVision(self, req):
        model_valid = req.data
        rospy.logwarn("Vision Model Validation: {}".format(req.data))
        return SetBoolResponse(success=True, message="")

if __name__ == "__main__":
    try:
        controller_ = StateController()

    except rospy.ROSInterruptException:
        logging.error("Error in the State Controller")

And this service client in JavaScript:

import { Ros, Service, ServiceRequest } from "roslib";

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;
    };
}

function validate(val) {
    const service_name = "/validate_vision_info";
    const service_type = "std_srvs/SetBool";
    const iface_ = new ROSInterface();

    const srv = iface_.createService(service_name, service_type);
    const req = iface_.requestService(val);

    srv.callService(
        req,
        (result) => {
            console.log(result);
        },
        (error) => {
            console.log(error);
        });
}

validate(true);
validate(false);

My problem is that: always the service request data is False no matter what is the sent request true or false!

Can you please tell me how can I solve my problem? thanks in advance.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-02-11 06:18:28 -0600

miura gravatar image

There seems to be a mistake in the way the requestService is written. I think the following will solve the problem.

requestService = (params) => {
    // let request = new ServiceRequest(params);
    let request = new ServiceRequest({data : params});
    return request;
};

ref : https://docs.ros.org/en/noetic/api/st...

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2023-02-09 04:44:45 -0600

Seen: 49 times

Last updated: Feb 11 '23