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.1KB

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