Old fashion design for PC-104
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.

361 lines
10 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: DPG3AD.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of DPG3AD.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #ifdef __BORLANDC__
  16. #include "..\LgLib\Device\DHebron.h"
  17. #include "..\LgLib\Comm\ProtLGSm.h"
  18. #include "..\LgLib\Comm\Hebron.h"
  19. #else
  20. #include "DHebron.h"
  21. #endif
  22. #define POSITION_QUALYTY 2
  23. #define MAX_NO_REPLY_COUNT 20
  24. #define MAX_NOT_IN_POSITION_COUNT 1
  25. #define DELAY_BEFORE_GET_POSITION 500
  26. CLgDevicePTZHebron::CLgDevicePTZHebron()
  27. {
  28. m_nMode = CMD_NONE;
  29. m_nStage = STAGE_GET_POSITION;
  30. m_nNoReplyCount = 0;
  31. m_nNotInPositionCount = 0;
  32. }
  33. CLgDevicePTZHebron::~CLgDevicePTZHebron()
  34. {
  35. }
  36. BOOL CLgDevicePTZHebron::CmdInit()
  37. {
  38. printf( _T("INIT ") );
  39. // check availibility
  40. return DoCmd( CMD_INIT );
  41. }
  42. void CLgDevicePTZHebron::OnPerformCmd()
  43. {
  44. // ready for new message
  45. ReleaseCmd();
  46. // ready to process new message
  47. m_nMode = CMD_NONE;
  48. }
  49. BOOL CLgDevicePTZHebron::WriteProc( void *pProtocol, PLONG pTimeout )
  50. {
  51. BYTE pSendBuffer[ CProtocolLGS::MAX_COMMAND_SIZE ];
  52. // PBYTE pSendBufferPtr = pSendBuffer;
  53. INT nSendCount = 0;
  54. // // prepare address
  55. // *pSendBufferPtr++ = GetAddress();
  56. // nSendCount++;
  57. // cast to protocol
  58. CProtocolLGS *pProtocolLGS = (CProtocolLGS*)pProtocol;
  59. // set working address
  60. // pProtocolLGS->SetAddress( GetAddress() );
  61. // check input command
  62. if( m_nMode == CMD_NONE )
  63. {
  64. switch( GetCmd() )
  65. {
  66. case CMD_INIT:
  67. printf( _T("CI ") );
  68. m_nMode = CMD_INIT;
  69. break;
  70. case CMD_GET_PTZ:
  71. printf( _T("CG ") );
  72. m_nMode = CMD_GET_PTZ;
  73. break;
  74. case CMD_SET_PTZ:
  75. printf( _T("CS ") );
  76. // LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("CLgDevicePTZHebron::WriteProc"), _T("CMD_SET_PTZ %d,%d,%d"), m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
  77. // validate positions
  78. PTZ_HEBRON::ValidatePan( m_Position.dPan );
  79. PTZ_HEBRON::ValidateTilt( m_Position.dTilt );
  80. PTZ_HEBRON::ValidateZoom( m_Position.dZoom );
  81. m_nMode = CMD_SET_PTZ;
  82. m_nStage = STAGE_GOTO_POSITION;
  83. m_nNotInPositionCount = 0;
  84. break;
  85. case CMD_MOVE_TILT:
  86. printf( _T("CT ") );
  87. m_nMode = CMD_MOVE_TILT;
  88. break;
  89. case CMD_MOVE_PAN:
  90. printf( _T("CP ") );
  91. m_nMode = CMD_MOVE_PAN;
  92. break;
  93. case CMD_MOVE_ZOOM:
  94. printf( _T("CZ ")) ;
  95. m_nMode = CMD_MOVE_ZOOM;
  96. break;
  97. /* case CMD_SET_IRIS:
  98. m_nStage = STAGE_GET_POSITION;
  99. m_nMode = CMD_SET_IRIS;
  100. break;
  101. */ case CMD_NONE:
  102. // no new commands
  103. break;
  104. case CMD_SET_FOCUS:
  105. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgDevicePTZHebron::WriteProc"), _T("CMD_FOCUS is not supported yet") );
  106. case CMD_WIPER:
  107. default:
  108. //command unsupported
  109. OnPerformCmd();
  110. OnEvtCommandError( m_nMode, _T("UNSUPPORTED") );
  111. break;
  112. }
  113. }
  114. LPBYTE pSendBufferPtr = pSendBuffer;
  115. // fill packet depend on command
  116. switch( m_nMode )
  117. {
  118. case CMD_SET_PTZ:
  119. switch( m_nStage )
  120. {
  121. case STAGE_GOTO_POSITION:
  122. // ask for pan position
  123. nSendCount += PTZ_HEBRON::BuildSetPosition( pSendBufferPtr, GetAddress(),
  124. PTZ_HEBRON::Degree2Pan( m_Position.dPan ),
  125. PTZ_HEBRON::Degree2Tilt( m_Position.dTilt ),
  126. PTZ_HEBRON::Percent2Zoom( m_Position.dZoom ) );
  127. break;
  128. case STAGE_GET_POSITION:
  129. // ask for pan position
  130. nSendCount += PTZ_HEBRON::BuildGetPosition( pSendBufferPtr, GetAddress() );
  131. break;
  132. }
  133. break;
  134. case CMD_GET_PTZ:
  135. // ask for pan position
  136. nSendCount += PTZ_HEBRON::BuildGetPosition( pSendBufferPtr, GetAddress() );
  137. break;
  138. case CMD_INIT:
  139. // ask for pan position
  140. nSendCount += PTZ_HEBRON::BuildGetPosition( pSendBufferPtr, GetAddress() );
  141. break;
  142. case CMD_MOVE_PAN:
  143. nSendCount += PTZ_HEBRON::BuildMove( pSendBufferPtr, GetAddress(), PTZ_HEBRON::MOVE_USE_PAN | ( m_MovePan.bRight ? PTZ_HEBRON::MOVE_PAN_RIGHT : 0 ), PTZ_HEBRON::Percent2PanSpeed(m_MovePan.dSpeed), 0, 0 );
  144. break;
  145. case CMD_MOVE_TILT:
  146. nSendCount += PTZ_HEBRON::BuildMove( pSendBufferPtr, GetAddress(), PTZ_HEBRON::MOVE_USE_TILT | ( m_MoveTilt.bUp ? PTZ_HEBRON::MOVE_TILT_UP : 0 ), 0, PTZ_HEBRON::Percent2TiltSpeed(m_MoveTilt.dSpeed), 0 );
  147. break;
  148. case CMD_MOVE_ZOOM:
  149. nSendCount += PTZ_HEBRON::BuildMove( pSendBufferPtr, GetAddress(), PTZ_HEBRON::MOVE_USE_ZOOM | ( m_MoveZoom.bTele ? PTZ_HEBRON::MOVE_ZOOM_TELE : 0 ), 0, 0, PTZ_HEBRON::Percent2ZoomSpeed(m_MoveZoom.dSpeed) );
  150. break;
  151. case CMD_NONE:
  152. default:
  153. *pTimeout = 10L;
  154. return FALSE;
  155. }
  156. printf( _T("CW %p %d"), pProtocolLGS, nSendCount );
  157. // write packet
  158. if( !pProtocolLGS->Write( pSendBuffer, nSendCount, 1000UL, TRUE ) )
  159. return FALSE;
  160. /*
  161. // if command without reply
  162. if( !bReadReply )
  163. {
  164. INT nMode;
  165. switch( m_nMode )
  166. {
  167. case CMD_SET_PTZ:
  168. m_nStage = STAGE_GET_POSITION;
  169. *pTimeout = DELAY_BEFORE_GET_POSITION;
  170. return FALSE;
  171. case CMD_MOVE_PAN:
  172. case CMD_MOVE_TILT:
  173. case CMD_MOVE_ZOOM:
  174. case CMD_SET_IRIS:
  175. nMode = m_nMode;
  176. OnPerformCmd();
  177. switch( nMode )
  178. {
  179. case CMD_MOVE_PAN: OnEvtMovePan( m_MovePan.bRight, m_MovePan.dSpeed ); break;
  180. case CMD_MOVE_TILT: OnEvtMoveTilt( m_MoveTilt.bUp, m_MoveTilt.dSpeed ); break;
  181. case CMD_MOVE_ZOOM: OnEvtMoveZoom( m_MoveZoom.bTele, m_MoveZoom.dSpeed ); break;
  182. case CMD_SET_IRIS: OnEvtSetIris( m_Iris ); break;
  183. }
  184. return FALSE;
  185. }
  186. return FALSE;
  187. }
  188. */
  189. *pTimeout = 250L; // TX + RX
  190. return TRUE;
  191. }
  192. BOOL CLgDevicePTZHebron::ReadProc( void *pProtocol, LONG lTimeout, BOOL bLastChance )
  193. {
  194. CProtocolLGS *pProtocolLGS = (CProtocolLGS*)pProtocol;
  195. // read reply
  196. PBYTE pReadBuffer = NULL;
  197. INT nReadCount = 0;
  198. if( !pProtocolLGS->Read( &pReadBuffer, &nReadCount, lTimeout, TRUE ) || !pReadBuffer || !nReadCount )
  199. {
  200. if( !bLastChance )
  201. return FALSE;
  202. m_nNoReplyCount++;
  203. if( m_nNoReplyCount > MAX_NO_REPLY_COUNT )
  204. {
  205. // perform goto idle state
  206. m_nMode = CMD_NONE;
  207. }
  208. return FALSE;
  209. }
  210. // reset retry counter
  211. m_nNoReplyCount = 0;
  212. WORD wPan;
  213. WORD wTilt;
  214. WORD wZoom;
  215. CLgDevicePTZ::PTZ_DEGREE vPan;
  216. CLgDevicePTZ::PTZ_DEGREE vTilt;
  217. CLgDevicePTZ::PTZ_PERCENT pZoom;
  218. // switch mode
  219. switch( m_nMode )
  220. {
  221. case CMD_GET_PTZ:
  222. // store position
  223. // m_Position.dPan = PTZ_HEBRON::Pan2Degree( PTZ_HEBRON::Pan2Value( pReadBuffer[2], pReadBuffer[3] ) );
  224. // m_Position.dTilt = PTZ_HEBRON::Tilt2Degree( PTZ_HEBRON::Tilt2Value( pReadBuffer[4], pReadBuffer[5] ) );
  225. // m_Position.dZoom = PTZ_HEBRON::Zoom2Percent( PTZ_HEBRON::Zoom2Value( pReadBuffer[6], pReadBuffer[7] ) );
  226. // LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("HEBRON GET PTZ"), _T("%d(%d,%d),%d(%d,%d),%d(%d,%d)"), m_Position.dPan, (INT)pReadBuffer[2], (INT)pReadBuffer[3], m_Position.dTilt, (INT)pReadBuffer[4], (INT)pReadBuffer[5], m_Position.dZoom, (INT)pReadBuffer[6], (INT)pReadBuffer[7] );
  227. printf( _T("RG ") );
  228. // ready to process new message
  229. OnPerformCmd();
  230. //notify
  231. OnEvtGetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
  232. break;
  233. case CMD_INIT:
  234. printf( _T("RI ") );
  235. wPan = PTZ_HEBRON::Pan2Value( pReadBuffer[2], pReadBuffer[3] );
  236. wTilt = PTZ_HEBRON::Tilt2Value( pReadBuffer[4], pReadBuffer[5] );
  237. wZoom = PTZ_HEBRON::Zoom2Value( pReadBuffer[6], pReadBuffer[7] );
  238. printf( _T("RI1 ") );
  239. vPan = PTZ_HEBRON::Pan2Degree( wPan );
  240. vTilt = PTZ_HEBRON::Tilt2Degree( wTilt );
  241. pZoom = PTZ_HEBRON::Zoom2Percent( wZoom );
  242. printf( _T("RI2 ") );
  243. // store position
  244. m_Position.dPan = vPan;
  245. m_Position.dTilt = vTilt;
  246. m_Position.dZoom = pZoom;
  247. // LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("HEBRON INIT"), _T("%d(%d,%d),%d(%d,%d),%d(%d,%d)"), m_Position.dPan, (INT)pReadBuffer[2], (INT)pReadBuffer[3], m_Position.dTilt, (INT)pReadBuffer[4], (INT)pReadBuffer[5], m_Position.dZoom, (INT)pReadBuffer[6], (INT)pReadBuffer[7] );
  248. printf( _T("RI3 ") );
  249. // ready to process new message
  250. OnPerformCmd();
  251. //notify
  252. OnEvtInit();
  253. break;
  254. case CMD_SET_PTZ:
  255. // m_TempPosition.dPan = PTZ_HEBRON::Pan2Degree( PTZ_HEBRON::Pan2Value( pReadBuffer[2], pReadBuffer[3] ) );
  256. // m_TempPosition.dTilt = PTZ_HEBRON::Tilt2Degree( PTZ_HEBRON::Tilt2Value( pReadBuffer[4], pReadBuffer[5] ) );
  257. // m_TempPosition.dZoom = PTZ_HEBRON::Zoom2Percent( PTZ_HEBRON::Zoom2Value( pReadBuffer[6], pReadBuffer[7] ) );
  258. // LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("HEBRON SET PTZ"), _T("%d(%d,%d),%d(%d,%d),%d(%d,%d)"), m_Position.dPan, (INT)pReadBuffer[2], (INT)pReadBuffer[3], m_Position.dTilt, (INT)pReadBuffer[4], (INT)pReadBuffer[5], m_Position.dZoom, (INT)pReadBuffer[6], (INT)pReadBuffer[7] );
  259. printf( _T("RS ") );
  260. /* // calculate difference between desired position and current position
  261. if( QualifyPosition( m_TempPosition.dPan - m_Position.dPan,
  262. m_TempPosition.dTilt - m_Position.dTilt,
  263. m_TempPosition.dZoom - m_Position.dZoom,
  264. POSITION_QUALYTY ) ||
  265. m_nNotInPositionCount >= MAX_NOT_IN_POSITION_COUNT )
  266. {
  267. // store position
  268. m_Position.dPan = m_TempPosition.dPan;
  269. m_Position.dTilt = m_TempPosition.dTilt;
  270. m_Position.dZoom = m_TempPosition.dZoom;
  271. */ // ready to process new message
  272. OnPerformCmd();
  273. //notify
  274. OnEvtSetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
  275. /* }
  276. else
  277. {
  278. // does not in posiiton
  279. m_nStage = STAGE_GOTO_POSITION;
  280. // increate fault counter
  281. m_nNotInPositionCount++;
  282. return TRUE;
  283. }
  284. */ break;
  285. case CMD_MOVE_PAN:
  286. // LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("HEBRON MOVE PAN"), _T("") );
  287. printf( _T("RP ") );
  288. // ready to process new message
  289. OnPerformCmd();
  290. OnEvtMovePan( m_MovePan.bRight, m_MovePan.dSpeed );
  291. break;
  292. case CMD_MOVE_TILT:
  293. // LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("HEBRON MOVE TILT"), _T("") );
  294. printf( _T("RT ") );
  295. // ready to process new message
  296. OnPerformCmd();
  297. OnEvtMoveTilt( m_MoveTilt.bUp, m_MoveTilt.dSpeed );
  298. break;
  299. case CMD_MOVE_ZOOM:
  300. // LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("HEBRON MOVE ZOOM"), _T("") );
  301. printf( _T("RZ ") );
  302. // ready to process new message
  303. OnPerformCmd();
  304. OnEvtMoveZoom( m_MoveZoom.bTele, m_MoveZoom.dSpeed );
  305. break;
  306. }
  307. return TRUE;
  308. }