Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.
Merged

Dev #52

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions pid_control/Makefile → complete_route/Makefile
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
# Compilador e flags
# Compiler and flags
CC = g++
CFLAGS = -Wall -Wextra -Werror -std=c++17 -O3 -g -Iincludes `pkg-config --cflags opencv4` -MMD
LDFLAGS = -lSDL2 -li2c -lpthread `pkg-config --libs opencv4` -lstdc++fs -lrt

# Diretórios
# Directories
SRCDIR = sources
APPDIR = apps
OBJDIR = build
BINDIR = bin

# Fontes por módulo
# Sources by module
SRC_TEST_PID = $(APPDIR)/main.cpp $(SRCDIR)/pid_controller.cpp $(SRCDIR)/jetracer.cpp $(SRCDIR)/i2c_device.cpp $(SRCDIR)/computer_vision.cpp

# Objetos gerados
# Generated objects
OBJ_TEST_PID = $(patsubst %.cpp,$(OBJDIR)/%.o,$(notdir $(SRC_TEST_PID)))

# Executáveis
# Executables
EXEC_TEST_PID = $(BINDIR)/jetracer_pid_controler

# Alvo principal
# Main target
all: $(EXEC_TEST_PID)

# Regras para executáveis
# Rules for executables
$(EXEC_TEST_PID): $(OBJ_TEST_PID) | $(BINDIR)
#$(CC) $(CFLAGS) -o $@ $(addprefix $(OBJDIR)/,$(notdir $^)) -lSDL2 -lpthread `pkg-config --libs opencv4` -lstdc++fs
$(CC) $(CFLAGS) -o $@ $(addprefix $(OBJDIR)/,$(notdir $^)) $(LDFLAGS)

# Regra para arquivos objeto
# Rule for object files
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp | $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@

$(OBJDIR)/%.o: $(APPDIR)/%.cpp | $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@

# Cria diretório build/ se necessário
# Create build/ directory if needed
$(OBJDIR):
mkdir -p $(OBJDIR)

# Cria bin/ se necessário
# Create bin/ directory if needed
$(BINDIR):
mkdir -p $(BINDIR)

# Comandos auxiliares
# Auxiliary commands
clean:
rm -rf $(OBJDIR) $(BINDIR)

re: clean all

# Alvos para rodar específicos
# Targets to run specific executables
testpid: $(EXEC_TEST_PID)

.PHONY: all clean re control video frames lane frameslane oneframe testpid run

