HardwareCanSourceTest.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
24 //----------------------------------------------------------------------
25 #include "HardwareCanSourceTest.h"
26 
27 #include <sstream>
28 #include <boost/date_time/posix_time/posix_time.hpp>
29 
30 #include <icl_core_config/Config.h>
32 #include <icl_sourcesink/SimpleURI.h>
33 
34 namespace icl_hardware {
35 namespace can {
36 
37 HardwareCanSourceTest::HardwareCanSourceTest(const std::string& uri, const std::string& name)
38  : HardwareCanSource(uri, name),
39  m_rng(),
40  m_can_id_distribution(1, 2047),
41  m_data_distribution(0, 255),
42  m_buffer(),
43  m_rate(100),
44  m_pattern(P_LINEAR),
45  m_next_id(1)
46 {
47  icl_sourcesink::SimpleURI parsed_uri(uri);
48  boost::optional<float> rate = parsed_uri.getQuery<float>("rate");
49  if (rate)
50  {
51  m_rate = *rate;
52  }
53 
54  boost::optional<std::string> pattern = parsed_uri.getQuery<std::string>("pattern");
55  if (pattern)
56  {
57  if (*pattern == "Fixed")
58  {
60  }
61  else if (*pattern == "ChangingRandom")
62  {
64  }
65  else if (*pattern == "ChangingWithRepeat")
66  {
68  }
69  else if (*pattern == "Linear")
70  {
72  }
73  else
74  {
76  "Unknown pattern '" << *pattern << "', falling back to Linear." << endl);
78  }
79  }
80 
81  if (m_pattern == P_FIXED)
82  {
83  boost::optional<uint16_t> id_value = parsed_uri.getQuery<uint16_t>("id_value");
84  if (id_value)
85  {
86  if (*id_value > 0 && *id_value < 2048)
87  {
88  m_next_id = *id_value;
89  }
90  else
91  {
93  "ID value " << *id_value << ", out of range, starting at " << m_next_id << "." << endl);
94  }
95  }
96  }
97 
98  advance();
99 }
100 
102 {
103  if (m_buffer)
104  {
105  // Simulate a frame rate.
106  icl_core::os::usleep(long(1e6 / m_rate));
107  }
108 
109  m_buffer.reset(
110  new CanMessageStamped(
113  "",
114  boost::posix_time::microsec_clock::local_time(),
115  m_buffer ? m_buffer->header().sequence_number+1 : 0)));
116  m_buffer->header().dsin = m_buffer->header().sequence_number;
117 
118  // Generate message payload.
119  (*m_buffer)->rtr = 0;
120  (*m_buffer)->dlc = 8;
121  for (std::size_t i = 0; i < 8; ++i)
122  {
123  (*m_buffer)->data[i] = m_data_distribution(m_rng);
124  }
125 
126  return true;
127 }
128 
130 {
131  unsigned int id;
132  switch (m_pattern)
133  {
134  case P_FIXED:
135  {
136  id = m_next_id;
137  break;
138  }
139  case P_CHANGING_RANDOM:
140  {
142  break;
143  }
145  {
146  id = m_next_id;
147  unsigned int factor(33), offset(54);
148  m_next_id = (m_next_id % (2048 / factor)) + offset;
149  break;
150  }
151  case P_LINEAR:
152  default:
153  {
154  id = m_next_id++;
155  if (m_next_id >= 2048)
156  {
157  m_next_id = 1;
158  }
159  break;
160  }
161  }
162 
163  return id;
164 }
165 
166 }
167 }
bool advance()
Advance to the next data element.
Pattern m_pattern
The message generation pattern.
boost::random::uniform_int_distribution< unsigned int > m_can_id_distribution
Uniform distribution over possible CAN IDs.
icl_sourcesink::DataSource< tCanMessage > HardwareCanSource
Base type for all sources providing tCanMessage data.
uint16_t m_next_id
The next CAN message ID to be used.
HardwareCanSourceTest(const std::string &uri="", const std::string &name="HardwareCanSourceTest")
Constructor.
float m_rate
Data generation rate in Hz.
#define LOGGING_WARNING_C(streamname, classname, arg)
int usleep(unsigned long useconds)
ThreadStream & endl(ThreadStream &stream)
CanMessageStamped::Ptr m_buffer
Buffers the latest generated message.
boost::random::uniform_int_distribution< uint8_t > m_data_distribution
Uniform distribution over possible payload bytes.
icl_core::Stamped< tCanMessage > CanMessageStamped
Definition: tCanMessage.h:79
The IDs are created by a function, IDs can repeat.
The IDs will be created randomly (seed is fixed).
icl_hardware::can::tCanMessage tCanMessage
Definition: UseMCACan.h:39
boost::random::mt19937 m_rng
Random number generator.
unsigned short uint16_t


fzi_icl_can
Author(s):
autogenerated on Mon Jun 10 2019 13:17:02