control_sensor.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. #define WIN32_LEAN_AND_MEAN
  2. #include <WinSock2.h>
  3. #include "./include/EgoInterface.h"
  4. #include "EgoWindow.h"
  5. #include "protocol.pb.h"
  6. #include "control_sensor.h"
  7. #include "../common/iobuffer.h"
  8. #include "protocol.pb.h"
  9. CControlSensor::CControlSensor(CMessageQueue* q)
  10. {
  11. _window = static_cast<CEgoWindow*>(q);
  12. assert(_window != nullptr);
  13. _collection = Sensor_Collection::Sensor_None;
  14. _brake = _bucket = _throttle = _steer = 0;
  15. _arm = 0;
  16. _bucket = 0;
  17. _emergency= _back_on = _front_on = _wiper_on = false;
  18. }
  19. void CControlSensor::Notify(int8_t* buffer, int32_t size)
  20. {
  21. int32_t receivedCnt = size / CAN_MSG_LEN;
  22. cannet_frame* p = (cannet_frame*)buffer;
  23. // char str[64];
  24. // ZeroMemory(str, sizeof(str));
  25. // std::string ret;
  26. for (int32_t i = 0; i < receivedCnt; i++)
  27. {
  28. int32_t id = ntohl(p->canid) & 0x0FFF;
  29. switch (id)
  30. {
  31. case 0x181:
  32. {
  33. OnSignal(p->data);
  34. //if (b == true)
  35. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Task);
  36. }
  37. break;
  38. case 0x633:
  39. {
  40. bool b = OnLeftJoyStick(p->data);
  41. if (b == true)
  42. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Left);
  43. }
  44. break;
  45. case 0x634:
  46. {
  47. bool b = OnRightJoyStick(p->data);
  48. if (b == true)
  49. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Right);
  50. }
  51. break;
  52. }
  53. //std::string str = std::to_string(id) +"\n";
  54. }
  55. if (_window->GetControlState()== ControlState::Process&&_collection == Sensor_Collection::Sensor_All)
  56. {
  57. RemoNet::CCRobotAnalog _robot;
  58. _robot.set_brake(_brake);
  59. _robot.set_arm(_arm);
  60. _robot.set_bucket(_bucket);
  61. _robot.set_steer(_steer);
  62. _robot.set_throttle(_throttle);
  63. _robot.set_frontlight(_front_on);
  64. _robot.set_backlight(_back_on);
  65. _robot.set_wipe(_wiper_on);
  66. _robot.set_emergency(_emergency);
  67. _robot.set_gears(_gear);
  68. _robot.set_resume(_resume);
  69. MessageHead Head;
  70. CIOBuffer pBuffer;
  71. Head.Command = RemoNet::CC_RobotAnalog;
  72. Head.Length = _robot.ByteSizeLong();
  73. Head.Serialize(pBuffer.Buffer);
  74. auto ptr = pBuffer.Buffer + MessageHead::Size();
  75. _robot.SerializeToArray(ptr, Head.Length);
  76. pBuffer.Length = MessageHead::Size() + Head.Length;
  77. _window->SendData(&pBuffer);
  78. _collection = Sensor_Collection::Sensor_None;
  79. }
  80. }
  81. bool CControlSensor::OnSignal(int8_t* data)
  82. {
  83. /*std::string str;
  84. char buffer[128];
  85. for (int i = 0; i < 8; i++)
  86. {
  87. int16_t t = (data[i]) & 0x00FF;
  88. sprintf_s(buffer, "%2x ", t);
  89. str += buffer;
  90. }
  91. str += "\n";
  92. OutputDebugStringA(str.c_str());
  93. */
  94. /*
  95. ret = (data[0] & 0x01) != 0;
  96. if (ret == true)
  97. {
  98. if (_bootstrap == false)
  99. {
  100. if (_window->GetControlState() == ControlState::Process)
  101. {
  102. _bootstrap = true;
  103. RemoNet::CCBootStrapReq Req;
  104. MessageHead Head;
  105. CIOBuffer pBuffer;
  106. Head.Command = RemoNet::CC_BootReq;
  107. Head.Length = Req.ByteSizeLong();
  108. Head.Serialize(pBuffer.Buffer);
  109. auto ptr = pBuffer.Buffer + MessageHead::Size();
  110. Req.SerializeToArray(ptr, Head.Length);
  111. pBuffer.Length = MessageHead::Size() + Head.Length;
  112. _window->SendData(&pBuffer);
  113. }
  114. }
  115. }
  116. ret = (data[0] & 0x02) != 0;
  117. if (ret == true)
  118. {
  119. if (!_startup && _bootstrapd)
  120. {
  121. if (_window->GetControlState() == ControlState::Process)
  122. {
  123. _startup = true;
  124. RemoNet::CCStartupReq Req;
  125. MessageHead Head;
  126. CIOBuffer pBuffer;
  127. Head.Command = RemoNet::CC_StartupReq;
  128. Head.Length = Req.ByteSizeLong();
  129. Head.Serialize(pBuffer.Buffer);
  130. auto ptr = pBuffer.Buffer + MessageHead::Size();
  131. Req.SerializeToArray(ptr, Head.Length);
  132. pBuffer.Length = MessageHead::Size() + Head.Length;
  133. _window->SendData(&pBuffer);
  134. }
  135. }
  136. }
  137. */
  138. /*if (data[0] == 0)
  139. {
  140. if (_front_on == true || _back_on == true)
  141. {
  142. _reseted = true;
  143. }
  144. }
  145. */
  146. bool ret = (data[0] & 0x10) != 0; //Ç°³µµÈ
  147. //if (ret == true)
  148. {
  149. _front_on = (data[0] & 0x10) != 0;
  150. //if (_window->GetControlState() == ControlState::Process)
  151. /* {
  152. if (_reseted == true)
  153. {
  154. _front_on = !_front_on;
  155. _reseted = false;
  156. }
  157. }*/
  158. }
  159. ret = (data[0] & 0x8) != 0; //ºó³µµÆ
  160. //if (ret == true)
  161. {
  162. _back_on = (data[0] & 0x8) != 0;
  163. /* if (_window->GetControlState() == ControlState::Process)
  164. {
  165. if (_reseted == true)
  166. {
  167. _back_on = !_back_on;
  168. _reseted = false;
  169. }
  170. }
  171. */
  172. }
  173. // if (ret == true)
  174. // {
  175. if (_window->GetControlState()==ControlState::Process)
  176. {
  177. _wiper_on = (data[1] & 0x1) != 0;
  178. /*
  179. RemoNet::Wiper req;
  180. req.set_ret(_front_on);
  181. CIOBuffer pBuffer;
  182. MessageHead Head;
  183. Head.Command = RemoNet::CC_Wiper;
  184. Head.Length = req.ByteSizeLong();
  185. Head.Serialize(pBuffer.Buffer);
  186. auto ptr = pBuffer.Buffer + MessageHead::Size();
  187. req.SerializeToArray(ptr, Head.Length);
  188. pBuffer.Length = MessageHead::Size() + Head.Length;
  189. _window->SendData(&pBuffer);
  190. */
  191. }
  192. //}
  193. _resume = (data[2]) != 0;
  194. _emergency = data[3] != 0;
  195. _throttle = data[4];
  196. _brake = data[5];
  197. return true;
  198. }
  199. bool CControlSensor::OnLeftJoyStick(int8_t* data)
  200. {
  201. int32_t signal = 0;
  202. if ((data[0] & 0x3) != 0)
  203. {
  204. if ((data[0] & 0x03) != 0x01)
  205. {
  206. return false;
  207. }
  208. signal = 0;
  209. }
  210. if ((data[0] & (0x3 << 2)) != 0)
  211. {
  212. int32_t value = data[0] >> 2;
  213. if ((value & 0x03) != 0x01)
  214. {
  215. return false;
  216. }
  217. signal = -1;
  218. }
  219. if ((data[0] & (0x3 << 4)) != 0)
  220. {
  221. int32_t value = data[0] >> 4;
  222. if ((value & 0x03) != 0x01)
  223. {
  224. return false;
  225. }
  226. signal = 1;
  227. }
  228. uint16_t xvalue = ((uint16_t)data[1]) & 0x00FF;
  229. xvalue <<= 2;
  230. uint16_t lvalue = ((uint16_t)data[0]) & 0x00FF;
  231. xvalue |= (lvalue >> 6);
  232. _steer = xvalue * signal;
  233. /*
  234. if (signal == -1)
  235. {
  236. _steer = xvalue + 1000;
  237. }
  238. else
  239. _steer = xvalue;
  240. */
  241. //OutputDebugString(std::to_string(_steer).c_str());
  242. /*
  243. char buffer[64];
  244. std::string ret;
  245. for (int i = 0; i < 8; i += 2)
  246. {
  247. int16_t value = (data[5] >> i) & 0x3;
  248. sprintf_s(buffer, "%x ", value);
  249. ret += buffer;
  250. }
  251. */
  252. _gear = RemoNet::Gears::N;
  253. int32_t d = (data[5] >> 4) & 0x3;
  254. int32_t r = (data[5] >> 6) & 0x3;
  255. if (d == 0x1)
  256. {
  257. _gear = RemoNet::Gears::D;
  258. }
  259. else if (r == 0x1)
  260. {
  261. _gear = RemoNet::Gears::R;
  262. }
  263. // for (int i = 0; i < 8; i += 2)
  264. // {
  265. // int16_t value = (data[6] >> i) & 0x3;
  266. // sprintf_s(buffer, "%x ", value);
  267. // ret += buffer;
  268. // }
  269. // for (int i = 5; i < 8; i++)
  270. // {
  271. // int16_t t = (data[i]) & 0x00FF;
  272. // sprintf_s(buffer, "%x ", t);
  273. // ret += buffer;
  274. // }
  275. return true;
  276. }
  277. bool CControlSensor::OnRightJoyStick(int8_t* data)
  278. {
  279. /*
  280. std::string str;
  281. char buffer[128];
  282. for (int i = 0; i < 8; i++)
  283. {
  284. int16_t t = (data[i]) & 0x00FF;
  285. sprintf_s(buffer, "%2x ", t);
  286. str += buffer;
  287. }
  288. str += "\n";
  289. OutputDebugStringA(str.c_str());
  290. */
  291. /*
  292. _side = OpSide::Empty;
  293. if (((data[5] >> 2) & 0x3) == 0x01)
  294. {
  295. _side = OpSide::Arm;
  296. }
  297. else if (((data[5] >> 4) & 0x3) == 0x01)
  298. {
  299. _side = OpSide::Bucket;
  300. }
  301. */
  302. int32_t signal = 0;
  303. /*if ((data[2] & 0x3) != 0)
  304. {
  305. if ((data[2] & 0x03) != 0x01)
  306. {
  307. return false;
  308. }
  309. signal = 0;
  310. }
  311. */
  312. if ((data[2] & (0x3 << 2)) != 0)
  313. {
  314. int32_t value = data[2] >> 2;
  315. if ((value & 0x03) != 0x01)
  316. {
  317. return false;
  318. }
  319. signal = -1;
  320. }
  321. else if ((data[2] & (0x3 << 4)) != 0)
  322. {
  323. int32_t value = data[2] >> 4;
  324. if ((value & 0x03) != 0x01)
  325. {
  326. return false;
  327. }
  328. signal = 1;
  329. }
  330. uint16_t xvalue = ((uint16_t)data[3]) & 0x00FF;
  331. xvalue <<= 2;
  332. uint16_t lvalue = ((uint16_t)(data[2]>>6)) & 0x0003;
  333. xvalue |= lvalue;
  334. _arm = xvalue * signal;
  335. //if (_side == OpSide::Arm)
  336. // {
  337. // _bucket = 0;
  338. // OutputDebugStringA(std::to_string(_arm).c_str());
  339. // OutputDebugStringA("\n");
  340. // }
  341. // else if (_side == OpSide::Bucket)
  342. /* {
  343. _bucket = xvalue * signal;
  344. _arm = 0;
  345. // OutputDebugStringA(std::to_string(_bucket).c_str());
  346. // OutputDebugStringA("\n");
  347. }
  348. */
  349. if ((data[0] & (0x3 << 2)) != 0)
  350. {
  351. int32_t value = data[0] >> 2;
  352. if ((value & 0x03) != 0x01)
  353. {
  354. return false;
  355. }
  356. signal = -1;
  357. }
  358. else if ((data[0] & (0x3 << 4)) != 0)
  359. {
  360. int32_t value = data[0] >> 4;
  361. if ((value & 0x03) != 0x01)
  362. {
  363. return false;
  364. }
  365. signal = 1;
  366. }
  367. xvalue = ((uint16_t)data[1]) & 0x00FF;
  368. xvalue <<= 2;
  369. lvalue = ((uint16_t)(data[0] >> 6)) & 0x0003;
  370. xvalue |= lvalue;
  371. _bucket = xvalue * signal;
  372. std::string str = "arm:";
  373. str +=std::to_string(_arm)+", bucket :"+ std::to_string(_bucket);
  374. str += "\n";
  375. OutputDebugStringA(str.c_str());
  376. return true;
  377. }
  378. ControlStatus CControlSensor::CheckStatus()
  379. {
  380. if (_gear != RemoNet::Gears::N) return ControlStatus::GearNotN;
  381. if (_bootstrap != false) return ControlStatus::BootStrap;
  382. if (_startup != false) return ControlStatus::Startup;
  383. //if (_emergenyd != false) return ControlStatus::Emergency;
  384. if (_steer != 0) return ControlStatus::Steer;
  385. if (abs(_throttle) > 5) return ControlStatus::Throttle;
  386. if (abs(_brake) > 5) return ControlStatus::Brake;
  387. // if (_side != OpSide::Empty) return ControlStatus::ArmTriger;
  388. return ControlStatus::Ok;
  389. }
  390. void CControlSensor::OnBootstrapd(bool ret)
  391. {
  392. _bootstrapd = ret;
  393. }
  394. void CControlSensor::OnStartupd(bool ret)
  395. {
  396. _startupd = ret;
  397. }
  398. void CControlSensor::Start()
  399. {
  400. }
  401. void CControlSensor::Stop()
  402. {
  403. }
  404. void CControlSensor::SetSensorSocket(SensorSocket<CControlSensor>* can)
  405. {
  406. }