# Inclui dependências geradas automaticamente
# Include automatically generated dependencies
-include $(OBJDIR)/*.d
89 changes: 89 additions & 0 deletions complete_route/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# PID Control + JetRacer Vision

C++ implementation of lane detection, PID control, and hardware interface for the JetRacer car, integrated with a Python pipeline that writes camera segmentation masks to shared memory.

> **Requirements**
> • CMake ≥ 3.18 • OpenCV ≥ 4.5 • SDL2 • Linux I2C (i2c-dev) • Python ≥ 3.9 (for camera_yolo_to_shm.py)

---

## Repository Structure

apps/ # example executables
└─ main.cpp # PID with joystick + shared memory
includes/jetracer/ # public headers (*.hpp)
sources/ # C++ implementations (*.cpp)
models/ # YOLO / LaneNet model (.pt)
scripts/ # Python utilities
docs/ # Markdown documentation + diagrams
Makefile # default target: make && make run

---

## Main Modules

| Module | Description | Docs |
| ------------------------ | -------------------------------------------- | ---------------------------------------------------------------------------- |
| `computer_vision` | Lane extraction, center calculation, overlay | [`docs/docs_computer_vision_module.md`](docs/docs_computer_vision_module.md) |
| `pid_controller` | Simplified PID with anti-wind-up | [`docs/docs_pid_module.md`](docs/docs_pid_module.md) |
| `control` (`JetRacer`) | PWM, servo, motors, joystick handling | [`docs/docs_control_module.md`](docs/docs_control_module.md) |
| `hardware` (`I2CDevice`) | RAII wrapper for `/dev/i2c-X` | [`docs/docs_hardware_module.md`](docs/docs_hardware_module.md) |
| `pwm_config` | Advanced PWM frequency and smoothing configs | [`docs/docs_pwm_improvements.md`](docs/docs_pwm_improvements.md) |
| `motor_control` | Advanced motor control and torque management | [`docs/docs_motor_control_improvements.md`](docs/docs_motor_control_improvements.md) |

---

## Execution

```bash
# Terminal 1: producer writes lane masks to shared memory
python3 scripts/camera_yolo_to_shm.py

# Terminal 2: run the C++ controller
./bin/jetracer_pid_controler
```

## Testing

```bash
# Compile and test manually
make clean && make
./bin/jetracer_pid_controler
```

---

## Execution Flow (Simplified)

```mermaid
graph TD
P1["Python – YOLO mask"] --> M1["/dev/shm/mask_shared"]
M1 --> C1["apps/main.cpp"]
C1 --> V1["computer_vision"]
V1 --> PID1["pid_controller"]
PID1 --> J1["JetRacer::smooth_steering"]
```

---

## PWM Improvements

The system now includes advanced PWM improvements to eliminate motor speed pulsation:

- **Higher PWM Frequency**: Increased from 100Hz to 1000Hz (configurable up to 2000Hz)
- **Speed Smoothing**: Moving average filter with configurable smoothing window
- **Rate Limiting**: Maximum speed change per update to prevent sudden movements
- **Configurable Parameters**: Easy adjustment for different use cases

## Motor Control Improvements

Advanced motor control system to eliminate "force to start" sound and improve acceleration:

- **Intelligent Power Curve**: Exponential power amplification for better low-speed response
- **Minimum PWM Threshold**: Guaranteed initial torque to eliminate startup resistance
- **Torque Amplification**: Boost for low-speed PWM values
- **Smart Acceleration Ramp**: Different rates for acceleration vs. deceleration
- **Emergency Braking**: Intelligent detection and response to sudden changes
- **Deadzone Control**: Eliminates oscillations at very low speeds

See [`docs/docs_motor_control_improvements.md`](docs/docs_motor_control_improvements.md) for detailed configuration options.
18 changes: 16 additions & 2 deletions pid_control/apps/main.cpp → complete_route/apps/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// File: sources/main.cpp
#include "jetracer/pid_controller.hpp"
#include "jetracer/jetracer.hpp"
#include "jetracer/computer_vision.hpp"
Expand All @@ -19,7 +18,8 @@ jetracer::control::JetRacer *jetracer_ptr = nullptr;

void signal_handler(int)
{
std::cout << std::endl << "[!] Ctrl+C detected. Stopping the JetRacer..." << std::endl;
std::cout << std::endl
<< "[!] Ctrl+C detected. Stopping the JetRacer..." << std::endl;
if (jetracer_ptr)
jetracer_ptr->stop();
std::_Exit(0);
Expand All @@ -35,6 +35,14 @@ int main()
jetracer_ptr = &jetracer;

signal(SIGINT, signal_handler);

jetracer.set_constant_speed_mode(false);

std::cout << "\n=== JOYSTICK MODE ACTIVATED ===" << std::endl;
std::cout << "Joystick control enabled" << std::endl;
std::cout << "Maximum speed limited to 27% (ideal configuration)" << std::endl;
std::cout << "Use the left joystick to control speed" << std::endl;

jetracer.start();

int shm_fd = shm_open("mask_shared", O_RDWR, 0666);
Expand All @@ -57,6 +65,12 @@ int main()
cv::Mat mask(jetracer::vision::HEIGHT, jetracer::vision::WIDTH, CV_8UC1, img_ptr);
int frame_id = 0;

std::cout << "\n=== MAIN SYSTEM RUNNING ===" << std::endl;
std::cout << "Joystick control active" << std::endl;
std::cout << "Maximum speed limited to 27% (ideal configuration)" << std::endl;
std::cout << "Use the left joystick to control speed" << std::endl;
std::cout << "Use Ctrl+C to stop the system at any time" << std::endl;

while (true)
{
if (flag_ptr[0] != 1)
Expand Down
Binary file added complete_route/bin/jetracer_pid_controler
Binary file not shown.
113 changes: 113 additions & 0 deletions complete_route/build/computer_vision.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
build/computer_vision.o: sources/computer_vision.cpp \
includes/jetracer/computer_vision.hpp \
/usr/include/opencv4/opencv2/opencv.hpp \
/usr/include/opencv4/opencv2/opencv_modules.hpp \
/usr/include/opencv4/opencv2/core.hpp \
/usr/include/opencv4/opencv2/core/cvdef.h \
/usr/include/opencv4/opencv2/core/version.hpp \
/usr/include/opencv4/opencv2/core/hal/interface.h \
/usr/include/opencv4/opencv2/core/cv_cpu_dispatch.h \
/usr/include/opencv4/opencv2/core/base.hpp \
/usr/include/opencv4/opencv2/core/cvstd.hpp \
/usr/include/opencv4/opencv2/core/cvstd_wrapper.hpp \
/usr/include/opencv4/opencv2/core/neon_utils.hpp \
/usr/include/opencv4/opencv2/core/vsx_utils.hpp \
/usr/include/opencv4/opencv2/core/check.hpp \
/usr/include/opencv4/opencv2/core/traits.hpp \
/usr/include/opencv4/opencv2/core/matx.hpp \
/usr/include/opencv4/opencv2/core/saturate.hpp \
/usr/include/opencv4/opencv2/core/fast_math.hpp \
/usr/include/opencv4/opencv2/core/types.hpp \
/usr/include/opencv4/opencv2/core/mat.hpp \
/usr/include/opencv4/opencv2/core/bufferpool.hpp \
/usr/include/opencv4/opencv2/core/mat.inl.hpp \
/usr/include/opencv4/opencv2/core/persistence.hpp \
/usr/include/opencv4/opencv2/core/operations.hpp \
/usr/include/opencv4/opencv2/core/cvstd.inl.hpp \
/usr/include/opencv4/opencv2/core/utility.hpp \
/usr/include/opencv4/opencv2/core/optim.hpp \
/usr/include/opencv4/opencv2/core/ovx.hpp \
/usr/include/opencv4/opencv2/core/cvdef.h \
/usr/include/opencv4/opencv2/calib3d.hpp \
/usr/include/opencv4/opencv2/features2d.hpp \
/usr/include/opencv4/opencv2/flann/miniflann.hpp \
/usr/include/opencv4/opencv2/flann/defines.h \
/usr/include/opencv4/opencv2/flann/config.h \
/usr/include/opencv4/opencv2/core/affine.hpp \
/usr/include/opencv4/opencv2/dnn.hpp \
/usr/include/opencv4/opencv2/dnn/dnn.hpp \
/usr/include/opencv4/opencv2/core/async.hpp \
/usr/include/opencv4/opencv2/dnn/../dnn/version.hpp \
/usr/include/opencv4/opencv2/dnn/dict.hpp \
/usr/include/opencv4/opencv2/dnn/layer.hpp \
/usr/include/opencv4/opencv2/dnn/dnn.inl.hpp \
/usr/include/opencv4/opencv2/dnn/utils/inference_engine.hpp \
/usr/include/opencv4/opencv2/dnn/utils/../dnn.hpp \
/usr/include/opencv4/opencv2/flann.hpp \
/usr/include/opencv4/opencv2/flann/flann_base.hpp \
/usr/include/opencv4/opencv2/flann/general.h \
/usr/include/opencv4/opencv2/flann/matrix.h \
/usr/include/opencv4/opencv2/flann/params.h \
/usr/include/opencv4/opencv2/flann/any.h \
/usr/include/opencv4/opencv2/flann/defines.h \
/usr/include/opencv4/opencv2/flann/saving.h \
/usr/include/opencv4/opencv2/flann/nn_index.h \
/usr/include/opencv4/opencv2/flann/result_set.h \
/usr/include/opencv4/opencv2/flann/all_indices.h \
/usr/include/opencv4/opencv2/flann/kdtree_index.h \
/usr/include/opencv4/opencv2/flann/dynamic_bitset.h \
/usr/include/opencv4/opencv2/flann/dist.h \
/usr/include/opencv4/opencv2/flann/heap.h \
/usr/include/opencv4/opencv2/flann/allocator.h \
/usr/include/opencv4/opencv2/flann/random.h \
/usr/include/opencv4/opencv2/flann/kdtree_single_index.h \
/usr/include/opencv4/opencv2/flann/kmeans_index.h \
/usr/include/opencv4/opencv2/flann/logger.h \
/usr/include/opencv4/opencv2/flann/composite_index.h \
/usr/include/opencv4/opencv2/flann/linear_index.h \
/usr/include/opencv4/opencv2/flann/hierarchical_clustering_index.h \
/usr/include/opencv4/opencv2/flann/lsh_index.h \
/usr/include/opencv4/opencv2/flann/lsh_table.h \
/usr/include/opencv4/opencv2/flann/autotuned_index.h \
/usr/include/opencv4/opencv2/flann/ground_truth.h \
/usr/include/opencv4/opencv2/flann/index_testing.h \
/usr/include/opencv4/opencv2/flann/timer.h \
/usr/include/opencv4/opencv2/flann/sampling.h \
/usr/include/opencv4/opencv2/highgui.hpp \
/usr/include/opencv4/opencv2/imgcodecs.hpp \
/usr/include/opencv4/opencv2/videoio.hpp \
/usr/include/opencv4/opencv2/imgproc.hpp \
/usr/include/opencv4/opencv2/./imgproc/segmentation.hpp \
/usr/include/opencv4/opencv2/ml.hpp \
/usr/include/opencv4/opencv2/ml/ml.inl.hpp \
/usr/include/opencv4/opencv2/objdetect.hpp \
/usr/include/opencv4/opencv2/objdetect/aruco_detector.hpp \
/usr/include/opencv4/opencv2/objdetect/aruco_dictionary.hpp \
/usr/include/opencv4/opencv2/objdetect/aruco_board.hpp \
/usr/include/opencv4/opencv2/objdetect/graphical_code_detector.hpp \
/usr/include/opencv4/opencv2/objdetect/detection_based_tracker.hpp \
/usr/include/opencv4/opencv2/objdetect/face.hpp \
/usr/include/opencv4/opencv2/objdetect/charuco_detector.hpp \
/usr/include/opencv4/opencv2/objdetect/barcode.hpp \
/usr/include/opencv4/opencv2/photo.hpp \
/usr/include/opencv4/opencv2/stitching.hpp \
/usr/include/opencv4/opencv2/stitching/warpers.hpp \
/usr/include/opencv4/opencv2/stitching/detail/warpers.hpp \
/usr/include/opencv4/opencv2/core/cuda.hpp \
/usr/include/opencv4/opencv2/core/cuda_types.hpp \
/usr/include/opencv4/opencv2/core/cuda.inl.hpp \
/usr/include/opencv4/opencv2/stitching/detail/warpers_inl.hpp \
/usr/include/opencv4/opencv2/stitching/detail/warpers.hpp \
/usr/include/opencv4/opencv2/stitching/detail/matchers.hpp \
/usr/include/opencv4/opencv2/stitching/detail/motion_estimators.hpp \
/usr/include/opencv4/opencv2/stitching/detail/matchers.hpp \
/usr/include/opencv4/opencv2/stitching/detail/util.hpp \
/usr/include/opencv4/opencv2/stitching/detail/util_inl.hpp \
/usr/include/opencv4/opencv2/stitching/detail/camera.hpp \
/usr/include/opencv4/opencv2/stitching/detail/exposure_compensate.hpp \
/usr/include/opencv4/opencv2/stitching/detail/seam_finders.hpp \
/usr/include/opencv4/opencv2/stitching/detail/blenders.hpp \
/usr/include/opencv4/opencv2/stitching/detail/camera.hpp \
/usr/include/opencv4/opencv2/video.hpp \
/usr/include/opencv4/opencv2/video/tracking.hpp \
/usr/include/opencv4/opencv2/video/background_segm.hpp
Binary file added complete_route/build/computer_vision.o
Binary file not shown.
2 changes: 2 additions & 0 deletions complete_route/build/i2c_device.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/i2c_device.o: sources/i2c_device.cpp \
includes/jetracer/i2c_device.hpp
Binary file added complete_route/build/i2c_device.o
Binary file not shown.
3 changes: 3 additions & 0 deletions complete_route/build/jetracer.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/jetracer.o: sources/jetracer.cpp includes/jetracer/jetracer.hpp \
includes/jetracer/i2c_device.hpp includes/jetracer/pwm_config.hpp \
includes/jetracer/motor_control.hpp
Binary file added complete_route/build/jetracer.o
Binary file not shown.
Loading