如何添加新的CAN卡?

本文做者:Apollo開發者社區git

簡介github

 

控制器區域網絡(CAN)是在許多微控制器和設備中密集使用的網絡,用於在沒有主計算機幫助的狀況下在設備之間傳輸數據。網絡

 

Apollo中使用的默認CAN卡是 ESD CAN-PCIe卡。您能夠使用如下步驟添加新的CAN卡:ide

 

添加新的CAN卡spa

 

添加新的CAN卡須要完成如下幾個步驟:code

 

  1. 實現新CAN卡的CanClient類。ci

  2. CanClientFactory中註冊新的CAN卡。開發

  3. 更新配置文件。get

 

如下步驟展現瞭如何添加新的CAN卡 - 示例添加CAN卡到您的工程。string

 

步驟1

 

實現新CAN卡的CanClient類,下面的代碼展現瞭如何實現 CANClient 類:

 

#include <string>
#include <vector>

#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 a 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<CanFrame>& 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<CanFrame>* 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

 

步驟2

 

在CanClientFactory中註冊新CAN卡, 在 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(); });  
}

 

步驟3

 

接下來,須要更新配置文件 在/modules/canbus/proto/can_card_parameter.proto添加 EXAMPLE_CAN;

 

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
}
... ...

 

如下是本文章在Github上的Apollo板塊的鏈接。

 

原文連接

 

https://github.com/ApolloAuto/apollo/blob/master/docs/howto/how_to_add_a_new_can_card_cn.md

原文連接地址:https://developer.baidu.com/topic/show/290022

相關文章
相關標籤/搜索