ControlSensor.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. }
  14. void CControlSensor::Notify(int8_t* buffer, int32_t size)
  15. {
  16. int32_t receivedCnt = size / CAN_MSG_LEN;
  17. cannet_frame* p = (cannet_frame*)buffer;
  18. // char str[64];
  19. // ZeroMemory(str, sizeof(str));
  20. // std::string ret;
  21. for (int32_t i = 0; i < receivedCnt; i++)
  22. {
  23. int32_t id = ntohl(p->canid) & 0xFFF;
  24. switch (id)
  25. {
  26. case 0x181:
  27. {
  28. OnSignal(p->data);
  29. //if (b == true)
  30. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Task);
  31. }
  32. break;
  33. case 0x633:
  34. {
  35. bool b = OnLeftJoyStick(p->data);
  36. if (b == true)
  37. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Left);
  38. }
  39. break;
  40. case 0x634:
  41. {
  42. bool b = OnRightJoyStick(p->data);
  43. if (b == true)
  44. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Right);
  45. }
  46. break;
  47. }
  48. //std::string str = std::to_string(id) +"\n";
  49. }
  50. if (_window->GetControlState()==ControlState::Process&&_collection == Sensor_Collection::Sensor_All)
  51. {
  52. RemoNet::CCRobotSignal _robot;
  53. _robot.set_brake(_brake);
  54. _robot.set_arm(_arm);
  55. _robot.set_bucket(_bucket);
  56. _robot.set_steer(_steer);
  57. _robot.set_throttle(_throttle);
  58. _robot.set_bootstrap(_bootstrap);
  59. _robot.set_startup(_startup);
  60. _robot.set_emergency(_emergeny);
  61. _robot.set_gears(_gear);
  62. _robot.set_resume(_resume);
  63. MessageHead Head;
  64. CIOBuffer pBuffer;
  65. Head.Command = RemoNet::CC_RobotSignal;
  66. Head.Length = _robot.ByteSizeLong();
  67. Head.Serialize(pBuffer.Buffer);
  68. auto ptr = pBuffer.Buffer + MessageHead::Size();
  69. _robot.SerializeToArray(ptr, Head.Length);
  70. pBuffer.Length = MessageHead::Size() + Head.Length;
  71. // _window->SendData(&pBuffer);
  72. _collection = Sensor_Collection::Sensor_None;
  73. }
  74. }
  75. bool CControlSensor::OnSignal(int8_t* data)
  76. {
  77. _bootstrap = (data[0] & 0x01) != 0;
  78. _startup = (data[0] & 0x02) != 0;
  79. _resume = (data[2]) != 0;
  80. _emergeny = (data[3]) != 0;
  81. _throttle = data[4];
  82. _brake = data[5];
  83. return true;
  84. }
  85. bool CControlSensor::OnLeftJoyStick(int8_t* data)
  86. {
  87. return true;
  88. int32_t signal = 0;
  89. if ((data[0] & 0x3)!=0)
  90. {
  91. if ((data[0] & 0x03) != 0x01)
  92. {
  93. return false;
  94. }
  95. signal = 0;
  96. }
  97. if ((data[0] & (0x3<<2)) !=0)
  98. {
  99. int32_t value = data[0] >> 2;
  100. if ((value & 0x03) != 0x01)
  101. {
  102. return false;
  103. }
  104. signal = -1;
  105. }
  106. if ((data[0] & (0x3 << 4)) != 0)
  107. {
  108. int32_t value = data[0] >> 4;
  109. if ((value & 0x03) != 0x01)
  110. {
  111. return false;
  112. }
  113. signal = 1;
  114. }
  115. uint16_t xvalue = ((uint16_t)data[1]) & 0x00FF;
  116. xvalue <<= 2;
  117. uint16_t lvalue = ((uint16_t)data[0]) & 0x00FF;
  118. xvalue |= (lvalue >> 6);
  119. if((data[6]&0x3)==0x01)
  120. _steer = xvalue * signal;
  121. //OutputDebugString(std::to_string(_steer).c_str());
  122. // char buffer[64];
  123. // std::string ret;
  124. // for (int i=0;i<8;i+=2)
  125. // {
  126. // int16_t value = (data[5] >> i) & 0x3;
  127. // sprintf_s(buffer, "%x ", value);
  128. // ret += buffer;
  129. // }
  130. _gear = RemoNet::Gears::N;
  131. int32_t d = (data[5] >> 4) & 0x3;
  132. int32_t r = (data[5] >> 6) & 0x3;
  133. if (d == 0x1)
  134. {
  135. _gear = RemoNet::Gears::D;
  136. }
  137. else if (r == 0x1)
  138. {
  139. _gear == RemoNet::Gears::R;
  140. }
  141. // for (int i = 0; i < 8; i += 2)
  142. // {
  143. // int16_t value = (data[6] >> i) & 0x3;
  144. // sprintf_s(buffer, "%x ", value);
  145. // ret += buffer;
  146. // }
  147. // for (int i = 5; i < 8; i++)
  148. // {
  149. // int16_t t = (data[i]) & 0x00FF;
  150. // sprintf_s(buffer, "%x ", t);
  151. // ret += buffer;
  152. // }
  153. //ret += "\n";
  154. //OutputDebugString(ret.c_str());
  155. return true;
  156. }
  157. bool CControlSensor::OnRightJoyStick(int8_t* data)
  158. {
  159. _side = OpSide::Empty;
  160. if (((data[5] >> 2) & 0x3) == 0x01)
  161. {
  162. _side = OpSide::Arm;
  163. }
  164. else if (((data[5] >> 4) & 0x3) == 0x01)
  165. {
  166. _side = OpSide::Bucket;
  167. }
  168. int32_t signal = 0;
  169. if ((data[0] & 0x3) != 0)
  170. {
  171. if ((data[0] & 0x03) != 0x01)
  172. {
  173. return false;
  174. }
  175. signal = 0;
  176. }
  177. if ((data[0] & (0x3 << 2)) != 0)
  178. {
  179. int32_t value = data[0] >> 2;
  180. if ((value & 0x03) != 0x01)
  181. {
  182. return false;
  183. }
  184. signal = -1;
  185. }
  186. if ((data[0] & (0x3 << 4)) != 0)
  187. {
  188. int32_t value = data[0] >> 4;
  189. if ((value & 0x03) != 0x01)
  190. {
  191. return false;
  192. }
  193. signal = 1;
  194. }
  195. uint16_t xvalue = ((uint16_t)data[1]) & 0x00FF;
  196. xvalue <<= 2;
  197. uint16_t lvalue = ((uint16_t)data[0]) & 0x00FF;
  198. xvalue |= (lvalue >> 6);
  199. if (_side == OpSide::Arm)
  200. {
  201. _arm = xvalue;
  202. }
  203. else if(_side=OpSide::Bucket)
  204. {
  205. _bucket = xvalue;
  206. }
  207. return true;
  208. }
  209. void CControlSensor::Start()
  210. {
  211. }
  212. void CControlSensor::Stop()
  213. {
  214. }
  215. void CControlSensor::SetSensorSocket(SensorSocket<CControlSensor>* can)
  216. {
  217. }