api_for_hs.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # update: 2024-3-20
  2. """
  3. 车辆控制器API
  4. """
  5. from urllib import parse
  6. import requests
  7. import time
  8. import traceback
  9. import sys
  10. import importlib
  11. sys.path.append(r'E:\casper\repositories\repositories\casperz.py-project\module-py') # for pc
  12. # sys.path.append(r'D:\casper\gitee\casperz.py-project\module-py') # for pc
  13. methods = importlib.import_module(f"xlib")
  14. class API(object):
  15. def __init__(self, address='192.168.191.91', port=8000):
  16. """
  17. start_plan 启动任务
  18. abort_plan 终止任务
  19. start_engine 远程打火
  20. stall_engine 远程熄火
  21. apply_use_by_ip 申请远程操作模式 1 申请远程操作模式 2 申请自动作业模式
  22. """
  23. # --- define ---
  24. self.port = port
  25. # self.url = f"http://{address}:{port}"
  26. def cmd1001(self, address='192.168.191.91', navigation=[]):
  27. """
  28. 启动行驶
  29. data = {
  30. # 指令类型
  31. "cmdid": 1001, # 执行行驶任务
  32. # 指令参数
  33. "param": {
  34. # 导航数据
  35. "navigation": [
  36. {
  37. 'type': 'forward', # 方向类型 forward 正向 backward 反向
  38. 'nav_coordinates': [
  39. # 直行轨迹坐标参数
  40. {
  41. "start_point_x": 37.7749,
  42. "start_point_y": 37.7749,
  43. "end_point_x": 40,
  44. "end_point_y": -120
  45. },
  46. # 转弯轨迹坐标参数
  47. {
  48. "start_point_x": 40.7128,
  49. "start_point_y": -74.0060,
  50. "center_point_x": 10,
  51. "center_point_y": 20,
  52. "end_point_x": 50.7128,
  53. "end_point_y": -70.0060
  54. },
  55. ]
  56. },
  57. {
  58. 'type': 'backward',
  59. 'nav_coordinates': [
  60. # 直行轨迹坐标参数
  61. {
  62. "start_point_x": 37.7749,
  63. "start_point_y": -122.4194,
  64. "end_point_x": 40,
  65. "end_point_y": -120
  66. },
  67. # 转弯轨迹坐标参数
  68. {
  69. "start_point_x": 40.7128,
  70. "start_point_y": -74.0060,
  71. "center_point_x": 10,
  72. "center_point_y": 20,
  73. "end_point_x": 50.7128,
  74. "end_point_y": -70.0060
  75. },
  76. ]
  77. },
  78. ],
  79. },
  80. }
  81. """
  82. data = {
  83. 'cmdid': 1001,
  84. 'param': {
  85. 'navigation': navigation,
  86. }
  87. }
  88. url = f"http://{address}:{self.port}"
  89. response = requests.post(self.url, json=data)
  90. print('API.start_plan.url:', self.url)
  91. print('API.start_plan.data:', data)
  92. print('API.start_plan.code:', response.status_code)
  93. print('API.start_plan.text:', response.text)
  94. def cmd100101(self):
  95. """
  96. 启动作业
  97. """
  98. data = {
  99. # 指令类型
  100. "cmdid": 100101, # 启动作业
  101. # 指令参数
  102. "param": {
  103. # 任务类型 102 叉包 103 放包 104 倒渣
  104. "task_type": 102,
  105. # 102
  106. "task_coordinates": {
  107. "pot_name": "M.24",
  108. "pot_x": 40,
  109. "pot_y": 50,
  110. "mark_pot_pose": 0.3,
  111. "e_point_x": 50,
  112. "e_point_y": 60
  113. },
  114. # 103
  115. # "task_coordinates": {
  116. # "pot_name": "M.24",
  117. # "pot_x": 40,
  118. # "pot_y": 50,
  119. # "goal_pot_pose": 0.3,
  120. # "e_point_x": 50,
  121. # "e_point_y": 60
  122. # },
  123. # 104
  124. # "task_coordinates": {},
  125. }
  126. }
  127. def cmd1002(self):
  128. """
  129. 中止行驶任务
  130. """
  131. data = {
  132. # 指令类型
  133. "cmdid": 1002, # 中止行驶任务
  134. # 指令参数
  135. "param": {}
  136. }
  137. def cmd1003(self):
  138. data = {
  139. # 指令类型
  140. "cmdid": 1003, # 远程打火
  141. # 指令参数
  142. "param": {}
  143. }
  144. def cmd1004(self):
  145. data = {
  146. # 指令类型
  147. "cmdid": 1004, # 远程熄火
  148. # 指令参数
  149. "param": {}
  150. }
  151. def cmd1005(self):
  152. data = {
  153. # 指令类型
  154. "cmdid": 1005, # 申请远程操作模式
  155. # 指令参数
  156. "param": {}
  157. }
  158. def cmd1006(self):
  159. data = {
  160. # 指令类型
  161. "cmdid": 1006, # 申请自动作业模式
  162. # 指令参数
  163. "param": {}
  164. }
  165. def test002(self, address):
  166. """
  167. """
  168. data = {
  169. # 指令类型
  170. "cmdid": 1001, # 执行行驶任务
  171. # 指令参数
  172. "param": {
  173. # 导航数据
  174. "navigation": [
  175. {
  176. 'type': 'forward',
  177. 'nav_coordinates': [
  178. {
  179. 'start_point_x': 2.495, # 当前位置
  180. 'start_point_y': 173.1666142856, # 当前位置
  181. 'end_point_x': 182.6588877142857,
  182. 'end_point_y': 169.8418064285714
  183. },
  184. {
  185. 'start_point_x': 182.6588877142857,
  186. 'start_point_y': 169.8418064285714,
  187. 'center_point_x': 182.92185485714285,
  188. 'center_point_y': 183.5769582142857,
  189. 'end_point_x': 196.65700664285714,
  190. 'end_point_y': 183.31399107142855
  191. },
  192. {
  193. 'start_point_x': 196.65700664285714,
  194. 'start_point_y': 183.31399107142855,
  195. 'center_point_x': 182.92185485714285,
  196. 'center_point_y': 183.5769582142857,
  197. 'end_point_x': 183.184822,
  198. 'end_point_y': 197.31211
  199. },
  200. {
  201. 'start_point_x': 183.184822,
  202. 'start_point_y': 197.31211,
  203. 'end_point_x': 178.045984 - 50,
  204. 'end_point_y': 197.219726
  205. }
  206. ]
  207. }
  208. ]
  209. },
  210. }
  211. url = f"http://{address}:{self.port}"
  212. response = requests.post(url, json=data)
  213. print('API.start_plan.url:', url)
  214. print('API.start_plan.data:', data)
  215. print('API.start_plan.code:', response.status_code)
  216. def test004(self, address):
  217. data = {
  218. # 指令类型
  219. "cmdid": 100101, # 启动作业
  220. # 指令参数
  221. "param": {
  222. "task_type": 103, # 放包
  223. "task_coordinates": {
  224. "pot_name": "N.20",
  225. "pot_x": 122.755687,
  226. "pot_y": 210.019149,
  227. "mark_pot_pose": -1.570920,
  228. "e_point_x": 0,
  229. "e_point_y": 0
  230. },
  231. }
  232. }
  233. url = f"http://{address}:{self.port}"
  234. response = requests.post(url, json=data)
  235. print('API.start_plan.url:', url)
  236. print('API.start_plan.data:', data)
  237. print('API.start_plan.code:', response.status_code)
  238. if __name__ == '__main__':
  239. # --- init ---
  240. api = API(port=8000)
  241. # --- test ---
  242. # out = api.test002(address='192.168.131.180') # 行驶
  243. out = api.test004(address='192.168.131.180') # 作业
  244. print(out)