ControllerHandler.cpp 4.9 KB

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