Browse Source

Added the Communication Module to the repo.

pull/1/head
Tim Zeuner 2 years ago
parent
commit
eb64c8eded
55 changed files with 225 additions and 0 deletions
  1. 0
    0
      AutonomousMode/CMakeLists.txt
  2. 0
    0
      AutonomousMode/ControlModule/CMakeLists.txt
  3. 0
    0
      AutonomousMode/ControlModule/control_module.cpp
  4. 0
    0
      AutonomousMode/ControlModule/control_module.h
  5. 0
    0
      AutonomousMode/Input/CMakeLists.txt
  6. 0
    0
      AutonomousMode/Input/input.cpp
  7. 0
    0
      AutonomousMode/Input/input.h
  8. 0
    0
      AutonomousMode/Interpreter/CMakeLists.txt
  9. 0
    0
      AutonomousMode/Interpreter/interpreter.cpp
  10. 0
    0
      AutonomousMode/Interpreter/interpreter.h
  11. 0
    0
      AutonomousMode/IntersectionHandler/CMakeLists.txt
  12. 0
    0
      AutonomousMode/IntersectionHandler/intersection_handler.cpp
  13. 0
    0
      AutonomousMode/IntersectionHandler/intersection_handler.h
  14. 0
    0
      AutonomousMode/Processing/CMakeLists.txt
  15. 0
    0
      AutonomousMode/Processing/processing.cpp
  16. 0
    0
      AutonomousMode/Processing/processing.h
  17. 0
    0
      AutonomousMode/Spielwiese/CMakeLists.txt
  18. 0
    0
      AutonomousMode/Spielwiese/spielwiese.cpp
  19. 0
    0
      AutonomousMode/Test_data/image1.jpeg
  20. 0
    0
      AutonomousMode/Test_data/image10.jpeg
  21. 0
    0
      AutonomousMode/Test_data/image11.jpeg
  22. 0
    0
      AutonomousMode/Test_data/image12.jpeg
  23. 0
    0
      AutonomousMode/Test_data/image13.jpeg
  24. 0
    0
      AutonomousMode/Test_data/image14.jpeg
  25. 0
    0
      AutonomousMode/Test_data/image15.jpeg
  26. 0
    0
      AutonomousMode/Test_data/image16.jpeg
  27. 0
    0
      AutonomousMode/Test_data/image17.jpeg
  28. 0
    0
      AutonomousMode/Test_data/image18.jpeg
  29. 0
    0
      AutonomousMode/Test_data/image19.jpeg
  30. 0
    0
      AutonomousMode/Test_data/image2.jpeg
  31. 0
    0
      AutonomousMode/Test_data/image20.jpeg
  32. 0
    0
      AutonomousMode/Test_data/image21.jpeg
  33. 0
    0
      AutonomousMode/Test_data/image22.jpeg
  34. 0
    0
      AutonomousMode/Test_data/image23.jpeg
  35. 0
    0
      AutonomousMode/Test_data/image24.jpeg
  36. 0
    0
      AutonomousMode/Test_data/image25.jpeg
  37. 0
    0
      AutonomousMode/Test_data/image26.jpeg
  38. 0
    0
      AutonomousMode/Test_data/image27.jpeg
  39. 0
    0
      AutonomousMode/Test_data/image28.jpeg
  40. 0
    0
      AutonomousMode/Test_data/image3.jpeg
  41. 0
    0
      AutonomousMode/Test_data/image4.jpeg
  42. 0
    0
      AutonomousMode/Test_data/image5.jpeg
  43. 0
    0
      AutonomousMode/Test_data/image6.jpeg
  44. 0
    0
      AutonomousMode/Test_data/image7.jpeg
  45. 0
    0
      AutonomousMode/Test_data/image8.jpeg
  46. 0
    0
      AutonomousMode/Test_data/image9.jpeg
  47. 0
    0
      AutonomousMode/Utils/CMakeLists.txt
  48. 0
    0
      AutonomousMode/Utils/utils.cpp
  49. 0
    0
      AutonomousMode/Utils/utils.h
  50. 0
    0
      AutonomousMode/autonomous_mode_main.cpp
  51. 0
    0
      AutonomousMode/lfr.cpp
  52. 0
    0
      AutonomousMode/lfr.h
  53. 17
    0
      Communication/CMakeLists.txt
  54. 152
    0
      Communication/uart_communication.cpp
  55. 56
    0
      Communication/uart_communication.h

