欧美日韩精品国产,欧美aⅴ99久久黑人专区,亚洲综合久久久久,中文字幕一区二区三区精彩视频

OpenHaptics編程環境搭建

文章來源:SouVR 作者:frank 發布時間:2018年11月05日 點擊數:

SensAble Technologies(已被3Dsystems收購)公司是3D可觸摸(力反饋)解決方案和技術領域中的領先開發商,其解決方案和技術不僅使用戶能夠看到并聽到屏幕計算機應用,還可以對該應用進行實際“感應”。該公司的PHANTOM系列觸覺與力反饋交互設備能使用戶接觸并操作虛擬物體。其觸覺技術廣泛應用于諸多領域,包括外科手術模擬、牙科整形、虛擬裝配與虛擬維修、3D 設計(藝術和雕塑),以及機器人遙操作等領域。

使用官方提供的OpenHaptics Toolkit可以方便的編寫基于Phantom觸覺力反饋設備的應用程序。OpenHaptics工具包包括QuickHaptics Micro API、Haptic Device API(HDAPI)、Haptic Library API (HLAPI)、PHANTOM Device Drivers(PDD)、實用工具和源代碼示例。最底層的是PDD設備驅動程序,它支持公司所有系列的力反饋設備;HDAPI提供了一個底層接口,通過它能直接產生反饋力以及獲取設備的狀態信息;HLAPI則為熟悉OpenGL的編程人員提供了一個上層接口。

下面來搭建OpenHaptics的編程環境(Win7 64位系統 + VS2013 + OpenHaptics 3.4.0),先在這里下載設備驅動程序和OpenHaptics Toolkit。參考安裝手冊連接好電腦和Phantom設備后可以打開官方自帶的Phantom Test程序進行測試,驗證設備是否連接成功。

接下來使用HDAPI創建一個力反饋應用程序(以examples\HD\console\HelloHapticDevice為例),程序基本流程如下:

  1. ?Initialize the device.

  2. ?Create a scheduler callback.

  3. ?Enable device forces.

  4. ?Start the scheduler.

  5. ?Cleanup the device and scheduler when the application is terminated.?

參考下面的程序結構圖,為了實現流暢穩定的觸覺力反饋,創建的任務將以1000Hz的頻率運行在優先級高的線程中(The scheduler manages a high frequency, high priority thread for sending forces and retrieving state information from the device.?Typically, force updates need to be sent at 1000 Hz frequency in order to create compelling and stable force feedback):

下面配置VS工程:

1)設置頭文件包含目錄(Set the correct include path, as shown below in “Additional Include Directories.”)

Examples for setting include paths:

$(OH_SDK_BASE)\include ? ? ? ? ? ? ? ? ? ? Main include directory for the HD library.
$(OH_SDK_BASE)\utilities\include ? ? ? ? Include directory for utilities.

2)添加庫文件(Add the appropriate library modules as shown below in “Additional Dependencies”)

3)設置附加庫目錄(Make sure the linker paths are set correctly on the “Additional Library Directories” line so that the library fles can be found whenyour application links)

As for the header fle include paths, the library directories will use the OH_SDK_BASE environment variable. In general VisualStudio?will automatically set the PlatformName to be one of Win32 or x64 and the ConfgurationName to be either Release or Debug.?

配置好工程屬性后就可以點擊生成按鈕,不過生成過程中出現了“error LNK2019:無法解析的外部符號”這種錯誤(很多人遇到過這個問題):

在網上搜索解決方案,3DSystems Haptics Forums上有一種辦法是將utilities中的lib文件重新編譯一遍,源文件就在utilities/src中:

使用VS013重新生成Win32、x64平臺下的Debug和Release版本的lib,然后將原先的替換掉,再進行編譯就OK了。

