Skip to content

Commit

Permalink
Smartereye : driver source code
Browse files Browse the repository at this point in the history
  • Loading branch information
lishuanghua authored and xiaoxq committed May 13, 2020
1 parent 44bd2d4 commit 939441e
Show file tree
Hide file tree
Showing 14 changed files with 975 additions and 0 deletions.
73 changes: 73 additions & 0 deletions modules/drivers/smartereye/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
load("//tools:cpplint.bzl", "cpplint")

package(default_visibility = ["//visibility:public"])

cc_binary(
name = "libsmartereye_component.so",
linkshared = True,
linkstatic = False,
deps = [
":smartereye_component_lib",
":compress_component_lib",
],
)

cc_library(
name = "smartereye_component_lib",
srcs = ["smartereye_component.cc"],
hdrs = ["smartereye_component.h"],
copts = ['-DMODULE_NAME=\\"camera\\"'],
deps = [
":smartereye_device",
":smartereye_handler",
"//cyber",
"//modules/common/adapters:adapter_gflags",
"//modules/drivers/proto:sensor_proto",
"//third_party/camera_library/smartereye:smartereye",
],
)

cc_library(
name = "smartereye_device",
srcs = ["smartereye_device.cc"],
hdrs = ["smartereye_device.h"],
copts = ['-DMODULE_NAME=\\"camera\\"'],
deps = [
":smartereye_handler",
"//cyber",
"//modules/drivers/proto:sensor_proto",
"//modules/drivers/smartereye/proto:camera_proto",
"//third_party/camera_library/smartereye:smartereye",
],
)

cc_library(
name = "smartereye_handler",
srcs = ["smartereye_handler.cc"],
hdrs = ["smartereye_handler.h"],
copts = ['-DMODULE_NAME=\\"camera\\"'],
deps = [
"//cyber",
"//modules/drivers/proto:sensor_proto",
"//modules/drivers/smartereye/proto:camera_proto",
"//third_party/camera_library/smartereye:smartereye",
],
)

cc_library(
name = "compress_component_lib",
srcs = ["compress_component.cc"],
hdrs = ["compress_component.h"],
copts = ['-DMODULE_NAME=\\"camera\\"'],
linkopts = [
"-lopencv_core",
"-lopencv_highgui",
],
deps = [
"//cyber",
"//modules/drivers/smartereye/proto:camera_proto",
"//modules/drivers/proto:sensor_proto",
],
)

