debug-real-time-tracer.cpp 3.16 KB
Newer Older
1
2
3
4
5
6
7
8
/* Copyright 2019, LAAS-CNRS
 *
 * Olivier Stasse
 *
 */

#include <iostream>

9
#include <dynamic-graph/command.h>
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/signal-ptr.h>
#include <dynamic-graph/signal-time-dependent.h>
#include <dynamic-graph/tracer-real-time.h>
#define BOOST_TEST_MODULE debug - tracer

#include <boost/test/output_test_stream.hpp>
#include <boost/test/unit_test.hpp>
using boost::test_tools::output_test_stream;

namespace dynamicgraph {
struct MyEntity : public dynamicgraph::Entity {
  static const std::string CLASS_NAME;

  dynamicgraph::Signal<double, int> m_sigdSIN;
  dynamicgraph::SignalTimeDependent<double, int> m_sigdTimeDepSOUT;
  dynamicgraph::SignalTimeDependent<double, int> m_sigdTwoTimeDepSOUT;

Olivier Stasse's avatar
Olivier Stasse committed
31
  explicit MyEntity(const std::string &name)
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
      : Entity(name),
        m_sigdSIN("MyEntity(" + name + ")::input(double)::in_double"),
        m_sigdTimeDepSOUT(boost::bind(&MyEntity::update, this, _1, _2),
                          m_sigdSIN,
                          "MyEntity(" + name + ")::input(double)::out_double"),
        m_sigdTwoTimeDepSOUT(
            boost::bind(&MyEntity::update, this, _1, _2), m_sigdSIN,
            "MyEntity(" + name + ")::input(double)::out2double")

  {
    signalRegistration(m_sigdSIN << m_sigdTimeDepSOUT << m_sigdTwoTimeDepSOUT);
  }

  double &update(double &res, const int &inTime) {
    const double &aDouble = m_sigdSIN(inTime);
    res = aDouble;
    return res;
  }
};
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(MyEntity, "MyEntity");
} // namespace dynamicgraph

BOOST_AUTO_TEST_CASE(test_tracer) {
  using namespace dynamicgraph;
56

57
58
  // Creates a tracer.
  TracerRealTime &atracer = *dynamic_cast<TracerRealTime *>(
59
      FactoryStorage::getInstance()->newEntity("TracerRealTime", "my-tracer"));
60

61
62
  MyEntity &entity = *dynamic_cast<MyEntity *>(
      FactoryStorage::getInstance()->newEntity("MyEntity", "my-entity"));
63
64
65
66
67
68
69
70
71
72

  std::string rootdir("/tmp");
  std::string basename("my-tracer");
  std::string suffix(".dat");

  /// Test openfiles
  atracer.openFiles(rootdir, basename, suffix);

  /// Add trace by name
  atracer.addSignalToTraceByName("my-entity.out_double", "output");
73

74
75
76
  /// Add trace by name
  SignalBase<int> &aSignal = entity.getSignal("out2double");

77
78
  entity.m_sigdTwoTimeDepSOUT.recompute(2);

79
80
  Signal<double, int> &aSignalInt =
      *(dynamic_cast<Signal<double, int> *>(&entity.getSignal("in_double")));
81
82
83

  aSignalInt.setConstant(1.5);
  atracer.start();
84

85
86
  atracer.trace();

87
88
89
  std::string emptybuf_cmd_str("empty");
  command::Command *acmd = atracer.getNewStyleCommand(emptybuf_cmd_str);
  acmd->execute();
90
91
92
93
94
  for (int i = 0; i < 1000; i++) {
    aSignal.setTime(i);
    aSignalInt.setTime(i);
    atracer.recordTrigger(i, i);
  }
95
96
  output_test_stream output;
  atracer.display(output);
97
98
99
100

  atracer.stop();
  atracer.clearSignalToTrace();
  atracer.closeFiles();
101
  acmd->execute();
102
103
  atracer.record();

104
105
106
107
108
  BOOST_CHECK(output.is_equal(
      "TracerRealTime my-tracer [mode=play] : \n"
      "  - Dep list: \n"
      "     -> MyEntity(my-entity)::input(double)::out_double (in output)"
      "	[0Mo/1Mo]	\n"));
109
}