ros::Exception

ROS(Robot Operating System)的ros::Exception在ros/roscpp_core/cpp_common/include/ros/exception.h中定义,作为所有ROS抛出异常的基础类。这个类继承自std::runtime_error,用于在ROS系统中捕获和处理错误。源码定义包含了版权信息和使用许可条件,并提供了构造函数以初始化异常信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在ros_comm中找了很久没有找到ros::Exception,原来ros::Exception是在另一个包roscpp_core中在 ros/roscpp_core/cpp_common/include/ros/exception.h中定义

定义如下:

/*
 * Copyright (C) 2009, Willow Garage, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
 *     contributors may be used to endorse or promote products derived from
 *     this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ROSLIB_EXCEPTION_H
#define ROSLIB_EXCEPTION_H

#include <stdexcept>

namespace ros
{

/**
 * \brief Base class for all exceptions thrown by ROS
 */
class Exception : public std::runtime_error
{
public:
  Exception(const std::string& what)
  : std::runtime_error(what)
  {}
};

} // namespace ros

#endif

 源码地址:

https://github.com/ros/roscpp_core/blob/noetic-devel/cpp_common/include/ros/exception.h

其他地址:

http://docs.ros.org/en/jade/api/cpp_common/html/classros_1_1Exception.html#a47b6cafac27d5bbe05e67977703da87d

http://docs.ros.org/en/jade/api/cpp_common/html/exception_8h_source.html

#include "ros/ros.h" #include "turtlesim/Pose.h" #include "tf2_ros/transform_broadcaster.h" #include "geometry_msgs/TransformStamped.h" #include "tf2/LinearMath/Quaternion.h" void doPose(const turtlesim::Pose::ConstPtr& pose) { static tf2_ros::TransformBroadcaster broadcaster; geometry_msgs::TransformStamped tfs; tfs.header.frame_id = "world"; tfs.header.stamp = ros::Time::now(); tfs.child_frame_id = "ting"; tfs.transform.translation.x = pose->x; tfs.transform.translation.y = pose->y; tfs.transform.translation.z = 0.0; tf2::Quaternion qtn; qtn.setRPY(0,0,pose ->theta); tfs.transform.rotation.x = qtn.getX(); tfs.transform.rotation.y = qtn.getY(); tfs.transform.rotation.z = qtn.getZ(); tfs.transform.rotation.w = qtn.getW(); broadcaster.sendTransform(tfs); } int main(int argc,char *argv[]) { setlocale(LC_ALL,""); ros::init(argc,argv,"one_tf_pub"); ros::NodeHandle nh; ros::Subscriber sub = nh.subscribe<turtlesim::Pose>("/ting/pose",1000,doPose); ros::spin(); return 0; } #include "ros/ros.h" #include "tf2_ros/transform_listener.h" #include "tf2_ros/buffer.h" #include "geometry_msgs/PointStamped.h" #include "tf2_geometry_msgs/tf2_geometry_msgs.h" int main(int argc,char *argv[]) { ros::init(argc,argv,"one_tf_sub"); ros::NodeHandle nh; tf2_ros::Buffer buffer; tf2_ros::TransformListener listener(buffer); ros::Rate r(1); while(ros::ok()) { geometry_msgs::PointStamped point_laser; point_laser.header.frame_id = "ting"; point_laser.header.stamp = ros::Time(); point_laser.point.x = 1; point_laser.point.y = 1; point_laser.point.z = 0; try { geometry_msgs::PointStamped point_base; point_base = buffer.transform(point_laser,"world"); ROS_INFO("坐标点相对于world的坐标为:(%.2f,%.2f,%.2f)",point_base.point.x,point_base.point.y,point_base.point.z); } catch(const std::exception& e) { std::cerr << e.what() << '\n'; } r.sleep(); ros::spinOnce(); } return 0; } 这两段代码要完成运行,CMakelists文件怎么配置
06-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值