diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..90ddbed --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,71 @@ +{ + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "charconv": "cpp", + "chrono": "cpp", + "cinttypes": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "csignal": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "list": "cpp", + "map": "cpp", + "set": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "format": "cpp", + "fstream": "cpp", + "future": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "semaphore": "cpp", + "shared_mutex": "cpp", + "span": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "typeinfo": "cpp", + "variant": "cpp" + } +} \ No newline at end of file diff --git a/middleware/inc/BackMotors.hpp b/middleware/inc/BackMotors.hpp index 948af19..f8f1583 100644 --- a/middleware/inc/BackMotors.hpp +++ b/middleware/inc/BackMotors.hpp @@ -8,13 +8,11 @@ #include // Para close(), usleep() #include // Para open() #include -#include // Interface padrão do Linux para I2C - #include // Biblioteca para manipulação de sinais - #include #include // Para cálculos de tempo -#include // Biblioteca para manipulação de sinais +#include // Interface padrão do Linux para I2C + class BackMotors{ private: diff --git a/middleware/inc/CarControl.hpp b/middleware/inc/CarControl.hpp index 2a4440b..87c78a2 100644 --- a/middleware/inc/CarControl.hpp +++ b/middleware/inc/CarControl.hpp @@ -1,5 +1,35 @@ #pragma once +#include + + + +class BackMotors; +class FServo; + +class CarControl { +private: + BackMotors _backMotors; + FServo _fServo; + + std::atomic _running; + std::thread _controlThread; + int _currenntGear; + double _currentSpeed; + double _gear[5] = {20, 40, 60, 80, 100}; + + void _shiftDown(); + void _shiftUp(); + +public: + CarControl(); + ~CarControl(); + + void processJoystick(); + void process(); + void start(); + void stop(); +}; #include "BackMotors.hpp" -#include "FrontMotors.hpp" \ No newline at end of file +#include "FServo.hpp" \ No newline at end of file diff --git a/middleware/inc/FServo.hpp b/middleware/inc/FServo.hpp index 1179650..f8a6cfa 100644 --- a/middleware/inc/FServo.hpp +++ b/middleware/inc/FServo.hpp @@ -14,7 +14,6 @@ #include #include // Para cálculos de tempo -#include // Biblioteca para manipulação de sinais class FServo{ private: diff --git a/middleware/inc/SpeedSensor.hpp b/middleware/inc/SpeedSensor.hpp index f301b4d..18cc773 100644 --- a/middleware/inc/SpeedSensor.hpp +++ b/middleware/inc/SpeedSensor.hpp @@ -11,11 +11,7 @@ #include #include -#include -#include #include -#include -#include #include class SpeedSensor { diff --git a/middleware/src/CarControl.cpp b/middleware/src/CarControl.cpp index e69de29..0e46944 100644 --- a/middleware/src/CarControl.cpp +++ b/middleware/src/CarControl.cpp @@ -0,0 +1,87 @@ +#include "CarControl.hpp" + +CarControl::CarControl() : _running(false), _currenntGear(0), _currentSpeed(0) { + +} + +CarControl::~CarControl() { +} + +void CarControl::processJoystick() { + int joy_fd = open("/dev/input/js0", O_RDONLY); + if (joy_fd < 0) { + std::cerr << "Erro ao abrir o joystick" << std::endl; + return; + } + + struct js_event js; + + while (_running) { + ssize_t n = read(joy_fd, &js, sizeof(js)); + if (n == sizeof(js)) { + js.type &= ~JS_EVENT_INIT; // Ignora eventos de inicialização + if (js.type == JS_EVENT_BUTTON){ + if (js.number == 4 && js.value == 1) { //Left Button BTN_TL + _shiftDown(); + std::cout << "Shift Down: " << _currenntGear + 1 << ", Max speed" << _gear[_currenntGear] << std::endl; + } + else if (js.number == 5 && js.value == 1) { //Right Button BTN_TR + _shiftUp(); + std::cout << "Shift Up: " << _currenntGear + 1 << ", Max speed" << _gear[_currenntGear] << std::endl; + } + else if ((js.number == 7 || js.number == 6) && js.value == 1) { //Start/Select Button BTN_START + _backMotors.setSpeed(0); + _running = false; + std::cout << "Stop" << std::endl; + break; + } + } + else if (js.type == JS_EVENT_AXIS) { + if (js.number == 0) { // ABS_X + _fServo.set_steering( js.value / 32767.0 * 100); + } + else if (js.number == 5) { // ABS_RZ + if (js.value == 0){ + _backMotors.setSpeed(0); + } + else { + _backMotors.setSpeed(js.value / 32767.0 * 100); + } + } + } + } + else if (n < 0) { + std::cerr << "Erro ao ler o joystick" << std::endl; + _running = false; + break; + } + } + close (joy_fd); +} + +void CarControl::start() { + _running = true; + _controlThread = std::thread(&CarControl::processJoystick, this); +} + +void CarControl::stop() { + _running = false; + if (_controlThread.joinable()) { + _controlThread.join(); + } + _backMotors.setSpeed(0); + _fServo.set_steering(0); +} + +void CarControl::_shiftDown() { + if (_currenntGear > 0) { + _currenntGear--; + } +} + +void CarControl::_shiftUp() { + if (_currenntGear < 4) { + _currenntGear++; + } +} + diff --git a/middleware/src/main.cpp b/middleware/src/main.cpp index 9171bd0..c448ccb 100644 --- a/middleware/src/main.cpp +++ b/middleware/src/main.cpp @@ -1,5 +1,6 @@ #include "BackMotors.hpp" #include "FServo.hpp" +#include "CarControl.hpp" // Variável para capturar o sinal de interrupção (Ctrl+C) void signalHandler(int signum) { @@ -10,6 +11,19 @@ int main() { // Conectar o manipulador de sinal ao SIGINT signal(SIGINT, signalHandler); + CarControl car; + car.start(); + + std::this_thread::sleep_for(std::chrono::seconds(10)); // Exemplo de execução + + car.stop(); + return 0; +} + +/* int main() { + // Conectar o manipulador de sinal ao SIGINT + signal(SIGINT, signalHandler); + try { FServo servo; BackMotors motors; @@ -59,4 +73,4 @@ int main() { } return 0; -} +} */