Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.31 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # -*- Python -*-
  4.  
  5. """
  6. @file Ministick.py
  7. @brief Phidget ministick sensor component
  8. @date $Date$
  9.  
  10.  
  11. """
  12. import sys
  13. import time
  14. sys.path.append(".")
  15.  
  16. # Import RTM module
  17. import RTC
  18. import OpenRTM_aist
  19. import math
  20. import spidev
  21.  
  22.  
  23. # Import Service implementation class
  24. # <rtc-template block="service_impl">
  25.  
  26. # </rtc-template>
  27.  
  28. # Import Service stub modules
  29. # <rtc-template block="consumer_import">
  30. # </rtc-template>
  31.  
  32.  
  33. # This module's spesification
  34. # <rtc-template block="module_spec">
  35. ministick_spec = ["implementation_id", "Ministick",
  36. "type_name", "Ministick",
  37. "description", "Phidget ministick sensor component",
  38. "version", "1.0.0",
  39. "vendor", "AIST",
  40. "category", "Input Devic",
  41. "activity_type", "STATIC",
  42. "max_instance", "1",
  43. "language", "Python",
  44. "lang_type", "SCRIPT",
  45. "conf.default.scaling", "1.0",
  46. "conf.default.tread", "0.2",
  47. "conf.default.print_xy", "NO",
  48. "conf.default.print_vel", "NO",
  49. "conf.default.print_wvel", "NO",
  50. "conf.__widget__.scaling", "slider.0.1",
  51. "conf.__widget__.tread", "slider.0.01",
  52. "conf.__widget__.print_xy", "radio",
  53. "conf.__widget__.print_vel", "radio",
  54. "conf.__widget__.print_wvel", "radio",
  55. "conf.__constraints__.scaling", "0.0<=x<=10.0",
  56. "conf.__constraints__.tread", "0.0<=x<=1.0",
  57. "conf.__constraints__.print_xy", "(YES,NO)",
  58. "conf.__constraints__.print_vel", "(YES,NO)",
  59. "conf.__constraints__.print_wvel", "(YES,NO)",
  60. ""]
  61. # </rtc-template>
  62.  
  63. ##
  64. # @class Ministick
  65. # @brief Phidget ministick sensor component
  66. #
  67. #
  68. class Ministick(OpenRTM_aist.DataFlowComponentBase):
  69.  
  70. ##
  71. # @brief constructor
  72. # @param manager Maneger Object
  73. #
  74. def __init__(self, manager):
  75. OpenRTM_aist.DataFlowComponentBase.__init__(self, manager)
  76.  
  77. self._d_pos = RTC.TimedFloatSeq(RTC.Time(0,0),[])
  78. """
  79. ジョイスティックのX-Y位置データ
  80. - Semantics: data[0]:x位置, data[1]:y位置
  81. """
  82. self._posOut = OpenRTM_aist.OutPort("pos", self._d_pos)
  83. self._d_vel = RTC.TimedVelocity2D(RTC.Time(0,0),0)
  84. """
  85. 移動ロボットの速度ベクトル
  86. - Semantics: vx:並進速度, vy:0.0m va:角速度
  87. - Unit: vx[m/s], va[rad/s]
  88. """
  89. self._velOut = OpenRTM_aist.OutPort("vel", self._d_vel)
  90. self._d_whell_vel = RTC.TimedFloatSeq(RTC.Time(0,0),[])
  91. """
  92. 車輪速度
  93. - Semantics: data[0]:左車輪角速度, data[1]:右車輪角速度
  94. - Unit: [rad/s]
  95. """
  96. self._whell_velOut = OpenRTM_aist.OutPort("whell_vel", self._d_whell_vel)
  97.  
  98.  
  99.  
  100.  
  101.  
  102. # initialize of configuration-data.
  103. # <rtc-template block="init_conf_param">
  104. """
  105. スケーリングファクタ
  106. - Name: scaling
  107. - DefaultValue: 1.0
  108. """
  109. self._scaling = [1.0]
  110. """
  111. 移動ロボットのトレッド値
  112. - Name: tread
  113. - DefaultValue: 0.2
  114. """
  115. self._tread = [0.2]
  116. """
  117.  
  118. - Name: print_xy
  119. - DefaultValue: NO
  120. """
  121. self._print_xy = ['NO']
  122. """
  123. velデータのデバッグプリントフラグ
  124. - Name: print_vel
  125. - DefaultValue: NO
  126. """
  127. self._print_vel = ['NO']
  128. """
  129. wheel_velデータのデバッグプリントフラグ
  130. - Name: print_wvel
  131. - DefaultValue: NO
  132. """
  133. self._print_wvel = ['NO']
  134.  
  135. # </rtc-template>
  136. self.x = 0.0
  137. self.y = 0.0
  138.  
  139. self.spi = spidev.SpiDev()
  140. self.spi.open(0, 0)
  141.  
  142. def get_adc(self, channel):
  143. sned_ch = [0x00, 0x08, 0x10, 0x18]
  144. if ((channel > 3) or (channel < 0)):
  145. return -1
  146. r = self.spi.xfer2([sned_ch[channel], 0, 0, 0])
  147. ret = ((r[2] << 6 ) & 0x300) | ((r[2] << 6) & 0xc0) | ((r[3] >> 2) & 0x3f)
  148. return ret
  149.  
  150. def xy_to_wvel(self, x, y):
  151. th = math.atan2(y, x)
  152. v = math.hypot(x, y)
  153. vl = v * math.cos(th - (math.pi/4.0))
  154. vr = v * math.sin(th - (math.pi/4.0))
  155. return (vl, vr)
  156.  
  157. def wvel_to_vel2d(self, vl, vr):
  158. v = (vr + vl) / 2.0
  159. if v < 0.0:
  160. w = - (vr - vl) / self._tread[0]
  161. else:
  162. w = (vr - vl) / self._tread[0]
  163. return RTC.Velocity2D(v, 0.0, w)
  164.  
  165. ##
  166. #
  167. # The initialize action (on CREATED->ALIVE transition)
  168. # formaer rtc_init_entry()
  169. #
  170. # @return RTC::ReturnCode_t
  171. #
  172. #
  173. def onInitialize(self):
  174. # Bind variables and configuration variable
  175. self.bindParameter("scaling", self._scaling, "1.0")
  176. self.bindParameter("tread", self._tread, "0.2")
  177. self.bindParameter("print_xy", self._print_xy, "NO")
  178. self.bindParameter("print_vel", self._print_vel, "NO")
  179. self.bindParameter("print_wvel", self._print_wvel, "NO")
  180.  
  181. # Set InPort buffers
  182.  
  183. # Set OutPort buffers
  184. self.addOutPort("pos",self._posOut)
  185. self.addOutPort("vel",self._velOut)
  186. self.addOutPort("whell_vel",self._whell_velOut)
  187.  
  188. # Set service provider to Ports
  189.  
  190. # Set service consumers to Ports
  191.  
  192. # Set CORBA Service Ports
  193.  
  194. self.x_offset_v = 0.0
  195. self.y_offset_v = 0.0
  196. for i in range(1, 100):
  197. self.x_offset_v += self.get_adc(0)
  198. self.y_offset_v += self.get_adc(1)
  199. self.x_offset_v = self.x_offset_v / 100.0
  200. self.y_offset_v = self.y_offset_v / 100.0
  201.  
  202. return RTC.RTC_OK
  203.  
  204. ##
  205. #
  206. # The finalize action (on ALIVE->END transition)
  207. # formaer rtc_exiting_entry()
  208. #
  209. # @return RTC::ReturnCode_t
  210.  
  211. #
  212. def onFinalize(self):
  213.  
  214. return RTC.RTC_OK
  215.  
  216. # ##
  217. # #
  218. # # The startup action when ExecutionContext startup
  219. # # former rtc_starting_entry()
  220. # #
  221. # # @param ec_id target ExecutionContext Id
  222. # #
  223. # # @return RTC::ReturnCode_t
  224. # #
  225. # #
  226. #def onStartup(self, ec_id):
  227. #
  228. # return RTC.RTC_OK
  229.  
  230. # ##
  231. # #
  232. # # The shutdown action when ExecutionContext stop
  233. # # former rtc_stopping_entry()
  234. # #
  235. # # @param ec_id target ExecutionContext Id
  236. # #
  237. # # @return RTC::ReturnCode_t
  238. # #
  239. # #
  240. #def onShutdown(self, ec_id):
  241. #
  242. # return RTC.RTC_OK
  243.  
  244. ##
  245. #
  246. # The activated action (Active state entry action)
  247. # former rtc_active_entry()
  248. #
  249. # @param ec_id target ExecutionContext Id
  250. #
  251. # @return RTC::ReturnCode_t
  252. #
  253. #
  254. #def onActivated(self, ec_id):
  255.  
  256. # return RTC.RTC_OK
  257.  
  258. ##
  259. #
  260. # The deactivated action (Active state exit action)
  261. # former rtc_active_exit()
  262. #
  263. # @param ec_id target ExecutionContext Id
  264. #
  265. # @return RTC::ReturnCode_t
  266. #
  267. #
  268. def onDeactivated(self, ec_id):
  269.  
  270. return RTC.RTC_OK
  271.  
  272. ##
  273. #
  274. # The execution action that is invoked periodically
  275. # former rtc_active_do()
  276. #
  277. # @param ec_id target ExecutionContext Id
  278. #
  279. # @return RTC::ReturnCode_t
  280. #
  281. #
  282. def onExecute(self, ec_id):
  283. self.x = - (self.get_adc(0) - self.x_offset_v) * self._scaling[0] / 1000.0
  284. self.y = (self.get_adc(1) - self.y_offset_v) * self._scaling[0] / 1000.0
  285.  
  286. if self._print_xy[0] != "NO":
  287. print "(x, y) = ", self.x, self.y
  288. self._d_pos.data = [self.x, self.y]
  289.  
  290. self._d_whell_vel.data = self.xy_to_wvel(self.x, self.y)
  291.  
  292.  
  293. if self._print_wvel[0] != "NO":
  294. print "(vl, vr) = ", self._d_whell_vel.data[0], self._d_whell_vel.data[1]
  295. self._d_vel.data = self.wvel_to_vel2d(self._d_whell_vel.data[0],
  296. self._d_whell_vel.data[1])
  297.  
  298. if self._print_vel[0] != "NO":
  299. print "(vx, va) = ", self._d_vel.data.vx, self._d_vel.data.va
  300.  
  301. self._posOut.write()
  302. self._velOut.write()
  303. self._whell_velOut.write()
  304.  
  305.  
  306. return RTC.RTC_OK
  307.  
  308. # ##
  309. # #
  310. # # The aborting action when main logic error occurred.
  311. # # former rtc_aborting_entry()
  312. # #
  313. # # @param ec_id target ExecutionContext Id
  314. # #
  315. # # @return RTC::ReturnCode_t
  316. # #
  317. # #
  318. #def onAborting(self, ec_id):
  319. #
  320. # return RTC.RTC_OK
  321.  
  322. # ##
  323. # #
  324. # # The error action in ERROR state
  325. # # former rtc_error_do()
  326. # #
  327. # # @param ec_id target ExecutionContext Id
  328. # #
  329. # # @return RTC::ReturnCode_t
  330. # #
  331. # #
  332. #def onError(self, ec_id):
  333. #
  334. # return RTC.RTC_OK
  335.  
  336. # ##
  337. # #
  338. # # The reset action that is invoked resetting
  339. # # This is same but different the former rtc_init_entry()
  340. # #
  341. # # @param ec_id target ExecutionContext Id
  342. # #
  343. # # @return RTC::ReturnCode_t
  344. # #
  345. # #
  346. #def onReset(self, ec_id):
  347. #
  348. # return RTC.RTC_OK
  349.  
  350. # ##
  351. # #
  352. # # The state update action that is invoked after onExecute() action
  353. # # no corresponding operation exists in OpenRTm-aist-0.2.0
  354. # #
  355. # # @param ec_id target ExecutionContext Id
  356. # #
  357. # # @return RTC::ReturnCode_t
  358. # #
  359.  
  360. # #
  361. #def onStateUpdate(self, ec_id):
  362. #
  363. # return RTC.RTC_OK
  364.  
  365. # ##
  366. # #
  367. # # The action that is invoked when execution context's rate is changed
  368. # # no corresponding operation exists in OpenRTm-aist-0.2.0
  369. # #
  370. # # @param ec_id target ExecutionContext Id
  371. # #
  372. # # @return RTC::ReturnCode_t
  373. # #
  374. # #
  375. #def onRateChanged(self, ec_id):
  376. #
  377. # return RTC.RTC_OK
  378.  
  379.  
  380.  
  381.  
  382. def MinistickInit(manager):
  383. profile = OpenRTM_aist.Properties(defaults_str=ministick_spec)
  384. manager.registerFactory(profile,
  385. Ministick,
  386. OpenRTM_aist.Delete)
  387.  
  388. def MyModuleInit(manager):
  389. MinistickInit(manager)
  390.  
  391. # Create a component
  392. comp = manager.createComponent("Ministick")
  393.  
  394. def main():
  395. mgr = OpenRTM_aist.Manager.init(sys.argv)
  396. mgr.setModuleInitProc(MyModuleInit)
  397. mgr.activateManager()
  398. mgr.runManager()
  399.  
  400. if __name__ == "__main__":
  401. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement