Projektarbeit Line Following Robot bei Prof. Chowanetz im WS22/23
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uart_communication.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "uart_communication.h"
  2. LFR_UART::LFR_UART() : fileDescriptor(-1) {
  3. this->openSerialPort();
  4. this->configureSerialPort();
  5. }
  6. LFR_UART::~LFR_UART() {
  7. this->closeSerialPort();
  8. }
  9. void LFR_UART::openFile(const char *fileName) {
  10. this->fileDescriptor = open(fileName, O_RDWR | O_NONBLOCK);
  11. }
  12. int LFR_UART::writeDataToFile(uint8_t *buff, uint32_t bufferLength) {
  13. //std::cout << "Sending uart telegram" << std::endl;
  14. return write(this->fileDescriptor, buff, bufferLength);
  15. }
  16. int LFR_UART::readFromFile(uint8_t *buff, uint32_t bufferLength) {
  17. return read(this->fileDescriptor, buff, bufferLength);
  18. }
  19. int LFR_UART::closeFile() {
  20. return close(this->fileDescriptor);
  21. }
  22. uint8_t LFR_UART::doubleToByte(double in){
  23. /*
  24. * Map the range of -1.0 to 1.0 as double to the range of a byte.
  25. *
  26. * -1 -> 0
  27. * 0.0 -> 127
  28. * 1.0 -> 254
  29. * Not using the full range upto 254 to hit the 0.0
  30. */
  31. double minDouble = -1.0;
  32. double maxDouble = 1.0;
  33. double rangeDouble = maxDouble - minDouble;
  34. double minByte = 0.0;
  35. double maxByte = 255.0;
  36. double rangeByte = maxByte - minByte;
  37. double progress = in - minDouble;
  38. double progressPercent = progress/rangeDouble;
  39. double inputInByteRange = minByte + progressPercent*rangeByte;
  40. return uint8_t(inputInByteRange);
  41. }
  42. double LFR_UART::byteToDouble(uint8_t in){
  43. double minDouble = -1.0;
  44. double maxDouble = 1.0;
  45. double rangeDouble = maxDouble - minDouble;
  46. double minByte = 0.0;
  47. double maxByte = 255.0;
  48. double rangeByte = maxByte - minByte;
  49. double progress = double(in) - minByte;
  50. double progressPercent = progress/rangeByte;
  51. double inputInDoubleRange = minDouble + progressPercent*rangeDouble;
  52. return inputInDoubleRange;
  53. }
  54. void LFR_UART::sendTelegram(double wheel1, double wheel2, double wheel3, double wheel4){
  55. if(wheel1 > 1.0 || wheel2 > 1.0 || wheel3 > 1.0 || wheel4 > 1.0){
  56. throw CommunicatorException("Wheel value must not be greater than 1.0");
  57. }
  58. if(wheel1 < -1.0 || wheel2 < -1.0 || wheel3 < -1.0 || wheel4 < -1.0){
  59. throw CommunicatorException("Wheel value must not be smaller than -1.0");
  60. }
  61. uint8_t wheel1B = this->doubleToByte(wheel1);
  62. uint8_t wheel2B = this->doubleToByte(wheel2);
  63. uint8_t wheel3B = this->doubleToByte(wheel3);
  64. uint8_t wheel4B = this->doubleToByte(wheel4);
  65. uint8_t checksum = wheel1B^wheel2B^wheel3B^wheel4B;
  66. uint8_t telegram_buffer[5] = {wheel1B, wheel2B, wheel3B, wheel4B, checksum};
  67. uint32_t telegram_length = 5;
  68. this->writeDataToFile(telegram_buffer, telegram_length);
  69. }
  70. bool LFR_UART::readTelegram(double* buffer){
  71. uint8_t tmp_buffer[5] = {0, 0, 0, 0, 0};
  72. uint32_t telegram_length = 5;
  73. this->readFromFile(tmp_buffer, telegram_length);
  74. //std::cout << "Read from file: " << std::bitset<8>(tmp_buffer[0]) << ", " << std::bitset<8>(tmp_buffer[1]) << ", " << std::bitset<8>(tmp_buffer[2]) << ", " << std::bitset<8>(tmp_buffer[3]) << ", " << std::bitset<8>(tmp_buffer[4]) << std::endl;
  75. if (std::bitset<8>(tmp_buffer[0]^tmp_buffer[1]^tmp_buffer[2]^tmp_buffer[3]) != std::bitset<8>(tmp_buffer[4])){
  76. return false;
  77. }
  78. else {
  79. buffer[0] = this->byteToDouble(tmp_buffer[0]);
  80. buffer[1] = this->byteToDouble(tmp_buffer[1]);
  81. buffer[2] = this->byteToDouble(tmp_buffer[2]);
  82. buffer[3] = this->byteToDouble(tmp_buffer[3]);
  83. }
  84. return true;
  85. }
  86. void LFR_UART::openSerialPort() {
  87. openFile(this->serialPortPath);
  88. if(this->fileDescriptor < 0) {
  89. throw CommunicatorException("LFR_UART couldn't open the serial port.");
  90. exit(EXIT_FAILURE);
  91. }
  92. }
  93. void LFR_UART::configureSerialPort() {
  94. if(tcgetattr(this->fileDescriptor, &this->tty)) {
  95. throw CommunicatorException("LFR_UART couldn't get the port attributes.");
  96. exit(EXIT_FAILURE);
  97. }
  98. cfsetispeed(&this->tty,B115200);
  99. cfsetospeed(&this->tty,B115200);
  100. cfmakeraw(&this->tty);
  101. if(tcsetattr(this->fileDescriptor,TCSANOW,&this->tty)) {
  102. throw CommunicatorException("LFR_UART couldn't set the port attributes.");
  103. exit(EXIT_FAILURE);
  104. }
  105. }
  106. void LFR_UART::closeSerialPort() {
  107. this->closeFile();
  108. }