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
/**
 * @file NaoController.h
 *
 * @author <a href="mailto:xu@informatik.hu-berlin.de">Xu, Yuan</a>
 * @author <a href="mailto:mellmann@informatik.hu-berlin.de">Mellmann, Heinrich</a>
 * @brief Interface for the real robot for both cognition and motion
 *
 */

#ifndef _NaoController_H_
#define _NaoController_H_

#include <string>
#include <fstream>
#include <iostream>
#include <sys/stat.h>

//
#include "PlatformInterface/PlatformInterface.h"
#include "Tools/Communication/MessageQueue/MessageQueue4Threads.h"
//#include "Tools/Debug/Stopwatch.h"

//
#include "V4lCameraHandler.h"
#include "CameraSettingsV5Manager.h"
#include "CameraSettingsV6Manager.h"


#include "SoundControl.h"
#include "SPLGameController.h"
#include "CPUTemperatureReader.h"
#include "DebugCommunication/DebugServer.h"
#include "AudioRecorder.h"

#include "Tools/Communication/Network/BroadCaster.h"
#include "Tools/Communication/Network/UDPReceiver.h"

// representations
#include <Representations/Infrastructure/FrameInfo.h>
#include "Representations/Infrastructure/TeamMessageData.h"
#include "Representations/Infrastructure/RemoteMessageData.h"
#include "Representations/Infrastructure/GameData.h"
#include "Representations/Infrastructure/SoundData.h"
#include "Representations/Infrastructure/AudioData.h"

// local tools
#include "Tools/DCMData.h"
#include "Tools/NaoTime.h"
#include "Tools/SharedMemoryIO.h"

namespace naoth
{

class NaoController : public PlatformInterface
{
public:
  NaoController();
  virtual ~NaoController();

  // platform info
  virtual std::string getBodyID() const { return theBodyID; }
  virtual std::string getBodyNickName() const { return theBodyNickName; }
  virtual std::string getHeadNickName() const { return theHeadNickName; }
  virtual std::string getRobotName() const { return theRobotName; }
  virtual std::string getPlatformName() const { return "Nao"; }
  virtual unsigned int getBasicTimeStep() const { return lolaAvailable?12:10; }
  
  // camera stuff
  void get(Image& data){ 
    theBottomCameraHandler.get(data); 
  } // blocking
  void get(ImageTop& data){ 
    theTopCameraHandler.get(data); 
  } // non blocking
  
  void get(CurrentCameraSettings& data) { 
    theBottomCameraHandler.getCameraSettings(data);
  }
  void get(CurrentCameraSettingsTop& data) { 
    theTopCameraHandler.getCameraSettings(data);
  }
  
  void set(const CameraSettingsRequest& data);<--- Function 'set' argument 1 names different: declaration 'data' definition 'request'.
  void set(const CameraSettingsRequestTop& data);<--- Function 'set' argument 1 names different: declaration 'data' definition 'request'.

  // sound
  void set(const SoundPlayData& data)
  {
    theSoundHandler->setSoundData(data);
  }

  // teamcomm stuff
  void get(TeamMessageDataIn& data) { theTeamCommListener->receive(data.data); }
  void set(const TeamMessageDataOut& data) { theTeamCommSender->send(data.data); }

  void get(RemoteMessageDataIn& data) { theRemoteCommandListener->receive(data.data); }

  // gamecontroller stuff
  void get(GameData& data){ theGameController->get(data); }
  void set(const GameReturnData& data) { theGameController->set(data); }

  // debug comm
  void get(DebugMessageInCognition& data) { theDebugServer->getDebugMessageInCognition(data); }
  void get(DebugMessageInMotion& data) { theDebugServer->getDebugMessageInMotion(data); }
  void set(const DebugMessageOut& data) { theDebugServer->setDebugMessageOut(data); }

  // time
  void get(FrameInfo& data)
  {
    //TODO: use naoSensorData.data().timeStamp
    data.setTime(NaoTime::getNaoTimeInMilliSeconds());
    data.setFrameNumber(data.getFrameNumber()+1);
  }

  // read directly from the shared memory
  void get(SensorJointData& data) { naoSensorData.get(data); }
  void get(AccelerometerData& data) { naoSensorData.get(data); }
  void get(GyrometerData& data) { naoSensorData.get(data); }
  void get(FSRData& data) { naoSensorData.get(data); }
  void get(InertialSensorData& data) { naoSensorData.get(data); }
  void get(ButtonData& data) { naoSensorData.get(data); }
  void get(BatteryData& data) { naoSensorData.get(data); }
  void get(UltraSoundReceiveData& data) { naoSensorData.get(data); }
  
  void get(AudioData& data) { theAudioRecorder.get(data); }
  void get(CpuData& data) { theCPUTemperatureReader.get(data); }

  // write directly to the shared memory
  // ACHTUNG: each set calls swapWriting()
  void set(const MotorJointData& data) { naoCommandMotorJointData.set(data); }
  void set(const LEDData& data) { naoCommandLEDData.set(data); }
  void set(const UltraSoundSendData& data) { naoCommandUltraSoundSendData.set(data); }

  void set(const AudioControl& data) { theAudioRecorder.set(data); }

  virtual void getMotionInput()
  {
    //STOPWATCH_START("getMotionInput");
    // try to get some data from the DCM
    if ( !naoSensorData.swapReading() )
    {
      std::cerr << "[NaoController] didn't get new sensor data" << std::endl;
    }
    PlatformInterface::getMotionInput();
    //STOPWATCH_STOP("getMotionInput");
  }


  virtual void setMotionOutput()
  {
    //STOPWATCH_START("setMotionOutput");
    PlatformInterface::setMotionOutput();
    //STOPWATCH_STOP("setMotionOutput");
  }


  virtual void getCognitionInput()
  {
    //STOPWATCH_START("getCognitionInput");
    PlatformInterface::getCognitionInput();
    //STOPWATCH_STOP("getCognitionInput");
  }


  virtual void setCognitionOutput()
  {
    //STOPWATCH_START("setCognitionOutput");
    PlatformInterface::setCognitionOutput();
    //STOPWATCH_STOP("setCognitionOutput");
  }


protected:
  virtual MessageQueue* createMessageQueue(const std::string& /*name*/)
  {
    return new MessageQueue4Threads();
  }

  inline bool fileExists (const std::string& filename) {
    struct stat buffer;   
    return (stat (filename.c_str(), &buffer) == 0); 
  }

protected:
  std::string theBodyID;
  std::string theBodyNickName;
  std::string theHeadNickName;
  std::string theRobotName;

  bool lolaAvailable;

  // -- begin -- shared memory access --
  // DCM --> NaoController
  SharedMemoryReader<DCMSensorData> naoSensorData;

  // NaoController --> DCM
  SharedMemoryWriter<Accessor<MotorJointData> > naoCommandMotorJointData;
  SharedMemoryWriter<Accessor<UltraSoundSendData> > naoCommandUltraSoundSendData;
  SharedMemoryWriter<Accessor<LEDData> > naoCommandLEDData;
  // -- end -- shared memory access --

  //
  
  V4lCameraHandler theBottomCameraHandler;
  V4lCameraHandler theTopCameraHandler;
  
  SoundControl *theSoundHandler;
  BroadCaster* theTeamCommSender;
  UDPReceiver* theTeamCommListener;
  UDPReceiver* theRemoteCommandListener;
  SPLGameController* theGameController;
  DebugServer* theDebugServer;
  CPUTemperatureReader theCPUTemperatureReader;
  AudioRecorder theAudioRecorder;
};

} // end namespace naoth

#endif // _NAO_CONTROLLER_BASE_H_