23 #include <condition_variable> 41 #include "modules/drivers/canbus/proto/can_card_parameter.pb.h" 42 #include "modules/drivers/canbus/proto/sensor_canbus_conf.pb.h" 58 using apollo::common::ErrorCode;
60 using apollo::common::monitor::MonitorMessageItem;
65 using apollo::drivers::canbus::SensorCanbusConf;
69 template <
typename SensorType>
75 : monitor_logger_buffer_(
76 apollo::common::monitor::MonitorMessageItem::CANBUS) {}
87 void PublishSensorData();
90 bool OnError(
const std::string &error_msg);
91 void RegisterCanClients();
93 SensorCanbusConf canbus_conf_;
94 std::unique_ptr<CanClient> can_client_;
96 std::unique_ptr<canbus::MessageManager<SensorType>> sensor_message_manager_;
97 std::unique_ptr<std::thread> thread_;
99 int64_t last_timestamp_ = 0;
100 std::unique_ptr<cyber::Timer> timer_;
103 volatile bool data_trigger_running_ =
false;
104 std::shared_ptr<Writer<SensorType>> sensor_writer_;
109 template <
typename SensorType>
117 ADEBUG <<
"Canbus_conf:" << canbus_conf_.ShortDebugString();
121 can_factory->RegisterCanClients();
122 can_client_ = can_factory->CreateCANClient(canbus_conf_.can_card_parameter());
124 return OnError(
"Failed to create can client.");
126 AINFO <<
"Can client is successfully created.";
129 if (sensor_message_manager_ ==
nullptr) {
130 return OnError(
"Failed to create message manager.");
132 AINFO <<
"Sensor message manager is successfully created.";
134 if (can_receiver_.Init(can_client_.get(), sensor_message_manager_.get(),
136 return OnError(
"Failed to init can receiver.");
138 AINFO <<
"The can receiver is successfully initialized.";
139 sensor_writer_ =
node_->CreateWriter<
SensorType>(FLAGS_sensor_node_name);
143 template <
typename SensorType>
147 return OnError(
"Failed to start can client");
149 AINFO <<
"Can client is started.";
153 return OnError(
"Failed to start can receiver.");
155 AINFO <<
"Can receiver is started.";
160 if (FLAGS_sensor_freq > 0) {
161 double duration_ms = 1000.0 / FLAGS_sensor_freq;
162 timer_.reset(
new cyber::Timer(static_cast<uint32_t>(duration_ms),
163 [
this]() { this->OnTimer(); },
false));
166 data_trigger_running_ =
true;
167 thread_.reset(
new std::thread([
this] { DataTrigger(); }));
168 if (thread_ ==
nullptr) {
169 AERROR <<
"Unable to create data trigger thread.";
170 return OnError(
"Failed to start data trigger thread.");
175 monitor_logger_buffer_.INFO(
"Canbus is started.");
180 template <
typename SensorType>
185 template <
typename SensorType>
187 std::condition_variable *cvar = sensor_message_manager_->GetMutableCVar();
188 while (data_trigger_running_) {
189 std::unique_lock<std::mutex> lock(mutex_);
192 sensor_message_manager_->ClearSensorData();
196 template <
typename SensorType>
198 if (FLAGS_sensor_freq > 0) {
202 can_receiver_.Stop();
205 if (data_trigger_running_) {
206 data_trigger_running_ =
false;
207 if (thread_ !=
nullptr && thread_->joinable()) {
208 sensor_message_manager_->GetMutableCVar()->notify_all();
213 AINFO <<
"Data trigger stopped [ok].";
217 template <
typename SensorType>
219 monitor_logger_buffer_.ERROR(error_msg);
::apollo::cyber::Time Time
Definition: racobit_radar_message_manager.h:41
CanClientFactory inherites apollo::common::util::Factory.
Definition: can_client_factory.h:44
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Defines the CanFrame struct and CanClient interface.
The class which defines the CAN client to send and receive message.
Definition: can_client.h:92
The class of MessageManager.
#define ADEBUG
Definition: log.h:41
Used to perform oneshot or periodic timing tasks.
Definition: timer.h:71
SensorCanbus()
Definition: sensor_canbus.h:74
~SensorCanbus()
Definition: sensor_canbus.h:197
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs, and merges the parsed content to the proto.
std::string config_file_path_
Definition: component_base.h:114
Defines CanReceiver class.
std::shared_ptr< Node > node_
Definition: component_base.h:113
The class of MonitorLogBuffer.
#define AERROR
Definition: log.h:44
template of canbus-based sensor module main class (e.g., mobileye).
Definition: sensor_canbus.h:70
message manager manages protocols. It supports parse and can get protocol data by message id...
Definition: message_manager.h:68
SensorType
Sensor types are set in the order of lidar, radar, camera, ultrasonic Please make sure SensorType has...
Definition: sensor_meta.h:29
bool OK()
Definition: state.h:44
Defines the CanClientFactory class.
This class help collect MonitorMessage pb to monitor topic. The messages can be published automatical...
Definition: monitor_log_buffer.h:60
bool Init() override
module initialization function
Definition: sensor_canbus.h:110
#define AINFO
Definition: log.h:42
CAN receiver.
Definition: can_receiver.h:55
The Component can process up to four channels of messages. The message type is specified when the com...
Definition: component.h:58