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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
 * @file SimSparkController.h
 *
 * @author <a href="mailto:xu@informatik.hu-berlin.de">Xu Yuan</a>
 * @breief Interface for the SimSpark simulator
 *
 */

#ifndef _SIMSPARKCONTROLLER_H
#define _SIMSPARKCONTROLLER_H


#include <map>

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


#include "SimSparkGameInfo.h"
#include "MessagesSPL/SPLStandardMessage.h"

#include <Tools/Communication/SocketStream/SocketStream.h>

#include "PlatformInterface/PlatformInterface.h"
#include <DebugCommunication/DebugCommandExecutor.h>
#include "DebugCommunication/DebugServer.h"

#include "sfsexp/SexpParser.h"
#include <Extern/libb64/decode.h>
#include <Extern/libb64/encode.h>
#include <set>
#include <mutex>
#include <condition_variable>

using namespace naoth;

// TODO: make this better
#define MAX_TEAM_MESSAGE_SIZE 1024

class SimSparkController : public PlatformInterface, DebugCommandExecutor
{
private:
  std::string thePlatformName;

  GSocket* socket;
  PrefixedSocketStream theSocket;

  // sensor data
  std::map<std::string, JointData::JointID> theJointSensorNameMap;
  std::map<JointData::JointID, std::string> theJointMotorNameMap;

  char* theTeamMessageReceiveBuffer;

  char* theImageData;
  unsigned int theImageSize;
  bool isNewImage;
  bool isNewVirtualVision;
  VirtualVision theVirtualVision;
  VirtualVisionTop theVirtualVisionTop;
  base64::Decoder theBase64Decoder;
  base64::Encoder theBase64Encoder;

  SensorJointData theLastSensorJointData;
  GyrometerData theGyroData;
  AccelerometerData theAccelerometerData;
  FSRData theFSRData;
  FrameInfo theFrameInfo;
  BatteryData theBatteryData;
  GPSData theGPSData;
  UltraSoundReceiveData theUSData;

  int theCameraId;
  CameraInfo theCameraInfo;

  double theSenseTime;
  double theStepTime; // the time of last step in seconds
  
  SimSparkGameInfo theGameInfo;
  InertialSensorData theInertialSensorData;
  SensorJointData theSensorJointData;
  TeamMessageDataOut theTeamMessageDataOut; // message to other robots
  TeamMessageDataIn theTeamMessageDataIn; // message from other robots
  double theIMU[2];

  std::list<MotorJointData> theMotorJointData;
  std::string theSync;
  bool theSyncMode;

  // set of unknown messages to be ignored
  std::set<std::string> ignore;
  bool ignoreOpponentMsg;

public:
  SimSparkController(const std::string& name);

  virtual ~SimSparkController();

  virtual std::string getBodyID() const;
  virtual std::string getBodyNickName() const;
  virtual std::string getHeadNickName() const;
  virtual std::string getRobotName() const { return getBodyNickName(); }
  virtual std::string getPlatformName() const { return thePlatformName; }
  virtual unsigned int getBasicTimeStep() const { return 20; }

  /////////////////////// init ///////////////////////
  bool init(const std::string& modelPath, const std::string& teamName, unsigned int teamNumber, unsigned int num, const std::string& server, unsigned int port, bool sync);<--- Function 'init' argument 4 names different: declaration 'num' definition 'playerNumber'.

  void main();

  virtual void executeDebugCommand(
    const std::string& command, const std::map<std::string,std::string>& arguments,
    std::ostream &outstream);

public:
  void get(FrameInfo& data);

  void get(SensorJointData& data);

  void get(AccelerometerData& data);

  void get(Image& data);

  void get(GyrometerData& data);

  void get(FSRData& data);

  void get(InertialSensorData& data);

  void get(BatteryData& data);
  void get(GPSData& data);
  void get(UltraSoundReceiveData& data);

  void get(VirtualVision& data);
  void get(VirtualVisionTop& data);

  void get(TeamMessageDataIn& data);

  void get(GameData& data);

  /////////////////////// set ///////////////////////
  void set(const MotorJointData& data);

  void set(const TeamMessageDataOut& data);

protected:
  virtual MessageQueue* createMessageQueue(const std::string& name);
  
  // the main loop in single thread
  void singleThreadMain();

  // the main loop in threads
  void multiThreadsMain();

  void updateInertialSensor();

private:
  bool connect(const std::string& host, int port);

  bool getSensorData(std::string& data);

  bool updateSensors(std::string& msg);

  int parseString(char* data, std::string& value);
  int parseInt(char* data, int& value);
  int parseImage(char* data);
  bool parsePoint3D(const sexp_t* sexp, Vector3d& result) const;
  bool parseTeamInfo(const sexp_t* team, std::vector<naoth::GameData::RobotInfo>& players);

  bool updateImage(const sexp_t* sexp);
  bool updateHingeJoint(const sexp_t* sexp);
  bool updateGyro(const sexp_t* sexp);
  bool updateAccelerometer(const sexp_t* sexp);
  bool updateBattery(const sexp_t* sexp);
  bool updateSonar(const sexp_t* sexp);
  bool updateGPS(const sexp_t* sexp);
  bool updateGameInfo(const sexp_t* sexp);
  bool updateFSR(const sexp_t* sexp);
  bool updateSee(VirtualVision& virtualVision, const sexp_t* sexp);
  bool updateIMU(const sexp_t* sexp);

  Vector3d decomposeForce(double f, double fx, double fy, const Vector3d& c0, const Vector3d& c1, const Vector3d& c2);

  void calFSRForce(double f, double x, double y, 
              const Vector3d* positions,
              std::vector<double>& values,
              FSRData::SensorID id0, FSRData::SensorID id1, FSRData::SensorID id2);

  void say();

  bool hear(const sexp_t* sexp);

  void beam(const Vector3<double>& p);

  void autoBeam();

  void jointControl();

  void initPosition();

public:
  void motionLoop();

  void cognitionLoop();

  void senseLoop();

  void actLoop();

  virtual void getMotionInput();

  virtual void setMotionOutput();

  virtual void getCognitionInput();

  virtual void setCognitionOutput();

  virtual void callCognition();

private:
  // members for threads
  std::mutex  theCognitionInputMutex;
  std::mutex  theCognitionOutputMutex;
  std::condition_variable theCognitionInputCond;
  double maxJointAbsSpeed;
  bool exiting;

  //
  unsigned int theLastActTime;
  unsigned int theLastSenseTime;
  unsigned int theNextActTime;
  void calculateNextActTime();
  std::condition_variable theTimeCond;
  std::mutex theTimeMutex;

  std::string theSensorData;
  std::mutex theSensorDataMutex;
  std::condition_variable theSensorDataCond;

  std::mutex  theActDataMutex;
  std::stringstream theActData;
  void act();

private:
  DebugServer theDebugServer;

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  /* _SIMSPARKCONTROLLER_H */