You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2644 lines
90 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
7 years ago
​
6 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
6 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
​
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
6 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
7 years ago
​
6 years ago
​
7 years ago
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
5 years ago
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
5 years ago
​
​
​
​
​
​
​
5 years ago
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
5 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
7 years ago
​
​
3 years ago
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
7 years ago
​
7 years ago
7 years ago
​
​
7 years ago
​
​
7 years ago
7 years ago
7 years ago
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
5 years ago
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
5 years ago
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
  1. //----------------------------------------------------------------------------
  2. // Dr.Frucht Systems Ltd.
  3. // Copyright � 2000-2017. All Rights Reserved.
  4. // Version 2.0 7/20/2017
  5. // Last modification: 7/20/2017
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: LRF_HardwareManager.cpp
  9. // AUTHOR: gagua
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Header file of LRF_HardwareManager.cpp
  13. //----------------------------------------------------------------------------
  14. //
  15. #include "LRF_HardwareManager.h"
  16. #define LOG( X ) m_pLog->Log( "HW Manager", this, X )
  17. #define LOG_ERROR( X ) m_pLog->Log( "HW Manager", this, _LOG_ERROR, X )
  18. // Constructor implementation
  19. LRF_HardwareManager::LRF_HardwareManager( config_setting_t *SNode ){
  20. m_pLog = basicLogCollection::getInstance();
  21. m_pSettingsProvider = basicSettingsProvider::getInstance();
  22. if( m_pSettingsProvider ) {
  23. m_pSettings = m_pSettingsProvider->getSubNode( SNode, "HARDWARE_MANAGER" );
  24. if( m_pSettingsProvider->isCompromised() ) {
  25. m_pSettingsProvider->validate();
  26. };
  27. } else {
  28. m_pSettings = NULL;
  29. };
  30. m_bWorkEnable = true;
  31. m_bInitOK = false;
  32. m_bDumpMemoryToDisk = false;
  33. m_nFlipEnaFLags = 0;
  34. m_nDetectionFilter = 0;
  35. m_nTHVisualFlags = 0;
  36. m_eSOC = SOC_UNKNOWN;
  37. memset( &m_sRuntimeStatus, 0, sizeof(HWM_CSTATUS));
  38. #ifdef IGNORE_PRU_FLIP
  39. m_bStudyCompleteFlag = 0;
  40. m_bDataFilledTH = 0;
  41. #endif
  42. #ifdef THREADED_FLIP
  43. m_pFlipThreadHandler = NULL;
  44. m_bWaitFirstFlip = true;
  45. m_bFlipUnlockRequested = false;
  46. m_pFlipMutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
  47. if( m_pFlipMutex != NULL ) {
  48. memset( m_pFlipMutex, 0, sizeof(pthread_mutex_t));
  49. pthread_mutexattr_t attrib;
  50. pthread_mutexattr_settype( &attrib, PTHREAD_MUTEX_ERRORCHECK );
  51. if( 0 != pthread_mutex_init( m_pFlipMutex, &attrib ) ){
  52. free( m_pFlipMutex );
  53. m_pFlipMutex = NULL;
  54. LOG_ERROR("Cannot Create Flip Protector Mutex!");
  55. } else {
  56. m_pFlipThreadHandler = (pthread_t *) malloc(sizeof(pthread_t));
  57. if( m_pFlipThreadHandler != NULL ) {
  58. memset( m_pFlipThreadHandler, 0, sizeof(pthread_t));
  59. } else {
  60. pthread_mutex_destroy(m_pFlipMutex);
  61. free(m_pFlipMutex);
  62. m_pFlipMutex = NULL;
  63. LOG_ERROR("Cannot Allocate Flip Thread!");
  64. };
  65. };
  66. } else {
  67. LOG_ERROR("Cannot Allocate Flip Protector Mutex!");
  68. };
  69. #endif
  70. #ifndef PRU_SLAVE
  71. #ifndef ONPC_TOTTAL_SIMULATOR
  72. #ifdef THREADED_MOTORS_START
  73. m_pMotorCtrlThreadHandler = NULL;
  74. m_pMotorCtrlMutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
  75. if( m_pMotorCtrlMutex != NULL ) {
  76. memset( m_pMotorCtrlMutex, 0, sizeof(pthread_mutex_t));
  77. pthread_mutexattr_t attrib;
  78. pthread_mutexattr_settype( &attrib, PTHREAD_MUTEX_ERRORCHECK );
  79. if( 0 != pthread_mutex_init( m_pMotorCtrlMutex, &attrib ) ){
  80. free( m_pMotorCtrlMutex );
  81. m_pMotorCtrlMutex = NULL;
  82. LOG_ERROR("Cannot Create Motor Ctrl Protector Mutex!");
  83. } else {
  84. m_pMotorCtrlThreadHandler = (pthread_t *) malloc(sizeof(pthread_t));
  85. if( m_pMotorCtrlThreadHandler != NULL ) {
  86. memset( m_pMotorCtrlThreadHandler, 0, sizeof(pthread_t));
  87. } else {
  88. pthread_mutex_destroy(m_pMotorCtrlMutex);
  89. free(m_pMotorCtrlMutex);
  90. m_pMotorCtrlMutex = NULL;
  91. LOG_ERROR("Cannot Allocate Motor Ctrl Thread!");
  92. };
  93. };
  94. } else {
  95. LOG_ERROR("Cannot Allocate Motor Ctrl Protector Mutex!");
  96. };
  97. #endif
  98. #else
  99. LOG("Motors are disabled by Build - TOTTAL SIMULATION BUILD!");
  100. #endif
  101. //m_bMotorsViaTGZ = false;
  102. m_pCanBus = NULL;
  103. m_pTGZdevice = NULL;
  104. #else
  105. LOG("Motors are disabled by Build - SLAVE LRF BUILD!");
  106. #endif
  107. memset( &m_sPARAMETERS, 0 ,sizeof( LGS_3D_PARAMETERS ));
  108. m_sPARAMETERS.flags.BaseScanCount = BASE_SCAN_COUNT;
  109. m_sPARAMETERS.flags.DetectorsCount = KERNEL_COUNT;
  110. m_sPARAMETERS.flags.MirrorScanCount = Q_SCAN_COUNT;
  111. m_sPARAMETERS.flags.MirrorsCount = Q_MIRROR_COUNT;
  112. m_vMemoryBlock[0] = NULL;
  113. m_vMemoryBlock[1] = NULL;
  114. m_vMemoryBlock[2] = NULL;
  115. m_vMemoryBlock[3] = NULL;
  116. #ifndef ONPC_TOTTAL_SIMULATOR
  117. m_pSOC = classSOC::getInstance();
  118. if( m_pSOC!= NULL ) {
  119. m_bInitOK = readPercs();
  120. } else {
  121. LOG_ERROR( "SOC Control module failure!" );
  122. };
  123. if( m_bInitOK && m_pSettings!= NULL ){
  124. m_bInitOK = create( m_pSettings );
  125. };
  126. #endif
  127. };
  128. // Destructor implementation
  129. LRF_HardwareManager::~LRF_HardwareManager(){
  130. endService();
  131. if( m_vMemoryBlock[0]!=NULL )free( m_vMemoryBlock[0]);
  132. if( m_vMemoryBlock[1]!=NULL )free( m_vMemoryBlock[1]);
  133. if( m_vMemoryBlock[2]!=NULL )free( m_vMemoryBlock[2]);
  134. if( m_vMemoryBlock[3]!=NULL )free( m_vMemoryBlock[3]);
  135. #ifndef PRU_SLAVE
  136. if( m_pTGZdevice ){
  137. LOG( "Deleting TGZ device");
  138. delete( m_pTGZdevice );
  139. };
  140. //ToDo: Check CAN Bus Destructor - Hangs
  141. //ToDo: Delete Threads Mem
  142. if( m_pCanBus ){
  143. LOG( "Deleting CAN bus");
  144. delete( m_pCanBus );
  145. };
  146. #endif
  147. LOG( "HW manager - DONE!");
  148. };
  149. bool LRF_HardwareManager::readPercs( void ){
  150. int MEM_FD;
  151. void *PERC_MMAP;
  152. char Buffer[256];
  153. LOG("Reading SOC Percs");
  154. MEM_FD = open( "/dev/mem", O_RDWR | O_SYNC );
  155. if( MEM_FD == -1 ) {
  156. sprintf( Buffer, "Memory File Open Failed Error: %s", strerror(errno));
  157. LOG_ERROR( Buffer );
  158. return false;
  159. };
  160. PERC_MMAP = mmap( NULL,
  161. 0x2000,
  162. PROT_READ | PROT_WRITE,
  163. MAP_SHARED | MAP_LOCKED,
  164. MEM_FD,
  165. AM437X_SIGN_ZONE );
  166. close( MEM_FD );
  167. if( PERC_MMAP == MAP_FAILED ) {
  168. sprintf( Buffer, "Memory Region Map Failed Error: %s\n", strerror(errno));
  169. LOG_ERROR( Buffer );
  170. return false;
  171. };
  172. if( (*((uint32_t *)PERC_MMAP))== AM437X_SIGN_VAL ) {
  173. LOG( "Detected AM437x SOC Machine");
  174. m_eSOC = SOC_AM437X;
  175. munmap( PERC_MMAP, 0x2000 );
  176. return true;
  177. };
  178. munmap( PERC_MMAP, 0x2000 );
  179. MEM_FD = open( "/dev/mem", O_RDWR | O_SYNC );
  180. if( MEM_FD == -1 ) {
  181. sprintf( Buffer, "Memory File Open Failed Error: %s", strerror(errno));
  182. LOG_ERROR( Buffer );
  183. return false;
  184. };
  185. PERC_MMAP = mmap( NULL,
  186. 0x2000,
  187. PROT_READ | PROT_WRITE,
  188. MAP_SHARED | MAP_LOCKED,
  189. MEM_FD,
  190. AM355X_SIGN_ZONE );
  191. close( MEM_FD );
  192. if( PERC_MMAP == MAP_FAILED ) {
  193. sprintf( Buffer, "Memory Region Map Failed Error: %s\n", strerror(errno));
  194. LOG_ERROR( Buffer );
  195. return false;
  196. };
  197. if( (*((volatile uint32_t *)PERC_MMAP))== AM355X_SIGN_VAL ) {
  198. LOG( "Detected AM355x SOC Machine");
  199. m_eSOC = SOC_AM335X;
  200. munmap( PERC_MMAP, 0x2000 );
  201. return true;
  202. };
  203. munmap( PERC_MMAP, 0x2000 );
  204. LOG( "Cannot detect SOC type");
  205. return false;
  206. };
  207. bool LRF_HardwareManager::create( config_setting_t *Settings ){
  208. config_setting_t *pSetting;
  209. int tVal;
  210. char tBuffer[256];
  211. if( Settings != NULL ) {
  212. LOG("HW Getting Settings ...");
  213. if( !config_setting_lookup_bool( Settings, HWM_SETTINGS_TH_DUMP, &tVal ) ){
  214. tVal = m_bDumpMemoryToDisk = false;
  215. pSetting = config_setting_add( Settings, HWM_SETTINGS_TH_DUMP, CONFIG_TYPE_BOOL );
  216. config_setting_set_bool( pSetting, tVal );
  217. m_pSettingsProvider->compromise();
  218. LOG("No TH Memory Dumping defined");
  219. } else {
  220. m_bDumpMemoryToDisk = (bool)tVal;
  221. sprintf( tBuffer, "TH Memory Dumping Read From Settings: %d", tVal );
  222. LOG( tBuffer );
  223. };
  224. if( !config_setting_lookup_bool( Settings, HWM_SETTINGS_ROLE, &tVal ) ){
  225. tVal = m_sPARAMETERS.flags.isMaster = true;
  226. pSetting = config_setting_add( Settings, HWM_SETTINGS_ROLE, CONFIG_TYPE_BOOL );
  227. config_setting_set_bool( pSetting, tVal );
  228. m_pSettingsProvider->compromise();
  229. LOG("No role defined");
  230. } else {
  231. m_sPARAMETERS.flags.isMaster = (bool)tVal;
  232. sprintf( tBuffer, "Role Read From Settings: %d", tVal );
  233. LOG( tBuffer );
  234. };
  235. if( !config_setting_lookup_bool( Settings, HWM_SETTINGS_SUNBIT, &tVal ) ){
  236. tVal = m_sPARAMETERS.flags.Sunbit_filter = false;
  237. pSetting = config_setting_add( Settings, HWM_SETTINGS_SUNBIT, CONFIG_TYPE_BOOL );
  238. config_setting_set_bool( pSetting, tVal );
  239. m_pSettingsProvider->compromise();
  240. LOG("No SUNBIT action defined - Disabling!");
  241. } else {
  242. m_sPARAMETERS.flags.Sunbit_filter = (bool)tVal;
  243. sprintf( tBuffer, "SUNBIT Action Read From Settings: %d", tVal );
  244. LOG( tBuffer );
  245. };
  246. if( !config_setting_lookup_bool( Settings, HWM_SETTINGS_LRF_ENABLED, &tVal ) ){
  247. tVal = m_sPARAMETERS.flags.LaserPulsesOn = true;
  248. pSetting = config_setting_add( Settings, HWM_SETTINGS_LRF_ENABLED, CONFIG_TYPE_BOOL );
  249. config_setting_set_bool( pSetting, tVal );
  250. m_pSettingsProvider->compromise();
  251. LOG("No LRF Enabled");
  252. } else {
  253. m_sPARAMETERS.flags.LaserPulsesOn = (bool)tVal;
  254. sprintf( tBuffer, "LRF ENA Read From Settings: %d", tVal );
  255. LOG( tBuffer );
  256. };
  257. if( !config_setting_lookup_bool( Settings, HWM_SETTINGS_SIG_SYMULATOR, &tVal ) ){
  258. tVal = m_sPARAMETERS.flags.SignalSimulator = false;
  259. pSetting = config_setting_add( Settings, HWM_SETTINGS_SIG_SYMULATOR, CONFIG_TYPE_BOOL );
  260. config_setting_set_bool( pSetting, tVal );
  261. m_pSettingsProvider->compromise();
  262. LOG("No Sig Symulator Settings");
  263. } else {
  264. m_sPARAMETERS.flags.SignalSimulator = (bool)tVal;
  265. sprintf( tBuffer, "Sig Sym Read From Settings: %d", tVal );
  266. LOG( tBuffer );
  267. };
  268. if( !m_sPARAMETERS.flags.SignalSimulator ) {
  269. if( !config_setting_lookup_bool( Settings, HWM_SETTINGS_AUTOSTART_ENABLED, &tVal ) ){
  270. tVal = m_sPARAMETERS.flags.AutostartMotors = false;
  271. pSetting = config_setting_add( Settings, HWM_SETTINGS_AUTOSTART_ENABLED, CONFIG_TYPE_BOOL );
  272. config_setting_set_bool( pSetting, tVal );
  273. m_pSettingsProvider->compromise();
  274. LOG("No Motor Autostart Settings");
  275. } else {
  276. m_sPARAMETERS.flags.AutostartMotors = (bool)tVal;
  277. if( m_sPARAMETERS.flags.AutostartMotors ) {
  278. m_sPARAMETERS.flags.VerticalMotorOn = true;
  279. m_sPARAMETERS.flags.HorizontalMotorOn = true;
  280. };
  281. sprintf( tBuffer, "AutoStart Read From Settings: %d", tVal );
  282. LOG( tBuffer );
  283. };
  284. if( !m_sPARAMETERS.flags.AutostartMotors ) {
  285. if( !config_setting_lookup_bool( Settings, HWM_SETTINGS_CTRL_V_MOTOR, &tVal ) ){
  286. tVal = m_sPARAMETERS.flags.VerticalMotorOn = false;
  287. pSetting = config_setting_add( Settings, HWM_SETTINGS_CTRL_V_MOTOR, CONFIG_TYPE_BOOL );
  288. config_setting_set_bool( pSetting, tVal );
  289. m_pSettingsProvider->compromise();
  290. LOG("No Vertical Controll");
  291. } else {
  292. m_sPARAMETERS.flags.VerticalMotorOn = (bool)tVal;
  293. sprintf( tBuffer, "V-M Ctrl Read From Settings: %d", tVal );
  294. LOG( tBuffer );
  295. };
  296. if( !config_setting_lookup_bool( Settings, HWM_SETTINGS_CTRL_H_MOTOR, &tVal ) ){
  297. tVal = m_sPARAMETERS.flags.HorizontalMotorOn = false;
  298. pSetting = config_setting_add( Settings, HWM_SETTINGS_CTRL_H_MOTOR, CONFIG_TYPE_BOOL );
  299. config_setting_set_bool( pSetting, tVal );
  300. m_pSettingsProvider->compromise();
  301. LOG("No Horizontal Controll");
  302. } else {
  303. m_sPARAMETERS.flags.HorizontalMotorOn = (bool)tVal;
  304. sprintf( tBuffer, "H-M Ctrl Read From Settings: %d", tVal );
  305. LOG( tBuffer );
  306. };
  307. };
  308. }
  309. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_V_MOTOR_SETPOINT, &tVal )){
  310. tVal = m_sPARAMETERS.flags.VerticalFreq = Q_WORK_FREQ;
  311. pSetting = config_setting_add(Settings, HWM_SETTINGS_V_MOTOR_SETPOINT, CONFIG_TYPE_INT);
  312. config_setting_set_int(pSetting, tVal );
  313. m_pSettingsProvider->compromise();
  314. LOG("No Mirror Block Freq");
  315. } else {
  316. m_sPARAMETERS.flags.VerticalFreq = tVal;
  317. sprintf( tBuffer, "V-M Setpoint Read From Settings: %d", tVal );
  318. LOG( tBuffer );
  319. };
  320. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_H_MOTOR_SETPOINT, &tVal )){
  321. tVal = m_sPARAMETERS.flags.HorizontalFreq = B_WORK_FREQ;
  322. pSetting = config_setting_add(Settings, HWM_SETTINGS_H_MOTOR_SETPOINT, CONFIG_TYPE_INT);
  323. config_setting_set_int(pSetting, tVal );
  324. m_pSettingsProvider->compromise();
  325. LOG("No Base Block Freq");
  326. } else {
  327. m_sPARAMETERS.flags.HorizontalFreq = tVal;
  328. sprintf( tBuffer, "H-M Setpoint Read From Settings: %d", tVal );
  329. LOG( tBuffer );
  330. };
  331. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_V_PULSES_INTERLEAVE, &tVal )){
  332. tVal = m_sPARAMETERS.flags.QQinterlace = Q_P_INTERLEAVE;
  333. pSetting = config_setting_add(Settings, HWM_SETTINGS_V_PULSES_INTERLEAVE, CONFIG_TYPE_INT);
  334. config_setting_set_int(pSetting, tVal );
  335. m_pSettingsProvider->compromise();
  336. LOG("No Q Pulses per LRF pulse Setting");
  337. } else {
  338. m_sPARAMETERS.flags.QQinterlace = tVal;
  339. sprintf( tBuffer, "Q-Pulses per LRF pulse Read From Settings: %d", tVal );
  340. LOG( tBuffer );
  341. };
  342. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_TH_OFFSET, &tVal )){
  343. tVal = m_sPARAMETERS.nOffset = SIGNAL_OFFSET;
  344. pSetting = config_setting_add(Settings, HWM_SETTINGS_TH_OFFSET, CONFIG_TYPE_INT);
  345. config_setting_set_int(pSetting, tVal );
  346. m_pSettingsProvider->compromise();
  347. LOG("No TH Offset Setting");
  348. } else {
  349. m_sPARAMETERS.nOffset = tVal;
  350. sprintf( tBuffer, "TH Offset Read From Settings: %d", tVal );
  351. LOG( tBuffer );
  352. };
  353. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_TH_ORDER, &tVal )){
  354. tVal = m_sPARAMETERS.nOrder = SIGNAL_ORDER;
  355. pSetting = config_setting_add(Settings, HWM_SETTINGS_TH_ORDER, CONFIG_TYPE_INT);
  356. config_setting_set_int(pSetting, tVal );
  357. m_pSettingsProvider->compromise();
  358. LOG("No TH Order Setting");
  359. } else {
  360. m_sPARAMETERS.nOrder = tVal;
  361. #if defined( STRONG_STAT ) || !defined( THREADED_FLIP )
  362. if( m_sPARAMETERS.nOrder > 7 ) m_sPARAMETERS.nOrder = 7;
  363. #endif
  364. sprintf( tBuffer, "TH Order Read From Settings: %d", m_sPARAMETERS.nOrder );
  365. LOG( tBuffer );
  366. };
  367. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_TH_DELTA, &tVal )){
  368. tVal = m_sPARAMETERS.nDelta = SIGNAL_DELTA;
  369. pSetting = config_setting_add(Settings, HWM_SETTINGS_TH_DELTA, CONFIG_TYPE_INT);
  370. config_setting_set_int(pSetting, tVal );
  371. m_pSettingsProvider->compromise();
  372. LOG("No TH Delta Setting");
  373. } else {
  374. m_sPARAMETERS.nDelta = tVal;
  375. #if !defined( THREADED_FLIP )
  376. if( m_sPARAMETERS.nDelta > 127 ) m_sPARAMETERS.nDelta = 127;
  377. #endif
  378. sprintf( tBuffer, "TH Delta Read From Settings: %d", m_sPARAMETERS.nDelta );
  379. LOG( tBuffer );
  380. };
  381. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_TH_FLIP_SIZE, &tVal )){
  382. tVal = m_sPARAMETERS.nFlip = SCANS_TO_FLIP;
  383. pSetting = config_setting_add(Settings, HWM_SETTINGS_TH_FLIP_SIZE, CONFIG_TYPE_INT);
  384. config_setting_set_int(pSetting, tVal );
  385. m_pSettingsProvider->compromise();
  386. LOG("No TH Flip Size Setting");
  387. } else {
  388. m_sPARAMETERS.nFlip = tVal;
  389. sprintf( tBuffer, "Flip Size Read From Settings: %d", tVal );
  390. LOG( tBuffer );
  391. };
  392. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_TH_GOOD_POINTS, &tVal )){
  393. tVal = m_sPARAMETERS.nGPointSamples = TH_POINT_GOOD;
  394. pSetting = config_setting_add(Settings, HWM_SETTINGS_TH_GOOD_POINTS, CONFIG_TYPE_INT);
  395. config_setting_set_int(pSetting, tVal );
  396. m_pSettingsProvider->compromise();
  397. LOG("No TH Good Points Setting");
  398. } else {
  399. m_sPARAMETERS.nGPointSamples = tVal;
  400. #if defined( STRONG_STAT ) || !defined( THREADED_FLIP )
  401. if( m_sPARAMETERS.nGPointSamples > 15 ) m_sPARAMETERS.nGPointSamples = 15;
  402. #endif
  403. sprintf( tBuffer, "TH Good Points Read From Settings: %d", m_sPARAMETERS.nGPointSamples );
  404. LOG( tBuffer );
  405. };
  406. if( !config_setting_lookup_bool( Settings, HWM_SETTINGS_AUTO_TH_GENERATE, &tVal ) ){
  407. tVal = m_sPARAMETERS.flags.BuildThByCore = true;
  408. pSetting = config_setting_add( Settings, HWM_SETTINGS_AUTO_TH_GENERATE, CONFIG_TYPE_BOOL );
  409. config_setting_set_bool( pSetting, tVal );
  410. m_pSettingsProvider->compromise();
  411. LOG("No TH Build By Core Setting");
  412. } else {
  413. m_sPARAMETERS.flags.BuildThByCore = (bool)tVal;
  414. sprintf( tBuffer, "TH Build by Core Read From Settings: %d", tVal );
  415. LOG( tBuffer );
  416. };
  417. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_TH_SPIKE_SIZE, &tVal )){
  418. tVal = m_sPARAMETERS.flags.Spike_TH = 1;
  419. pSetting = config_setting_add(Settings, HWM_SETTINGS_TH_SPIKE_SIZE, CONFIG_TYPE_INT);
  420. config_setting_set_int(pSetting, tVal );
  421. m_pSettingsProvider->compromise();
  422. LOG("No Spike Threshold Setting");
  423. } else {
  424. m_sPARAMETERS.flags.Spike_TH = tVal;
  425. sprintf( tBuffer, "Spike Threshold Read From Settings: %d", tVal );
  426. LOG( tBuffer );
  427. };
  428. if( !config_setting_lookup_bool( Settings, HWM_SETTINGS_CAN_CONTROL_ENABLE, &tVal ) ){
  429. tVal = m_sPARAMETERS.flags.TGZ_canENA = false;
  430. pSetting = config_setting_add( Settings, HWM_SETTINGS_CAN_CONTROL_ENABLE, CONFIG_TYPE_BOOL );
  431. config_setting_set_bool( pSetting, tVal );
  432. m_pSettingsProvider->compromise();
  433. LOG("No CAN Enable Setting");
  434. } else {
  435. m_sPARAMETERS.flags.TGZ_canENA = (bool)tVal;
  436. sprintf( tBuffer, "CAN ENA Read From Settings: %d", tVal );
  437. LOG( tBuffer );
  438. };
  439. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_CAN_BUS_ID, &tVal )){
  440. tVal = m_sPARAMETERS.flags.TGZ_canBus = 1;
  441. pSetting = config_setting_add(Settings, HWM_SETTINGS_CAN_BUS_ID, CONFIG_TYPE_INT);
  442. config_setting_set_int(pSetting, tVal );
  443. m_pSettingsProvider->compromise();
  444. LOG("No CAN Bus ID Setting");
  445. } else {
  446. m_sPARAMETERS.flags.TGZ_canBus = tVal;
  447. sprintf( tBuffer, "CAN Bus ID Read From Settings: %d", tVal );
  448. LOG( tBuffer );
  449. };
  450. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_CAN_MY_ID, &tVal )){
  451. #ifndef TGD_VERSION_2
  452. tVal = m_sPARAMETERS.flags.TGZ_canSRC = 0x200;
  453. #else
  454. tVal = m_sPARAMETERS.flags.TGZ_canSRC = 0x300;
  455. #endif
  456. pSetting = config_setting_add(Settings, HWM_SETTINGS_CAN_MY_ID, CONFIG_TYPE_INT);
  457. config_setting_set_int(pSetting, tVal );
  458. m_pSettingsProvider->compromise();
  459. LOG("No CAN My ID Setting");
  460. } else {
  461. m_sPARAMETERS.flags.TGZ_canSRC = tVal;
  462. sprintf( tBuffer, "CAN My ID Read From Settings: 0x%X", tVal );
  463. LOG( tBuffer );
  464. };
  465. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_CAN_TGZ_ID, &tVal )){
  466. #ifndef TGD_VERSION_2
  467. tVal = m_sPARAMETERS.flags.TGZ_canDST = 0x201;
  468. #else
  469. tVal = m_sPARAMETERS.flags.TGZ_canDST = 0x301;
  470. #endif
  471. pSetting = config_setting_add(Settings, HWM_SETTINGS_CAN_TGZ_ID, CONFIG_TYPE_INT);
  472. config_setting_set_int(pSetting, tVal );
  473. m_pSettingsProvider->compromise();
  474. LOG("No CAN TGZ ID Setting");
  475. } else {
  476. m_sPARAMETERS.flags.TGZ_canDST = tVal;
  477. sprintf( tBuffer, "CAN TGZ ID Read From Settings: 0x%X", tVal );
  478. LOG( tBuffer );
  479. };
  480. #ifndef TGD_VERSION_2
  481. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_TGZ_DIVIDER, &tVal )){
  482. tVal = m_sPARAMETERS.flags.TGZ_divider = 1;
  483. pSetting = config_setting_add(Settings, HWM_SETTINGS_TGZ_DIVIDER, CONFIG_TYPE_INT);
  484. config_setting_set_int(pSetting, tVal );
  485. m_pSettingsProvider->compromise();
  486. LOG("No TGZ Divider Setting");
  487. } else {
  488. m_sPARAMETERS.flags.TGZ_divider = tVal;
  489. sprintf( tBuffer, "TGZ Div Read From Settings: %d", tVal );
  490. LOG( tBuffer );
  491. };
  492. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_TGZ_PRF, &tVal )){
  493. tVal = m_sPARAMETERS.flags.TGZ_ParamQ = 35; // 35000 kHz
  494. pSetting = config_setting_add(Settings, HWM_SETTINGS_TGZ_PRF, CONFIG_TYPE_INT);
  495. config_setting_set_int(pSetting, tVal );
  496. m_pSettingsProvider->compromise();
  497. LOG("No TGZ PRF Setting");
  498. } else {
  499. m_sPARAMETERS.flags.TGZ_ParamQ = tVal;
  500. sprintf( tBuffer, "TGZ PRF Read From Settings: %d", tVal );
  501. LOG( tBuffer );
  502. };
  503. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_TGZ_RATIO, &tVal )){
  504. tVal = m_sPARAMETERS.flags.TGZ_ParamB = 64;
  505. pSetting = config_setting_add(Settings, HWM_SETTINGS_TGZ_RATIO, CONFIG_TYPE_INT);
  506. config_setting_set_int(pSetting, tVal );
  507. m_pSettingsProvider->compromise();
  508. LOG("No TGZ Ratio Setting");
  509. } else {
  510. m_sPARAMETERS.flags.TGZ_ParamB = tVal;
  511. sprintf( tBuffer, "TGZ Ratio Read From Settings: %d", tVal );
  512. LOG( tBuffer );
  513. };
  514. #else
  515. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_TGZ_MIRROR_SPEED, &tVal )){
  516. tVal = m_sPARAMETERS.flags.TGZ_ParamQ = ( Q_WORK_FREQ >> 2 );
  517. pSetting = config_setting_add(Settings, HWM_SETTINGS_TGZ_MIRROR_SPEED, CONFIG_TYPE_INT);
  518. config_setting_set_int(pSetting, tVal );
  519. m_pSettingsProvider->compromise();
  520. LOG("No TGZ Mirror Block Speed");
  521. } else {
  522. m_sPARAMETERS.flags.TGZ_ParamQ = tVal;
  523. sprintf( tBuffer, "TGZ Mirror Block Speed Read From Settings: %d", tVal );
  524. LOG( tBuffer );
  525. };
  526. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_TGZ_BASE_SPEED, &tVal )){
  527. tVal = m_sPARAMETERS.flags.TGZ_ParamB = ( B_WORK_FREQ >> 4 );
  528. pSetting = config_setting_add(Settings, HWM_SETTINGS_TGZ_BASE_SPEED, CONFIG_TYPE_INT);
  529. config_setting_set_int(pSetting, tVal );
  530. m_pSettingsProvider->compromise();
  531. LOG("No TGZ Base Motor Speed");
  532. } else {
  533. m_sPARAMETERS.flags.TGZ_ParamB = tVal;
  534. sprintf( tBuffer, "TGZ Base Motor Speed Read From Settings: %d", tVal );
  535. LOG( tBuffer );
  536. };
  537. #endif
  538. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_B_SHIFT, &tVal )){
  539. tVal = m_sPARAMETERS.flags.B_shift = 0;
  540. pSetting = config_setting_add(Settings, HWM_SETTINGS_B_SHIFT, CONFIG_TYPE_INT);
  541. config_setting_set_int(pSetting, tVal );
  542. m_pSettingsProvider->compromise();
  543. LOG("No Base Shift Specified");
  544. } else {
  545. m_sPARAMETERS.flags.B_shift = tVal;
  546. sprintf( tBuffer, "Base Shift Read From Settings: %d", tVal );
  547. LOG( tBuffer );
  548. };
  549. if (!config_setting_lookup_int(Settings, HWM_SETTINGS_Q_SHIFT, &tVal )){
  550. tVal = m_sPARAMETERS.flags.Q_shift = 0;
  551. pSetting = config_setting_add(Settings, HWM_SETTINGS_Q_SHIFT, CONFIG_TYPE_INT);
  552. config_setting_set_int(pSetting, tVal );
  553. m_pSettingsProvider->compromise();
  554. LOG("No Mirror Shift Specified");
  555. } else {
  556. m_sPARAMETERS.flags.Q_shift = tVal;
  557. sprintf( tBuffer, "Mirror Shift Read From Settings: %d", tVal );
  558. LOG( tBuffer );
  559. };
  560. if( m_pSettingsProvider->isCompromised() ) {
  561. LOG("Save Settings");
  562. m_pSettingsProvider->validate();
  563. };
  564. LOG("HW Create(1) Started");
  565. beginService();
  566. LOG("HW Create(1) Done");
  567. };
  568. return m_bInitOK;
  569. };
  570. bool LRF_HardwareManager::getHardwareParams( HW_PARAMS &Params ){
  571. memcpy( &Params, &m_sPARAMETERS.flags, sizeof(HW_PARAMS) );
  572. return true;
  573. };
  574. bool LRF_HardwareManager::setHardwareParams( PHW_PARAMS Params ){
  575. memcpy( &m_sPARAMETERS.flags, &Params, sizeof(HW_PARAMS) );
  576. return true;
  577. };
  578. bool LRF_HardwareManager::getParams( LGS_3D_PARAMETERS &Params ){
  579. memcpy( &Params, &m_sPARAMETERS, sizeof(LGS_3D_PARAMETERS) );
  580. return true;
  581. };
  582. bool LRF_HardwareManager::create( HW_PARAMS &HS ){
  583. LOG("HW Create(2) Started");
  584. m_sPARAMETERS.flags.isMaster = HS.isMaster;
  585. beginService();
  586. LOG("HW Create(2) Done");
  587. return m_bInitOK;
  588. };
  589. void LRF_HardwareManager::beginService( void ){
  590. m_bInitOK = false;
  591. m_bWorkEnable = true;
  592. #ifndef ONPC_TOTTAL_SIMULATOR
  593. if( m_pSOC != NULL ) {
  594. LOG("Begin HW init process");
  595. LOG("Stoping all PRU Cores");
  596. if(!m_pSOC->pru_stop(0,0)){
  597. LOG_ERROR("Error Stoping PRU 0 0 Core");
  598. };
  599. if(!m_pSOC->pru_sreset(0,0)){
  600. LOG_ERROR("Error Resetting PRU 0 0 Core");
  601. };
  602. if(!m_pSOC->pru_disable(0,0)){
  603. LOG_ERROR("Error Disabling PRU 0 0 Core");
  604. };
  605. LOG("PRU 0 0 stoped");
  606. if(!m_pSOC->pru_stop(0,1)){
  607. LOG_ERROR("Error Stoping PRU 0 1 Core");
  608. };
  609. if(!m_pSOC->pru_sreset(0,1)){
  610. LOG_ERROR("Error Resetting PRU 0 1 Core");
  611. };
  612. if(!m_pSOC->pru_disable(0,1)){
  613. LOG_ERROR("Error Disabling PRU 0 1 Core");
  614. };
  615. LOG("PRU 0 1 stoped");
  616. if(!m_pSOC->pru_stop(1,0)){
  617. LOG_ERROR("Error Stoping PRU 1 0 Core");
  618. };
  619. if(!m_pSOC->pru_sreset(1,0)){
  620. LOG_ERROR("Error Resetting PRU 1 0 Core");
  621. };
  622. if(!m_pSOC->pru_disable(1,0)){
  623. LOG_ERROR("Error Disabling PRU 1 0 Core");
  624. };
  625. LOG("PRU 1 0 stoped");
  626. if(!m_pSOC->pru_stop(1,1)){
  627. LOG_ERROR("Error Stoping PRU 1 1 Core");
  628. };
  629. if(!m_pSOC->pru_sreset(1,1)){
  630. LOG_ERROR("Error Resetting PRU 1 1 Core");
  631. };
  632. if(!m_pSOC->pru_disable(1,1)){
  633. LOG_ERROR("Error Disabling PRU 1 1 Core");
  634. };
  635. LOG("PRU 1 1 stoped");
  636. #ifndef PRU_SLAVE
  637. if( m_sPARAMETERS.flags.isMaster ) {
  638. // If enable TGZ CAN connection
  639. if( m_sPARAMETERS.flags.TGZ_canENA ) {
  640. // Init TGZ connection
  641. char tBuf[16];
  642. sprintf( tBuf, "can%d", m_sPARAMETERS.flags.TGZ_canBus );
  643. m_pCanBus = new canBus( tBuf,
  644. m_sPARAMETERS.flags.TGZ_canSRC,
  645. m_sPARAMETERS.flags.TGZ_canDST );
  646. if( m_pCanBus != NULL ) {
  647. LOG("CAN Bus creation success");
  648. m_pTGZdevice = new protocolTGZ( m_pCanBus );
  649. if( m_pTGZdevice == NULL ) {
  650. LOG_ERROR("TGZ protocol creation failure");
  651. delete( m_pCanBus );
  652. m_pCanBus = NULL;
  653. }else{
  654. LOG("TGZ protocol creation success");
  655. };
  656. } else {
  657. LOG_ERROR("CAN Bus creation failure");;
  658. };
  659. };
  660. if( m_sPARAMETERS.flags.AutostartMotors ) {
  661. #ifndef NO_MOTORS
  662. LOG("Startup Motors");
  663. startMotors();
  664. #else
  665. LOG("Startup Motors Disabled by Compilation");
  666. #endif
  667. } else {
  668. LOG("No Auto Startup Motors");
  669. };
  670. LOG("Loading MASTER set of PRU Modules");
  671. /*
  672. The Order of cores to be loadedd is crucial.
  673. The first to load - last to function (Data Acq) that depends on others
  674. */
  675. if( !m_pSOC->pru_load( 1, 1, "PRU_1_1.bin", false ) ){
  676. LOG_ERROR("PRU 1 1 Load - FAILURE");
  677. return;
  678. };
  679. if( !m_pSOC->pru_load( 1, 0, "PRU_1_0.bin", false ) ){
  680. LOG_ERROR("PRU 1 0 Load - FAILURE");
  681. return;
  682. };
  683. if( !m_pSOC->pru_load( 0, 1, "PRU_0_1.bin", true ) ){
  684. LOG_ERROR("PRU 0 1 Load - FAILURE");
  685. return;
  686. };
  687. #ifdef NO_MOTORS
  688. LOG("Loading simulation of Motors");
  689. if( !m_pSOC->pru_load( 0, 0, "CORE0_NOMO.bin", true ) ){
  690. LOG_ERROR("PRU 0 0 Load - FAILURE");
  691. return;
  692. };
  693. #else
  694. if( !m_pSOC->pru_load( 0, 0, "PRU_0_0.bin", true ) ){
  695. LOG_ERROR("PRU 0 0 Load - FAILURE");
  696. return;
  697. };
  698. #endif
  699. // Make Handshake with cores
  700. if( !m_pSOC->setCoreOnLine(3) ){
  701. LOG_ERROR("Core 3 Startup Failure");
  702. m_bInitOK = false;
  703. return;
  704. };
  705. if( !m_pSOC->setCoreOnLine(2) ){
  706. LOG_ERROR("Core 2 Startup Failure");
  707. m_bInitOK = false;
  708. return;
  709. };
  710. if( !m_pSOC->setCoreOnLine(1) ){
  711. LOG_ERROR("Core 1 Startup Failure");
  712. m_bInitOK = false;
  713. return;
  714. };
  715. if( !m_pSOC->setCoreOnLine(0) ){
  716. LOG_ERROR("Core 0 Startup Failure");
  717. m_bInitOK = false;
  718. return;
  719. };
  720. // Fill Software Info
  721. //
  722. if( NULL != m_pSOC ){
  723. gsSOFT_INFO.FW_CORE1_VER = m_pSOC->getPRU_ID(0);
  724. gsSOFT_INFO.FW_CORE1_TYP = m_pSOC->getPRU_TYPE(0);
  725. gsSOFT_INFO.FW_CORE2_VER = m_pSOC->getPRU_ID(1);
  726. gsSOFT_INFO.FW_CORE2_TYP = m_pSOC->getPRU_TYPE(1);
  727. gsSOFT_INFO.FW_CORE3_VER = m_pSOC->getPRU_ID(2);
  728. gsSOFT_INFO.FW_CORE3_TYP = m_pSOC->getPRU_TYPE(2);
  729. gsSOFT_INFO.FW_CORE4_VER = m_pSOC->getPRU_ID(3);
  730. gsSOFT_INFO.FW_CORE4_TYP = m_pSOC->getPRU_TYPE(3);
  731. gsSOFT_INFO.DATA_VALID = true;
  732. };
  733. // Configure Cores to current Config
  734. // Core 0
  735. if( !m_pSOC->setSunbitMode( m_sPARAMETERS.flags.Sunbit_filter ) ){
  736. LOG_ERROR("Set SUNBIT Mode Failure");
  737. m_bInitOK = false;
  738. return;
  739. };
  740. if( !m_pSOC->setMotorMonitoringMode( m_sPARAMETERS.flags.VerticalMotorOn , m_sPARAMETERS.flags.HorizontalMotorOn ) ){
  741. LOG_ERROR("Set Motor's Monitor Mode Failure");
  742. m_bInitOK = false;
  743. return;
  744. };
  745. if( !m_pSOC->setMotorInterleaveMode( m_sPARAMETERS.flags.QQinterlace ) ) {
  746. LOG_ERROR("Set Motor's Interleave Mode Failure");
  747. m_bInitOK = false;
  748. return;
  749. };
  750. if( !m_pSOC->setMotorSetPoints( m_sPARAMETERS.flags.VerticalFreq , m_sPARAMETERS.flags.HorizontalFreq ) ) {
  751. LOG_ERROR("Set Motor's Setpoints Failure");
  752. m_bInitOK = false;
  753. return;
  754. };
  755. if( !m_pSOC->setLrfPulseMode( m_sPARAMETERS.flags.LaserPulsesOn ) ) {
  756. LOG_ERROR("Set LRF Pulses Mode Failure");
  757. m_bInitOK = false;
  758. return;
  759. };
  760. if( !m_pSOC->setSimulationMode( m_sPARAMETERS.flags.SignalSimulator ) ) {
  761. LOG_ERROR("Set LRF Simulation Mode Failure");
  762. m_bInitOK = false;
  763. return;
  764. };
  765. // Core 2
  766. // Set Shifts
  767. if( !m_pSOC->setShifts( m_sPARAMETERS.flags.B_shift, m_sPARAMETERS.flags.Q_shift ) ) {
  768. LOG_ERROR("Set B-Q Shifts Failure");
  769. m_bInitOK = false;
  770. return;
  771. };
  772. // Core 3
  773. m_sCP_Detector.delta = m_sPARAMETERS.nDelta;
  774. m_sCP_Detector.flip_size = m_sPARAMETERS.nFlip;
  775. m_sCP_Detector.good_points = m_sPARAMETERS.nGPointSamples;
  776. m_sCP_Detector.offset = m_sPARAMETERS.nOffset;
  777. m_sCP_Detector.order = m_sPARAMETERS.nOrder;
  778. m_sCP_Detector.self_build_ena = m_sPARAMETERS.flags.BuildThByCore;
  779. m_sCP_Detector.master = m_sPARAMETERS.flags.isMaster;
  780. #ifdef DEBUG
  781. char Buffer[256];
  782. sprintf( Buffer, "Detector[%s] Parameters: Delta[%d] Flip_Size[%d] Good_Points[%d] Order[%d] Offset[%d] TH_Automatic_Build[%d]",
  783. ((m_sCP_Detector.master)?"M":"S"),
  784. m_sCP_Detector.delta,
  785. m_sCP_Detector.flip_size,
  786. m_sCP_Detector.good_points,
  787. m_sCP_Detector.order,
  788. m_sCP_Detector.offset,
  789. m_sCP_Detector.self_build_ena
  790. );
  791. LOG( Buffer );
  792. #endif
  793. if( !m_pSOC->setThCoreParams( &m_sCP_Detector )){
  794. LOG_ERROR("Detection Core Set Params Failure");
  795. m_bInitOK = false;
  796. return;
  797. };
  798. m_sCS_Detector.h_dencity = 2;
  799. m_sCS_Detector.mode = 0;
  800. m_sCS_Detector.online = 0;
  801. m_sCS_Detector.param_err = 0;
  802. m_sCS_Detector.req = 0;
  803. m_sCS_Detector.spike_th = m_sPARAMETERS.flags.Spike_TH;
  804. m_sCS_Detector.v_dencity = 2;
  805. m_sCS_Detector.work_en = 0;
  806. #ifdef DEBUG
  807. sprintf( Buffer, "Detector Status: Mode[%d] Spike_TH[%d] V_dencity[%d] H_dencity[%d]",
  808. m_sCS_Detector.mode,
  809. m_sCS_Detector.spike_th,
  810. m_sCS_Detector.v_dencity,
  811. m_sCS_Detector.h_dencity
  812. );
  813. LOG( Buffer );
  814. #endif
  815. if( !m_pSOC->setThCoreStatus( &m_sCS_Detector )){
  816. LOG_ERROR("Detection Core Set Statuses Failure");
  817. m_bInitOK = false;
  818. return;
  819. };
  820. // If Work is Enabled Enable Detection Work
  821. if( !m_pSOC->setToWork( m_sPARAMETERS.flags.LaserPulsesOn ) ) {
  822. LOG_ERROR("Detection Core Enabling Failure");
  823. m_bInitOK = false;
  824. return;
  825. };
  826. m_bInitOK = true;
  827. } else { // Slave Mode
  828. #else // PRU_SLAVE
  829. {
  830. #endif
  831. LOG_ERROR("Motors control Disabled by Slave Mode!");
  832. LOG("Loading SLAVE set of PRU Modules");
  833. /*
  834. The Order of cores to be loadedd is crucial.
  835. The first to load - last to function (Data Acq) that depends on others
  836. */
  837. if( !m_pSOC->pru_load( 1, 1, "PRU_1_1.bin", false ) ){
  838. LOG_ERROR("PRU 1 1 Load - FAILURE");
  839. return;
  840. };
  841. if( !m_pSOC->pru_load( 1, 0, "PRU_1_0.bin", false ) ){
  842. LOG_ERROR("PRU 1 0 Load - FAILURE");
  843. return;
  844. };
  845. if( !m_pSOC->pru_load( 0, 1, "PRU_0_1.bin", true ) ){
  846. LOG_ERROR("PRU 0 1 Load - FAILURE");
  847. return;
  848. };
  849. if( !m_pSOC->pru_load( 0, 0, "PRU_0_0.bin", true ) ){
  850. LOG_ERROR("PRU 0 0 Load - FAILURE");
  851. return;
  852. };
  853. // Make Handshake with cores
  854. if( !m_pSOC->setCoreOnLine(3) ){
  855. LOG_ERROR("Core 3 Startup Failure");
  856. m_bInitOK = false;
  857. return;
  858. };
  859. if( !m_pSOC->setCoreOnLine(2) ){
  860. LOG_ERROR("Core 2 Startup Failure");
  861. m_bInitOK = false;
  862. return;
  863. };
  864. if( !m_pSOC->setCoreOnLine(1) ){
  865. LOG_ERROR("Core 1 Startup Failure");
  866. m_bInitOK = false;
  867. return;
  868. };
  869. if( !m_pSOC->setCoreOnLine(0) ){
  870. LOG_ERROR("Core 0 Startup Failure");
  871. m_bInitOK = false;
  872. return;
  873. };
  874. // Fill Software Info
  875. //
  876. if( NULL != m_pSOC ){
  877. gsSOFT_INFO.FW_CORE1_VER = m_pSOC->getPRU_ID(0);
  878. gsSOFT_INFO.FW_CORE2_VER = m_pSOC->getPRU_ID(1);
  879. gsSOFT_INFO.FW_CORE3_VER = m_pSOC->getPRU_ID(2);
  880. gsSOFT_INFO.FW_CORE4_VER = m_pSOC->getPRU_ID(3);
  881. gsSOFT_INFO.DATA_VALID = true;
  882. };
  883. // Configure Cores to current Config
  884. // Core 0
  885. if( !m_pSOC->setSunbitMode( m_sPARAMETERS.flags.Sunbit_filter ) ){
  886. LOG_ERROR("Set SUNBIT Mode Failure");
  887. m_bInitOK = false;
  888. return;
  889. };
  890. if( !m_pSOC->setMotorMonitoringMode( m_sPARAMETERS.flags.VerticalMotorOn , m_sPARAMETERS.flags.HorizontalMotorOn ) ){
  891. LOG_ERROR("Set Motor's Monitor Mode Failure");
  892. m_bInitOK = false;
  893. return;
  894. };
  895. if( !m_pSOC->setMotorInterleaveMode( m_sPARAMETERS.flags.QQinterlace ) ) {
  896. LOG_ERROR("Set Motor's Interleave Mode Failure");
  897. m_bInitOK = false;
  898. return;
  899. };
  900. if( !m_pSOC->setMotorSetPoints( m_sPARAMETERS.flags.VerticalFreq , m_sPARAMETERS.flags.HorizontalFreq ) ) {
  901. LOG_ERROR("Set Motor's Setpoints Failure");
  902. m_bInitOK = false;
  903. return;
  904. };
  905. if( !m_pSOC->setLrfPulseMode( m_sPARAMETERS.flags.LaserPulsesOn ) ) {
  906. LOG_ERROR("Set LRF Pulses Mode Failure");
  907. m_bInitOK = false;
  908. return;
  909. };
  910. if( !m_pSOC->setSimulationMode( m_sPARAMETERS.flags.SignalSimulator ) ) {
  911. LOG_ERROR("Set LRF Simulation Mode Failure");
  912. m_bInitOK = false;
  913. return;
  914. };
  915. // Core 2
  916. // Set Shifts
  917. if( !m_pSOC->setShifts( m_sPARAMETERS.flags.B_shift, m_sPARAMETERS.flags.Q_shift ) ) {
  918. LOG_ERROR("Set B-Q Shifts Failure");
  919. m_bInitOK = false;
  920. return;
  921. };
  922. // Core 3
  923. m_sCP_Detector.delta = m_sPARAMETERS.nDelta;
  924. m_sCP_Detector.flip_size = m_sPARAMETERS.nFlip;
  925. m_sCP_Detector.good_points = m_sPARAMETERS.nGPointSamples;
  926. m_sCP_Detector.offset = m_sPARAMETERS.nOffset;
  927. m_sCP_Detector.order = m_sPARAMETERS.nOrder;
  928. m_sCP_Detector.self_build_ena = m_sPARAMETERS.flags.BuildThByCore;
  929. #ifdef DEBUG
  930. char Buffer[256];
  931. sprintf( Buffer, "Detector Parameters: Delta[%d] Flip_Size[%d] Good_Points[%d] Order[%d] Offset[%d] TH_Automatic_Build[%d]",
  932. m_sCP_Detector.delta,
  933. m_sCP_Detector.flip_size,
  934. m_sCP_Detector.good_points,
  935. m_sCP_Detector.order,
  936. m_sCP_Detector.offset,
  937. m_sCP_Detector.self_build_ena
  938. );
  939. LOG( Buffer );
  940. #endif
  941. if( !m_pSOC->setThCoreParams( &m_sCP_Detector )){
  942. LOG_ERROR("Detection Core Set Params Failure");
  943. m_bInitOK = false;
  944. return;
  945. };
  946. m_sCS_Detector.h_dencity = 2;
  947. m_sCS_Detector.mode = 0;
  948. m_sCS_Detector.online = 0;
  949. m_sCS_Detector.param_err = 0;
  950. m_sCS_Detector.req = 0;
  951. m_sCS_Detector.spike_th = m_sPARAMETERS.flags.Spike_TH;
  952. m_sCS_Detector.v_dencity = 2;
  953. m_sCS_Detector.work_en = 0;
  954. #ifdef DEBUG
  955. sprintf( Buffer, "Detector Status: Mode[%d] Spike_TH[%d] V_dencity[%d] H_dencity[%d]",
  956. m_sCS_Detector.mode,
  957. m_sCS_Detector.spike_th,
  958. m_sCS_Detector.v_dencity,
  959. m_sCS_Detector.h_dencity
  960. );
  961. LOG( Buffer );
  962. #endif
  963. if( !m_pSOC->setThCoreStatus( &m_sCS_Detector )){
  964. LOG_ERROR("Detection Core Set Statuses Failure");
  965. m_bInitOK = false;
  966. return;
  967. };
  968. // If Work is Enabled Enable Detection Work
  969. if( !m_pSOC->setToWork( m_sPARAMETERS.flags.LaserPulsesOn ) ) {
  970. LOG_ERROR("Detection Core Enabling Failure");
  971. m_bInitOK = false;
  972. return;
  973. };
  974. memset( m_bTHSteadyCounter, ( m_sCP_Detector.flip_size && 0xFF ), KERNEL_COUNT);
  975. m_bInitOK = true;
  976. };
  977. };
  978. #else //#ifndef ONPC_TOTTAL_SIMULATOR
  979. LOG("Dummy Init of Hardware");
  980. gsSOFT_INFO.FW_CORE1_VER = 0x01234567;
  981. gsSOFT_INFO.FW_CORE1_TYP = 0x01234567;
  982. gsSOFT_INFO.FW_CORE2_VER = 0x01234567;
  983. gsSOFT_INFO.FW_CORE2_TYP = 0x01234567;
  984. gsSOFT_INFO.FW_CORE3_VER = 0x01234567;
  985. gsSOFT_INFO.FW_CORE3_TYP = 0x01234567;
  986. gsSOFT_INFO.FW_CORE4_VER = 0x01234567;
  987. gsSOFT_INFO.FW_CORE4_TYP = 0x01234567;
  988. gsSOFT_INFO.DATA_VALID = true;
  989. m_bInitOK = true;
  990. #endif //#ifndef ONPC_TOTTAL_SIMULATOR
  991. };
  992. void LRF_HardwareManager::endService( void ){
  993. m_bWorkEnable = false;
  994. #ifndef ONPC_TOTTAL_SIMULATOR
  995. if( m_pSOC!=NULL ){
  996. #ifndef PRU_SLAVE
  997. if( m_sPARAMETERS.flags.isMaster ) {
  998. #ifndef NO_MOTORS
  999. stopMotors();
  1000. #endif
  1001. } else {
  1002. };
  1003. #endif
  1004. LOG("Stoping all PRU Cores");
  1005. if(!m_pSOC->pru_stop(0,0)){
  1006. LOG_ERROR("Error Stoping PRU 0 0 Core");
  1007. };
  1008. if(!m_pSOC->pru_sreset(0,0)){
  1009. LOG_ERROR("Error Resetting PRU 0 0 Core");
  1010. };
  1011. if(!m_pSOC->pru_disable(0,0)){
  1012. LOG_ERROR("Error Disabling PRU 0 0 Core");
  1013. };
  1014. LOG("PRU 0 0 stoped");
  1015. if(!m_pSOC->pru_stop(0,1)){
  1016. LOG_ERROR("Error Stoping PRU 0 1 Core");
  1017. };
  1018. if(!m_pSOC->pru_sreset(0,1)){
  1019. LOG_ERROR("Error Resetting PRU 0 1 Core");
  1020. };
  1021. if(!m_pSOC->pru_disable(0,1)){
  1022. LOG_ERROR("Error Disabling PRU 0 1 Core");
  1023. };
  1024. LOG("PRU 0 1 stoped");
  1025. if(!m_pSOC->pru_stop(1,0)){
  1026. LOG_ERROR("Error Stoping PRU 1 0 Core");
  1027. };
  1028. if(!m_pSOC->pru_sreset(1,0)){
  1029. LOG_ERROR("Error Resetting PRU 1 0 Core");
  1030. };
  1031. if(!m_pSOC->pru_disable(1,0)){
  1032. LOG_ERROR("Error Disabling PRU 1 0 Core");
  1033. };
  1034. LOG("PRU 1 0 stoped");
  1035. if(!m_pSOC->pru_stop(1,1)){
  1036. LOG_ERROR("Error Stoping PRU 1 1 Core");
  1037. };
  1038. if(!m_pSOC->pru_sreset(1,1)){
  1039. LOG_ERROR("Error Resetting PRU 1 1 Core");
  1040. };
  1041. if(!m_pSOC->pru_disable(1,1)){
  1042. LOG_ERROR("Error Disabling PRU 1 1 Core");
  1043. };
  1044. LOG("PRU 1 1 stoped");
  1045. if( !m_pSOC->pru_load( 0, 0, "halt.bin" ) ){
  1046. LOG_ERROR("PRU 0 0 HALT - FAILURE");
  1047. };
  1048. if( !m_pSOC->pru_load( 0, 1, "halt.bin" ) ){
  1049. LOG_ERROR("PRU 0 1 HALT - FAILURE");
  1050. };
  1051. if( !m_pSOC->pru_load( 1, 0, "halt.bin" ) ){
  1052. LOG_ERROR("PRU 1 0 HALT - FAILURE");
  1053. };
  1054. if( !m_pSOC->pru_load( 1, 1, "halt.bin" ) ){
  1055. LOG_ERROR("PRU 1 1 HALT - FAILURE");
  1056. };
  1057. };
  1058. #else //#ifndef ONPC_TOTTAL_SIMULATOR
  1059. LOG("Dummy Shutdown Hardware");
  1060. #endif //#ifndef ONPC_TOTTAL_SIMULATOR
  1061. };
  1062. #ifndef PRU_SLAVE
  1063. #ifndef ONPC_TOTTAL_SIMULATOR
  1064. #ifdef THREADED_MOTORS_START
  1065. void LRF_HardwareManager::performStartMotors( void ){
  1066. if( m_sPARAMETERS.flags.TGZ_canENA ) {
  1067. LOG("Starting TGZ device");
  1068. if( m_pTGZdevice!= NULL ) {
  1069. #ifndef TGD_VERSION_2
  1070. m_pTGZdevice->setDivider( m_sPARAMETERS.flags.TGZ_divider );
  1071. m_pTGZdevice->setPrfMode( m_sPARAMETERS.flags.TGZ_ParamQ );
  1072. m_pTGZdevice->setRatio( m_sPARAMETERS.flags.TGZ_ParamB );
  1073. #else //TGD_VERSION_2
  1074. m_pTGZdevice->setSpeeds( m_sPARAMETERS.flags.TGZ_ParamQ,
  1075. m_sPARAMETERS.flags.TGZ_ParamB );
  1076. #endif //TGD_VERSION_2
  1077. m_pTGZdevice->setRunMode( true );
  1078. m_pTGZdevice->setErrorMode( true );
  1079. m_pTGZdevice->startDeviceStateInquiry();
  1080. //ToDo: Wait for TGD starts Motors!
  1081. //LOG( "Wait Startup TGZ");
  1082. //SLEEP(30000); // 30 seconds
  1083. //LOG( "Done");
  1084. } else {
  1085. LOG_ERROR("Error TGZ Device object!" );
  1086. };
  1087. } else {
  1088. volatile sysPwmss *mV_PWMSS = NULL;
  1089. volatile sysPwmss *mH_PWMSS = NULL;
  1090. LOG("StartMotorsProcedure");
  1091. if( m_pSOC!=NULL && m_sPARAMETERS.flags.isMaster ){
  1092. mV_PWMSS = m_pSOC->getPWMSS_CTRL( 1 );
  1093. mH_PWMSS = m_pSOC->getPWMSS_CTRL( 2 );
  1094. if( mV_PWMSS!=NULL && mH_PWMSS!=NULL ){
  1095. // Disable PWM;
  1096. LOG("Disable Motors PWMs");
  1097. mV_PWMSS->CLKCONFIG_bit.EPWMCLK_EN = 0;
  1098. mH_PWMSS->CLKCONFIG_bit.EPWMCLK_EN = 0;
  1099. while( mV_PWMSS->CLKCONFIG_bit.EPWMCLK_EN == 1 || mH_PWMSS->CLKCONFIG_bit.EPWMCLK_EN == 1 )usleep(5);
  1100. LOG("Disable Motors PWMs - Done");
  1101. // Polarity
  1102. mV_PWMSS->ECAP_ECCTL2_bit.APWMPOL = PWM_POLARITY_INVERSE;
  1103. mH_PWMSS->ECAP_ECCTL2_bit.APWMPOL = PWM_POLARITY_INVERSE;
  1104. LOG("Set Motors PWMs Config");
  1105. // Set PWM Period
  1106. mV_PWMSS->ECAP_CAP3 = VM_WORK_PERIOD;
  1107. mH_PWMSS->ECAP_CAP3 = HM_WORK_PERIOD;
  1108. mV_PWMSS->ECAP_CAP1 = VM_WORK_PERIOD;
  1109. mH_PWMSS->ECAP_CAP1 = HM_WORK_PERIOD;
  1110. LOG("Set Motors PWMs Period");
  1111. // Set Intitial Zeroes DC
  1112. mV_PWMSS->ECAP_CAP4 = 1;
  1113. mH_PWMSS->ECAP_CAP4 = 1;
  1114. mV_PWMSS->ECAP_CAP2 = 1;
  1115. mH_PWMSS->ECAP_CAP2 = 1;
  1116. LOG("Set Motors PWMs DC");
  1117. // Config Timer
  1118. // Running
  1119. //ECCTL1: 0x00008000
  1120. //ECCTL2: 0x000006D6
  1121. // Stoped
  1122. //ECCTL1: 0x00008000
  1123. //ECCTL2: 0x000004C6
  1124. mV_PWMSS->ECAP_ECCTL1 = 0;
  1125. mV_PWMSS->ECAP_ECCTL1_bit.FREE_SOFT = 2;
  1126. mV_PWMSS->ECAP_ECCTL2_bit.TSCTRSTOP = 1;
  1127. mH_PWMSS->ECAP_ECCTL1 = 0;
  1128. mH_PWMSS->ECAP_ECCTL1_bit.FREE_SOFT = 2;
  1129. mH_PWMSS->ECAP_ECCTL2_bit.TSCTRSTOP = 1;
  1130. // Set APWM mode
  1131. mV_PWMSS->ECAP_ECCTL2_bit.CAP_APWM = 1;
  1132. mH_PWMSS->ECAP_ECCTL2_bit.CAP_APWM = 1;
  1133. // Set module enable
  1134. mV_PWMSS->CLKCONFIG_bit.EPWMCLK_EN = 1;
  1135. mH_PWMSS->CLKCONFIG_bit.EPWMCLK_EN = 1;
  1136. LOG("Set Motors PWMs Enabled");
  1137. uint32_t index = 0;
  1138. LOG("Start Motors PWMs Spin Up");
  1139. while( index < VM_WORK_DUTY_CYCLE || index < HM_WORK_DUTY_CYCLE ){
  1140. if( index < VM_WORK_DUTY_CYCLE ){
  1141. mV_PWMSS->ECAP_CAP4 = index;
  1142. while( mV_PWMSS->ECAP_CAP2 != index )usleep(5);
  1143. };
  1144. if( index < HM_WORK_DUTY_CYCLE ){
  1145. mH_PWMSS->ECAP_CAP4 = index;
  1146. while( mH_PWMSS->ECAP_CAP2 != index )usleep(5);
  1147. };
  1148. usleep( 700 ); //700 microseconds;
  1149. index++;
  1150. };
  1151. LOG("Motors PWMs Spin Up Done");
  1152. // Set PRU to controll
  1153. if( !m_pSOC->setMotorMonitoringMode( m_sPARAMETERS.flags.VerticalMotorOn , m_sPARAMETERS.flags.HorizontalMotorOn ) ){
  1154. LOG_ERROR("Set Motor's Monitor Mode Failure");
  1155. return;
  1156. };
  1157. if( !m_pSOC->setMotorInterleaveMode( m_sPARAMETERS.flags.QQinterlace ) ) {
  1158. LOG_ERROR("Set Motor's Interleave Mode Failure");
  1159. return;
  1160. };
  1161. if( !m_pSOC->setMotorSetPoints( m_sPARAMETERS.flags.VerticalFreq , m_sPARAMETERS.flags.HorizontalFreq ) ) {
  1162. LOG_ERROR("Set Motor's Setpoints Failure");
  1163. return;
  1164. };
  1165. } else {
  1166. LOG_ERROR("Cannot get access to PWMSS module");
  1167. }
  1168. };
  1169. };
  1170. };
  1171. void LRF_HardwareManager::performStopMotors( void ){
  1172. if( m_sPARAMETERS.flags.TGZ_canENA ) {
  1173. LOG("Stoping TGZ device");
  1174. if( m_pTGZdevice != NULL ) {
  1175. m_pTGZdevice->setRunMode( false );
  1176. m_pTGZdevice->setErrorMode( true );
  1177. m_pTGZdevice->startDeviceStateInquiry();
  1178. } else {
  1179. LOG_ERROR("Error TGZ Device object!" );
  1180. };
  1181. } else {
  1182. if( m_pSOC!=NULL && m_sPARAMETERS.flags.isMaster ){
  1183. //if( m_sPARAMETERS.flags.HorizontalMotorOn )
  1184. {
  1185. volatile sysPwmss *mH_PWMSS = NULL;
  1186. LOG("Stop Horizontal Motor's PWM");
  1187. mH_PWMSS = m_pSOC->getPWMSS_CTRL( 2 );
  1188. if( mH_PWMSS!=NULL ){
  1189. // Set PWM to 0 timeout 10 msec.
  1190. int to = 2000;
  1191. mH_PWMSS->ECAP_CAP4 = 1;
  1192. while( ( to-- ) && ( mH_PWMSS->ECAP_CAP2 != 1 ) ) usleep(5);
  1193. // Turn Off Timer
  1194. mH_PWMSS->ECAP_ECCTL2_bit.TSCTRSTOP = 1;
  1195. // For Security switch Mode to eCap
  1196. mH_PWMSS->ECAP_ECCTL2_bit.CAP_APWM = 0;
  1197. // Disable PWM;
  1198. mH_PWMSS->CLKCONFIG_bit.EPWMCLK_EN = 0;
  1199. LOG("Horizontal Motor's PWM Stoped");
  1200. } else {
  1201. LOG_ERROR("Can not Stop Motors PWMs");
  1202. return;
  1203. };
  1204. //m_sPARAMETERS.flags.HorizontalMotorOn = 0;
  1205. };
  1206. //if( m_sPARAMETERS.flags.VerticalMotorOn )
  1207. {
  1208. volatile sysPwmss *mV_PWMSS = NULL;
  1209. LOG("Stop Vertical Motor's PWM");
  1210. mV_PWMSS = m_pSOC->getPWMSS_CTRL( 1 );
  1211. if( mV_PWMSS!=NULL ){
  1212. // Set PWM to 0 timeout 10 msec.
  1213. int to = 2000;
  1214. mV_PWMSS->ECAP_CAP4 = 1;
  1215. while( ( to-- ) && ( mV_PWMSS->ECAP_CAP2 != 1 ) )usleep(5);
  1216. // Turn Off Timer
  1217. mV_PWMSS->ECAP_ECCTL2_bit.TSCTRSTOP = 1;
  1218. // For Security switch Mode to eCap
  1219. mV_PWMSS->ECAP_ECCTL2_bit.CAP_APWM = 0;
  1220. // Disable PWM;
  1221. mV_PWMSS->CLKCONFIG_bit.EPWMCLK_EN = 0;
  1222. LOG("Vertical Motor's PWM Stoped");
  1223. } else {
  1224. LOG_ERROR("Can not Stop Motors PWMs");
  1225. return;
  1226. };
  1227. //m_sPARAMETERS.flags.VerticalMotorOn = 0;
  1228. };
  1229. } else {
  1230. LOG_ERROR("Slave Device Can not Stop Motors PWMs");
  1231. return;
  1232. };
  1233. }; // TGZ - PWM
  1234. };
  1235. #endif
  1236. bool LRF_HardwareManager::startMotors( bool Auto ){
  1237. #ifdef THREADED_MOTORS_START
  1238. if( m_sPARAMETERS.flags.TGZ_canENA ) {
  1239. if( !Auto ) {
  1240. if( m_sPARAMETERS.flags.AutostartMotors == 0 ) {
  1241. m_sPARAMETERS.flags.AutostartMotors = 1;
  1242. saveCurrentParameters();
  1243. };
  1244. };
  1245. } else {
  1246. if( !Auto ) {
  1247. if( m_sPARAMETERS.flags.AutostartMotors == 0 || m_sPARAMETERS.flags.VerticalMotorOn == 0 || m_sPARAMETERS.flags.HorizontalMotorOn == 0 ) {
  1248. m_sPARAMETERS.flags.VerticalMotorOn = 1;
  1249. m_sPARAMETERS.flags.HorizontalMotorOn = 1;
  1250. m_sPARAMETERS.flags.AutostartMotors = 1;
  1251. saveCurrentParameters();
  1252. };
  1253. };
  1254. };
  1255. if( m_pMotorCtrlMutex != NULL ) {
  1256. if( 0 == pthread_mutex_trylock( m_pMotorCtrlMutex ) ){
  1257. if( 0 != pthread_detach( *m_pMotorCtrlThreadHandler ) ){
  1258. LOG_ERROR("Thread Detach Failed!");
  1259. pthread_mutex_unlock( m_pMotorCtrlMutex );
  1260. };
  1261. if( 0 != pthread_create( m_pMotorCtrlThreadHandler, NULL, m_fMotorStartThreadFunction, ( void * ) this ) ) {
  1262. LOG_ERROR("Thread Creation Faled.");
  1263. pthread_mutex_unlock( m_pMotorCtrlMutex );
  1264. };
  1265. #ifdef DEBUG_EXTRA
  1266. LOG("TH Flip Thread Started");
  1267. #endif
  1268. } else {
  1269. #ifdef DEBUG_EXTRA
  1270. LOG("TH Flip Request Overlapped!");
  1271. #endif
  1272. };
  1273. };
  1274. #else //THREADED_MOTORS_START
  1275. if( m_sPARAMETERS.flags.TGZ_canENA ) {
  1276. LOG("Starting TGZ device");
  1277. if( m_pTGZdevice!= NULL ) {
  1278. if( !Auto ) {
  1279. if( m_sPARAMETERS.flags.AutostartMotors == 0 ) {
  1280. m_sPARAMETERS.flags.AutostartMotors = 1;
  1281. saveCurrentParameters();
  1282. };
  1283. };
  1284. #ifndef TGD_VERSION_2
  1285. m_pTGZdevice->setDivider( m_sPARAMETERS.flags.TGZ_divider );
  1286. m_pTGZdevice->setPrfMode( m_sPARAMETERS.flags.TGZ_ParamQ );
  1287. m_pTGZdevice->setRatio( m_sPARAMETERS.flags.TGZ_ParamB );
  1288. #else //TGD_VERSION_2
  1289. m_pTGZdevice->setSpeeds( m_sPARAMETERS.flags.TGZ_ParamQ,
  1290. m_sPARAMETERS.flags.TGZ_ParamB );
  1291. #endif //TGD_VERSION_2
  1292. m_pTGZdevice->setRunMode( true );
  1293. m_pTGZdevice->setErrorMode( true );
  1294. m_pTGZdevice->startDeviceStateInquiry();
  1295. //ToDo: Wait for TGD starts Motors!
  1296. //LOG( "Wait Startup TGZ");
  1297. //SLEEP(30000); // 30 seconds
  1298. //LOG( "Done");
  1299. } else {
  1300. LOG_ERROR("Error TGZ Device object!" );
  1301. };
  1302. } else {
  1303. volatile sysPwmss *mV_PWMSS = NULL;
  1304. volatile sysPwmss *mH_PWMSS = NULL;
  1305. LOG("StartMotorsProcedure");
  1306. if( m_pSOC!=NULL && m_sPARAMETERS.flags.isMaster ){
  1307. mV_PWMSS = m_pSOC->getPWMSS_CTRL( 1 );
  1308. mH_PWMSS = m_pSOC->getPWMSS_CTRL( 2 );
  1309. if( mV_PWMSS!=NULL && mH_PWMSS!=NULL ){
  1310. // Disable PWM;
  1311. LOG("Disable Motors PWMs");
  1312. mV_PWMSS->CLKCONFIG_bit.EPWMCLK_EN = 0;
  1313. mH_PWMSS->CLKCONFIG_bit.EPWMCLK_EN = 0;
  1314. while( mV_PWMSS->CLKCONFIG_bit.EPWMCLK_EN == 1 || mH_PWMSS->CLKCONFIG_bit.EPWMCLK_EN == 1 )usleep(5);
  1315. LOG("Disable Motors PWMs - Done");
  1316. // Polarity
  1317. mV_PWMSS->ECAP_ECCTL2_bit.APWMPOL = PWM_POLARITY_INVERSE;
  1318. mH_PWMSS->ECAP_ECCTL2_bit.APWMPOL = PWM_POLARITY_INVERSE;
  1319. LOG("Set Motors PWMs Config");
  1320. // Set PWM Period
  1321. mV_PWMSS->ECAP_CAP3 = VM_WORK_PERIOD;
  1322. mH_PWMSS->ECAP_CAP3 = HM_WORK_PERIOD;
  1323. mV_PWMSS->ECAP_CAP1 = VM_WORK_PERIOD;
  1324. mH_PWMSS->ECAP_CAP1 = HM_WORK_PERIOD;
  1325. LOG("Set Motors PWMs Period");
  1326. // Set Intitial Zeroes DC
  1327. mV_PWMSS->ECAP_CAP4 = 1;
  1328. mH_PWMSS->ECAP_CAP4 = 1;
  1329. mV_PWMSS->ECAP_CAP2 = 1;
  1330. mH_PWMSS->ECAP_CAP2 = 1;
  1331. LOG("Set Motors PWMs DC");
  1332. // Config Timer
  1333. // Running
  1334. //ECCTL1: 0x00008000
  1335. //ECCTL2: 0x000006D6
  1336. // Stoped
  1337. //ECCTL1: 0x00008000
  1338. //ECCTL2: 0x000004C6
  1339. mV_PWMSS->ECAP_ECCTL1 = 0;
  1340. mV_PWMSS->ECAP_ECCTL1_bit.FREE_SOFT = 2;
  1341. mV_PWMSS->ECAP_ECCTL2_bit.TSCTRSTOP = 1;
  1342. mH_PWMSS->ECAP_ECCTL1 = 0;
  1343. mH_PWMSS->ECAP_ECCTL1_bit.FREE_SOFT = 2;
  1344. mH_PWMSS->ECAP_ECCTL2_bit.TSCTRSTOP = 1;
  1345. // Set APWM mode
  1346. mV_PWMSS->ECAP_ECCTL2_bit.CAP_APWM = 1;
  1347. mH_PWMSS->ECAP_ECCTL2_bit.CAP_APWM = 1;
  1348. // Set module enable
  1349. mV_PWMSS->CLKCONFIG_bit.EPWMCLK_EN = 1;
  1350. mH_PWMSS->CLKCONFIG_bit.EPWMCLK_EN = 1;
  1351. LOG("Set Motors PWMs Enabled");
  1352. uint32_t index = 0;
  1353. LOG("Start Motors PWMs Spin Up");
  1354. while( index < VM_WORK_DUTY_CYCLE || index < HM_WORK_DUTY_CYCLE ){
  1355. if( index < VM_WORK_DUTY_CYCLE ){
  1356. mV_PWMSS->ECAP_CAP4 = index;
  1357. while( mV_PWMSS->ECAP_CAP2 != index )usleep(5);
  1358. };
  1359. if( index < HM_WORK_DUTY_CYCLE ){
  1360. mH_PWMSS->ECAP_CAP4 = index;
  1361. while( mH_PWMSS->ECAP_CAP2 != index )usleep(5);
  1362. };
  1363. usleep( 700 ); //700 microseconds;
  1364. index++;
  1365. };
  1366. LOG("Motors PWMs Spin Up Done");
  1367. // Set PRU to controll
  1368. if( !Auto ) {
  1369. if( m_sPARAMETERS.flags.AutostartMotors == 0 || m_sPARAMETERS.flags.VerticalMotorOn == 0 || m_sPARAMETERS.flags.HorizontalMotorOn == 0 ) {
  1370. m_sPARAMETERS.flags.VerticalMotorOn = 1;
  1371. m_sPARAMETERS.flags.HorizontalMotorOn = 1;
  1372. m_sPARAMETERS.flags.AutostartMotors = 1;
  1373. saveCurrentParameters();
  1374. };
  1375. };
  1376. if( !m_pSOC->setMotorMonitoringMode( m_sPARAMETERS.flags.VerticalMotorOn , m_sPARAMETERS.flags.HorizontalMotorOn ) ){
  1377. LOG_ERROR("Set Motor's Monitor Mode Failure");
  1378. return false;
  1379. };
  1380. if( !m_pSOC->setMotorInterleaveMode( m_sPARAMETERS.flags.QQinterlace ) ) {
  1381. LOG_ERROR("Set Motor's Interleave Mode Failure");
  1382. return false;
  1383. };
  1384. if( !m_pSOC->setMotorSetPoints( m_sPARAMETERS.flags.VerticalFreq , m_sPARAMETERS.flags.HorizontalFreq ) ) {
  1385. LOG_ERROR("Set Motor's Setpoints Failure");
  1386. return false;
  1387. };
  1388. } else {
  1389. LOG_ERROR("Cannot get access to PWMSS module");
  1390. }
  1391. };
  1392. };
  1393. #endif //THREADED_MOTORS_START
  1394. return true;
  1395. };
  1396. bool LRF_HardwareManager::stopMotors( bool Auto ){
  1397. #ifdef THREADED_MOTORS_START
  1398. if( m_sPARAMETERS.flags.TGZ_canENA ) {
  1399. if( !Auto ) {
  1400. if( m_sPARAMETERS.flags.AutostartMotors == 1 ) {
  1401. m_sPARAMETERS.flags.AutostartMotors = 0;
  1402. saveCurrentParameters();
  1403. };
  1404. };
  1405. } else {
  1406. if( !Auto ) {
  1407. if( m_sPARAMETERS.flags.AutostartMotors == 1 || m_sPARAMETERS.flags.VerticalMotorOn == 1 || m_sPARAMETERS.flags.HorizontalMotorOn == 1 ) {
  1408. m_sPARAMETERS.flags.VerticalMotorOn = 0;
  1409. m_sPARAMETERS.flags.HorizontalMotorOn = 0;
  1410. m_sPARAMETERS.flags.AutostartMotors = 0;
  1411. saveCurrentParameters();
  1412. };
  1413. };
  1414. };
  1415. if( m_pMotorCtrlMutex != NULL ) {
  1416. if( 0 == pthread_mutex_trylock( m_pMotorCtrlMutex ) ){
  1417. if( 0 != pthread_detach( *m_pMotorCtrlThreadHandler ) ){
  1418. LOG_ERROR("Thread Detach Failed!");
  1419. pthread_mutex_unlock( m_pMotorCtrlMutex );
  1420. };
  1421. if( 0 != pthread_create( m_pMotorCtrlThreadHandler, NULL, m_fMotorStopThreadFunction, ( void * ) this ) ) {
  1422. LOG_ERROR("Thread Creation Faled.");
  1423. pthread_mutex_unlock( m_pMotorCtrlMutex );
  1424. };
  1425. #ifdef DEBUG_EXTRA
  1426. LOG("TH Flip Thread Started");
  1427. #endif
  1428. } else {
  1429. #ifdef DEBUG_EXTRA
  1430. LOG("TH Flip Request Overlapped!");
  1431. #endif
  1432. };
  1433. };
  1434. #else //THREADED_MOTORS_START
  1435. if( m_sPARAMETERS.flags.TGZ_canENA ) {
  1436. LOG("Stoping TGZ device");
  1437. if( m_pTGZdevice != NULL ) {
  1438. if( !Auto ) {
  1439. if( m_sPARAMETERS.flags.AutostartMotors == 1 ) {
  1440. m_sPARAMETERS.flags.AutostartMotors = 0;
  1441. saveCurrentParameters();
  1442. };
  1443. };
  1444. m_pTGZdevice->setRunMode( false );
  1445. m_pTGZdevice->setErrorMode( true );
  1446. m_pTGZdevice->startDeviceStateInquiry();
  1447. } else {
  1448. LOG_ERROR("Error TGZ Device object!" );
  1449. };
  1450. } else {
  1451. if( m_pSOC!=NULL && m_sPARAMETERS.flags.isMaster ){
  1452. if( m_sPARAMETERS.flags.HorizontalMotorOn ) {
  1453. volatile sysPwmss *mH_PWMSS = NULL;
  1454. LOG("Stop Horizontal Motor's PWM");
  1455. mH_PWMSS = m_pSOC->getPWMSS_CTRL( 2 );
  1456. if( mH_PWMSS!=NULL ){
  1457. // Set PWM to 0 timeout 10 msec.
  1458. int to = 2000;
  1459. mH_PWMSS->ECAP_CAP4 = 1;
  1460. while( ( to-- ) && ( mH_PWMSS->ECAP_CAP2 != 1 ) ) usleep(5);
  1461. // Turn Off Timer
  1462. mH_PWMSS->ECAP_ECCTL2_bit.TSCTRSTOP = 1;
  1463. // For Security switch Mode to eCap
  1464. mH_PWMSS->ECAP_ECCTL2_bit.CAP_APWM = 0;
  1465. // Disable PWM;
  1466. mH_PWMSS->CLKCONFIG_bit.EPWMCLK_EN = 0;
  1467. LOG("Horizontal Motor's PWM Stoped");
  1468. } else {
  1469. LOG_ERROR("Can not Stop Motors PWMs");
  1470. return false;
  1471. };
  1472. };
  1473. if( m_sPARAMETERS.flags.VerticalMotorOn ) {
  1474. volatile sysPwmss *mV_PWMSS = NULL;
  1475. LOG("Stop Vertical Motor's PWM");
  1476. mV_PWMSS = m_pSOC->getPWMSS_CTRL( 1 );
  1477. if( mV_PWMSS!=NULL ){
  1478. if( !Auto ) {
  1479. if( m_sPARAMETERS.flags.AutostartMotors == 1 || m_sPARAMETERS.flags.VerticalMotorOn == 1 || m_sPARAMETERS.flags.HorizontalMotorOn == 1 ) {
  1480. m_sPARAMETERS.flags.VerticalMotorOn = 0;
  1481. m_sPARAMETERS.flags.HorizontalMotorOn = 0;
  1482. m_sPARAMETERS.flags.AutostartMotors = 0;
  1483. saveCurrentParameters();
  1484. };
  1485. };
  1486. // Set PWM to 0 timeout 10 msec.
  1487. int to = 2000;
  1488. mV_PWMSS->ECAP_CAP4 = 1;
  1489. while( ( to-- ) && ( mV_PWMSS->ECAP_CAP2 != 1 ) )usleep(5);
  1490. // Turn Off Timer
  1491. mV_PWMSS->ECAP_ECCTL2_bit.TSCTRSTOP = 1;
  1492. // For Security switch Mode to eCap
  1493. mV_PWMSS->ECAP_ECCTL2_bit.CAP_APWM = 0;
  1494. // Disable PWM;
  1495. mV_PWMSS->CLKCONFIG_bit.EPWMCLK_EN = 0;
  1496. LOG("Vertical Motor's PWM Stoped");
  1497. } else {
  1498. LOG_ERROR("Can not Stop Motors PWMs");
  1499. return false;
  1500. };
  1501. };
  1502. } else {
  1503. LOG_ERROR("Slave Device Can not Stop Motors PWMs");
  1504. return false;
  1505. };
  1506. }; // TGZ <-> PWM
  1507. #endif // THREADED_MOTORS_START
  1508. return true;
  1509. };
  1510. #endif //#ifndef ONPC_TOTTAL_SIMULATOR
  1511. #endif // PRU_SLAVE
  1512. static uint32_t nMemoryDumpID = 0;
  1513. #ifdef ONPC_TOTTAL_SIMULATOR
  1514. static thresholdNode * DUMMY_TH_MEMORY = NULL;
  1515. #endif
  1516. thresholdNode *LRF_HardwareManager::getWorkingMemory( void ){
  1517. #ifndef ONPC_TOTTAL_SIMULATOR
  1518. if( m_pSOC!= NULL ){
  1519. thresholdNode *tNode;
  1520. thresholdNode *rNode;
  1521. rNode = tNode = (thresholdNode *) m_pSOC->getTH_MEMORY();
  1522. if( m_bDumpMemoryToDisk ) {
  1523. if( tNode != NULL ) {
  1524. LOG( "Save Memory Dump!");
  1525. FILE *fd;
  1526. char tName[64];
  1527. for( int index = 0; index < KERNEL_COUNT; index++ ) {
  1528. sprintf( tName, "memdump_%03d-%02d.raw", nMemoryDumpID, index );
  1529. fd = fopen(tName, "wb+");
  1530. if( fd != NULL ) {
  1531. uint32_t tSize;
  1532. tSize = TH_SIZE_NODES;
  1533. fwrite( &tSize, sizeof(uint32_t), 1, fd );
  1534. fwrite( tNode, sizeof(thresholdNode), TH_SIZE_NODES, fd );
  1535. fclose(fd);
  1536. } else {
  1537. LOG_ERROR( "Mem Dump Failure!");
  1538. };
  1539. tNode += TH_SIZE_NODES;
  1540. };
  1541. nMemoryDumpID++;
  1542. };
  1543. };
  1544. return rNode;
  1545. };
  1546. return NULL;
  1547. #else
  1548. if( DUMMY_TH_MEMORY == NULL ) {
  1549. LOG("Allocate Dummy buffer for TH memories");
  1550. DUMMY_TH_MEMORY = (thresholdNode *)malloc( 2 * KERNEL_COUNT * TH_SIZE_NODES * sizeof(thresholdNode));
  1551. };
  1552. return DUMMY_TH_MEMORY;
  1553. #endif
  1554. };
  1555. int LRF_HardwareManager::getWorkingMemoryID( void ){
  1556. return ( nMemoryDumpID - 1 );
  1557. };
  1558. uint32_t *LRF_HardwareManager::getEventMemory( void ){
  1559. #ifndef ONPC_TOTTAL_SIMULATOR
  1560. if( m_pSOC!= NULL ){
  1561. return m_pSOC->getEVT_MEMORY();
  1562. };
  1563. #endif
  1564. return NULL;
  1565. };
  1566. bool LRF_HardwareManager::getCoreStatus( DT_STATUS &Status ){
  1567. #ifndef ONPC_TOTTAL_SIMULATOR
  1568. if( m_pSOC!=NULL ){
  1569. *((uint32_t*)&m_sCS_Detector) = m_pSOC->getThCoreStatus();
  1570. Status = m_sCS_Detector;
  1571. if( Status.online ) return true;
  1572. };
  1573. return false;
  1574. #else
  1575. return true;
  1576. #endif
  1577. };
  1578. bool LRF_HardwareManager::getDetectionParams( TH_PARAMS &Params ){
  1579. #ifndef ONPC_TOTTAL_SIMULATOR
  1580. if( m_pSOC!=NULL ){
  1581. *((uint32_t*)&m_sCP_Detector) = m_pSOC->getThCoreParams();
  1582. Params = m_sCP_Detector;
  1583. if( m_sCP_Detector.self_build_ena ) {
  1584. m_nDetectionFilter = 0xFF;
  1585. };
  1586. return true;
  1587. };
  1588. return false;
  1589. #else
  1590. return true;
  1591. #endif
  1592. };
  1593. bool LRF_HardwareManager::setParams( LGS_3D_PARAMETERS *Params ){
  1594. if(Params == NULL) return false;
  1595. memcpy( &m_sPARAMETERS, Params, sizeof(LGS_3D_PARAMETERS) );
  1596. // Save into settings
  1597. m_sCP_Detector.delta = m_sPARAMETERS.nDelta;
  1598. m_sCP_Detector.flip_size = m_sPARAMETERS.nFlip;
  1599. m_sCP_Detector.good_points = m_sPARAMETERS.nGPointSamples;
  1600. m_sCP_Detector.offset = m_sPARAMETERS.nOffset;
  1601. m_sCP_Detector.order = m_sPARAMETERS.nOrder;
  1602. m_sCP_Detector.self_build_ena = m_sPARAMETERS.flags.BuildThByCore;
  1603. m_sCP_Detector.master = m_sPARAMETERS.flags.isMaster;
  1604. #ifdef DEBUG
  1605. char Buffer[256];
  1606. sprintf( Buffer, "Detector[%s] NEW Parameters: Delta[%d] Flip_Size[%d] Good_Points[%d] Order[%d] Offset[%d] TH_Automatic_Build[%d]",
  1607. ((m_sCP_Detector.master)?"M":"S"),
  1608. m_sCP_Detector.delta,
  1609. m_sCP_Detector.flip_size,
  1610. m_sCP_Detector.good_points,
  1611. m_sCP_Detector.order,
  1612. m_sCP_Detector.offset,
  1613. m_sCP_Detector.self_build_ena
  1614. );
  1615. LOG( Buffer );
  1616. #endif
  1617. #ifndef ONPC_TOTTAL_SIMULATOR
  1618. if(m_pSOC == NULL ) return false;
  1619. // Apply new Parameters
  1620. if(!m_pSOC->setThCoreParams( &m_sCP_Detector )){
  1621. LOG_ERROR("Detection Core Set NEW Params Failure");
  1622. return false;
  1623. };
  1624. *((uint32_t*)&m_sCP_Detector) = m_pSOC->getThCoreParams();
  1625. #endif
  1626. saveCurrentParameters();
  1627. return true;
  1628. };
  1629. bool LRF_HardwareManager::setDetectionParams( TH_PARAMS *Params ){
  1630. if( Params == NULL) return false;
  1631. memcpy( &m_sCP_Detector, Params, sizeof(TH_PARAMS) );
  1632. #ifdef DEBUG
  1633. char Buffer[256];
  1634. sprintf( Buffer, "Detector[%s] NEW Parameters: Delta[%d] Flip_Size[%d] Good_Points[%d] Order[%d] Offset[%d] TH_Automatic_Build[%d]",
  1635. ((m_sCP_Detector.master)?"M":"S"),
  1636. m_sCP_Detector.delta,
  1637. m_sCP_Detector.flip_size,
  1638. m_sCP_Detector.good_points,
  1639. m_sCP_Detector.order,
  1640. m_sCP_Detector.offset,
  1641. m_sCP_Detector.self_build_ena
  1642. );
  1643. LOG( Buffer );
  1644. #endif
  1645. #ifndef ONPC_TOTTAL_SIMULATOR
  1646. if( m_pSOC == NULL ) return false;
  1647. // Apply new Parameters
  1648. if(!m_pSOC->setThCoreParams( &m_sCP_Detector )){
  1649. LOG_ERROR("Detection Core Set NEW Params Failure");
  1650. return false;
  1651. };
  1652. *((uint32_t*)&m_sCP_Detector) = m_pSOC->getThCoreParams();
  1653. #endif
  1654. saveCurrentParameters();
  1655. return true;
  1656. };
  1657. #ifdef SIMULATE_FLIPS
  1658. static uint32_t pips_counter = 1;
  1659. #endif
  1660. //static uint32_t stam = 0;
  1661. bool LRF_HardwareManager::validateCores( void ){
  1662. #ifdef SIMULATE_FLIPS
  1663. pips_counter++;
  1664. #endif
  1665. #ifndef ONPC_TOTTAL_SIMULATOR
  1666. if( !m_bInitOK ) return false;
  1667. if( m_pSOC != NULL ){
  1668. #ifndef PRU_SLAVE
  1669. *((uint32_t*)&m_sCS_Motors) = m_pSOC->getMotorStatus();
  1670. if( m_sCS_Motors.online ){
  1671. bool tErrors = false;
  1672. if( m_sCS_Motors.param_err ) {
  1673. LOG_ERROR("Motor Parameter Error!");
  1674. tErrors = true;
  1675. };
  1676. if( m_sCS_Motors.h_motor_en && !m_sCS_Motors.h_motor_ok ) {
  1677. LOG_ERROR("Basis Motor Error!");
  1678. tErrors = true;
  1679. };
  1680. if( m_sCS_Motors.v_motor_en && !m_sCS_Motors.v_motor_ok ) {
  1681. LOG_ERROR("Mirror Block Motor Error!");
  1682. tErrors = true;
  1683. };
  1684. if( tErrors )return false;
  1685. };
  1686. #endif
  1687. *((uint32_t*)&m_sCS_Detector) = m_pSOC->getThCoreStatus();
  1688. *((uint32_t*)&m_sCP_Detector) = m_pSOC->getThCoreParams();
  1689. if( m_sCP_Detector.self_build_ena ) {
  1690. m_nDetectionFilter = 0xFF;
  1691. };
  1692. if( m_sCS_Detector.online ) {
  1693. #ifdef SIMULATE_FLIPS
  1694. if( ( pips_counter % 100 ) == 0 ){
  1695. LOG("INJECT Flip Request!");
  1696. m_pSOC->injectThCoreRequest( CORE3_REQ_FLIP_TH );
  1697. };
  1698. #endif
  1699. #ifdef THREADED_FLIP
  1700. if( m_bFlipUnlockRequested ){
  1701. #ifdef DEBUG_EXTRA
  1702. LOG("Unlocking Flip protecting Mutex");
  1703. #endif
  1704. if( m_pFlipMutex != NULL ){
  1705. if( 0 != pthread_mutex_unlock( m_pFlipMutex ) ){
  1706. LOG_ERROR("Error Unlocking Flip protecting Mutex!");
  1707. };
  1708. };
  1709. m_bFlipUnlockRequested = false;
  1710. };
  1711. #endif
  1712. if( m_sCS_Detector.req ) {
  1713. #ifdef DEBUG
  1714. //LOG("Detector CORE REQ");
  1715. #endif
  1716. uint32_t mCoreReq;
  1717. mCoreReq = m_pSOC->getThCoreRequest();
  1718. switch( mCoreReq ) {
  1719. #ifndef IGNORE_PRU_FLIP
  1720. case CORE3_REQ_FLIP_TH:{
  1721. #ifdef THREADED_FLIP
  1722. if( m_pFlipMutex != NULL ) {
  1723. if( 0 == pthread_mutex_trylock( m_pFlipMutex ) ){
  1724. if( !m_bWaitFirstFlip ) {
  1725. if( 0 != pthread_detach( *m_pFlipThreadHandler ) ){
  1726. LOG_ERROR("Thread Detach Failed!");
  1727. pthread_mutex_unlock( m_pFlipMutex );
  1728. };
  1729. } else m_bWaitFirstFlip = false;
  1730. #ifdef DEBUG_EXTRA
  1731. LOG("Build Working Threshold Memories MT Mode!");
  1732. #endif
  1733. if( 0 != pthread_create( m_pFlipThreadHandler, NULL, m_fFlipThreadFunction, ( void * ) this ) ) {
  1734. LOG_ERROR("Thread Creation Faled.");
  1735. pthread_mutex_unlock( m_pFlipMutex );
  1736. };
  1737. #ifdef DEBUG_EXTRA
  1738. LOG("TH Flip Thread Started");
  1739. #endif
  1740. } else {
  1741. #ifdef DEBUG_EXTRA
  1742. LOG("TH Flip Request Overlapped!");
  1743. #endif
  1744. };
  1745. }
  1746. #else //!THREADED_FLIP
  1747. #ifdef DEBUG_EXTRA
  1748. LOG("Build Working Threshold Memories NT Mode!");
  1749. #endif
  1750. if( performFlip() ){
  1751. #ifdef DEBUG_EXTRA
  1752. LOG("CORE REQ Done.");
  1753. #endif
  1754. } else {
  1755. LOG_ERROR("Building Detector's TH Error!");
  1756. };
  1757. #endif//!THREADED_FLIP
  1758. break;
  1759. };
  1760. case CORE3_REQ_CLEAR_TH:{
  1761. #ifdef DEBUG
  1762. LOG("Clear Threshold Memories");
  1763. #endif
  1764. if( cleanTH(0) ){
  1765. #ifdef DEBUG
  1766. //LOG("CORE REQ Done.");
  1767. #endif
  1768. } else {
  1769. LOG_ERROR("Cleanup Detector's TH Error!");
  1770. }
  1771. break;
  1772. };
  1773. #endif //!IGNORE_PRU_FLIP
  1774. default:{
  1775. LOG_ERROR("Unrecognized Core Request!");
  1776. };
  1777. };
  1778. m_pSOC->clearThCoreRequest();
  1779. };
  1780. if( m_sCS_Detector.param_err ) {
  1781. LOG_ERROR("Detector Parameter Error!");
  1782. return false;
  1783. };
  1784. };
  1785. return true;
  1786. } else {
  1787. LOG_ERROR( "SOC Not Defined!");
  1788. };
  1789. return false;
  1790. #else //#ifndef ONPC_TOTTAL_SIMULATOR
  1791. return true;
  1792. #endif //#ifndef ONPC_TOTTAL_SIMULATOR
  1793. };
  1794. void LRF_HardwareManager::restartService( void ){
  1795. m_nFlipEnaFLags = 0;
  1796. m_nDetectionFilter = 0;
  1797. m_nTHVisualFlags = 0;
  1798. };
  1799. bool LRF_HardwareManager::reset( bool Hard ){
  1800. uint8_t lrfID;
  1801. m_nFlipEnaFLags = 0;
  1802. m_nDetectionFilter = 0;
  1803. m_nTHVisualFlags = 0;
  1804. if( Hard ){
  1805. #ifdef DEBUG
  1806. LOG("Waiting Till Flip Process is Done");
  1807. #endif
  1808. if( 0 == pthread_mutex_lock( m_pFlipMutex ) ){
  1809. if( 0 != pthread_detach( *m_pFlipThreadHandler ) ){
  1810. LOG_ERROR("Thread Detach Failed!");
  1811. };
  1812. };
  1813. #ifdef DEBUG
  1814. LOG("Stoping Cores!");
  1815. #endif
  1816. endService();
  1817. #ifdef DEBUG
  1818. LOG("Begin Reset TH Mems");
  1819. #endif
  1820. // Clear TH + Working TH
  1821. for( lrfID = 0; lrfID < ( KERNEL_COUNT << 1 ); lrfID++ ){
  1822. #ifdef DEBUG
  1823. LOG("Cleanup TH");
  1824. #endif
  1825. cleanTH( lrfID );
  1826. #ifdef DEBUG
  1827. LOG("Done Cleanup TH");
  1828. #endif
  1829. };
  1830. #ifdef DEBUG
  1831. LOG("Starting Cores!");
  1832. #endif
  1833. beginService();
  1834. #ifdef DEBUG
  1835. LOG("Core Startup is Done!");
  1836. #endif
  1837. pthread_mutex_unlock( m_pFlipMutex );
  1838. #ifdef DEBUG
  1839. LOG("TH Reset is Done!");
  1840. #endif
  1841. } else {
  1842. #ifdef DEBUG
  1843. LOG("Waiting Till Flip Process is Done");
  1844. #endif
  1845. if( 0 == pthread_mutex_lock( m_pFlipMutex ) ){
  1846. if( 0 != pthread_detach( *m_pFlipThreadHandler ) ){
  1847. LOG_ERROR("Thread Detach Failed!");
  1848. };
  1849. };
  1850. #ifdef DEBUG
  1851. LOG("Begin Reset TH Mems");
  1852. #endif
  1853. // Clean Only TH
  1854. for( lrfID = 0; lrfID < KERNEL_COUNT; lrfID++ ){
  1855. #ifdef DEBUG
  1856. LOG("Cleanup TH");
  1857. #endif
  1858. cleanTH( lrfID );
  1859. #ifdef DEBUG
  1860. LOG("Done Cleanup TH");
  1861. #endif
  1862. };
  1863. pthread_mutex_unlock( m_pFlipMutex );
  1864. #ifdef DEBUG
  1865. LOG("TH Reset is Done!");
  1866. #endif
  1867. };
  1868. return true;
  1869. };
  1870. #ifndef PRU_SLAVE
  1871. #ifndef ONPC_TOTTAL_SIMULATOR
  1872. #ifdef THREADED_MOTORS_START
  1873. void *LRF_HardwareManager::m_fMotorStartThreadFunction( void *ptr ){
  1874. LRF_HardwareManager *me;
  1875. me = (LRF_HardwareManager*)ptr;
  1876. me->performStartMotors();
  1877. pthread_exit( NULL );
  1878. return NULL;
  1879. };
  1880. void *LRF_HardwareManager::m_fMotorStopThreadFunction( void *ptr ){
  1881. LRF_HardwareManager *me;
  1882. me = (LRF_HardwareManager*)ptr;
  1883. me->performStopMotors();
  1884. pthread_exit( NULL );
  1885. return NULL;
  1886. };
  1887. #endif //THREADED_MOTORS_START
  1888. #endif //#ifndef ONPC_TOTTAL_SIMULATOR
  1889. #endif //PRU_SLAVE
  1890. #ifdef THREADED_FLIP
  1891. void *LRF_HardwareManager::m_fFlipThreadFunction( void *ptr ){
  1892. LRF_HardwareManager *me;
  1893. me = (LRF_HardwareManager*)ptr;
  1894. me->performFlip();
  1895. me->m_bFlipUnlockRequested = true;
  1896. pthread_exit( NULL );
  1897. return NULL;
  1898. };
  1899. #endif
  1900. static uint32_t m_sgnFlipCounter = 0;
  1901. bool LRF_HardwareManager::performFlip( void ){
  1902. if( !m_bInitOK ) return false;
  1903. #ifndef ONPC_TOTTAL_SIMULATOR
  1904. if( m_pSOC != NULL ){
  1905. volatile thresholdNode * th_node[16];
  1906. uint32_t LBA_ID;
  1907. uint8_t LRF_ID;
  1908. uint32_t cMax;
  1909. uint32_t cMin;
  1910. thresholdNode tTN;
  1911. int32_t cNodeID;
  1912. int32_t sNodeID; // Start point
  1913. bool QAreaOK;
  1914. #ifdef STRONG_STAT
  1915. uint32_t cGPCount;
  1916. uint32_t cQGPCount;
  1917. uint32_t cWGPCount;
  1918. uint8_t bLooseQFlags;
  1919. uint8_t bGPointsFlags;
  1920. #endif
  1921. th_node[0] = (volatile thresholdNode *) m_pSOC->getTH_MEMORY();
  1922. if( th_node[0] != NULL ){
  1923. #ifdef DEBUG
  1924. char Buffer[256];
  1925. #ifdef DEBUG_EXTRA
  1926. LOG("Start TH Build Cycle");
  1927. sprintf( Buffer, "NF TH pointer is: %p", th_node );
  1928. LOG( Buffer );
  1929. #endif
  1930. #endif
  1931. th_node[1] = th_node[0]+ TH_SIZE_NODES;
  1932. th_node[2] = th_node[1]+ TH_SIZE_NODES;
  1933. th_node[3] = th_node[2]+ TH_SIZE_NODES;
  1934. th_node[4] = th_node[3]+ TH_SIZE_NODES;
  1935. th_node[5] = th_node[4]+ TH_SIZE_NODES;
  1936. th_node[6] = th_node[5]+ TH_SIZE_NODES;
  1937. th_node[7] = th_node[6]+ TH_SIZE_NODES;
  1938. th_node[8] = th_node[7]+ TH_SIZE_NODES;
  1939. th_node[9] = th_node[8]+ TH_SIZE_NODES;
  1940. th_node[10] = th_node[9]+ TH_SIZE_NODES;
  1941. th_node[11] = th_node[10]+ TH_SIZE_NODES;
  1942. th_node[12] = th_node[11]+ TH_SIZE_NODES;
  1943. th_node[13] = th_node[12]+ TH_SIZE_NODES;
  1944. th_node[14] = th_node[13]+ TH_SIZE_NODES;
  1945. th_node[15] = th_node[14]+ TH_SIZE_NODES;
  1946. bGPointsFlags = 0;
  1947. #ifdef STRONG_STAT
  1948. bLooseQFlags = 0;
  1949. #endif
  1950. for( LRF_ID = 0; LRF_ID < KERNEL_COUNT; LRF_ID++ ){
  1951. if( !m_bWorkEnable ) return false;
  1952. #ifdef STRONG_STAT
  1953. cGPCount = 0;
  1954. cQGPCount = 0;
  1955. cWGPCount = 0;
  1956. #endif
  1957. //Filter Disabled Kernels
  1958. if( ((1 << LRF_ID ) & m_nKernelFilter ) == 0 ) continue;
  1959. // Begin Scan Threshold
  1960. for( LBA_ID = 0; LBA_ID < TH_SIZE_NODES; LBA_ID++ ){
  1961. if( !m_bWorkEnable ) return false;
  1962. uint32_t LocalStrideX = LBA_ID % TH_LINE_NODES;
  1963. uint32_t LocalStrideY = LBA_ID / TH_LINE_NODES;
  1964. // Get Central TH Point
  1965. tTN.data = th_node[LRF_ID][LBA_ID].data;
  1966. // if point was blocked by SunBit
  1967. if( tTN.values.sbit ) {
  1968. tTN.values.sbit = 0;
  1969. tTN.values.cnt = 0;
  1970. tTN.values.min = 0;
  1971. tTN.values.max = 4095;
  1972. for( int x = -m_sPARAMETERS.nOrder; x < (m_sPARAMETERS.nOrder+1); x++ ) {
  1973. for( int y = -m_sPARAMETERS.nOrder; y < (m_sPARAMETERS.nOrder+1); y++ ) {
  1974. if( !m_bWorkEnable ) return false;
  1975. int LocalX = LocalStrideX+x;
  1976. int LocalY = LocalStrideY+y;
  1977. if( LocalY < 0 ) LocalY += Q_SCAN_COUNT;
  1978. if( LocalY >= Q_SCAN_COUNT ) LocalY -= Q_SCAN_COUNT;
  1979. if( LocalX < 0 ) LocalX += M_SCAN_POINTS;
  1980. if( LocalX >= TH_LINE_NODES ) LocalX -= TH_LINE_NODES;
  1981. uint32_t CleanID = ( LocalY * TH_LINE_NODES ) + LocalX;
  1982. //Masking out WTH Area
  1983. th_node[8|LRF_ID][CleanID].data = tTN.data;
  1984. };
  1985. };
  1986. //Treat as SUNBIT points as GOOD points
  1987. cGPCount++;
  1988. cQGPCount++;
  1989. cWGPCount++;
  1990. continue;
  1991. };
  1992. if ( ((1 << LRF_ID ) & m_nFlipEnaFLags ) == 0 ) {
  1993. //If Building Working TH disabled for TH
  1994. #ifdef STRONG_STAT
  1995. if( tTN.values.cnt >= m_sPARAMETERS.nGPointSamples ){
  1996. cGPCount++;
  1997. cQGPCount++;
  1998. } else {
  1999. if( tTN.values.cnt ) {
  2000. cGPCount++;
  2001. };
  2002. };
  2003. #else
  2004. if( tTN.values.cnt ){
  2005. cGPCount++;
  2006. cQGPCount++;
  2007. };
  2008. #endif
  2009. } else {
  2010. //If Building Working TH enabled for TH
  2011. #ifdef STRONG_STAT
  2012. if( tTN.values.cnt >= m_sPARAMETERS.nGPointSamples ){
  2013. #else
  2014. if( tTN.values.cnt ){
  2015. #endif
  2016. // if point was measured
  2017. cMax = 0;
  2018. cMin = 4095;
  2019. QAreaOK = true;
  2020. for( int x = -m_sPARAMETERS.nOrder; x < (m_sPARAMETERS.nOrder+1); x++ ) {
  2021. for( int y = -m_sPARAMETERS.nOrder; y < (m_sPARAMETERS.nOrder+1); y++ ) {
  2022. if( !m_bWorkEnable ) return false;
  2023. int LocalX = LocalStrideX+x;
  2024. int LocalY = LocalStrideY+y;
  2025. if( LocalY < 0 ) LocalY += Q_SCAN_COUNT;
  2026. if( LocalY >= Q_SCAN_COUNT ) LocalY -= Q_SCAN_COUNT;
  2027. if( LocalX < 0 ) LocalX += M_SCAN_POINTS;
  2028. if( LocalX >= TH_LINE_NODES ) LocalX -= TH_LINE_NODES;
  2029. uint32_t TestID = ( LocalY * TH_LINE_NODES ) + LocalX;
  2030. tTN.data = th_node[LRF_ID][TestID].data;
  2031. #ifdef STRONG_STAT
  2032. if( tTN.values.cnt >= m_sPARAMETERS.nGPointSamples ){
  2033. #else
  2034. if( tTN.values.cnt ){
  2035. #endif
  2036. if( cMax < tTN.values.max ) cMax = tTN.values.max;
  2037. if( cMin > tTN.values.min ) cMin = tTN.values.min;
  2038. } else {
  2039. QAreaOK = false;
  2040. break;
  2041. };
  2042. };
  2043. if( !QAreaOK ) break;
  2044. };
  2045. if( QAreaOK ){
  2046. tTN.values.cnt = 0x0F;
  2047. tTN.values.min =(cMin>m_sCP_Detector.offset)?cMin-m_sCP_Detector.offset:0;
  2048. tTN.values.max =(cMax<(4095-m_sCP_Detector.offset))?cMax+m_sCP_Detector.offset:4095;
  2049. } else {
  2050. // disable detection of that point
  2051. tTN.values.cnt = 0;
  2052. tTN.values.min = 0;
  2053. tTN.values.max = 4095;
  2054. };
  2055. #ifdef STRONG_STAT
  2056. cGPCount++;
  2057. cQGPCount++;
  2058. #endif
  2059. } else {
  2060. #ifdef STRONG_STAT
  2061. if( tTN.values.cnt ) {
  2062. cGPCount++;
  2063. };
  2064. #endif
  2065. // Not Enought points in values.cnt
  2066. // disable detection of that point
  2067. tTN.values.cnt = 0;
  2068. #ifndef TH_JUST_UPDATE_WORKING
  2069. tTN.values.min = 0;
  2070. tTN.values.max = 4095;
  2071. #endif
  2072. };
  2073. #ifdef TH_JUST_UPDATE_WORKING
  2074. // Just Update - Without Remove TH Old Info
  2075. if( tTN.values.cnt ){
  2076. th_node[8|LRF_ID][LBA_ID].data = tTN.data;
  2077. cWGPCount++;
  2078. };
  2079. #else
  2080. if( tTN.values.cnt ){
  2081. cWGPCount++;
  2082. };
  2083. th_node[8|LRF_ID][LBA_ID].data = tTN.data;
  2084. #endif
  2085. }; // If Flip Enabled to Update Working TH
  2086. };//for lba_id
  2087. #ifdef STRONG_STAT
  2088. if ( cGPCount > 600000 ) {
  2089. bLooseQFlags |= ( 1 << LRF_ID );
  2090. if( ( 600 + cQGPCount ) >= cGPCount ){
  2091. //Turn On Detection on the Kernel X
  2092. bGPointsFlags |= ( 1 << LRF_ID );
  2093. } else {
  2094. //Turn Off Detection on the Kernel X
  2095. bGPointsFlags &= ~( 1 << LRF_ID );
  2096. };
  2097. };
  2098. #endif
  2099. m_sRuntimeStatus._GP[LRF_ID] = cQGPCount;
  2100. m_sRuntimeStatus._MP[LRF_ID] = cGPCount;
  2101. if( m_nDetectionFilter & (1<<LRF_ID) ) {
  2102. m_sRuntimeStatus._WP[LRF_ID] = TH_SIZE_NODES;
  2103. } else {
  2104. m_sRuntimeStatus._WP[LRF_ID] = cWGPCount;
  2105. };
  2106. #ifdef DEBUG
  2107. //sprintf( Buffer, "Current TH [AP:MP:QMP:WP] [%d:%d:%d:%d]", TH_SIZE_NODES, cGPCount, cQGPCount, cWGPCount );
  2108. //LOG( Buffer );
  2109. #endif
  2110. };
  2111. #ifdef TH_FORCED_BOOM_CLEAN
  2112. if( m_nDetectionFilter == m_nKernelFilter ){
  2113. #ifdef DEBUG
  2114. LOG("Forced Boom Clean TH");
  2115. #endif
  2116. for( LRF_ID = 0; LRF_ID < KERNEL_COUNT; LRF_ID++ ){
  2117. if( bGPointsFlags & (1<<LRF_ID) ){
  2118. if( !m_bWorkEnable ) return false;
  2119. if( m_bTHSteadyCounter[LRF_ID] ){
  2120. m_bTHSteadyCounter[LRF_ID] --;
  2121. } else {
  2122. #ifdef DEBUG
  2123. sprintf( Buffer, "Cleanup TH[%d]", LRF_ID );
  2124. LOG(Buffer);
  2125. #endif
  2126. cleanTH( LRF_ID );
  2127. #ifdef DEBUG
  2128. sprintf( Buffer, "Done Cleanup TH[%d]", LRF_ID );
  2129. LOG( Buffer );
  2130. #endif
  2131. m_bTHSteadyCounter[LRF_ID] = m_sCP_Detector.flip_size;
  2132. //clean Good TH
  2133. bGPointsFlags &= ~( 1 << LRF_ID );
  2134. bLooseQFlags &= ~( 1 << LRF_ID );
  2135. };
  2136. };
  2137. };
  2138. }
  2139. #endif //TH_FORCED_BOOM_CLEAN
  2140. // To perform cleaning of Threshold next time
  2141. m_nFlipEnaFLags = bGPointsFlags;
  2142. // To Enable Detection
  2143. m_nDetectionFilter |= bGPointsFlags;
  2144. // To enable Draw
  2145. m_nTHVisualFlags = bLooseQFlags;
  2146. m_sgnFlipCounter++;
  2147. m_sRuntimeStatus._STUDING_LRF = m_nFlipEnaFLags;
  2148. m_sRuntimeStatus._MEASURED_LRF = m_nTHVisualFlags;
  2149. m_sRuntimeStatus._DETECTING_LRF = m_nDetectionFilter;
  2150. m_sRuntimeStatus._FLIP_CNT = m_sgnFlipCounter;
  2151. #ifdef DEBUG
  2152. sprintf( Buffer, "Flip [%d] is Done. FEN:[0x%X] DEN:[0x%X] VIS:[0x%X]", m_sgnFlipCounter, m_nFlipEnaFLags, m_nDetectionFilter, m_nTHVisualFlags );
  2153. LOG( Buffer );
  2154. #endif
  2155. if( m_bDumpMemoryToDisk && ( ( m_sgnFlipCounter % 6 ) == 0 ) ) {
  2156. thresholdNode *tNode;
  2157. thresholdNode *rNode;
  2158. rNode = tNode = (thresholdNode *) m_pSOC->getTH_MEMORY();
  2159. if( tNode != NULL ) {
  2160. #ifdef DEBUG
  2161. LOG( "Save Memory Dump!");
  2162. #endif
  2163. FILE *fd;
  2164. char tName[64];
  2165. for( int index = 0; index < KERNEL_COUNT; index++ ) {
  2166. sprintf( tName, "memdump_%03d-%02d.raw", nMemoryDumpID, index );
  2167. fd = fopen(tName, "wb+");
  2168. if( fd != NULL ) {
  2169. uint32_t tSize;
  2170. tSize = TH_SIZE_NODES;
  2171. fwrite( &tSize, sizeof(uint32_t), 1, fd );
  2172. fwrite( tNode, sizeof(thresholdNode), TH_SIZE_NODES, fd );
  2173. fclose(fd);
  2174. } else {
  2175. LOG_ERROR( "Mem Dump Failure!");
  2176. };
  2177. tNode += TH_SIZE_NODES;
  2178. };
  2179. nMemoryDumpID++;
  2180. };
  2181. };
  2182. EVENT_CONTAINER *tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR_3D, EVT_FLIP, Buffer, NULL );
  2183. if( tEvt != NULL ){
  2184. basicEventHeap::getInstance()->broadcast( tEvt );
  2185. // Evt utilisation done by EventHeap thread
  2186. tEvt = NULL;
  2187. };
  2188. #ifdef HIBERNATE_ENABLE
  2189. return saveHibernatedTH();
  2190. #else
  2191. return true;
  2192. #endif
  2193. } else {
  2194. LOG_ERROR("Cannot access TH Memory!");
  2195. };
  2196. } else {
  2197. LOG_ERROR("No SOC Object!");
  2198. };
  2199. return false;
  2200. #else //#ifndef ONPC_TOTTAL_SIMULATOR
  2201. return true;
  2202. #endif //#ifndef ONPC_TOTTAL_SIMULATOR
  2203. };
  2204. #ifdef HIBERNATE_ENABLE
  2205. bool LRF_HardwareManager::saveHibernatedTH( CSTR fname ){
  2206. FILE *fd;
  2207. fd = fopen( fname, "wb+" );
  2208. if( fd!= NULL) {
  2209. uint32_t *mem;
  2210. uint32_t size;
  2211. mem = m_pSOC->getTH_MEMORY();
  2212. if(mem!= NULL) {
  2213. size = TH_SIZE_NODES << 4;
  2214. fwrite( &mem, sizeof(mem), 1, fd ); // origin
  2215. fwrite( &size, sizeof(size), 1, fd ); // size
  2216. fwrite( mem, sizeof(size), size, fd ); // content
  2217. } else {
  2218. LOG_ERROR("Can not access to TH memories during save Hibernate!");
  2219. return false;
  2220. };
  2221. fclose( fd );
  2222. } else {
  2223. LOG_ERROR("Can not create Hibernate File!");
  2224. return false;
  2225. };
  2226. return true;
  2227. };
  2228. bool LRF_HardwareManager::loadHibernatedTH( CSTR fname ){
  2229. FILE *fd;
  2230. fd = fopen( fname, "wb+" );
  2231. if( fd!= NULL) {
  2232. uint32_t *mem;
  2233. uint32_t *pem;
  2234. uint32_t size;
  2235. mem = m_pSOC->getTH_MEMORY();
  2236. if(mem!= NULL) {
  2237. fread( &pem, sizeof(mem), 1, fd ); // origin
  2238. fread( &size, sizeof(size), 1, fd ); // size
  2239. fread( mem, sizeof(size), size, fd ); // content
  2240. } else {
  2241. LOG_ERROR("Can not access to TH memories during load Hibernate!");
  2242. return false;
  2243. };
  2244. fclose( fd );
  2245. } else {
  2246. LOG_ERROR("Can not read Hibernate File!");
  2247. return false;
  2248. };
  2249. return true;
  2250. };
  2251. #endif
  2252. bool LRF_HardwareManager::cleanTH( uint8_t tID ){
  2253. #ifndef ONPC_TOTTAL_SIMULATOR
  2254. if( m_pSOC != NULL ) {
  2255. uint32_t *thPtr;
  2256. thPtr = m_pSOC->getTH_MEMORY();
  2257. if( thPtr ){
  2258. thPtr += ( tID & 0x0F ) * TH_SIZE_NODES;
  2259. int index = 0; // fl|Max|Min
  2260. for(; index < TH_SIZE_NODES; thPtr++, index++ ){
  2261. if( !m_bWorkEnable ) return false;
  2262. *thPtr = 0x00000FFF;
  2263. }
  2264. return true;
  2265. };
  2266. };
  2267. return false;
  2268. #else //#ifndef ONPC_TOTTAL_SIMULATOR
  2269. return true;
  2270. #endif //#ifndef ONPC_TOTTAL_SIMULATOR
  2271. };
  2272. void LRF_HardwareManager::saveCurrentParameters( void ){
  2273. if( m_pSettings != NULL ) {
  2274. config_setting_t *pSetting;
  2275. int tVal;
  2276. char tBuffer[256];
  2277. LOG("HW Saving Settings ...");
  2278. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_TH_DUMP ) ){
  2279. tVal = m_bDumpMemoryToDisk;
  2280. config_setting_set_bool( pSetting, tVal );
  2281. m_pSettingsProvider->compromise();
  2282. } else {
  2283. LOG_ERROR( "Error Saving Settings!" );
  2284. return;
  2285. };
  2286. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_ROLE ) ){
  2287. tVal = m_sPARAMETERS.flags.isMaster;
  2288. config_setting_set_bool( pSetting, tVal );
  2289. m_pSettingsProvider->compromise();
  2290. LOG("No role defined");
  2291. } else {
  2292. LOG_ERROR( "Error Saving Settings!" );
  2293. return;
  2294. };
  2295. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_SUNBIT ) ){
  2296. tVal = m_sPARAMETERS.flags.Sunbit_filter;
  2297. config_setting_set_bool( pSetting, tVal );
  2298. m_pSettingsProvider->compromise();
  2299. LOG("No SUNBIT action defined - Disabling!");
  2300. } else {
  2301. LOG_ERROR( "Error Saving Settings!" );
  2302. return;
  2303. };
  2304. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_LRF_ENABLED ) ){
  2305. tVal = m_sPARAMETERS.flags.LaserPulsesOn;
  2306. config_setting_set_bool( pSetting, tVal );
  2307. m_pSettingsProvider->compromise();
  2308. LOG("No LRF Enabled");
  2309. } else {
  2310. LOG_ERROR( "Error Saving Settings!" );
  2311. return;
  2312. };
  2313. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_SIG_SYMULATOR ) ){
  2314. tVal = m_sPARAMETERS.flags.SignalSimulator;
  2315. config_setting_set_bool( pSetting, tVal );
  2316. m_pSettingsProvider->compromise();
  2317. LOG("No Sig Symulator Settings");
  2318. } else {
  2319. LOG_ERROR( "Error Saving Settings!" );
  2320. return;
  2321. };
  2322. if( !m_sPARAMETERS.flags.SignalSimulator ) {
  2323. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_AUTOSTART_ENABLED ) ){
  2324. tVal = m_sPARAMETERS.flags.AutostartMotors;
  2325. config_setting_set_bool( pSetting, tVal );
  2326. m_pSettingsProvider->compromise();
  2327. LOG("No Motor Autostart Settings");
  2328. } else {
  2329. LOG_ERROR( "Error Saving Settings!" );
  2330. return;
  2331. };
  2332. if( !m_sPARAMETERS.flags.AutostartMotors ) {
  2333. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_CTRL_V_MOTOR ) ){
  2334. tVal = m_sPARAMETERS.flags.VerticalMotorOn;
  2335. config_setting_set_bool( pSetting, tVal );
  2336. m_pSettingsProvider->compromise();
  2337. LOG("No Vertical Controll");
  2338. } else {
  2339. LOG_ERROR( "Error Saving Settings!" );
  2340. return;
  2341. };
  2342. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_CTRL_H_MOTOR ) ){
  2343. tVal = m_sPARAMETERS.flags.HorizontalMotorOn;
  2344. config_setting_set_bool( pSetting, tVal );
  2345. m_pSettingsProvider->compromise();
  2346. LOG("No Horizontal Controll");
  2347. } else {
  2348. LOG_ERROR( "Error Saving Settings!" );
  2349. return;
  2350. };
  2351. };
  2352. }
  2353. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_V_MOTOR_SETPOINT ) ){
  2354. tVal = m_sPARAMETERS.flags.VerticalFreq;
  2355. config_setting_set_int(pSetting, tVal );
  2356. m_pSettingsProvider->compromise();
  2357. LOG("No Mirror Block Freq");
  2358. } else {
  2359. LOG_ERROR( "Error Saving Settings!" );
  2360. return;
  2361. };
  2362. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_H_MOTOR_SETPOINT ) ){
  2363. tVal = m_sPARAMETERS.flags.HorizontalFreq;
  2364. config_setting_set_int(pSetting, tVal );
  2365. m_pSettingsProvider->compromise();
  2366. LOG("No Base Block Freq");
  2367. } else {
  2368. LOG_ERROR( "Error Saving Settings!" );
  2369. return;
  2370. };
  2371. if ( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_V_PULSES_INTERLEAVE ) ){
  2372. tVal = m_sPARAMETERS.flags.QQinterlace;
  2373. config_setting_set_int(pSetting, tVal );
  2374. m_pSettingsProvider->compromise();
  2375. LOG("No Q Pulses per LRF pulse Setting");
  2376. } else {
  2377. LOG_ERROR( "Error Saving Settings!" );
  2378. return;
  2379. };
  2380. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_TH_OFFSET ) ){
  2381. tVal = m_sPARAMETERS.nOffset;
  2382. config_setting_set_int(pSetting, tVal );
  2383. m_pSettingsProvider->compromise();
  2384. LOG("No TH Offset Setting");
  2385. } else {
  2386. LOG_ERROR( "Error Saving Settings!" );
  2387. return;
  2388. };
  2389. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_TH_ORDER ) ){
  2390. tVal = m_sPARAMETERS.nOrder;
  2391. config_setting_set_int(pSetting, tVal );
  2392. m_pSettingsProvider->compromise();
  2393. LOG("No TH Order Setting");
  2394. } else {
  2395. LOG_ERROR( "Error Saving Settings!" );
  2396. return;
  2397. };
  2398. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_TH_DELTA ) ){
  2399. tVal = m_sPARAMETERS.nDelta;
  2400. config_setting_set_int(pSetting, tVal );
  2401. m_pSettingsProvider->compromise();
  2402. LOG("No TH Delta Setting");
  2403. } else {
  2404. LOG_ERROR( "Error Saving Settings!" );
  2405. return;
  2406. };
  2407. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_TH_FLIP_SIZE ) ){
  2408. tVal = m_sPARAMETERS.nFlip;
  2409. config_setting_set_int(pSetting, tVal );
  2410. m_pSettingsProvider->compromise();
  2411. LOG("No TH Flip Size Setting");
  2412. } else {
  2413. LOG_ERROR( "Error Saving Settings!" );
  2414. return;
  2415. };
  2416. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_TH_GOOD_POINTS ) ){
  2417. tVal = m_sPARAMETERS.nGPointSamples;
  2418. config_setting_set_int(pSetting, tVal );
  2419. m_pSettingsProvider->compromise();
  2420. LOG("No TH Good Points Setting");
  2421. } else {
  2422. LOG_ERROR( "Error Saving Settings!" );
  2423. return;
  2424. };
  2425. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_AUTO_TH_GENERATE ) ){
  2426. tVal = m_sPARAMETERS.flags.BuildThByCore;
  2427. config_setting_set_bool( pSetting, tVal );
  2428. m_pSettingsProvider->compromise();
  2429. LOG("No TH Build By Core Setting");
  2430. } else {
  2431. LOG_ERROR( "Error Saving Settings!" );
  2432. return;
  2433. };
  2434. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_TH_SPIKE_SIZE ) ){
  2435. tVal = m_sPARAMETERS.flags.Spike_TH;
  2436. config_setting_set_int(pSetting, tVal );
  2437. m_pSettingsProvider->compromise();
  2438. LOG("No Spike Threshold Setting");
  2439. } else {
  2440. LOG_ERROR( "Error Saving Settings!" );
  2441. return;
  2442. };
  2443. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_CAN_CONTROL_ENABLE ) ){
  2444. tVal = m_sPARAMETERS.flags.TGZ_canENA;
  2445. config_setting_set_bool( pSetting, tVal );
  2446. m_pSettingsProvider->compromise();
  2447. LOG("No CAN Enable Setting");
  2448. } else {
  2449. LOG_ERROR( "Error Saving Settings!" );
  2450. return;
  2451. };
  2452. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_CAN_BUS_ID ) ){
  2453. tVal = m_sPARAMETERS.flags.TGZ_canBus;
  2454. config_setting_set_int(pSetting, tVal );
  2455. m_pSettingsProvider->compromise();
  2456. LOG("No CAN Bus ID Setting");
  2457. } else {
  2458. LOG_ERROR( "Error Saving Settings!" );
  2459. return;
  2460. };
  2461. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_CAN_MY_ID ) ){
  2462. tVal = m_sPARAMETERS.flags.TGZ_canSRC;
  2463. config_setting_set_int(pSetting, tVal );
  2464. m_pSettingsProvider->compromise();
  2465. LOG("No CAN My ID Setting");
  2466. } else {
  2467. LOG_ERROR( "Error Saving Settings!" );
  2468. return;
  2469. };
  2470. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_CAN_TGZ_ID ) ){
  2471. tVal = m_sPARAMETERS.flags.TGZ_canDST;
  2472. config_setting_set_int(pSetting, tVal );
  2473. m_pSettingsProvider->compromise();
  2474. LOG("No CAN TGZ ID Setting");
  2475. } else {
  2476. LOG_ERROR( "Error Saving Settings!" );
  2477. return;
  2478. };
  2479. #ifndef TGD_VERSION_2
  2480. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_TGZ_DIVIDER ) ){
  2481. tVal = m_sPARAMETERS.flags.TGZ_divider;
  2482. config_setting_set_int(pSetting, tVal );
  2483. m_pSettingsProvider->compromise();
  2484. LOG("No TGZ Divider Setting");
  2485. } else {
  2486. LOG_ERROR( "Error Saving Settings!" );
  2487. return;
  2488. };
  2489. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_TGZ_PRF ) ){
  2490. tVal = m_sPARAMETERS.flags.TGZ_ParamQ;
  2491. config_setting_set_int(pSetting, tVal );
  2492. m_pSettingsProvider->compromise();
  2493. LOG("No TGZ PRF Setting");
  2494. } else {
  2495. LOG_ERROR( "Error Saving Settings!" );
  2496. return;
  2497. };
  2498. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_TGZ_RATIO ) ){
  2499. tVal = m_sPARAMETERS.flags.TGZ_ParamB;
  2500. config_setting_set_int(pSetting, tVal );
  2501. m_pSettingsProvider->compromise();
  2502. LOG("No TGZ Ratio Setting");
  2503. } else {
  2504. LOG_ERROR( "Error Saving Settings!" );
  2505. return;
  2506. };
  2507. #else
  2508. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_TGZ_MIRROR_SPEED ) ){
  2509. tVal = m_sPARAMETERS.flags.TGZ_ParamQ;
  2510. config_setting_set_int(pSetting, tVal );
  2511. m_pSettingsProvider->compromise();
  2512. LOG("No TGZ Mirror Block Speed");
  2513. } else {
  2514. LOG_ERROR( "Error Saving Settings!" );
  2515. return;
  2516. };
  2517. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_TGZ_BASE_SPEED ) ){
  2518. tVal = m_sPARAMETERS.flags.TGZ_ParamB;
  2519. config_setting_set_int(pSetting, tVal );
  2520. m_pSettingsProvider->compromise();
  2521. LOG("No TGZ Base Motor Speed");
  2522. } else {
  2523. LOG_ERROR( "Error Saving Settings!" );
  2524. return;
  2525. };
  2526. #endif
  2527. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_B_SHIFT ) ){
  2528. tVal = m_sPARAMETERS.flags.B_shift;
  2529. config_setting_set_int(pSetting, tVal );
  2530. m_pSettingsProvider->compromise();
  2531. LOG("No Base Shift");
  2532. } else {
  2533. LOG_ERROR( "Error Saving Settings!" );
  2534. return;
  2535. };
  2536. if( pSetting = config_setting_get_member( m_pSettings, HWM_SETTINGS_Q_SHIFT ) ){
  2537. tVal = m_sPARAMETERS.flags.Q_shift;
  2538. config_setting_set_int(pSetting, tVal );
  2539. m_pSettingsProvider->compromise();
  2540. LOG("No Mirror Shift");
  2541. } else {
  2542. LOG_ERROR( "Error Saving Settings!" );
  2543. return;
  2544. };
  2545. if( m_pSettingsProvider->isCompromised() ) {
  2546. LOG("Save Settings");
  2547. m_pSettingsProvider->validate();
  2548. };
  2549. };
  2550. };
  2551. bool LRF_HardwareManager::getRunTimeStat( HWM_CSTATUS *ptr ){
  2552. if ( ptr == NULL ) return false;
  2553. memcpy( ptr, &m_sRuntimeStatus, sizeof(HWM_CSTATUS));
  2554. ptr->HW_STATUS = ((m_bWaitFirstFlip)?0x2000:0x00000000) |
  2555. ((m_bInitOK)?0x1000:0x00000000) |
  2556. #ifndef PRU_SLAVE
  2557. *(( uint32_t *) &m_sCS_Motors ) |
  2558. #endif
  2559. ((m_bDumpMemoryToDisk)?0x4000:0x00000000) |
  2560. ((m_bWorkEnable)?0x8000:0x00000000);
  2561. return true;
  2562. };
  2563. #undef LOG
  2564. #undef LOG_ERROR