PageRenderTime 64ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/man/Man.cpp

https://github.com/alucyk/nbites
C++ | 238 lines | 145 code | 45 blank | 48 comment | 6 complexity | ee9a05cf5688c45e16448764cf686165 MD5 | raw file
  1. // This file is part of Man, a robotic perception, locomotion, and
  2. // team strategy application created by the Northern Bites RoboCup
  3. // team of Bowdoin College in Brunswick, Maine, for the Aldebaran
  4. // Nao robot.
  5. //
  6. // Man is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Lesser Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // Man is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Lesser Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // and the GNU Lesser Public License along with Man. If not, see
  18. // <http://www.gnu.org/licenses/>.
  19. #include <iostream>
  20. #include <sstream>
  21. #include <time.h>
  22. #include <sys/time.h>
  23. #include <Python.h>
  24. #include <boost/shared_ptr.hpp>
  25. #include <boost/assign/std/vector.hpp>
  26. using namespace boost::assign;
  27. #include "Man.h"
  28. #include "manconfig.h"
  29. #include "corpus/synchro.h"
  30. #include "VisionDef.h"
  31. #include "Common.h"
  32. #include "PyRoboGuardian.h"
  33. #include "PySensors.h"
  34. #include "PyLights.h"
  35. #include "PySpeech.h"
  36. #include "memory/log/IOProviderFactory.h"
  37. //#include <valgrind/callgrind.h>
  38. using namespace std;
  39. using boost::shared_ptr;
  40. using namespace man::memory;
  41. using namespace man::memory::log;
  42. /////////////////////////////////////////
  43. // //
  44. // Module class function definitions //
  45. // //
  46. /////////////////////////////////////////
  47. Man::Man (shared_ptr<Profiler> _profiler,
  48. shared_ptr<Sensors> _sensors,
  49. shared_ptr<Transcriber> _transcriber,
  50. shared_ptr<ImageTranscriber> _imageTranscriber,
  51. shared_ptr<MotionEnactor> _enactor,
  52. shared_ptr<Synchro> synchro,
  53. shared_ptr<Lights> _lights,
  54. shared_ptr<Speech> _speech)
  55. : profiler(_profiler),
  56. sensors(_sensors),
  57. transcriber(_transcriber),
  58. imageTranscriber(_imageTranscriber),
  59. enactor(_enactor),
  60. lights(_lights),
  61. speech(_speech)
  62. {
  63. // initialize system helper modules
  64. #ifdef USE_TIME_PROFILING
  65. profiler->profiling = true;
  66. profiler->profileFrames(1400);
  67. #endif
  68. // give python a pointer to the sensors structure. Method defined in
  69. // Sensors.h
  70. set_sensors_pointer(sensors);
  71. imageTranscriber->setSubscriber(this);
  72. pose = shared_ptr<NaoPose>(new NaoPose(sensors));
  73. guardian = shared_ptr<RoboGuardian>(new RoboGuardian(synchro, sensors));
  74. // initialize core processing modules
  75. #ifdef USE_MOTION
  76. motion = shared_ptr<Motion>(new Motion(synchro, enactor, sensors,pose));
  77. guardian->setMotionInterface(motion->getInterface());
  78. #endif
  79. // initialize python roboguardian module.
  80. // give python a pointer to the guardian. Method defined in PyRoboguardian.h
  81. set_guardian_pointer(guardian);
  82. set_lights_pointer(_lights);
  83. set_speech_pointer(_speech);
  84. vision = shared_ptr<Vision>(new Vision(pose));
  85. set_vision_pointer(vision);
  86. comm = shared_ptr<Comm>(new Comm(synchro, sensors, vision));
  87. memory = shared_ptr<Memory>(new Memory(vision, sensors));
  88. loggingBoard = shared_ptr<LoggingBoard>(new LoggingBoard(memory));
  89. set_logging_board_pointer(loggingBoard);
  90. memory->addSubscriber(loggingBoard.get());
  91. #ifdef USE_MEMORY
  92. loggingBoard->newIOProvider(IOProviderFactory::newAllObjectsProvider());
  93. #endif
  94. #ifdef USE_NOGGIN
  95. noggin = shared_ptr<Noggin>(new Noggin(vision,comm,guardian,
  96. sensors, loggingBoard,
  97. motion->getInterface()));
  98. #endif// USE_NOGGIN
  99. }
  100. Man::~Man ()
  101. {
  102. cout << "Man destructor" << endl;
  103. exit(0);
  104. }
  105. void Man::startSubThreads() {
  106. #ifdef DEBUG_MAN_THREADING
  107. cout << "Man::start" << endl;
  108. #endif
  109. // Start Comm thread (it handles its own threading
  110. if (comm->start() != 0)
  111. cerr << "Comm failed to start" << endl;
  112. else
  113. comm->getTrigger()->await_on();
  114. #ifdef USE_MOTION
  115. // Start Motion thread (it handles its own threading
  116. if (motion->start() != 0)
  117. cerr << "Motion failed to start" << endl;
  118. else
  119. motion->getTrigger()->await_on();
  120. #endif
  121. if(guardian->start() != 0)
  122. cout << "RoboGuardian failed to start" << endl;
  123. else
  124. guardian->getTrigger()->await_on();
  125. #ifdef DEBUG_MAN_THREADING
  126. cout << " run :: Signalling start" << endl;
  127. #endif
  128. // printf("Start time: %lli \n", process_micro_time());
  129. // CALLGRIND_START_INSTRUMENTATION;
  130. // CALLGRIND_TOGGLE_COLLECT;
  131. }
  132. void Man::stopSubThreads() {
  133. guardian->stop();
  134. guardian->getTrigger()->await_off();
  135. #ifdef DEBUG_MAN_THREADING
  136. cout << " Guardian thread is stopped" << endl;
  137. #endif
  138. #ifdef DEBUG_MAN_THREADING
  139. cout << " Man stoping:" << endl;
  140. #endif
  141. #ifdef USE_MOTION
  142. // Finished with run loop, stop sub-threads and exit
  143. motion->stop();
  144. motion->getTrigger()->await_off();
  145. #ifdef DEBUG_MAN_THREADING
  146. cout << " Motion thread is stopped" << endl;
  147. #endif
  148. #endif
  149. //TODO: fix this from hanging
  150. // comm->stop();
  151. // comm->getTrigger()->await_off();
  152. // @jfishman - tool will not exit, due to socket blocking
  153. //comm->getTOOLTrigger()->await_off();
  154. #ifdef DEBUG_MAN_THREADING
  155. cout << " Comm thread is stopped" << endl;
  156. #endif
  157. //hack - this ensures we exit with no segfault
  158. //atm naoqi crashes when it tries to call exit on the dcm
  159. }
  160. void
  161. Man::processFrame ()
  162. {
  163. #ifdef USE_VISION
  164. // Need to lock image and vision angles for duration of
  165. // vision processing to ensure consistency.
  166. sensors->lockImage();
  167. #ifdef USE_MEMORY
  168. // TODO: this is temporarily here
  169. loggingBoard->log(MIMAGE_ID);
  170. #endif
  171. PROF_ENTER(P_VISION);
  172. vision->notifyImage(sensors->getImage());
  173. PROF_EXIT(P_VISION);
  174. sensors->releaseImage();
  175. #endif
  176. #ifdef USE_MEMORY
  177. memory->updateVision();
  178. loggingBoard->log(MVISION_ID);
  179. #endif
  180. #ifdef USE_NOGGIN
  181. noggin->runStep();
  182. #endif
  183. PROF_ENTER(P_LIGHTS);
  184. lights->sendLights();
  185. PROF_EXIT(P_LIGHTS);
  186. }
  187. void Man::notifyNextVisionImage() {
  188. transcriber->postVisionSensors();
  189. // Process current frame
  190. processFrame();
  191. // Make sure messages are printed
  192. fflush(stdout);
  193. }