ControllerHandler.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // Created by janco on 5/23/16.
  3. //
  4. #include "ControllerHandler.h"
  5. #include <iostream>
  6. #include <stdlib.h>
  7. #include <chrono>
  8. #include <thread>
  9. /*
  10. * String split helper functions
  11. */
  12. std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
  13. std::stringstream ss(s);
  14. std::string item;
  15. while (std::getline(ss, item, delim)) {
  16. elems.push_back(item);
  17. }
  18. return elems;
  19. }
  20. std::vector<std::string> split(const std::string &s, char delim) {
  21. std::vector<std::string> elems;
  22. split(s, delim, elems);
  23. return elems;
  24. }
  25. ControllerHandler::ControllerHandler(){
  26. readthread = std::thread(&ControllerHandler::BasestationReader, this);
  27. };
  28. Controller* ControllerHandler::getLeftController(void){
  29. for (auto e : controllers)
  30. {
  31. if(e != nullptr && e->isConnected()){
  32. return e;
  33. }
  34. }
  35. return nullptr;
  36. }
  37. void ControllerHandler::SearchBasestation(void){
  38. basestationFound = false;
  39. std::vector<serial::PortInfo> devices_found = serial::list_ports();
  40. std::vector<serial::PortInfo>::iterator iter = devices_found.begin();
  41. while( iter != devices_found.end() )
  42. {
  43. serial::PortInfo device = *iter++;
  44. if(device.hardware_id == "USB VID:PID=10c4:ea60 SNR=02133"){
  45. basestationFound = true;
  46. baseStation = new serial::Serial(device.port, BasestationBaudrate, serial::Timeout::simpleTimeout(1000));
  47. break;
  48. }
  49. }
  50. }
  51. void ControllerHandler::rumble(int idController, int duration, int power){
  52. if (basestationFound)
  53. {
  54. char sendbuffer[16];
  55. sprintf(sendbuffer, "1|%01d|%06d|%03d\n", idController, duration, power);
  56. baseStation->write(sendbuffer);
  57. }
  58. }
  59. /*
  60. * Reading from basestation
  61. */
  62. void ControllerHandler::BasestationReader() {
  63. while(1){
  64. while(!basestationFound){
  65. SearchBasestation();
  66. if(basestationFound){
  67. baseStation ->write("stop\n");
  68. baseStation ->write("clientlist\n");
  69. }
  70. std::this_thread::sleep_for(std::chrono::milliseconds(2000));
  71. }
  72. while(baseStation->isOpen()){
  73. std::string basestationMessage = baseStation->readline();
  74. std::vector<std::string> basestationMessageSplit = split(basestationMessage, '|');
  75. if(basestationMessageSplit.size() > 1){ //Valid message
  76. std::cout << "DEBUG: Message received. Length: " << basestationMessageSplit.size() << " Type: " << basestationMessageSplit[0] << " Message: " << basestationMessage;
  77. int messageId = std::stoi(basestationMessageSplit[0]);
  78. switch(messageId){
  79. //commandList[messageId](basestationMessageSplit);
  80. case 0: commandDebug(basestationMessageSplit); break; //Debug message
  81. case 1: commandControllerData(basestationMessageSplit); break; //Data from controller
  82. case 2: commandControllerEvent(basestationMessageSplit); break; //Controller events
  83. case 3: commandControllerList(basestationMessageSplit); break; //Connected controller list
  84. }
  85. }
  86. }
  87. }
  88. }
  89. void ControllerHandler::commandDebug(std::vector<std::string> data) {
  90. std::cout << "DEBUG: " << data[1];
  91. }
  92. void ControllerHandler::commandControllerData(std::vector<std::string> data) {
  93. if(data.size() == 10){
  94. Controller* c = controllers[std::stoi(data[1])];
  95. c->ypr.x = std::stoi(data[5])/100.0f;
  96. c->ypr.y = std::stoi(data[6])/100.0f;
  97. c->ypr.z = std::stoi(data[7])/100.0f;
  98. c->joystick.x = std::stoi(data[2])/3000.0f;
  99. c->joystick.y = std::stoi(data[3])/3000.0f;
  100. c->joystickButton = !(data[4] == "0");
  101. c->magnetSwitch = !(data[9] == "0");
  102. }
  103. }
  104. void ControllerHandler::commandControllerEvent(std::vector<std::string> data) {
  105. if(data.size() == 3){
  106. if(data[1] == "connected"){
  107. int controllerId = std::stoi(data[2]);
  108. controllers[controllerId] = new Controller(controllerId);
  109. controllers[controllerId]->setConnected(true);
  110. }else if(data[1] == "disconnected"){
  111. // controllers.erase(*controllers[std::stoi(data[2])]);
  112. }
  113. }
  114. }
  115. void ControllerHandler::commandControllerList(std::vector<std::string> data) {
  116. for(unsigned int i = 1; i < data.size() -1; i++){
  117. int controllerId = std::stoi(data[i]);
  118. controllers[controllerId] = new Controller(controllerId);
  119. }
  120. if(basestationFound)
  121. baseStation->write("start\n");
  122. }