简体   繁体   中英

Publishing a value calculated in a function in ROS

I am working on ROS for an autonomous driving system. I have defined and calculated a value 'priority' in a function as follows:

def odometry_weight_model(velocity, orientation, steering_angle):
    """Odometry based camera view prioritization model"""
    rospy.loginfo(velocity)
    rospy.loginfo(orientation)
    rospy.loginfo(steering_angle)
....
for yaw_ang in CAM_LOC:
        force_dir = yaw_ang + steer_angle
        force = np.array([velocity, force_dir])
        delta = (np.linalg.norm(force)) / (2 * math.pi) * const
        **priority = prio_orig + delta**

Now, I want to publish the value in ROS via the publisher I have defined. I created a new topic and have used the 'Float32' type from the 'std_msgs.msg' pack since the 'priority' is a float value. My attempt for the publisher is as follows:

def main():
    """Run the view prioritization"""
    rospy.init_node("view_prioritization", anonymous=True)

    role_name = rospy.get_param("~role_name", "ego_vehicle")
    sensor_definition_file = rospy.get_param(
        "~sensor_definition_file",
        "$(find telecarla_manual_control)/config/multi_sensors.json",
    )

    global CAM_LOC
    with open(sensor_definition_file) as sensor_data:
        multi_sensors = json.load(sensor_data)
        CAM_LOC = []
        for i in multi_sensors["sensors"]:
            camera_loc.append(i["yaw"])

    pub = rospy.Publisher(
        "/carla/{role_name}/camera/rgb/view/priority",
        Float32,
        queue_size=10,
    )
    rospy.Subscriber(
        "/carla/{role_name}/vehicle_status",
        CarlaEgoVehicleStatus,
        vehicle_status_callback,
    )
    rate = rospy.Rate(1)
    while not rospy.is_shutdown():
        pub.publish()
        r.sleep()

    rospy.spin()

In the above code - I am creating this topic - /carla/{role_name}/camera/rgb/view/priority - and would like to publish the value of 'priority' to this topic - however, I have not been able to figure out a way to do it, I am still stuck at how to send this priority value on my topic by using only the Float32 type from the msg pack. Any suggestions would be really helpful.

I'm a bit of a noob myself, you aren't creating a Float32 msg to publish.

msg=Float32(some value)

pub.publish(msg)

Take a look at the basic ROS publisher/subscriber tutorial - it is exactly what you want

http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM