How to add a new can card
This article mainly introduces how to add a new can card, the article is very detailed, has a certain reference value, interested friends must read it!
Brief introduction
The Controller area Network (CAN) is a network that is densely used in many microcontrollers and devices to transfer data between devices without the help of a host computer.
The default can card used in Apollo is the ESD CAN- PCI card. You can use the following steps to add a new can card:
Add a new can card
Adding a new can card requires the following steps:
Implement the CanClient class of the new can card.
Register a new can card in CanClientFactory.
Update the configuration file.
The following steps show how to add a new can card-an example of adding a can card to your project.
Step 1
Implement the CanClient class of the new can card, and the following code shows how to implement the CANClient class:
# include # include # include "hermes_can/include/bcan.h" # include "modules/canbus/can_client/can_client.h" # include "modules/canbus/common/canbus_consts.h" # include "modules/common/proto/error_code.pb.h" / * * @ namespace apollo::canbus::can * @ brief apollo::canbus::can * / namespace apollo {namespace canbus {namespace can {/ * * @ class ExampleCanClient * @ brief The class which defines an Example CAN client which inherits CanClient. * / class ExampleCanClient: public CanClient {public: / * * @ brief Initialize the Example CAN client by specified CAN card parameters. * @ param parameter CAN card parameters to initialize the CAN client. * @ return If the initialization is successful. * / bool Init (const CANCardParameter& parameter) override; / * * @ brief Destructor * / virtual ~ ExampleCanClient () = default; / * * @ brief Start the Example CAN client. * @ return The status of the start action which is defined by * apollo::common::ErrorCode. * / apollo::common::ErrorCode Start () override; / * * @ brief Stop the Example CAN client. * / void Stop () override; / * * @ brief Send messages * @ param frames The messages to send. * @ param frame_num The amount of messages to send. * @ return The status of the sending action which is defined by * apollo::common::ErrorCode. * / apollo::common::ErrorCode Send (const std::vector& frames, int32_t* const frame_num) override; / * * @ brief Receive messages * @ param frames The messages to receive. * @ param frame_num The amount of messages to receive. * @ return The status of the receiving action which is defined by * apollo::common::ErrorCode. * / apollo::common::ErrorCode Receive (std::vector* const frames, int32_t* const frame_num) override; / * @ brief Get the error string. * @ param status The status to get the error string. * / std::string GetErrorString (const int32_t status) override; private:...};} / / namespace can} / / namespace canbus} / / namespace apollo
Step 2
Register the new can card in CanClientFactory and add the following code to CanClientFactory:
Void CanClientFactory::RegisterCanClients () {Register (CANCardParameter::ESD_CAN, [] ()-> CanClient* {return new can::EsdCanClient ();}); / / register the new CAN card here. Register (CANCardParameter::EXAMPLE_CAN, [] ()-> CanClient* {return new can::ExampleCanClient ();});}
Step 3
Next, you need to update the configuration file to add EXAMPLE_CAN at / modules/canbus/proto/can_card_parameter.proto
Message CANCardParameter {enum CANCardBrand {FAKE_CAN = 0; ESD_CAN = 1; EXAMPLE_CAN = 2; / / add new CAN card here. }......}
Update / modules/canbus/conf/canbus_conf.pb.txt
Can_card_parameter {brand:EXAMPLE_CAN type: PCI_CARD / / suppose the new can card is PCI_CARD channel_id: CHANNEL_ID_ZERO / / suppose the new can card has CHANNEL_ID_ZERO}...... The above is all the contents of the article "how to add a new can card". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!