dbc_bag.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2015-2020, Dataspeed Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Dataspeed Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 #include <ros/ros.h>
36 #include <ros/package.h>
37 #include <rosbag/bag.h>
38 #include <rosbag/view.h>
39 #include <boost/foreach.hpp>
40 #include <can_msgs/Frame.h>
41 #include <dataspeed_can_msgs/Frame.h>
42 
43 #include "CanExtractor.h"
44 
45 void printHelp() {
46  printf("Usage: dbc_bag <bag_file> <topic> <dbc_file> [dbc_files]... [-O output_file]\n");
47  printf(" [--unknown / -u] [--expand / -e]\n");
48  printf(" [--lz4] [--bz2]\n");
49  printf(" [--help / -h]\n");
50 }
51 
52 int main(int argc, char** argv)
53 {
54  // Arguments
55  std::string bag_file_in;
56  std::string bag_file_out;
57  std::string topic;
58  std::vector<std::string> dbc_files;
59  bool _expand = false;
60  bool _unknown = false;
61  bool _lz4 = false;
62  bool _bz2 = false;
63 
64  // Parse command line arguments
65  unsigned int count = 0;
66  for (int i = 1; i < argc; i++) {
67  std::string str = argv[i];
68  if (str == "--help" || str == "-h") {
69  printHelp();
70  return 0;
71  } else if (str == "--unknown" || str == "-u") {
72  _unknown = true;
73  } else if (str == "--expand" || str == "-e") {
74  _expand = true;
75  } else if (str == "--lz4") {
76  _lz4 = true;
77  } else if (str == "--bz2") {
78  _bz2 = true;
79  } else if (str == "-O") {
80  i++;
81  if (i < argc) {
82  bag_file_out = argv[i];
83  }
84  } else {
85  if (count == 0) {
86  bag_file_in = str;
87  } else if (count == 1) {
88  topic = str;
89  } else {
90  dbc_files.push_back(str);
91  }
92  count++;
93  }
94  }
95  if (count < 3) {
96  printHelp();
97  return 1;
98  }
99  if (bag_file_out.empty()) {
100  bag_file_out = bag_file_in + ".dbc.bag";
101  }
102 
103  printf("Opening input bag file: '%s'\n", bag_file_in.c_str());
104  rosbag::Bag raw_bag;
105  raw_bag.open(bag_file_in, rosbag::bagmode::Read);
106 
107  printf("Processing can_msgs/Frame on topic: '%s'\n", topic.c_str());
108  rosbag::View view(raw_bag, rosbag::TopicQuery(topic));
109 
110  printf("Opening dbc files: \n");
111  for (size_t i = 0; i < dbc_files.size(); i++) {
112  printf(" - %s\n", dbc_files[i].c_str());
113  }
114  dataspeed_can_tools::CanExtractor extractor(dbc_files, true, _expand, _unknown);
115 
116  printf("Opening output bag file: '%s'\n", bag_file_out.c_str());
118  if (_lz4) {
119  compression = rosbag::compression::LZ4;
120  } else if (_bz2) {
121  compression = rosbag::compression::BZ2;
122  }
123  extractor.openBag(bag_file_out, compression);
124 
125  const ros::Time stamp_end = view.getEndTime();
126  const ros::Time stamp_begin = view.getBeginTime();
127  if (stamp_end > stamp_begin) {
128  int last_percent = 0;
129  BOOST_FOREACH(rosbag::MessageInstance const m, view) {
130  ros::Time stamp = ros::TIME_MAX;
131  if (m.isType<can_msgs::Frame>()) {
132  can_msgs::Frame::ConstPtr msg = m.instantiate<can_msgs::Frame>();
134  can_msg.id = msg->id | (msg->is_extended ? 0x80000000 : 0x00000000);
135  extractor.getMessage(can_msg);
136  extractor.pubMessage(msg, m.getTime());
137  stamp = msg->header.stamp;
138  } else if (m.isType<dataspeed_can_msgs::Frame>()) {
139  dataspeed_can_msgs::Frame::ConstPtr msg = m.instantiate<dataspeed_can_msgs::Frame>();
141  can_msg.id = msg->id | (msg->extended ? 0x80000000 : 0x00000000);
142  extractor.getMessage(can_msg);
143  extractor.pubMessage(msg, m.getTime());
144  stamp = msg->header.stamp;
145  } else {
146  static bool warned = false;
147  if (!warned) {
148  warned = true;
149  printf("Unknown message type %s %s\n", m.getDataType().c_str(), m.getMD5Sum().c_str());
150  }
151  }
152  if (stamp != ros::TIME_MAX) {
153  int percent = 100 * (stamp - stamp_begin).toSec() / (stamp_end - stamp_begin).toSec();
154  if (percent >= last_percent) {
155  printf("Processing: %d%% complete\n", last_percent);
156  last_percent += 10;
157  }
158  }
159  }
160  } else {
161  printf("Warning: no messages\n");
162  }
163 
164  extractor.closeBag();
165  printf("Successfully wrote parsed CAN data to bag\n");
166 
167  return 0;
168 }
void open(std::string const &filename, uint32_t mode=bagmode::Read)
ros::Time const & getTime() const
std::string const & getMD5Sum() const
void printHelp()
Definition: dbc_bag.cpp:45
ROSTIME_DECL const Time TIME_MAX
bool openBag(const std::string &fname, rosbag::compression::CompressionType compression=rosbag::compression::Uncompressed)
boost::shared_ptr< T > instantiate() const
std::string const & getDataType() const
void pubMessage(const can_msgs::Frame &msg, const ros::Time &stamp=ros::Time(0))
bool getMessage(RosCanMsgStruct &can_msg)
int main(int argc, char **argv)
Definition: dbc_bag.cpp:52


dataspeed_can_tools
Author(s): Micho Radovnikovich
autogenerated on Wed Feb 8 2023 03:16:49