Apollo3.0 canbus代码解析(未完成,待修改)

本文详细解析了Apollo3.0自动驾驶系统的Canbus模块,从main.cc的APOLLO_MAIN宏开始,深入到canbus.cc的init()函数,探讨了初始化过程,包括消息订阅、车辆配置、消息解析器生成、信号接收器与发送器的建立,以及vehicle_controller_的作用。此外,介绍了OnControlCommand和OnGuardianCommand两个回调函数的功能,OnControlCommand用于处理控制命令,而OnGuardianCommand的作用尚不明确。

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

本文参考https://blog.youkuaiyun.com/xiaoxiao123jun/article/details/102811212

1、从main.cc中的宏定义APOLLO_MAIN(apollo::canbus::Canbus);进入主程序
2、跳转到common/apollo_app.h中
	#define APOLLO_MAIN(APP)                                       \
  	int main(int argc, char **argv) {
                                 \
    google::InitGoogleLogging(argv[0]);                        \
    google::ParseCommandLineFlags(&argc, &argv, true);         \
    signal(SIGINT, apollo::common::apollo_app_sigint_handler); \
    APP apollo_app_;                                           \
    ros::init(argc, argv, apollo_app_.Name());                 \
    apollo_app_.Spin();                                        \
    return 0;                                                  \
  }

其中APP为apollo::canbus::Canbus类,函数主要实现功能为:使用Google命令行解析工具解析相关参数,注册接收中止信号“SIGINT”的处理函数,进入模块核心代码函数ros::Spin()。

首先进入到init状态

3、跳转到canbus.cc中的init()初始化函数
Status Canbus::Init() {
   

  /*完成AdapterManager的初始化,
  FLAGS_canbus_adapter_config_filename对应于modules/canbus/common/canbus_gflags.cc中的
  DEFINE_string(canbus_adapter_config_filename, "modules/canbus/conf/adapter.conf", 
  "The adapter config file");
  adapter.conf中配置了canbus模块订阅和发布的topic:
  订阅:control_command, guardian
  发布:chassis,chassis_detail,monitor(监视器?)
  
  //如果改成原生ROS的话,这里的AdapterManager配置删掉,改成ROS的topic订阅和发布
  */

  AdapterManager::Init(FLAGS_canbus_adapter_config_filename);
  AINFO << "The adapter manager is successfully initialized.";

  // load conf

  // Flags文件在canbus/common/canbus_gflags.cc

  //导入配置文件modules/canbus/conf/canbus_conf.pb.txt
  /*
  vehicle_parameter {
	  brand: LINCOLN_MKZ//指定车辆,后面生成对应的vehicle_factory,进而生成对应的message_manager_
	  max_enable_fail_attempt: 5
	  driving_mode: COMPLETE_AUTO_DRIVE
	}
	
	can_card_parameter {
	  brand: ESD_CAN//指定车辆,后面生成对应的can_client_
	  type: PCI_CARD
	  channel_id: CHANNEL_ID_ZERO
	}
	
	enable_debug_mode: false
	enable_receiver_log: false
	enable_sender_log: false
  */
  //如果改成原生ROS的话此处也可以删除,工厂模式也可以放一边,上面导入的配置文件就是用来生成具体的产品工厂
  //对象和产品对象,我们直接用ROS跑自己的工程一般车辆和CAN卡是固定的,可以改动后直接生成对应的产品对象

  if (!common::util::GetProtoFromFile(FLAGS_canbus_conf_file, &canbus_conf_)) {
   
    return OnError("Unable to load canbus conf file: " +
                   FLAGS_canbus_conf_file);
  }

  AINFO << "The canbus conf file is loaded: " << FLAGS_canbus_conf_file;
  ADEBUG << "Canbus_conf:" << canbus_conf_.ShortDebugString();

  // Init can client
  // 注意,在编译之前是找不到instance()函数的,因为canclient在编译之前还没确定是什么,需要通过编译配置文件生成相关头文件才能确定用什么canlient。
  auto *can_factory = CanClientFactory
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值