首先,要明白只有can_client才会与设备通信,在apollo中can_client提供了三种通信方式
enum CANCardBrand {
FAKE_CAN = 0; //假的数据通信,一般模拟使用
ESD_CAN = 1; //esd canbus卡通信
SOCKET_CAN_RAW = 2; //socket 通信,在gem车上使用
}
需要新增的通信方式,以serial为例
1.在modules/drivers/canbus/proto/can_card_parameter.proto
中新增如下
enum CANCardBrand {
FAKE_CAN = 0;
ESD_CAN = 1;
SOCKET_CAN_RAW = 2;
SERIAL_CAN = 3; //新的通讯方式
}
2.在modules/drivers/canbus/can_client/
下新增文件夹serial,并实现如下文件。
socket_can_client_raw.h 内容
class **SerialCanClientRaw** : public CanClient {
public:
bool Init(const CANCardParameter ¶meter) override;
virtual ~SocketCanClientRaw();
apollo::common::ErrorCode Start() override;
void Stop() override;
apollo::common::ErrorCode Send(const std::vector<CanFrame> &frames,
int32_t *const frame_num) override;
apollo::common::ErrorCode Receive(std::vector<CanFrame> *const frames,
int32_t *const frame_num) override;
std::string GetErrorString(const int32_t status) override;
private:
int dev_handler_ = 0;
CANCardParameter::CANChannelId port_;
can_frame send_frames_[MAX_CAN_SEND_FRAME_LEN];
can_frame recv_frames_[MAX_CAN_RECV_FRAME_LEN];
};
socket_can_client_raw.cc
是SerialCanClientRaw
类的实现,BUILD是编译脚本。可参考socket通信方案。
3.在modules/drivers/canbus/can_client/can_client_factory.cc
中添加
void CanClientFactory::RegisterCanClients() {
AINFO << "CanClientFactory::RegisterCanClients";
Register(CANCardParameter::FAKE_CAN,
[]() -> CanClient* { return new can::FakeCanClient(); });
#if USE_ESD_CAN
AINFO << "register can: " << CANCardParameter::ESD_CAN;
Register(CANCardParameter::ESD_CAN,
[]() -> CanClient* { return new can::EsdCanClient(); });
#endif
Register(CANCardParameter::SOCKET_CAN_RAW,
[]() -> CanClient* { return new can::SocketCanClientRaw(); });
Register(CANCardParameter::SERIAL_CAN_RAW,
[]() -> CanClient* { return new can::SerialCanClientRaw(); });
}
做完以上三步,就可以在sensors模块中使用该通信方式,以毫米波为例
在modules/drivers/new_radar/conti_radar/conf/conti_radar_conf.pb.txt
can_card_parameter {
brand:SERIAL_CAN
type: PCI_CARD //只在ESD CAN时生效,其余模式可去掉
channel_id: CHANNEL_ID_ZERO //只在ESD CAN时生效,其余模式可去掉
}
还可以参考 docs/howto/how_to_add_a_new_can_card.md
文件