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

我的訂單|我的收藏|我的商城|幫助中心|返回首頁
搜維爾科技[m.gentlemenlisten.com]>服務(wù)>VR研究院 孵化器>VR研究院>虛擬交互>Geomagic

OpenHaptics編程環(huán)境搭建

文章來源:SouVR 作者:frank 發(fā)布時(shí)間:2018年11月05日 點(diǎn)擊數(shù): 字號(hào):

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

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

下面來搭建OpenHaptics的編程環(huán)境(Win7 64位系統(tǒng) + VS2013 + OpenHaptics 3.4.0),先在這里下載設(shè)備驅(qū)動(dòng)程序和OpenHaptics Toolkit。參考安裝手冊(cè)連接好電腦和Phantom設(shè)備后可以打開官方自帶的Phantom Test程序進(jìn)行測試,驗(yàn)證設(shè)備是否連接成功。

接下來使用HDAPI創(chuàng)建一個(gè)力反饋應(yīng)用程序(以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.?

參考下面的程序結(jié)構(gòu)圖,為了實(shí)現(xiàn)流暢穩(wěn)定的觸覺力反饋,創(chuàng)建的任務(wù)將以1000Hz的頻率運(yùn)行在優(yōu)先級(jí)高的線程中(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)設(shè)置頭文件包含目錄(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)設(shè)置附加庫目錄(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.?

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

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

使用VS013重新生成Win32、x64平臺(tái)下的Debug和Release版本的lib,然后將原先的替換掉,再進(jìn)行編譯就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. }?

?  

該程序在循環(huán)執(zhí)行的任務(wù)中實(shí)時(shí)獲取操作手柄的位置信息,并以此來計(jì)算輸出力來模擬彈簧力。這里有幾個(gè)需要注意的地方:

1. Haptic Frames:

為了保證數(shù)據(jù)訪問的一致性,OpenHaptics提供了一種Frame框架結(jié)構(gòu),反饋給用戶力的過程一般都是在力反饋幀中處理的,使用hdBeginFrame和hdEndFrame作為訪問的開始和結(jié)束。在同一幀中多次調(diào)用hdGet類函數(shù)獲取信息會(huì)得到相同的結(jié)果;多次調(diào)用hdSet類函數(shù)設(shè)置同一狀態(tài),則最后的調(diào)用會(huì)替代掉以前的(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)。即在幀的結(jié)尾,所有的屬性改變才會(huì)得以應(yīng)用。

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. 同步調(diào)用與異步調(diào)用:

所謂同步就是在發(fā)出一個(gè)“調(diào)用”時(shí),在沒有得到結(jié)果之前,該“調(diào)用”就不返回;而異步則是相反,“調(diào)用”在發(fā)出之后這個(gè)調(diào)用就直接返回了,即當(dāng)一個(gè)異步過程調(diào)用發(fā)出后,調(diào)用者不會(huì)立刻得到結(jié)果。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.?

同步調(diào)用主要用于獲取設(shè)備當(dāng)前狀態(tài),比如位置、力、開關(guān)狀態(tài)等。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.

下面是同步調(diào)用的一個(gè)例子:

  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);?

?

異步調(diào)用主要用于循環(huán)處理任務(wù)中,例如根據(jù)力反饋設(shè)備操作末端的位置來計(jì)算并輸出力。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. 任務(wù)返回值:

?HD_CALLBACK_DONE (只執(zhí)行一次)
?HD_CALLBACK_CONTINUE(循環(huán)執(zhí)行)

根據(jù)不同的返回值,回調(diào)函數(shù)會(huì)在當(dāng)前幀運(yùn)行完畢后判斷是否在下一幀再次運(yùn) 行,當(dāng)返回值為HD_CALLBACK_CONTINUE時(shí),此回調(diào)函數(shù)在下一幀時(shí)會(huì)繼續(xù)重新 運(yùn)行;而當(dāng)返回值為HD_CALLBACK_DONE?時(shí),此回調(diào)函數(shù)在下一幀時(shí)不再次運(yùn)行。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. 任務(wù)優(yōu)先級(jí):

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.?

下面再看一個(gè)獲取設(shè)備信息的典型例子(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. }?

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

