Skip to content

Commit

Permalink
chore: Update include directories and source file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
BelmiroRodrigues committed Dec 16, 2024
1 parent f181f02 commit ff23893
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 11 deletions.
71 changes: 71 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
6 changes: 2 additions & 4 deletions middleware/inc/BackMotors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
#include <unistd.h> // Para close(), usleep()
#include <fcntl.h> // Para open()
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>// Interface padrão do Linux para I2C

#include <csignal> // Biblioteca para manipulação de sinais

#include <atomic>
#include <chrono> // Para cálculos de tempo
#include <csignal> // Biblioteca para manipulação de sinais
#include <linux/i2c-dev.h>// Interface padrão do Linux para I2C


class BackMotors{
private:
Expand Down
32 changes: 31 additions & 1 deletion middleware/inc/CarControl.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
#pragma once

#include <linux/joystick.h>



class BackMotors;
class FServo;

class CarControl {
private:
BackMotors _backMotors;
FServo _fServo;

std::atomic<bool> _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"
#include "FServo.hpp"
1 change: 0 additions & 1 deletion middleware/inc/FServo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#include <atomic>
#include <chrono> // Para cálculos de tempo
#include <csignal> // Biblioteca para manipulação de sinais

class FServo{
private:
Expand Down
4 changes: 0 additions & 4 deletions middleware/inc/SpeedSensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
#include <string>
#include <chrono>

#include <atomic>
#include <thread>
#include <functional>
#include <string>
#include <fstream>
#include <csignal>

class SpeedSensor {
Expand Down
87 changes: 87 additions & 0 deletions middleware/src/CarControl.cpp
Original file line number Diff line number Diff line change
@@ -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++;
}
}

16 changes: 15 additions & 1 deletion middleware/src/main.cpp
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -59,4 +73,4 @@ int main() {
}
return 0;
}
} */

0 comments on commit ff23893

Please sign in to comment.