[英]Publishing a value calculated in a function in ROS
I am working on ROS for an autonomous driving system.我正在为自动驾驶系统开发 ROS。 I have defined and calculated a value 'priority' in a function as follows:我在 function 中定义并计算了一个值“优先级”,如下所示:
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.现在,我想通过我定义的发布者发布 ROS 中的值。 I created a new topic and have used the 'Float32' type from the 'std_msgs.msg' pack since the 'priority' is a float value.我创建了一个新主题并使用了“std_msgs.msg”包中的“Float32”类型,因为“优先级”是一个浮点值。 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.在上面的代码中 - 我正在创建这个主题 - /carla/{role_name}/camera/rgb/view/priority - 并希望将“优先级”的值发布到这个主题 - 但是,我无法弄清楚想办法做到这一点,我仍然坚持如何通过仅使用 msg 包中的 Float32 类型来发送关于我的主题的优先级值。 Any suggestions would be really helpful.任何建议都会非常有帮助。
I'm a bit of a noob myself, you aren't creating a Float32 msg to publish.我自己有点菜鸟,你没有创建一个 Float32 消息来发布。
msg=Float32(some value) msg=Float32(某个值)
pub.publish(msg) pub.publish(味精)
Take a look at the basic ROS publisher/subscriber tutorial - it is exactly what you want看看基本的 ROS 发布者/订阅者教程——这正是你想要的
http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29 http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.