CMakeLists.txt → AutonomousMode/CMakeLists.txt View File


ControlModule/CMakeLists.txt → AutonomousMode/ControlModule/CMakeLists.txt View File


ControlModule/control_module.cpp → AutonomousMode/ControlModule/control_module.cpp View File


ControlModule/control_module.h → AutonomousMode/ControlModule/control_module.h View File


Input/CMakeLists.txt → AutonomousMode/Input/CMakeLists.txt View File


Input/input.cpp → AutonomousMode/Input/input.cpp View File


Input/input.h → AutonomousMode/Input/input.h View File


Interpreter/CMakeLists.txt → AutonomousMode/Interpreter/CMakeLists.txt View File


Interpreter/interpreter.cpp → AutonomousMode/Interpreter/interpreter.cpp View File


Interpreter/interpreter.h → AutonomousMode/Interpreter/interpreter.h View File


IntersectionHandler/CMakeLists.txt → AutonomousMode/IntersectionHandler/CMakeLists.txt View File


IntersectionHandler/intersection_handler.cpp → AutonomousMode/IntersectionHandler/intersection_handler.cpp View File


IntersectionHandler/intersection_handler.h → AutonomousMode/IntersectionHandler/intersection_handler.h View File


Processing/CMakeLists.txt → AutonomousMode/Processing/CMakeLists.txt View File


Processing/processing.cpp → AutonomousMode/Processing/processing.cpp View File


Processing/processing.h → AutonomousMode/Processing/processing.h View File


Spielwiese/CMakeLists.txt → AutonomousMode/Spielwiese/CMakeLists.txt View File


Spielwiese/spielwiese.cpp → AutonomousMode/Spielwiese/spielwiese.cpp View File


Test_data/image1.jpeg → AutonomousMode/Test_data/image1.jpeg View File


Test_data/image10.jpeg → AutonomousMode/Test_data/image10.jpeg View File


Test_data/image11.jpeg → AutonomousMode/Test_data/image11.jpeg View File


Test_data/image12.jpeg → AutonomousMode/Test_data/image12.jpeg View File


Test_data/image13.jpeg → AutonomousMode/Test_data/image13.jpeg View File


Test_data/image14.jpeg → AutonomousMode/Test_data/image14.jpeg View File


Test_data/image15.jpeg → AutonomousMode/Test_data/image15.jpeg View File


Test_data/image16.jpeg → AutonomousMode/Test_data/image16.jpeg View File


Test_data/image17.jpeg → AutonomousMode/Test_data/image17.jpeg View File


Test_data/image18.jpeg → AutonomousMode/Test_data/image18.jpeg View File


Test_data/image19.jpeg → AutonomousMode/Test_data/image19.jpeg View File


Test_data/image2.jpeg → AutonomousMode/Test_data/image2.jpeg View File


Test_data/image20.jpeg → AutonomousMode/Test_data/image20.jpeg View File


Test_data/image21.jpeg → AutonomousMode/Test_data/image21.jpeg View File


Test_data/image22.jpeg → AutonomousMode/Test_data/image22.jpeg View File


Test_data/image23.jpeg → AutonomousMode/Test_data/image23.jpeg View File


Test_data/image24.jpeg → AutonomousMode/Test_data/image24.jpeg View File


Test_data/image25.jpeg → AutonomousMode/Test_data/image25.jpeg View File


Test_data/image26.jpeg → AutonomousMode/Test_data/image26.jpeg View File