下面看看HelloHapticDevice的代碼:

  1. /***************************************************************************** ?
  2. Description:? ?
  3. ?
  4. ??This?application?creates?a?gravity?well,?which?will?attract ?
  5. ??the?device?towards?its?center?when?the?device?enters?its?proximity.?? ?
  6. *******************************************************************************/?
  7. ?
  8. #include?<stdio.h> ?
  9. #include?<string.h> ?
  10. #include?<conio.h> ?
  11. ?
  12. #include?<HD/hd.h> ?
  13. #include?<HDU/hduError.h> ?
  14. #include?<HDU/hduVector.h> ?
  15. ?
  16. HDCallbackCode?HDCALLBACK?gravityWellCallback(void?*data); ?
  17. ?
  18. ?
  19. /*******************************************************************************
  20. ?Main?function. ?
  21. ?Initializes?the?device,?starts?the?schedule,?creates?a?schedule?callback ?
  22. ?to?handle?gravity?well?forces,?waits?for?the?user?to?press?a?button,?exits ?
  23. ?the?application. ?
  24. *******************************************************************************/?
  25. int?main(int?argc,?char*?argv[]) ?
  26. {???? ?
  27. ????HDErrorInfo?error; ?
  28. ????HDSchedulerHandle?hGravityWell; ?
  29. ?
  30. ????/*?Initialize?the?device,?must?be?done?before?attempting?to?call?any?hd?
  31. ???????functions.?Passing?in?HD_DEFAULT_DEVICE?causes?the?default?device?to?be? ?
  32. ???????initialized.?*/?
  33. ????HHD?hHD?=?hdInitDevice(HD_DEFAULT_DEVICE); ?
  34. ????if?(HD_DEVICE_ERROR(error?=?hdGetError()))? ?
  35. ????{ ?
  36. ????????hduPrintError(stderr,?&error,?"Failed?to?initialize?haptic?device"); ?
  37. ????????fprintf(stderr,?"\nPress?any?key?to?quit.\n"); ?
  38. ????????return?-1; ?
  39. ????} ?
  40. ?
  41. ????printf("Hello?Haptic?Device!\n"); ?
  42. ????printf("Found?device?model:?%s.\n\n",?hdGetString(HD_DEVICE_MODEL_TYPE)); ?
  43. ?
  44. ????/*?Schedule?the?main?callback?that?will?render?forces?to?the?device.?*/?
  45. ????hGravityWell?=?hdScheduleAsynchronous(gravityWellCallback,?0,?HD_MAX_SCHEDULER_PRIORITY); ?
  46. ?
  47. ????hdEnable(HD_FORCE_OUTPUT); ?
  48. ????hdStartScheduler(); ?
  49. ?
  50. ????/*?Check?for?errors?and?abort?if?so.?*/?
  51. ????if?(HD_DEVICE_ERROR(error?=?hdGetError())) ?
  52. ????{ ?
  53. ????????hduPrintError(stderr,?&error,?"Failed?to?start?scheduler"); ?
  54. ????????fprintf(stderr,?"\nPress?any?key?to?quit.\n"); ?
  55. ????????return?-1; ?
  56. ????} ?
  57. ?
  58. ????/*?Wait?until?the?user?presses?a?key.??Meanwhile,?the?scheduler
  59. ????runs?and?applies?forces?to?the?device.?*/?
  60. ????printf("Feel?around?for?the?gravity?well...\n"); ?
  61. ????printf("Press?any?key?to?quit.\n\n"); ?
  62. ????while?(!kbhit()) ?
  63. ????{ ?
  64. ????????/*?Periodically?check?if?the?gravity?well?callback?has?exited.?*/?
  65. ????????if?(!hdWaitForCompletion(hGravityWell,?HD_WAIT_CHECK_STATUS))??//?Checks?if?a?callback?is?still?scheduled?for?execution. ?
  66. ????????{ ?
  67. ????????????fprintf(stderr,?"Press?any?key?to?quit.\n");????? ?
  68. ????????????break; ?
  69. ????????} ?
  70. ????} ?
  71. ?
  72. ????/*?For?cleanup,?unschedule?callback?and?stop?the?scheduler.?*/?
  73. ????hdStopScheduler();??????????//?Typically?call?this?as?a?frst?step?for?cleanup?and?shutdown?of?devices ?
  74. ????hdUnschedule(hGravityWell);?//?removing?the?associated?callback?from?the?scheduler. ?
  75. ????hdDisableDevice(hHD);???????//?Disables?a?device.?The?handle?should?not?be?used?afterward ?
  76. ?
  77. ????return?0; ?
  78. } ?
  79. ?
  80. ?
  81. ?
  82. /*******************************************************************************
  83. ?Servo?callback.?? ?
  84. ?Called?every?servo?loop?tick.??Simulates?a?gravity?well,?which?sucks?the?device? ?
  85. ?towards?its?center?whenever?the?device?is?within?a?certain?range. ?
  86. *******************************************************************************/?
  87. HDCallbackCode?HDCALLBACK?gravityWellCallback(void?*data) ?
  88. { ?
  89. ????const?HDdouble?kStiffness?=?0.075;?/*?N/mm?*/?
  90. ????const?HDdouble?kGravityWellInfluence?=?40;?/*?mm?*/?
  91. ?
  92. ????/*?This?is?the?position?of?the?gravity?well?in?cartesian(i.e.?x,y,z)?space.?*/?
  93. ????static?const?hduVector3Dd?wellPos?=?{0,0,0}; ?
  94. ?
  95. ????HDErrorInfo?error; ?
  96. ????hduVector3Dd?position; ?
  97. ????hduVector3Dd?force; ?
  98. ????hduVector3Dd?positionTwell; ?
  99. ?
  100. ????HHD?hHD?=?hdGetCurrentDevice();??//?Gets?the?handle?of?the?current?device ?
  101. ?
  102. ????/*?Begin?haptics?frame.??(?In?general,?all?state-related?haptics?calls
  103. ???????should?be?made?within?a?frame.?)?*/?
  104. ????hdBeginFrame(hHD); ?
  105. ?
  106. ????/*?Get?the?current?position?of?the?device.?*/?
  107. ????hdGetDoublev(HD_CURRENT_POSITION,?position); ?
  108. ???? ?
  109. ????memset(force,?0,?sizeof(hduVector3Dd)); ?
  110. ???? ?
  111. ????/*?>??positionTwell?=?wellPos-position??<?
  112. ???????Create?a?vector?from?the?device?position?towards?the?gravity? ?
  113. ???????well's?center.?*/?
  114. ????hduVecSubtract(positionTwell,?wellPos,?position); ?
  115. ???? ?
  116. ????/*?If?the?device?position?is?within?some?distance?of?the?gravity?well's?
  117. ???????center,?apply?a?spring?force?towards?gravity?well's?center.??The?force ?
  118. ???????calculation?differs?from?a?traditional?gravitational?body?in?that?the ?
  119. ???????closer?the?device?is?to?the?center,?the?less?force?the?well?exerts; ?
  120. ???????the?device?behaves?as?if?a?spring?were?connected?between?itself?and ?
  121. ???????the?well's?center.?*/?
  122. ????if?(hduVecMagnitude(positionTwell)?<?kGravityWellInfluence) ?
  123. ????{ ?
  124. ????????/*?>??F?=?k?*?x??<?
  125. ???????????F:?Force?in?Newtons?(N) ?
  126. ???????????k:?Stiffness?of?the?well?(N/mm) ?
  127. ???????????x:?Vector?from?the?device?endpoint?position?to?the?center? ?
  128. ???????????of?the?well.?*/?
  129. ????????hduVecScale(force,?positionTwell,?kStiffness); ?
  130. ????} ?
  131. ?
  132. ????/*?Send?the?force?to?the?device.?*/?
  133. ????hdSetDoublev(HD_CURRENT_FORCE,?force); ?
  134. ???? ?
  135. ????/*?End?haptics?frame.?*/?
  136. ????hdEndFrame(hHD); ?
  137. ?
  138. ????/*?Check?for?errors?and?abort?the?callback?if?a?scheduler?error
  139. ???????is?detected.?*/?
  140. ????if?(HD_DEVICE_ERROR(error?=?hdGetError())) ?
  141. ????{ ?
  142. ????????hduPrintError(stderr,?&error,? ?
  143. ??????????????????????"Error?detected?while?rendering?gravity?well\n"); ?
  144. ???????? ?
  145. ????????if?(hduIsSchedulerError(&error)) ?
  146. ????????{ ?
  147. ????????????return?HD_CALLBACK_DONE; ?
  148. ????????} ?
  149. ????} ?
  150. ?
  151. ????/*?Signify?that?the?callback?should?continue?running,?i.e.?that
  152. ???????it?will?be?called?again?the?next?scheduler?tick.?*/?
  153. ????return?HD_CALLBACK_CONTINUE; ?
  154. }?