值得注意的是主線程中函數(shù)獲取Servo Loop線程(任務(wù)線程)中的數(shù)據(jù),可以通過同步調(diào)用來實(shí)現(xiàn),是一種線程安全(thread-safe)方式。線程安全就是多線程訪問時(shí),采用了加鎖機(jī)制,當(dāng)一個(gè)線程訪問該類的某個(gè)數(shù)據(jù)時(shí),進(jìn)行保護(hù),其他線程不能進(jìn)行訪問直到該線程讀取完,其他線程才可使用。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函數(shù)中也可以直接訪問全局變量gServoDeviceData來獲取設(shè)備信息,但這種方式是線程不安全的,即不提供數(shù)據(jù)訪問保護(hù),有可能出現(xiàn)多個(gè)線程先后更改數(shù)據(jù)造成所得到的數(shù)據(jù)是臟數(shù)據(jù)。如果其他線程企圖訪問一個(gè)處于不可用狀態(tài)的對(duì)象,該對(duì)象將不能正確響應(yīng)從而產(chǎn)生無法預(yù)料的結(jié)果,如何避免這種情況發(fā)生是線程安全性的核心問題。

電話:010-50951355 傳真:010-50951352  郵箱:sales@m.gentlemenlisten.com ;點(diǎn)擊查看區(qū)域負(fù)責(zé)人電話
手機(jī):13811546370 / 13720091697 / 13720096040 / 13811548270 / 13811981522 / 18600440988 /13810279720 /13581546145

  • 暫無資料
  • 暫無資料
  • 暫無資料
  • 暫無資料
  • 暫無資料