cpplint()
84 changes: 84 additions & 0 deletions modules/drivers/smartereye/compress_component.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/******************************************************************************
* Copyright 2020 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/

#include "modules/drivers/smartereye/compress_component.h"

#include <exception>
#include <vector>

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

namespace apollo {
namespace drivers {
namespace smartereye {

bool CompressComponent::Init() {
if (!GetProtoConfig(&config_)) {
AERROR << "Parse config file failed: " << ConfigFilePath();
return false;
}
AINFO << "Camera config: \n" << config_.DebugString();
try {
image_pool_.reset(new CCObjectPool<CompressedImage>(
config_.compress_conf().image_pool_size()));
image_pool_->ConstructAll();
} catch (const std::bad_alloc& e) {
AERROR << e.what();
return false;
}

writer_ = node_->CreateWriter<CompressedImage>(
config_.compress_conf().output_channel());
return true;
}

bool CompressComponent::Proc(const std::shared_ptr<Image>& image) {
ADEBUG << "procing compressed";
auto compressed_image = image_pool_->GetObject();
compressed_image->mutable_header()->CopyFrom(image->header());
compressed_image->set_frame_id(image->frame_id());
compressed_image->set_measurement_time(image->measurement_time());
compressed_image->set_format(image->encoding() + "; jpeg compressed bgr8");

std::vector<int> params;
params.resize(3, 0);
params[0] = CV_IMWRITE_JPEG_QUALITY;
params[1] = 95;

try {
cv::Mat mat_image(image->height(), image->width(), CV_8UC3,
const_cast<char*>(image->data().data()), image->step());
cv::Mat tmp_mat;
cv::cvtColor(mat_image, tmp_mat, cv::COLOR_RGB2BGR);
std::vector<uint8_t> compress_buffer;
if (!cv::imencode(".jpg", tmp_mat, compress_buffer, params)) {
AERROR << "cv::imencode (jpeg) failed on input image";
return false;
}
compressed_image->set_data(compress_buffer.data(), compress_buffer.size());
writer_->Write(compressed_image);
} catch (std::exception& e) {
AERROR << "cv::imencode (jpeg) exception :" << e.what();
return false;
}
return true;
}

} // namespace smartereye
} // namespace drivers
} // namespace apollo
50 changes: 50 additions & 0 deletions modules/drivers/smartereye/compress_component.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/******************************************************************************
* Copyright 2020 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/

#pragma once

#include <memory>

#include "cyber/base/concurrent_object_pool.h"
#include "cyber/cyber.h"
#include "modules/drivers/proto/sensor_image.pb.h"
#include "modules/drivers/smartereye/proto/config.pb.h"

namespace apollo {
namespace drivers {
namespace smartereye {

using apollo::cyber::Component;
using apollo::cyber::Writer;
using apollo::cyber::base::CCObjectPool;
using apollo::drivers::Image;
using apollo::drivers::smartereye::config::Config;

class CompressComponent : public Component<Image> {
public:
bool Init() override;
bool Proc(const std::shared_ptr<Image>& image) override;

private:
std::shared_ptr<CCObjectPool<CompressedImage>> image_pool_ = nullptr;
std::shared_ptr<Writer<CompressedImage>> writer_ = nullptr;
Config config_;
};

CYBER_REGISTER_COMPONENT(CompressComponent)
} // namespace smartereye
} // namespace drivers
} // namespace apollo
33 changes: 33 additions & 0 deletions modules/drivers/smartereye/conf/smartereye.pb.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
camera_dev: "/dev/camera/smartereye"
frame_id: "smartereye"
pixel_format: "yuyv"
io_method: IO_METHOD_MMAP
width: 1280
height: 720
frame_rate: 20
monochrome: false
brightness: -1
contrast: -1
saturation: -1
sharpness: -1
gain: -1
auto_focus: false
focus: -1
auto_exposure: true
exposure: 100
auto_white_balance: true
white_balance: 4000
bytes_per_pixel: 2
trigger_internal: 0
trigger_fps: 20
channel_name: "/apollo/sensor/smartereye"
channel_name_image: "/apollo/sensor/smartereye/image"
channel_name_image_compressed: "/apollo/sensor/smartereye/image/compressed"
device_wait_ms: 2000
spin_rate: 200
output_type: RGB

compress_conf {
output_channel: "/apollo/sensor/smartereye/image/compressed"
image_pool_size: 100
}
22 changes: 22 additions & 0 deletions modules/drivers/smartereye/dag/smartereye.dag
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Define all coms in DAG streaming.
module_config {
module_library : "/apollo/bazel-bin/modules/drivers/smartereye/libsmartereye_component.so"
components {
class_name : "SmartereyeComponent"
config {
name : "smartereye"
config_file_path : "/apollo/modules/drivers/smartereye/conf/smartereye.pb.txt"
}
}
components {
class_name : "CompressComponent"
config {
name : "smartereye_compress"
config_file_path : "/apollo/modules/drivers/smartereye/conf/smartereye.pb.txt"
readers {
channel: "/apollo/sensor/smartereye/image"
pending_queue_size: 10
}
}
}
}
7 changes: 7 additions & 0 deletions modules/drivers/smartereye/launch/smartereye.launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<cyber>
<module>
<name>smartereye</name>
<dag_conf>/apollo/modules/drivers/smartereye/dag/smartereye.dag</dag_conf>
<process_name>smartereye</process_name>
</module>
</cyber>
27 changes: 27 additions & 0 deletions modules/drivers/smartereye/proto/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
load("//tools:cpplint.bzl", "cpplint")

package(default_visibility = ["//visibility:public"])

cc_library(
name = "camera_proto",
deps = [
":sensor_camera_proto",
],
)

cc_proto_library(
name = "sensor_camera_proto",
deps = [
":camera_proto_lib",
],
)

proto_library(
name = "camera_proto_lib",
srcs = ["config.proto"],
deps = [
"//modules/common/proto:header_proto_lib",
],
)

cpplint()
58 changes: 58 additions & 0 deletions modules/drivers/smartereye/proto/config.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
syntax = "proto2";

package apollo.drivers.smartereye.config;

enum IOMethod {
IO_METHOD_UNKNOWN = 0;
IO_METHOD_READ = 1;
IO_METHOD_MMAP = 2;
IO_METHOD_USERPTR = 3;
}

enum OutputType {
YUYV = 0;
RGB = 1;
}

message Config {
optional string camera_dev = 1;
optional string frame_id = 2;
// v4l pixel format
optional string pixel_format = 3 [default = "yuyv"];
// mmap, userptr, read
optional IOMethod io_method = 4;
optional uint32 width = 5;
optional uint32 height = 6;
optional uint32 frame_rate = 7;
optional bool monochrome = 8 [default = false];

optional int32 brightness = 9 [default = -1];
optional int32 contrast = 10 [default = -1];
optional int32 saturation = 11 [default = -1];
optional int32 sharpness = 12 [default = -1];
optional int32 gain = 13 [default = -1];

optional bool auto_focus = 14 [default = false];
optional int32 focus = 15 [default = -1];
optional bool auto_exposure = 16 [default = true];
optional int32 exposure = 17 [default = 100];
optional bool auto_white_balance = 18 [default = true];
optional int32 white_balance = 19 [default = 4000];
optional uint32 bytes_per_pixel = 20 [default = 3];
optional uint32 trigger_internal = 21 [default = 0];
optional uint32 trigger_fps = 22 [default = 30];
optional string channel_name = 23;
optional string channel_name_image = 24;
optional string channel_name_image_compressed = 25;
// wait time when camera select timeout
optional uint32 device_wait_ms = 26 [default = 2000];
// camera select spin time
optional uint32 spin_rate = 27 [default = 200];
optional OutputType output_type = 28;

message CompressConfig {
optional string output_channel = 1;
optional uint32 image_pool_size = 2 [default = 20];
}
optional CompressConfig compress_conf = 29;
}
Loading

0 comments on commit 939441e

Please sign in to comment.