?  

該程序在循環執行的任務中實時獲取操作手柄的位置信息,并以此來計算輸出力來模擬彈簧力。這里有幾個需要注意的地方:

1. Haptic Frames:

為了保證數據訪問的一致性,OpenHaptics提供了一種Frame框架結構,反饋給用戶力的過程一般都是在力反饋幀中處理的,使用hdBeginFrame和hdEndFrame作為訪問的開始和結束。在同一幀中多次調用hdGet類函數獲取信息會得到相同的結果;多次調用hdSet類函數設置同一狀態,則最后的調用會替代掉以前的(Forces are not actually sent to the device until the end of the frame. Setting the same state twice will replace the frst with the?second)。即在幀的結尾,所有的屬性改變才會得以應用。

Haptic frames defne a scope within which the device state is guaranteed to be consistent. Frames are bracketed by?hdBeginFrame()?and?hdEndFrame()?statements. At the start of the frame, the device state is updated and stored for use in that frame so that?all state?queries in the frame re?ects a snapshot of that data. At the end of the frame, new state such as forces is written out to the device .?Most haptics operations should be run within a frame. Calling operations within a frame ensures consistency for the data being used?because state remains the same within the frame. Getting state outside a frame typically returns the state from the last frame.?Setting?state outside a frame typically results in an error.

HDAPI力反饋程序框架如下圖所示:

2. 同步調用與異步調用:

