arduino_handle.ino 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /********************************************************************
  2. Copyright: 2016-2018 ROS小课堂 www.corvin.cn
  3. Author: corvin
  4. Description:
  5. ROS控制手柄的arduino部分代码,主要就是获取各传感器信息.将其组装好后通过串口
  6. 发送给上位机部分的ROS python代码来解析.
  7. History:
  8. 20181024:initial this code.
  9. 20181030:修改为发送各模块的原始数据到上位机,数据解析处理在上位机进行,尽可能
  10. 保持该固件代码结构简单,易于调试维护.
  11. *********************************************************************/
  12. #define BAUD_RATE 57600
  13. #define ROTATE_RANGE 100
  14. #define SEND_RATE 10
  15. /*
  16. define sensors pin
  17. */
  18. int self_lock_pin = 11;
  19. int handle_x_pin = A2;
  20. int handle_y_pin = A1;
  21. int handle_z_pin = 10;
  22. int rotation_pin = A0;
  23. void setup() {
  24. pinMode(self_lock_pin, INPUT);
  25. pinMode(rotation_pin, INPUT);
  26. pinMode(handle_z_pin, INPUT_PULLUP);
  27. Serial.begin(BAUD_RATE);
  28. }
  29. void loop() {
  30. int lock_status = digitalRead(self_lock_pin);
  31. int rotation_value = analogRead(rotation_pin);
  32. int rotate_percent = map(rotation_value, 1023, 0, 0, ROTATE_RANGE);
  33. //turn left or right
  34. int handle_x = analogRead(handle_x_pin);
  35. //move forward or back
  36. int handle_y = analogRead(handle_y_pin);
  37. //click handle
  38. int handle_z = digitalRead(handle_z_pin);
  39. //output all value by serial port
  40. outPutData(lock_status, handle_x, handle_y, handle_z, rotate_percent);
  41. delay(1000 / SEND_RATE);
  42. }
  43. /*
  44. formate output data
  45. */
  46. void outPutData(int lock_status, int handle_x, int handle_y, int handle_z, int rotate_percent)
  47. {
  48. Serial.print(lock_status, DEC);
  49. Serial.print(" ");
  50. Serial.print(handle_x, DEC);
  51. Serial.print(" ");
  52. Serial.print(handle_y, DEC);
  53. Serial.print(" ");
  54. Serial.print(handle_z, DEC);
  55. Serial.print(" ");
  56. Serial.println(rotate_percent, DEC);
  57. }