Apollo 10.0
自动驾驶开放平台
camera_component.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19namespace apollo {
20namespace drivers {
21namespace camera {
22
24 camera_config_ = std::make_shared<Config>();
26 camera_config_.get())) {
27 return false;
28 }
29 AINFO << "UsbCam config: " << camera_config_->DebugString();
30
31 camera_device_.reset(new UsbCam());
32 camera_device_->init(camera_config_);
33 raw_image_.reset(new CameraImage);
34 raw_image_for_compress_.reset(new CameraImage);
35
36 raw_image_->width = camera_config_->width();
37 raw_image_->height = camera_config_->height();
38 raw_image_->bytes_per_pixel = camera_config_->bytes_per_pixel();
39
40 raw_image_for_compress_->width = camera_config_->width();
41 raw_image_for_compress_->height = camera_config_->height();
42 raw_image_for_compress_->bytes_per_pixel = camera_config_->bytes_per_pixel();
43
44 if (camera_config_->pixel_format() == "yuyv" ||
45 camera_config_->pixel_format() == "uyvy" ||
46 camera_config_->pixel_format() == "yuvmono10") {
47 raw_image_for_compress_->image_size =
48 raw_image_for_compress_->width * raw_image_for_compress_->height * 2;
49 } else if (camera_config_->pixel_format() == "mjpeg") {
50 AINFO << "Disable sensor raw camera output with format mjpeg";
51 raw_image_for_compress_->image_size = 0;
52 } else if (camera_config_->pixel_format() == "rgb24") {
53 raw_image_for_compress_->image_size =
54 raw_image_for_compress_->width * raw_image_for_compress_->height * 3;
55 } else {
56 AERROR << "Wrong pixel fromat:" << camera_config_->pixel_format()
57 << ",must be yuyv | uyvy | mjpeg | yuvmono10 | rgb24";
58 return false;
59 }
60
61 if (raw_image_for_compress_->image_size == 0) {
62 raw_image_for_compress_->image = nullptr;
63 } else {
64 raw_image_for_compress_->image = reinterpret_cast<char*>(
65 calloc(raw_image_for_compress_->image_size, sizeof(char)));
66 }
67
68 device_wait_ = camera_config_->device_wait_ms();
69 spin_rate_ = static_cast<uint32_t>((1.0 / camera_config_->spin_rate()) * 1e6);
70
71 if (camera_config_->output_type() == YUYV) {
72 raw_image_->image_size = raw_image_->width * raw_image_->height * 2;
73 } else if (camera_config_->output_type() == RGB) {
74 raw_image_->image_size = raw_image_->width * raw_image_->height * 3;
75 }
76 if (raw_image_->image_size > MAX_IMAGE_SIZE) {
77 AERROR << "image size is too big ,must less than " << MAX_IMAGE_SIZE
78 << " bytes.";
79 return false;
80 }
81 raw_image_->is_new = 0;
82 // free memory in this struct desturctor
83 raw_image_->image =
84 reinterpret_cast<char*>(calloc(raw_image_->image_size, sizeof(char)));
85 if (raw_image_->image == nullptr) {
86 AERROR << "system calloc memory error, size:" << raw_image_->image_size;
87 return false;
88 }
89
90 for (int i = 0; i < buffer_size_; ++i) {
91 auto pb_image = std::make_shared<Image>();
92 pb_image->mutable_header()->set_frame_id(camera_config_->frame_id());
93 pb_image->set_width(raw_image_->width);
94 pb_image->set_height(raw_image_->height);
95 pb_image->mutable_data()->reserve(raw_image_->image_size);
96
97 if (camera_config_->output_type() == YUYV) {
98 pb_image->set_encoding("yuyv");
99 pb_image->set_step(2 * raw_image_->width);
100 } else if (camera_config_->output_type() == RGB) {
101 pb_image->set_encoding("rgb8");
102 pb_image->set_step(3 * raw_image_->width);
103 }
104
105 pb_image_buffer_.push_back(pb_image);
106
107 auto raw_image = std::make_shared<Image>();
108 raw_image->mutable_header()->set_frame_id(camera_config_->frame_id());
109 raw_image->set_width(raw_image_for_compress_->width);
110 raw_image->set_height(raw_image_for_compress_->height);
111 raw_image->mutable_data()->reserve(raw_image_for_compress_->image_size);
112 raw_image->set_encoding(camera_config_->pixel_format());
113
114 raw_image_buffer_.push_back(raw_image);
115 }
116
117 writer_ = node_->CreateWriter<Image>(camera_config_->channel_name());
118 raw_writer_ = node_->CreateWriter<Image>(camera_config_->raw_channel_name());
119 async_result_ = cyber::Async(&CameraComponent::run, this);
120 return true;
121}
122
123void CameraComponent::run() {
124 running_.exchange(true);
125 while (!cyber::IsShutdown()) {
126 if (!camera_device_->wait_for_device()) {
127 // sleep for next check
128 cyber::SleepFor(std::chrono::milliseconds(device_wait_));
129 continue;
130 }
131
132 if (!camera_device_->poll(raw_image_, raw_image_for_compress_)) {
133 AERROR << "camera device poll failed";
134 continue;
135 }
136
137 cyber::Time image_time(raw_image_->tv_sec, 1000 * raw_image_->tv_usec);
138 if (index_ >= buffer_size_) {
139 index_ = 0;
140 }
141 auto pb_image = pb_image_buffer_.at(index_);
142 auto header_time = cyber::Time::Now().ToSecond();
143 auto measurement_time = image_time.ToSecond();
144 pb_image->mutable_header()->set_timestamp_sec(header_time);
145 pb_image->set_measurement_time(measurement_time);
146 pb_image->set_data(raw_image_->image, raw_image_->image_size);
147 writer_->Write(pb_image);
148
149 auto raw_image_for_compress = raw_image_buffer_.at(index_++);
150 raw_image_for_compress->mutable_header()->set_timestamp_sec(
151 header_time);
152 raw_image_for_compress->set_measurement_time(measurement_time);
153 raw_image_for_compress->set_data(raw_image_for_compress_->image,
154 raw_image_for_compress_->image_size);
155 raw_writer_->Write(raw_image_for_compress);
156
157 cyber::SleepFor(std::chrono::microseconds(spin_rate_));
158 }
159}
160
162 if (running_.load()) {
163 running_.exchange(false);
164 free(raw_image_->image);
165 async_result_.wait();
166 }
167}
168
169} // namespace camera
170} // namespace drivers
171} // namespace apollo
std::shared_ptr< Node > node_
Cyber has builtin time type Time.
Definition time.h:31
static Time Now()
get the current time.
Definition time.cc:57
double ToSecond() const
convert time to second.
Definition time.cc:77
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
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,...
Definition file.cc:132
bool IsShutdown()
Definition state.h:46
class register implement
Definition arena_queue.h:37