所謂同步就是在發出一個“調用”時,在沒有得到結果之前,該“調用”就不返回;而異步則是相反,“調用”在發出之后這個調用就直接返回了,即當一個異步過程調用發出后,調用者不會立刻得到結果。Synchronous calls only return after they are completed, so?the application thread waits for a synchronous call before continuing. Asynchronous calls return immediately after being scheduled.?

同步調用主要用于獲取設備當前狀態,比如位置、力、開關狀態等。Synchronous calls are primarily used for getting a snapshot of the state of the scheduler for the application. For example, if the?application needs to query position or button state, or any other variable or state that the scheduler is changing, it should do so using a?synchronous call.

下面是同步調用的一個例子:

  1. //?client?data?declaration ?
  2. struct?DeviceDisplayState ?
  3. { ?
  4. ????HDdouble?position[3]; ?
  5. ????HDdouble?force[3]; ?
  6. }? ?
  7. ?
  8. //?usage?of?the?above?client?data,?within?a?simple?callback. ?
  9. HDCallbackCode?HDCALLBACK?DeviceStateCallback(void?*pUserData)? ?
  10. { ?
  11. ????DeviceDisplayState?*pDisplayState?=?(DeviceDisplayState?*)pUserData; ?
  12. ???? ?
  13. ????hdGetDoublev(HD_CURRENT_POSITION,?pDisplayState->position); ?
  14. ????hdGetDoublev(HD_CURRENT_FORCE,????pDisplayState->force); ?
  15. ???? ?
  16. ????return?HD_CALLBACK_DONE;??//?execute?this?only?once ?
  17. }? ?
  18. ?
  19. ?
  20. //?get?the?current?position?of?end-effector ?
  21. DeviceDisplayState?state; ?
  22. ?
  23. hdScheduleSynchronous(DeviceStateCallback,?&state,?HD_MIN_SCHEDULER_PRIORITY);?

?

異步調用主要用于循環處理任務中,例如根據力反饋設備操作末端的位置來計算并輸出力。Asynchronous calls are often the best mechanism for managing the haptics loop. For example, an asynchronous callback can persist in?the scheduler to represent a haptics effect: during each iteration, the callback applies the effect to the device.?

  1. HDCallbackCode?HDCALLBACK?CoulombCallback(void?*data) ?
  2. { ?
  3. ????HHD?hHD?=?hdGetCurrentDevice(); ?
  4. ???? ?
  5. ????hdBeginFrame(hHD); ?
  6. ???? ?
  7. ????HDdouble?pos[3]; ?
  8. ????hdGetDoublev(HD_CURRENT_POSITION,?pos);?//retrieve?the?position?of?the?end-effector. ?
  9. ???? ?
  10. ????HDdouble?force[3]; ?
  11. ????forceField(pos,?force);?????????????????//?given?the?position,?calculate?a?force ?
  12. ????hdSetDoublev(HD_CURRENT_FORCE,?force);??//?set?the?force?to?the?device ?
  13. ???? ?
  14. ????hdEndFrame(hHD);????????????????????????//??ush?the?force ?
  15. ???? ?
  16. ????return?HD_CALLBACK_CONTINUE;????????????//?run?at?every?servo?loop?tick. ?
  17. } ?
  18. ?
  19. hdScheduleAsynchronous(AForceSettingCallback,?0,?HD_DEFAULT_SCHEDULER_PRIORITY);?

?

3. 任務返回值:

?HD_CALLBACK_DONE (只執行一次)
?HD_CALLBACK_CONTINUE(循環執行)

根據不同的返回值,回調函數會在當前幀運行完畢后判斷是否在下一幀再次運 行,當返回值為HD_CALLBACK_CONTINUE時,此回調函數在下一幀時會繼續重新 運行;而當返回值為HD_CALLBACK_DONE?時,此回調函數在下一幀時不再次運行。Callbacks can be set to run either once or multiple times, depending on the callback’s return value. If the return value requests for?the callback to continue, it is rescheduled and run again during the next scheduler tick. Otherwise it is taken off the scheduler and?considered complete, and control is returned to the calling thread in the case of synchronous operations.

4. 任務優先級:

Callbacks are scheduled with a priority, which determines what order they are run in the scheduler.?For every scheduler tick, each?callback is always executed. The order the callbacks are executed depends on the priority;?highest priority items are run before lowest.?Operations with equal priority are executed in arbitrary order.?

