ControlSensor.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #include "stdafx.h"
  2. #include "resource.h"
  3. #include "Protocol.pb.h"
  4. #include "ControlSensor.h"
  5. #include "MainDlg.h"
  6. #include <stdio.h>
  7. #include "../common/iobuffer.h"
  8. #include "WindowThread.h"
  9. CControlSensor::CControlSensor(CThreadWindow* dlg) :_window(dlg)
  10. {
  11. _collection = Sensor_Collection::Sensor_None;
  12. _brake = _bucket = _throttle = _steer = 0;
  13. _bootstrap = _startup = false;
  14. }
  15. void CControlSensor::OnBootstrapd(bool ret)
  16. {
  17. _bootstrapd = ret;
  18. }
  19. void CControlSensor::OnStartupd(bool ret)
  20. {
  21. _startupd = ret;
  22. }
  23. void CControlSensor::Notify(int8_t* buffer, int32_t size)
  24. {
  25. int32_t receivedCnt = size / CAN_MSG_LEN;
  26. cannet_frame* p = (cannet_frame*)buffer;
  27. // char str[64];
  28. // ZeroMemory(str, sizeof(str));
  29. // std::string ret;
  30. for (int32_t i = 0; i < receivedCnt; i++)
  31. {
  32. int32_t id = ntohl(p->canid) & 0xFFF;
  33. // char text[64];
  34. // sprintf(text, "%x\n", id);
  35. //// std::string ret=std::to_string(id)
  36. // OutputDebugString(text);
  37. switch (id)
  38. {
  39. case 0x181:
  40. {
  41. OnSignal(p->data);
  42. //if (b == true)
  43. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Task);
  44. }
  45. break;
  46. case 0x633:
  47. {
  48. bool b = OnLeftJoyStick(p->data);
  49. if (b == true)
  50. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Left);
  51. }
  52. break;
  53. case 0x634:
  54. {
  55. bool b = OnRightJoyStick(p->data);
  56. if (b == true)
  57. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Right);
  58. }
  59. break;
  60. }
  61. p++;
  62. //std::string str = std::to_string(id) +"\n";
  63. }
  64. if (_startup&&_collection == Sensor_Collection::Sensor_All)
  65. {
  66. RemoNet::CCRobotAnalog _robot;
  67. _robot.set_brake(_brake);
  68. _robot.set_arm(_arm);
  69. _robot.set_bucket(_bucket);
  70. _robot.set_steer(_steer);
  71. _robot.set_throttle(_throttle);
  72. // _robot.set_emergency(_emergeny);
  73. _robot.set_gears(_gear);
  74. _robot.set_resume(_resume);
  75. MessageHead Head;
  76. CIOBuffer pBuffer;
  77. Head.Command = RemoNet::CC_RobotAnalog;
  78. Head.Length = _robot.ByteSizeLong();
  79. Head.Serialize(pBuffer.Buffer);
  80. auto ptr = pBuffer.Buffer + MessageHead::Size();
  81. _robot.SerializeToArray(ptr, Head.Length);
  82. pBuffer.Length = MessageHead::Size() + Head.Length;
  83. _window->SendData(&pBuffer);
  84. _collection = Sensor_Collection::Sensor_None;
  85. }
  86. }
  87. bool CControlSensor::OnSignal(int8_t* data)
  88. {
  89. bool ret = (data[0] & 0x01) != 0;
  90. if(ret==true)
  91. {
  92. if (_bootstrap == false)
  93. {
  94. if (_window->GetControlState() == ControlState::Process)
  95. {
  96. _bootstrap = true;
  97. RemoNet::CCBootStrapReq Req;
  98. MessageHead Head;
  99. CIOBuffer pBuffer;
  100. Head.Command = RemoNet::CC_BootReq;
  101. Head.Length = Req.ByteSizeLong();
  102. Head.Serialize(pBuffer.Buffer);
  103. auto ptr = pBuffer.Buffer + MessageHead::Size();
  104. Req.SerializeToArray(ptr, Head.Length);
  105. pBuffer.Length = MessageHead::Size() + Head.Length;
  106. _window->SendData(&pBuffer);
  107. }
  108. }
  109. }
  110. ret = (data[0] & 0x02) != 0;
  111. if (ret == true)
  112. {
  113. if (!_startup&&_bootstrapd)
  114. {
  115. if (_window->GetControlState() == ControlState::Process)
  116. {
  117. _startup = true;
  118. RemoNet::CCStartupReq Req;
  119. MessageHead Head;
  120. CIOBuffer pBuffer;
  121. Head.Command = RemoNet::CC_StartupReq;
  122. Head.Length = Req.ByteSizeLong();
  123. Head.Serialize(pBuffer.Buffer);
  124. auto ptr = pBuffer.Buffer + MessageHead::Size();
  125. Req.SerializeToArray(ptr, Head.Length);
  126. pBuffer.Length = MessageHead::Size() + Head.Length;
  127. _window->SendData(&pBuffer);
  128. }
  129. }
  130. }
  131. _resume = (data[2]) != 0;
  132. _throttle = data[4];
  133. _brake = data[5];
  134. return true;
  135. }
  136. bool CControlSensor::OnLeftJoyStick(int8_t* data)
  137. {
  138. return true;
  139. int32_t signal = 0;
  140. if ((data[0] & 0x3)!=0)
  141. {
  142. if ((data[0] & 0x03) != 0x01)
  143. {
  144. return false;
  145. }
  146. signal = 0;
  147. }
  148. if ((data[0] & (0x3<<2)) !=0)
  149. {
  150. int32_t value = data[0] >> 2;
  151. if ((value & 0x03) != 0x01)
  152. {
  153. return false;
  154. }
  155. signal = -1;
  156. }
  157. if ((data[0] & (0x3 << 4)) != 0)
  158. {
  159. int32_t value = data[0] >> 4;
  160. if ((value & 0x03) != 0x01)
  161. {
  162. return false;
  163. }
  164. signal = 1;
  165. }
  166. uint16_t xvalue = ((uint16_t)data[1]) & 0x00FF;
  167. xvalue <<= 2;
  168. uint16_t lvalue = ((uint16_t)data[0]) & 0x00FF;
  169. xvalue |= (lvalue >> 6);
  170. if((data[6]&0x3)==0x01)
  171. _steer = xvalue * signal;
  172. //OutputDebugString(std::to_string(_steer).c_str());
  173. // char buffer[64];
  174. // std::string ret;
  175. // for (int i=0;i<8;i+=2)
  176. // {
  177. // int16_t value = (data[5] >> i) & 0x3;
  178. // sprintf_s(buffer, "%x ", value);
  179. // ret += buffer;
  180. // }
  181. _gear = RemoNet::Gears::N;
  182. int32_t d = (data[5] >> 4) & 0x3;
  183. int32_t r = (data[5] >> 6) & 0x3;
  184. if (d == 0x1)
  185. {
  186. _gear = RemoNet::Gears::D;
  187. }
  188. else if (r == 0x1)
  189. {
  190. _gear = RemoNet::Gears::R;
  191. }
  192. // for (int i = 0; i < 8; i += 2)
  193. // {
  194. // int16_t value = (data[6] >> i) & 0x3;
  195. // sprintf_s(buffer, "%x ", value);
  196. // ret += buffer;
  197. // }
  198. // for (int i = 5; i < 8; i++)
  199. // {
  200. // int16_t t = (data[i]) & 0x00FF;
  201. // sprintf_s(buffer, "%x ", t);
  202. // ret += buffer;
  203. // }
  204. //ret += "\n";
  205. //OutputDebugString(ret.c_str());
  206. return true;
  207. }
  208. bool CControlSensor::OnRightJoyStick(int8_t* data)
  209. {
  210. _side = OpSide::Empty;
  211. if (((data[5] >> 2) & 0x3) == 0x01)
  212. {
  213. _side = OpSide::Arm;
  214. }
  215. else if (((data[5] >> 4) & 0x3) == 0x01)
  216. {
  217. _side = OpSide::Bucket;
  218. }
  219. int32_t signal = 0;
  220. if ((data[0] & 0x3) != 0)
  221. {
  222. if ((data[0] & 0x03) != 0x01)
  223. {
  224. return false;
  225. }
  226. signal = 0;
  227. }
  228. if ((data[0] & (0x3 << 2)) != 0)
  229. {
  230. int32_t value = data[0] >> 2;
  231. if ((value & 0x03) != 0x01)
  232. {
  233. return false;
  234. }
  235. signal = -1;
  236. }
  237. if ((data[0] & (0x3 << 4)) != 0)
  238. {
  239. int32_t value = data[0] >> 4;
  240. if ((value & 0x03) != 0x01)
  241. {
  242. return false;
  243. }
  244. signal = 1;
  245. }
  246. uint16_t xvalue = ((uint16_t)data[1]) & 0x00FF;
  247. xvalue <<= 2;
  248. uint16_t lvalue = ((uint16_t)data[0]) & 0x00FF;
  249. xvalue |= (lvalue >> 6);
  250. if (_side == OpSide::Arm)
  251. {
  252. _arm = xvalue;
  253. }
  254. else if(_side==OpSide::Bucket)
  255. {
  256. _bucket = xvalue;
  257. }
  258. return true;
  259. }
  260. void CControlSensor::Start()
  261. {
  262. }
  263. void CControlSensor::Stop()
  264. {
  265. }
  266. void CControlSensor::SetSensorSocket(SensorSocket<CControlSensor>* can)
  267. {
  268. }