HardwareCanSourceTest.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00024 //----------------------------------------------------------------------
00025 #include "HardwareCanSourceTest.h"
00026 
00027 #include <sstream>
00028 #include <boost/date_time/posix_time/posix_time.hpp>
00029 
00030 #include <icl_core_config/Config.h>
00031 #include <icl_hardware_can/Logging.h>
00032 #include <icl_sourcesink/SimpleURI.h>
00033 
00034 namespace icl_hardware {
00035 namespace can {
00036 
00037 HardwareCanSourceTest::HardwareCanSourceTest(const std::string& uri, const std::string& name)
00038   : HardwareCanSource(uri, name),
00039     m_rng(),
00040     m_can_id_distribution(1, 2047),
00041     m_data_distribution(0, 255),
00042     m_buffer(),
00043     m_rate(100),
00044     m_pattern(P_LINEAR),
00045     m_next_id(1)
00046 {
00047   icl_sourcesink::SimpleURI parsed_uri(uri);
00048   boost::optional<float> rate = parsed_uri.getQuery<float>("rate");
00049   if (rate)
00050   {
00051     m_rate = *rate;
00052   }
00053 
00054   boost::optional<std::string> pattern = parsed_uri.getQuery<std::string>("pattern");
00055   if (pattern)
00056   {
00057     if (*pattern == "Fixed")
00058     {
00059       m_pattern = P_FIXED;
00060     }
00061     else if (*pattern == "ChangingRandom")
00062     {
00063       m_pattern = P_CHANGING_RANDOM;
00064     }
00065     else if (*pattern == "ChangingWithRepeat")
00066     {
00067       m_pattern = P_CHANGING_WITH_REPEAT;
00068     }
00069     else if (*pattern == "Linear")
00070     {
00071       m_pattern = P_LINEAR;
00072     }
00073     else
00074     {
00075       LOGGING_WARNING_C(CAN, HardwareCanSourceTest,
00076                         "Unknown pattern '" << *pattern << "', falling back to Linear." << endl);
00077       m_pattern = P_LINEAR;
00078     }
00079   }
00080 
00081   if (m_pattern == P_FIXED)
00082   {
00083     boost::optional<uint16_t> id_value = parsed_uri.getQuery<uint16_t>("id_value");
00084     if (id_value)
00085     {
00086       if (*id_value > 0 && *id_value < 2048)
00087       {
00088         m_next_id = *id_value;
00089       }
00090       else
00091       {
00092         LOGGING_WARNING_C(CAN, HardwareCanSourceTest,
00093                           "ID value " << *id_value << ", out of range, starting at " << m_next_id << "." << endl);
00094       }
00095     }
00096   }
00097 
00098   advance();
00099 }
00100 
00101 bool HardwareCanSourceTest::advance()
00102 {
00103   if (m_buffer)
00104   {
00105     // Simulate a frame rate.
00106     icl_core::os::usleep(long(1e6 / m_rate));
00107   }
00108 
00109   m_buffer.reset(
00110     new CanMessageStamped(
00111       tCanMessage(createCanMessageId()),
00112       icl_core::DataHeader(
00113         "",
00114         boost::posix_time::microsec_clock::local_time(),
00115         m_buffer ? m_buffer->header().sequence_number+1 : 0)));
00116   m_buffer->header().dsin = m_buffer->header().sequence_number;
00117 
00118   // Generate message payload.
00119   (*m_buffer)->rtr = 0;
00120   (*m_buffer)->dlc = 8;
00121   for (std::size_t i = 0; i < 8; ++i)
00122   {
00123     (*m_buffer)->data[i] = m_data_distribution(m_rng);
00124   }
00125 
00126   return true;
00127 }
00128 
00129 unsigned int HardwareCanSourceTest::createCanMessageId()
00130 {
00131   unsigned int id;
00132   switch (m_pattern)
00133   {
00134     case P_FIXED:
00135     {
00136       id = m_next_id;
00137       break;
00138     }
00139     case P_CHANGING_RANDOM:
00140     {
00141       id = m_can_id_distribution(m_rng);
00142       break;
00143     }
00144     case P_CHANGING_WITH_REPEAT:
00145     {
00146       id = m_next_id;
00147       unsigned int factor(33), offset(54);
00148       m_next_id = (m_next_id % (2048 / factor)) + offset;
00149       break;
00150     }
00151     case P_LINEAR:
00152     default:
00153     {
00154       id = m_next_id++;
00155       if (m_next_id >= 2048)
00156       {
00157         m_next_id = 1;
00158       }
00159       break;
00160     }
00161   }
00162 
00163   return id;
00164 }
00165 
00166 }
00167 }


fzi_icl_can
Author(s):
autogenerated on Thu Jun 6 2019 20:26:01