javascript ROS setBool服务始终为假

e0bqpujr  于 2023-02-11  发布在  Java
关注(0)|答案(1)|浏览(93)

我正在使用python中的ROS服务器

#! /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")

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

    callService_ = (service_name, service_type, params) => {
        return new Promise((resolve, reject) => {
            const srv = this.createService(service_name, service_type);
            const req = this.requestService(params);
            srv.callService(
                req,
                (result) => {
                    console.log(result);
                    resolve(result);
                },
                (error) => {
                    console.log(error);
                    reject(error);
                }
            );
        });
    };
}

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

    console.log(val);
    const iface_ = new ROSInterface();
    let serv = await iface_
        .callService_(service_name, service_type, val)
        .then((result) => {
            return result;
        }).catch(e => {
            console.log(e);
        });
    console.log("service result", serv);
}

validate(true);
validate(false);

在两个服务调用中,我都在Python代码的Service Service Request中获得了False
你能告诉我怎么解吗?先谢了。

rqenqsqc

rqenqsqc1#

1.当我使用VueJS3时,我不得不使用带有Reactivity的状态管理:变量存储在store.js中。
1.消息(参数)必须以{data: store.variable}传递,以符合服务setBool描述

相关问题