velodyne_driver_component.cc 2.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/******************************************************************************
 * Copyright 2017 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.
 *****************************************************************************/

fengqikai1414's avatar
fengqikai1414 已提交
17
#include <memory>
18 19 20
#include <string>
#include <thread>

G
GoLancer 已提交
21
#include "cyber/cyber.h"
22 23

#include "modules/common/util/message_util.h"
24
#include "modules/drivers/lidar/velodyne/driver/velodyne_driver_component.h"
25 26 27 28 29 30 31 32

namespace apollo {
namespace drivers {
namespace velodyne {

bool VelodyneDriverComponent::Init() {
  AINFO << "Velodyne driver component init";
  Config velodyne_config;
fengqikai1414's avatar
fengqikai1414 已提交
33
  if (!GetProtoConfig(&velodyne_config)) {
34 35 36 37 38
    return false;
  }
  AINFO << "Velodyne config: " << velodyne_config.DebugString();
  // start the driver
  writer_ = node_->CreateWriter<VelodyneScan>(velodyne_config.scan_channel());
fengqikai1414's avatar
fengqikai1414 已提交
39
  VelodyneDriver *driver = VelodyneDriverFactory::CreateDriver(velodyne_config);
40 41 42 43
  if (driver == nullptr) {
    return false;
  }
  dvr_.reset(driver);
fengqikai1414's avatar
fengqikai1414 已提交
44
  dvr_->Init();
45 46 47 48 49 50 51 52 53 54 55
  // spawn device poll thread
  runing_ = true;
  device_thread_ = std::shared_ptr<std::thread>(
      new std::thread(std::bind(&VelodyneDriverComponent::device_poll, this)));
  device_thread_->detach();

  return true;
}

/** @brief Device poll thread main loop. */
void VelodyneDriverComponent::device_poll() {
G
GoLancer 已提交
56
  while (!apollo::cyber::IsShutdown()) {
57 58
    // poll device until end of file
    std::shared_ptr<VelodyneScan> scan = std::make_shared<VelodyneScan>();
fengqikai1414's avatar
fengqikai1414 已提交
59
    bool ret = dvr_->Poll(scan);
60
    if (ret) {
61
      common::util::FillHeader("velodyne", scan.get());
62 63 64 65 66 67 68 69 70 71 72 73 74
      writer_->Write(scan);
    } else {
      AWARN << "device poll failed";
    }
  }

  AERROR << "CompVelodyneDriver thread exit";
  runing_ = false;
}

}  // namespace velodyne
}  // namespace drivers
}  // namespace apollo