Test_data/image27.jpeg → AutonomousMode/Test_data/image27.jpeg View File


Test_data/image28.jpeg → AutonomousMode/Test_data/image28.jpeg View File


Test_data/image3.jpeg → AutonomousMode/Test_data/image3.jpeg View File


Test_data/image4.jpeg → AutonomousMode/Test_data/image4.jpeg View File


Test_data/image5.jpeg → AutonomousMode/Test_data/image5.jpeg View File


Test_data/image6.jpeg → AutonomousMode/Test_data/image6.jpeg View File


Test_data/image7.jpeg → AutonomousMode/Test_data/image7.jpeg View File


Test_data/image8.jpeg → AutonomousMode/Test_data/image8.jpeg View File


Test_data/image9.jpeg → AutonomousMode/Test_data/image9.jpeg View File


Utils/CMakeLists.txt → AutonomousMode/Utils/CMakeLists.txt View File


Utils/utils.cpp → AutonomousMode/Utils/utils.cpp View File


Utils/utils.h → AutonomousMode/Utils/utils.h View File


autonomous_mode_main.cpp → AutonomousMode/autonomous_mode_main.cpp View File


lfr.cpp → AutonomousMode/lfr.cpp View File


lfr.h → AutonomousMode/lfr.h View File


+ 17
- 0
Communication/CMakeLists.txt View File

cmake_minimum_required(VERSION 3.1.0)
project(lfr_uart VERSION 0.1.0)

include(CTest)
enable_testing()

set(THREADS_PREFER_PTHREAD_FLAG ON)

find_package(Threads REQUIRED)

add_executable(lfr_uart uart_communication.cpp)

target_link_libraries( lfr_uart Threads::Threads)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

+ 152
- 0
Communication/uart_communication.cpp View File

#include "uart_communication.h"


LFR_UART::LFR_UART() : fileDescriptor(-1) {
this->openSerialPort();
this->configureSerialPort();
}
LFR_UART::~LFR_UART() {
this->closeSerialPort();
}

void LFR_UART::openFile(const char *fileName) {
this->fileDescriptor = open(fileName, O_RDWR | O_NONBLOCK);
}

int LFR_UART::writeDataToFile(uint8_t *buff, uint32_t bufferLength) {
return write(this->fileDescriptor, buff, bufferLength);
}

int LFR_UART::readFromFile(uint8_t *buff, uint32_t bufferLength) {
return read(this->fileDescriptor, buff, bufferLength);
}

int LFR_UART::closeFile() {
return close(this->fileDescriptor);
}

uint8_t LFR_UART::doubleToByte(double in){
/*
* Map the range of -1.0 to 1.0 as double to the range of a byte.
*
* -1 -> 0
* 0.0 -> 127
* 1.0 -> 254
* Not using the full range upto 254 to hit the 0.0
*/
double minDouble = -1.0;
double maxDouble = 1.0;
double rangeDouble = maxDouble - minDouble;

double minByte = 0.0;
double maxByte = 255.0;
double rangeByte = maxByte - minByte;

double progress = in - minDouble;
double progressPercent = progress/rangeDouble;

double inputInByteRange = minByte + progressPercent*rangeByte;
return uint8_t(inputInByteRange);
}

double LFR_UART::byteToDouble(uint8_t in){
double minDouble = -1.0;
double maxDouble = 1.0;
double rangeDouble = maxDouble - minDouble;

double minByte = 0.0;
double maxByte = 255.0;
double rangeByte = maxByte - minByte;

double progress = double(in) - minByte;
double progressPercent = progress/rangeByte;

double inputInDoubleRange = minDouble + progressPercent*rangeDouble;
return inputInDoubleRange;
}