下面再看一個獲取設備信息的典型例子(examples\HD\console\QueryDevice):

  1. #include?<stdio.h> ?
  2. #include?<string.h> ?
  3. #include?<conio.h> ?
  4. ?
  5. #include?<HD/hd.h> ?
  6. #include?<HDU/hduError.h> ?
  7. #include?<HDU/hduVector.h> ?
  8. ?
  9. /*?Holds?data?retrieved?from?HDAPI.?*/?
  10. typedef?struct?
  11. { ?
  12. ????HDboolean?m_buttonState;???????/*?Has?the?device?button?has?been?pressed.?*/?
  13. ????hduVector3Dd?m_devicePosition;?/*?Current?device?coordinates.?*/?
  14. ????HDErrorInfo?m_error; ?
  15. }?DeviceData; ?
  16. ?
  17. static?DeviceData?gServoDeviceData; ?
  18. ?
  19. /*******************************************************************************
  20. Checks?the?state?of?the?gimbal?button?and?gets?the?position?of?the?device. ?
  21. *******************************************************************************/?
  22. HDCallbackCode?HDCALLBACK?updateDeviceCallback(void?*pUserData) ?
  23. { ?
  24. ????int?nButtons?=?0; ?
  25. ?
  26. ????hdBeginFrame(hdGetCurrentDevice()); ?
  27. ?
  28. ????/*?Retrieve?the?current?button(s).?*/?
  29. ????hdGetIntegerv(HD_CURRENT_BUTTONS,?&nButtons); ?
  30. ?
  31. ????/*?In?order?to?get?the?specific?button?1?state,?we?use?a?bitmask?to
  32. ????test?for?the?HD_DEVICE_BUTTON_1?bit.?*/?
  33. ????gServoDeviceData.m_buttonState?= ?
  34. ????????(nButtons?&?HD_DEVICE_BUTTON_1)???HD_TRUE?:?HD_FALSE; ?
  35. ?
  36. ????/*?Get?the?current?location?of?the?device?(HD_GET_CURRENT_POSITION)
  37. ????We?declare?a?vector?of?three?doubles?since?hdGetDoublev?returns ?
  38. ????the?information?in?a?vector?of?size?3.?*/?
  39. ????hdGetDoublev(HD_CURRENT_POSITION,?gServoDeviceData.m_devicePosition); ?
  40. ?
  41. ????/*?Also?check?the?error?state?of?HDAPI.?*/?
  42. ????gServoDeviceData.m_error?=?hdGetError(); ?
  43. ?
  44. ????/*?Copy?the?position?into?our?device_data?tructure.?*/?
  45. ????hdEndFrame(hdGetCurrentDevice()); ?
  46. ?
  47. ????return?HD_CALLBACK_CONTINUE; ?
  48. } ?
  49. ?
  50. ?
  51. /*******************************************************************************
  52. Checks?the?state?of?the?gimbal?button?and?gets?the?position?of?the?device. ?
  53. *******************************************************************************/?
  54. HDCallbackCode?HDCALLBACK?copyDeviceDataCallback(void?*pUserData) ?
  55. { ?
  56. ????DeviceData?*pDeviceData?=?(DeviceData?*)pUserData; ?
  57. ?
  58. ????//?void?*memcpy(void?*dest,?const?void?*src,?size_t?n); ?
  59. ????memcpy(pDeviceData,?&gServoDeviceData,?sizeof(DeviceData));?? ?
  60. ?
  61. ????return?HD_CALLBACK_DONE; ?
  62. } ?
  63. ?
  64. ?
  65. /*******************************************************************************
  66. Prints?out?a?help?string?about?using?this?example. ?
  67. *******************************************************************************/?
  68. void?printHelp(void) ?
  69. { ?
  70. ????static?const?char?help[]?=?{?"\ ?
  71. ?????????????????????????????????Press?and?release?the?stylus?button?to?print?out?the?current?device?location.\n\ ?
  72. ?????????????????????????????????Press?and?hold?the?stylus?button?to?exit?the?application\n"?}; ?
  73. ?
  74. ????fprintf(stdout,?"%s\n",?help); ?
  75. } ?
  76. ?
  77. ?
  78. /*******************************************************************************
  79. This?routine?allows?the?device?to?provide?information?about?the?current ?
  80. location?of?the?stylus,?and?contains?a?mechanism?for?terminating?the ?
  81. application. ?
  82. Pressing?the?button?causes?the?application?to?display?the?current?location ?
  83. of?the?device. ?
  84. Holding?the?button?down?for?N?iterations?causes?the?application?to?exit. ?
  85. *******************************************************************************/?
  86. void?mainLoop(void) ?
  87. { ?
  88. ????static?const?int?kTerminateCount?=?1000; ?
  89. ????int?buttonHoldCount?=?0; ?
  90. ?
  91. ????/*?Instantiate?the?structure?used?to?capture?data?from?the?device.?*/?
  92. ????DeviceData?currentData; ?
  93. ????DeviceData?prevData; ?
  94. ?
  95. ????/*?Perform?a?synchronous?call?to?copy?the?most?current?device?state.?*/?
  96. ????hdScheduleSynchronous(copyDeviceDataCallback, ?
  97. ????????&currentData,?HD_MIN_SCHEDULER_PRIORITY); ?
  98. ?
  99. ????memcpy(&prevData,?&currentData,?sizeof(DeviceData)); ?
  100. ?
  101. ????printHelp(); ?
  102. ?
  103. ????/*?Run?the?main?loop?until?the?gimbal?button?is?held.?*/?
  104. ????while?(1) ?
  105. ????{ ?
  106. ????????/*?Perform?a?synchronous?call?to?copy?the?most?current?device?state.
  107. ????????This?synchronous?scheduler?call?ensures?that?the?device?state ?
  108. ????????is?obtained?in?a?thread-safe?manner.?*/?
  109. ????????hdScheduleSynchronous(copyDeviceDataCallback, ?
  110. ????????????&currentData, ?
  111. ????????????HD_MIN_SCHEDULER_PRIORITY); ?
  112. ?
  113. ????????/*?If?the?user?depresses?the?gimbal?button,?display?the?current
  114. ????????location?information.?*/?
  115. ????????if?(currentData.m_buttonState?&&?!prevData.m_buttonState) ?
  116. ????????{ ?
  117. ????????????fprintf(stdout,?"Current?position:?(%g,?%g,?%g)\n", ?
  118. ????????????????currentData.m_devicePosition[0], ?
  119. ????????????????currentData.m_devicePosition[1], ?
  120. ????????????????currentData.m_devicePosition[2]); ?
  121. ????????} ?
  122. ????????else?if?(currentData.m_buttonState?&&?prevData.m_buttonState) ?
  123. ????????{ ?
  124. ????????????/*?Keep?track?of?how?long?the?user?has?been?pressing?the?button.
  125. ????????????If?this?exceeds?N?ticks,?then?terminate?the?application.?*/?
  126. ????????????buttonHoldCount++; ?
  127. ?
  128. ????????????if?(buttonHoldCount?>?kTerminateCount) ?
  129. ????????????{ ?
  130. ????????????????/*?Quit,?since?the?user?held?the?button?longer?than
  131. ????????????????the?terminate?count.?*/?
  132. ????????????????break; ?
  133. ????????????} ?
  134. ????????} ?
  135. ????????else?if?(!currentData.m_buttonState?&&?prevData.m_buttonState) ?
  136. ????????{ ?
  137. ????????????/*?Reset?the?button?hold?count,?since?the?user?stopped?holding
  138. ????????????down?the?stylus?button.?*/?
  139. ????????????buttonHoldCount?=?0; ?
  140. ????????} ?
  141. ?
  142. ?
  143. ????????/*?Check?if?an?error?occurred.?*/?
  144. ????????if?(HD_DEVICE_ERROR(currentData.m_error)) ?
  145. ????????{ ?
  146. ????????????hduPrintError(stderr,?&currentData.m_error,?"Device?error?detected"); ?
  147. ?
  148. ????????????if?(hduIsSchedulerError(&currentData.m_error)) ?
  149. ????????????{ ?
  150. ????????????????/*?Quit,?since?communication?with?the?device?was?disrupted.?*/?
  151. ????????????????fprintf(stderr,?"\nPress?any?key?to?quit.\n"); ?
  152. ????????????????break; ?
  153. ????????????} ?
  154. ????????} ?
  155. ?
  156. ????????/*?Store?off?the?current?data?for?the?next?loop.?*/?
  157. ????????memcpy(&prevData,?&currentData,?sizeof(DeviceData)); ?
  158. ????} ?
  159. } ?
  160. ?
  161. /*******************************************************************************
  162. Main?function. ?
  163. Sets?up?the?device,?runs?main?application?loop,?cleans?up?when?finished. ?
  164. *******************************************************************************/?
  165. int?main(int?argc,?char*?argv[]) ?
  166. { ?
  167. ????HDSchedulerHandle?hUpdateHandle?=?0; ?
  168. ????HDErrorInfo?error; ?
  169. ?
  170. ????/*?Initialize?the?device,?must?be?done?before?attempting?to?call?any?hd
  171. ????functions.?*/?
  172. ????HHD?hHD?=?hdInitDevice(HD_DEFAULT_DEVICE); ?
  173. ????if?(HD_DEVICE_ERROR(error?=?hdGetError())) ?
  174. ????{ ?
  175. ????????hduPrintError(stderr,?&error,?"Failed?to?initialize?the?device"); ?
  176. ????????fprintf(stderr,?"\nPress?any?key?to?quit.\n"); ?
  177. ????????return?-1; ?
  178. ????} ?
  179. ?
  180. ????/*?Schedule?the?main?scheduler?callback?that?updates?the?device?state.?*/?
  181. ????hUpdateHandle?=?hdScheduleAsynchronous( ?
  182. ????????updateDeviceCallback,?0,?HD_MAX_SCHEDULER_PRIORITY); ?
  183. ?
  184. ????/*?Start?the?servo?loop?scheduler.?*/?
  185. ????hdStartScheduler(); ?
  186. ????if?(HD_DEVICE_ERROR(error?=?hdGetError())) ?
  187. ????{ ?
  188. ????????hduPrintError(stderr,?&error,?"Failed?to?start?the?scheduler"); ?
  189. ????????fprintf(stderr,?"\nPress?any?key?to?quit.\n"); ?
  190. ????????return?-1; ?
  191. ????} ?
  192. ?
  193. ????/*?Run?the?application?loop.?*/?
  194. ????mainLoop(); ?
  195. ?
  196. ????/*?For?cleanup,?unschedule?callbacks?and?stop?the?servo?loop.?*/?
  197. ????hdStopScheduler(); ?
  198. ????hdUnschedule(hUpdateHandle); ?
  199. ????hdDisableDevice(hHD); ?
  200. ?
  201. ????return?0; ?
  202. }?

主函數中開啟updateDeviceCallback異步任務后,該任務不停地刷新設備末端的位置以及按鈕狀態等信息,保存在靜態全局變量gServoDeviceData中。在主程序的mainLoop循環中使用copyDeviceDataCallback同步調用方式獲取設備狀態信息,然后進行判斷。按一下操作手柄上的按鈕打印一條當前位置信息;長按按鈕超過一定時間則退出mainLoop循環,結束程序。

值得注意的是主線程中函數獲取Servo Loop線程(任務線程)中的數據,可以通過同步調用來實現,是一種線程安全(thread-safe)方式。線程安全就是多線程訪問時,采用了加鎖機制,當一個線程訪問該類的某個數據時,進行保護,其他線程不能進行訪問直到該線程讀取完,其他線程才可使用。hdScheduleSynchronous:Typically used as a synchronization mechanism between the servo loop thread and other threads in the application. For example,?if the main application thread needs to access the position or?button state, it can do so through a synchronous scheduler call. Can be used for synchronously?copying state from the servo loop.

在mainLoop函數中也可以直接訪問全局變量gServoDeviceData來獲取設備信息,但這種方式是線程不安全的,即不提供數據訪問保護,有可能出現多個線程先后更改數據造成所得到的數據是臟數據。如果其他線程企圖訪問一個處于不可用狀態的對象,該對象將不能正確響應從而產生無法預料的結果,如何避免這種情況發生是線程安全性的核心問題。

欧美日韩精品国产,欧美aⅴ99久久黑人专区,亚洲综合久久久久,中文字幕一区二区三区精彩视频
久久久久久久国产精品影院| 成人性生交大片免费看视频在线| 国产性色一区二区| 日韩三级免费观看| 欧美伦理电影网| 91麻豆精品国产91久久久久| 欧美日韩国产色站一区二区三区| 欧美性猛交xxxx乱大交退制版| 在线观看日韩国产| 欧美日韩国产高清一区二区| 欧美一区二区三区四区高清 | 亚洲欧洲另类国产综合| 综合激情网...| 亚洲免费在线观看| 水野朝阳av一区二区三区| 午夜久久久久久久久| 精品一区二区三区日韩| 国产aⅴ综合色| 欧洲av一区二区嗯嗯嗯啊| 欧美精品乱码久久久久久| 精品国产三级电影在线观看| 中文字幕久久午夜不卡| 亚洲免费观看高清完整版在线| 亚洲靠逼com| 蜜桃av一区二区三区| 成人免费视频一区二区| 欧美性淫爽ww久久久久无| 欧美另类z0zxhd电影| 日本一区二区三区四区在线视频| 亚洲免费av在线| 久久精品久久久精品美女| 成年人国产精品| 欧美一区二区三区思思人| 国产精品久久三区| 日韩成人精品视频| 99综合电影在线视频| 欧美一区二区三区视频免费播放| 国产日本亚洲高清| 日韩精品一卡二卡三卡四卡无卡| 成人h版在线观看| 精品理论电影在线| 亚洲一区二区三区在线播放| 国产中文字幕精品| 欧美电影在线免费观看| 最近日韩中文字幕| 国产精品伊人色| 日韩欧美精品在线视频| 亚洲卡通欧美制服中文| 成人免费福利片| 欧美成人vr18sexvr| 午夜久久电影网| 欧美在线一区二区| 亚洲精品美腿丝袜| 波多野结衣在线一区| 欧美精品一区二区精品网| 午夜精品一区在线观看| 日本精品一区二区三区高清| 欧美激情综合在线| 精品在线亚洲视频| 日韩午夜在线观看视频| 婷婷国产v国产偷v亚洲高清| 欧美制服丝袜第一页| 亚洲女女做受ⅹxx高潮| 99久久综合精品| 亚洲图片另类小说| 99re8在线精品视频免费播放| 久久久精品免费网站| 国产乱码字幕精品高清av | 国产馆精品极品| 精品欧美久久久| 国产美女主播视频一区| 久久久夜色精品亚洲| 国产精品一区一区| 国产精品入口麻豆九色| 91丨九色丨国产丨porny| 国产精品欧美一区二区三区| 高清视频一区二区| 亚洲乱码中文字幕综合| 色综合久久综合网97色综合 | 欧美另类变人与禽xxxxx| 爽好多水快深点欧美视频| 欧美日韩国产影片| 男人的j进女人的j一区| 精品精品国产高清a毛片牛牛 | 日本成人在线电影网| 欧美一区二区视频观看视频| 青草国产精品久久久久久| 日韩三级高清在线| 成人中文字幕电影| 亚洲福利电影网| 精品剧情在线观看| 成人av第一页| 日韩—二三区免费观看av| 久久女同互慰一区二区三区| caoporn国产精品| 一区二区三区资源| 精品国产一区二区三区久久影院| 国产福利精品一区| 亚洲一二三区视频在线观看| 精品处破学生在线二十三| 99久久亚洲一区二区三区青草| 一个色妞综合视频在线观看| 日韩美女在线视频| 91视视频在线观看入口直接观看www | 国产剧情一区二区| 一区二区中文字幕在线| 91精品国产综合久久国产大片 | 欧美久久一区二区| 国产福利精品一区| 污片在线观看一区二区| 国产欧美一区二区在线观看| 欧美午夜精品久久久久久孕妇 | 国产精品色在线观看| 欧美日韩久久久久久| 国产成人在线观看免费网站| 亚洲国产va精品久久久不卡综合| 国产欧美一区二区在线观看| 欧美精品在线观看播放| av成人老司机| 国产成人av一区二区三区在线观看| 亚洲最大成人网4388xx| 欧美国产丝袜视频| 日韩一区二区三区高清免费看看| 91激情在线视频| k8久久久一区二区三区| 国模一区二区三区白浆| 日韩电影在线观看网站| 亚洲精品成a人| 亚洲三级免费电影| 最新国产成人在线观看| 国产午夜精品美女毛片视频| 欧美第一区第二区| 欧美日韩国产电影| 91国产福利在线| 91国产成人在线| 91福利精品第一导航| av一二三不卡影片| 成人av影院在线| 99免费精品在线观看| 99久久精品国产导航| 国产91丝袜在线18| 国产福利视频一区二区三区| 国产一区二区免费看| 国产伦精品一区二区三区视频青涩| 日韩成人dvd| 精品在线你懂的| 国产精品一区二区免费不卡 | 亚洲乱码国产乱码精品精98午夜 | 亚洲国产精品av| 国产日韩精品久久久| 国产精品美女久久久久aⅴ国产馆| 久久久国产精品午夜一区ai换脸| 久久看人人爽人人| 国产精品美女久久久久久| 最新日韩在线视频| 尤物av一区二区| 日韩av中文字幕一区二区| 美女视频黄 久久| 国产一区二区在线影院| 成人激情小说乱人伦| www.成人在线| 欧美日韩在线三级| 欧美一区二区三区思思人| 久久久久国产免费免费| 国产精品乱码久久久久久| 亚洲欧美日韩电影| 日韩av一级片| 国产精品亚洲第一| 欧美在线看片a免费观看| 日韩精品一区二区三区四区视频| 久久一区二区三区国产精品| 国产精品毛片久久久久久久| 亚洲一区免费视频| 国内成人精品2018免费看| 99精品视频在线观看| 欧美日韩成人激情| 国产喷白浆一区二区三区| 亚洲自拍偷拍麻豆| 韩国一区二区视频| 欧洲一区二区三区在线| 久久久久免费观看| 亚洲午夜久久久久久久久久久| 久久精品国产99久久6| 91在线云播放| 久久久久久影视| 日本大胆欧美人术艺术动态| av日韩在线网站| 精品欧美一区二区久久| 日韩理论片在线| 国产精品一区二区黑丝| 欧美福利视频导航| 亚洲男人的天堂在线观看| 国产在线一区二区| 69成人精品免费视频| 亚洲少妇最新在线视频| 国产老肥熟一区二区三区| 欧美一级免费观看| 一区二区三区资源| 波波电影院一区二区三区| 26uuu另类欧美|