1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* 
 * File:   Simulator.h
 * Author: thomas
 *
 * Created on 4. Dezember 2008, 16:51
 */

#ifndef _SIMULATOR_H
#define _SIMULATOR_H

#include <iostream>
#include <strstream>

#include <Representations/Infrastructure/FrameInfo.h>
#include <Representations/Infrastructure/JointData.h>
#include <Representations/Infrastructure/Image.h>
#include <Representations/Infrastructure/AccelerometerData.h>
#include <Representations/Infrastructure/GyrometerData.h>
#include <Representations/Infrastructure/InertialSensorData.h>
#include <Representations/Infrastructure/CameraSettings.h>
#include <Representations/Infrastructure/ButtonData.h>
#include <Representations/Infrastructure/FSRData.h>
#include <Representations/Infrastructure/BatteryData.h>
#include <Representations/Infrastructure/UltraSoundData.h>
#include <Representations/Infrastructure/LEDData.h>
#include <Representations/Infrastructure/SoundData.h>
#include <Representations/Infrastructure/GameData.h>

#include "DebugCommunication/DebugServer.h"
#include "PlatformInterface/PlatformInterface.h"

#include <Tools/Debug/DebugRequest.h>

#include <Tools/Logfile/LogFileScanner.h>
#include "LogProviderModule.h"
#include <google/protobuf/io/zero_copy_stream_impl.h>

class Simulator : public PlatformInterface
{
public:
  Simulator(const std::string& filePath, bool backendMode, bool realTime, unsigned short port);
  virtual ~Simulator(){}

  virtual std::string getBodyID() const { return "naoth-logsimulator"; }
  virtual std::string getBodyNickName() const {return "naoth"; }
  virtual std::string getHeadNickName() const {return "naoth"; }
  virtual std::string getRobotName() const { return "naoth-logsimulator"; }
  virtual std::string getPlatformName() const { return "LogSimulator"; }
  virtual unsigned int getBasicTimeStep() const { return 20; }

  void main(bool start = false);<--- Function 'main' argument 1 names different: declaration 'start' definition 'autostart'.

  void printRepresentations();
  void printHelp();
  void printCurrentLineInfo();

  void stepForward();
  void stepBack();
  void jumpToBegin();
  void jumpToEnd();
  void jumpTo(unsigned int position);
  void play(bool loop = false);

  int getCurrentFrame() { return *currentFrame; }
  int getMinFrame() { return *(logFileScanner.begin());}
  int getMaxFrame() { return *(logFileScanner.last());}
  
  /////////////////////// get ///////////////////////
  template<class T> void generalGet(T& data, std::string name) const;

  #define SIM_GET(rep) void get(rep& data) const {generalGet(data,#rep);}
  
  // generic get
  template<class T> void get(T& data) const { generalGet(data, typeid(T).name()); }
  
  // do nothing
  template<class T> void set(const T& /*data*/){}

  //SIM_GET(FrameInfo);
  void get(unsigned int& /*timestamp*/) const {}

  
  SIM_GET(FrameInfo);
  SIM_GET(SensorJointData);
  SIM_GET(AccelerometerData);
  SIM_GET(Image);
  SIM_GET(ImageTop);
  SIM_GET(GyrometerData);
  SIM_GET(FSRData);
  SIM_GET(InertialSensorData);
  SIM_GET(CurrentCameraSettings);
  SIM_GET(ButtonData);
  SIM_GET(BatteryData);
  SIM_GET(UltraSoundReceiveData);
  

  /////////////////////// set ///////////////////////
  //virtual void set(const MotorJointData& /*data*/){};
  //virtual void set(const CameraSettingsRequest& /*data*/){};
  //virtual void set(const LEDData& /*data*/){};
  //virtual void set(const UltraSoundSendData& /*data*/){};
  //virtual void set(const SoundData& /*data*/){};

  /////////////////////// init ///////////////////////
  void open(const std::string& filePath);
  virtual void init();
  
  const LogFileScanner::Frame& getRepresentations() {
    return representations;
  }

  const std::set<std::string>& getIncludedRepresentations() {
    return logFileScanner.getIncludedRepresentations();
  }

protected:
  virtual MessageQueue* createMessageQueue(const std::string& name);


public:
  // the flag for backend mode, which is enables reading commands from stdin
  bool backendMode;
  // play the logfile according to the time of the frames
  bool realTime;

  LogFileScanner logFileScanner;
  LogFileScanner::FrameIterator currentFrame;
  LogFileScanner::Frame representations;

  unsigned int lastFrameTime;
  // the simulated time and the framenumber contineously increases even if the logfile is played backwards (!)
  unsigned int simulatedTime;
  unsigned int simulatedFrameNumber;

  char getInput();

  /**
   * reads in the frame which begins at the current frame position and executes
   * the "processes"
   */
  void executeCurrentFrame();

  /** 
   * if the frame info is not available: create new one
   * the time grows monotonously
   */
  void adjust_frame_time();

private:
  DebugServer theDebugServer;
  unsigned short debugPort;

public:
  void get(DebugMessageInCognition& data) {
    theDebugServer.getDebugMessageInCognition(data);
  }
  void get(DebugMessageInMotion& data) {
    theDebugServer.getDebugMessageInMotion(data);
  }

  void set(const DebugMessageOut& data) {
    if(data.answers.size() > 0) {
      theDebugServer.setDebugMessageOut(data);
    }
  }
};

#endif  /* _SIMULATOR_H */