void LFR_UART::sendTelegram(double wheel1, double wheel2, double wheel3, double wheel4){
if(wheel1 > 1.0 || wheel2 > 1.0 || wheel3 > 1.0 || wheel4 > 1.0){
throw CommunicatorException("Wheel value must not be greater than 1.0");
}
if(wheel1 < -1.0 || wheel2 < -1.0 || wheel3 < -1.0 || wheel4 < -1.0){
throw CommunicatorException("Wheel value must not be smaller than -1.0");
}

uint8_t wheel1B = this->doubleToByte(wheel1);
uint8_t wheel2B = this->doubleToByte(wheel2);
uint8_t wheel3B = this->doubleToByte(wheel3);
uint8_t wheel4B = this->doubleToByte(wheel4);

uint8_t checksum = wheel1B^wheel2B^wheel3B^wheel4B;

uint8_t telegram_buffer[5] = {wheel1B, wheel2B, wheel3B, wheel4B, checksum};
uint32_t telegram_length = 5;
this->writeDataToFile(telegram_buffer, telegram_length);
}

bool LFR_UART::readTelegram(double* buffer){
uint8_t tmp_buffer[5] = {0, 0, 0, 0, 0};
uint32_t telegram_length = 5;
this->readFromFile(tmp_buffer, telegram_length);

//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;

if (std::bitset<8>(tmp_buffer[0]^tmp_buffer[1]^tmp_buffer[2]^tmp_buffer[3]) != std::bitset<8>(tmp_buffer[4])){
return false;
}
else {
buffer[0] = this->byteToDouble(tmp_buffer[0]);
buffer[1] = this->byteToDouble(tmp_buffer[1]);
buffer[2] = this->byteToDouble(tmp_buffer[2]);
buffer[3] = this->byteToDouble(tmp_buffer[3]);
}
return true;
}

void LFR_UART::openSerialPort() {
openFile(this->serialPortPath);
if(this->fileDescriptor < 0) {
throw CommunicatorException("LFR_UART couldn't open the serial port.");
exit(EXIT_FAILURE);
}
}

void LFR_UART::configureSerialPort() {
if(tcgetattr(this->fileDescriptor, &this->tty)) {
throw CommunicatorException("LFR_UART couldn't get the port attributes.");
exit(EXIT_FAILURE);
}

cfsetispeed(&this->tty,B115200);
cfsetospeed(&this->tty,B115200);

cfmakeraw(&this->tty);

if(tcsetattr(this->fileDescriptor,TCSANOW,&this->tty)) {
throw CommunicatorException("LFR_UART couldn't set the port attributes.");
exit(EXIT_FAILURE);
}
}

void LFR_UART::closeSerialPort() {
this->closeFile();
}

int main(void) {
printf("Starting the loopback application...\r\n");
LFR_UART uartCommunicator;
uartCommunicator.sendTelegram(-1.0, -0.99, 0.99, 1.0);
double buffer[4] = {0.0, 0.0, 0.0, 0.0};
sleep(1);
if(uartCommunicator.readTelegram(buffer)){
for(int i = 0; i < 4; i++) {
std::cout << buffer[i] << " ";
}
}
else {
std::cout<<"ne";
}
return 0;
}

+ 56
- 0
Communication/uart_communication.h View File

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <iostream>
#include <exception>

#include <bitset>


class LFR_UART
{
int fileDescriptor;
const char* serialPortPath = "/dev/ttyS0";
void openFile(const char *fileName);
int closeFile();

struct termios tty;
void openSerialPort();
void configureSerialPort();
void closeSerialPort();

int writeDataToFile(uint8_t *buff, uint32_t bufferLength);
int readFromFile(uint8_t *buff, uint32_t bufferLength);

public:

uint8_t doubleToByte(double in);
double byteToDouble(uint8_t in);

void sendTelegram(double wheel1, double wheel2, double wheel3, double wheel4);
bool readTelegram(double* buffer);
LFR_UART();
~LFR_UART();

};

class CommunicatorException: public std::exception
{
const char* msg;
public:
CommunicatorException(const char* msg): msg(msg) {}
virtual const char* what() const throw()
{
return msg;
}
};

Loading…
Cancel
Save