0
$\begingroup$

Rosanswers logo

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.


Originally posted by bhomaidan on ROS Answers with karma: 114 on 2023-02-09

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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/std_srvs/html/srv/SetBool.html


Originally posted by miura with karma: 1908 on 2023-02-11

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.