欧美日韩精品国产,欧美aⅴ99久久黑人专区,亚洲综合久久久久,中文字幕一区二区三区精彩视频
91精品国产福利| 日韩美女久久久| 欧美日韩久久一区| 欧美午夜理伦三级在线观看| 菠萝蜜视频在线观看一区| 国产麻豆一精品一av一免费| 国模大尺度一区二区三区| 福利一区二区在线| 成人h精品动漫一区二区三区| 国产成人一区在线| fc2成人免费人成在线观看播放| 成人av中文字幕| 日本韩国一区二区三区视频| 91精品办公室少妇高潮对白| 欧美日韩免费在线视频| 日韩亚洲欧美在线| 国产亚洲综合色| 亚洲欧洲成人精品av97| 亚洲成人免费av| 久久精品国产亚洲高清剧情介绍 | 宅男噜噜噜66一区二区66| 日韩一区二区精品在线观看| 国产亚洲制服色| 亚洲色图在线播放| 日韩成人午夜精品| 国产精品一区二区三区99| av毛片久久久久**hd| 日本高清不卡视频| 欧美一级一级性生活免费录像| 日韩欧美电影在线| 亚洲欧洲美洲综合色网| 亚洲丶国产丶欧美一区二区三区| 美腿丝袜一区二区三区| 成人免费观看男女羞羞视频| 欧美视频在线不卡| 欧美激情一二三区| 五月天视频一区| 成人福利视频网站| 在线91免费看| 中文字幕制服丝袜一区二区三区| 图片区小说区区亚洲影院| 国产69精品一区二区亚洲孕妇| 色一情一乱一乱一91av| 久久久蜜臀国产一区二区| 亚洲综合色网站| 国产成人啪午夜精品网站男同| 欧美三级日韩三级国产三级| 国产蜜臀97一区二区三区| 日韩激情视频在线观看| 99久久伊人精品| 精品国产91久久久久久久妲己 | 日本成人在线不卡视频| 99国产精品久久久久久久久久久| 欧美一区二区三区免费大片| 亚洲天堂成人网| 成人一区二区三区视频在线观看| 欧美日韩激情在线| 亚洲影院理伦片| 成人av在线看| 国产欧美一区在线| 久久成人免费网站| 91精品午夜视频| 亚洲午夜视频在线观看| proumb性欧美在线观看| 亚洲国产高清aⅴ视频| 精品综合免费视频观看| 日韩一区二区三区三四区视频在线观看 | 国产a精品视频| 久久久综合网站| 国产老妇另类xxxxx| 精品国产成人系列| 国产精品自产自拍| 久久久久久久久久久电影| 激情综合色综合久久| 精品区一区二区| 国产精品系列在线播放| 久久日韩精品一区二区五区| 精品一区二区三区欧美| 国产亚洲欧美日韩俺去了| 高清免费成人av| 亚洲人妖av一区二区| 91国在线观看| 日韩黄色免费电影| 日韩欧美的一区| 国产在线一区二区综合免费视频| 久久精品亚洲精品国产欧美| 激情久久久久久久久久久久久久久久| www成人在线观看| 欧美xxxxx裸体时装秀| 日本一区二区三区高清不卡| 乱中年女人伦av一区二区| 91麻豆精品国产自产在线| 午夜视频在线观看一区| 欧美日本在线一区| 日韩中文字幕91| 日韩亚洲欧美综合| 国产成人欧美日韩在线电影| 欧美浪妇xxxx高跟鞋交| 免费成人小视频| 精品国产91乱码一区二区三区| 成人禁用看黄a在线| 国产精品进线69影院| 99国产精品久久久久久久久久久 | 麻豆久久久久久| 日韩欧美一区二区久久婷婷| www.成人在线| 亚洲成人免费观看| 日韩精品影音先锋| 国产精品白丝jk白祙喷水网站| 国产亚洲va综合人人澡精品| 成人精品在线视频观看| 天天综合色天天综合色h| 欧美成人激情免费网| 高清beeg欧美| 夜夜亚洲天天久久| 91精品婷婷国产综合久久性色| 国产一区中文字幕| 中文字幕欧美日韩一区| 日本道精品一区二区三区| 日本特黄久久久高潮| 亚洲国产精品v| 制服.丝袜.亚洲.另类.中文 | 国产精品私房写真福利视频| 成人午夜碰碰视频| 亚洲123区在线观看| 久久综合狠狠综合久久综合88| 99国产精品99久久久久久| ...av二区三区久久精品| www久久精品| 欧美三级中文字| 国产成人亚洲综合a∨猫咪| 一区二区三区日韩精品视频| 日韩精品一区二区三区老鸭窝 | 亚洲精品自拍动漫在线| 久久新电视剧免费观看| 欧美久久一二区| 91老师片黄在线观看| 国产综合久久久久久久久久久久 | 欧美老人xxxx18| 成人h版在线观看| 久久国产剧场电影| 亚洲精品国产无天堂网2021| 久久综合九色综合97婷婷| 欧美亚洲精品一区| 成人激情校园春色| 国产成人免费视频精品含羞草妖精 | 极品少妇xxxx精品少妇| 午夜电影网一区| 综合中文字幕亚洲| 2020日本不卡一区二区视频| 91精品在线一区二区| 欧美性猛交xxxx乱大交退制版| 99久久久久久| 成人一区二区三区视频在线观看| 韩国视频一区二区| 麻豆专区一区二区三区四区五区| 亚洲一区在线看| 夜夜操天天操亚洲| 亚洲欧美另类小说| 亚洲人成在线观看一区二区| 国产欧美一区二区三区网站| 国产欧美1区2区3区| 亚洲日本丝袜连裤袜办公室| 中文字幕在线不卡| 亚洲人成亚洲人成在线观看图片 | 亚洲欧洲av色图| 国产亚洲精品bt天堂精选| 国产人成一区二区三区影院| 久久精品视频在线免费观看 | 亚洲国产aⅴ成人精品无吗| 亚洲女爱视频在线| 亚洲电影一级片| 久久国产精品99久久人人澡| 国产九色精品成人porny| 成人av集中营| 欧美亚洲一区二区三区四区| 成人动漫中文字幕| 欧美中文字幕一区二区三区 | 国产成人综合视频| 91在线小视频| 91精品国产乱码久久蜜臀| 精品免费视频一区二区| 欧美国产日韩精品免费观看| 久久综合久久久久88| 一区二区三区四区激情| 日韩国产精品91| 国产乱码精品一区二区三区忘忧草| 福利一区福利二区| 99久久er热在这里只有精品15| 风流少妇一区二区| 国产一区二区三区| 国产精品一级二级三级| 91九色最新地址| 日韩女优制服丝袜电影| 国产精品免费人成网站| 亚洲一区二区四区蜜桃| 国产精品一区二区91| 欧美日韩综合一区| 国产亚洲精品久| 天天综合天天综合色|