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.

2646 lines
88 KiB

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