A Processing simulator of a differential line-following robot with traditional control (PID), machine learning (recurrent neural networks), and PSO-based weight optimization (Particle Swarm Optimization). Includes a simplified physical model of the robot, virtual sensors, and an interactive interface for parameter visualization and tuning.
- Differential-drive robot with dynamic and kinematic model
- Odometry and virtual front/side sensors for line detection
- PID control and Recurrent Neural Network (RNN) control
- In-simulation PSO optimization of neural network weights
- Menu interface for adjusting evolutionary parameters and viewing information
- Optional logging of simulation data into .txt files
- Processing 4 (Java Mode)
- Processing Sound library (used for audio feedback)
- Install at: Sketch > Import Library… > Add Library… > search for “Sound” and install “Sound” (Processing Foundation)
Media files (images) are included in the project’s data/ folder.
- Install Processing 4 and the Sound library (see above).
- Open the project in Processing: File > Open… and select this repository’s directory (the
.pdetabs will load automatically). - Make sure the image files exist in
data/:data/Iron Cup 2019.pngdata/Ratão 4.0.png
- Run the sketch (Play button or Ctrl/Cmd + R). The main file is
PSOFollowLineDeepLearning04.pde.
Optional (command line): if you have processing-java installed, you can run:
processing-java --sketch="/path/to/this/project" --run
- S / N: toggle sound on/off
- A / D: toggle trail drawing
- M / U: show/hide multiple “ghost” robots (visual comparison)
- I / O: open PID interface (informational text)
- C: toggle a guiding circle under the mouse
- Right-click: reposition the robot (center)
- Top bar: click to open the menu; hover tabs to display content; in some tabs you can drag sliders to adjust parameters
The top menu has tabs:
- Genetic Algorithm / PSO: displays and allows tuning of evolutionary parameters
- Population size, mutation rate, and fitness function weights (stability, speed, distance)
- Physics: displays physical parameters (mass, inertia, gravity)
- PID Control: shows current gains of the outer control loop
- Neural Network: shows network input/hidden/output sizes
Notes:
- PSO optimization runs in a background thread (
trainNet) during execution and periodically updates the best weights. - Metrics such as “generation”, “fitness”, etc. are printed to the Processing console and shown in the menu.
Main configurable points are in the setup() of PSOFollowLineDeepLearning04.pde:
- Initial PID gains:
m[0].setPID(200, 0, 8) m[0].malha_ext_esq = new Control(200, 0, 8); m[0].malha_ext_dir = new Control(200, 0, 8);
- Neural network (sizes):
int[] deepLearning = {11, 5, 2}; // input, hidden, output int[] temporalLayers = {2, 0, 0}; // temporal layers for RNN
- Dynamics/time:
m[0].setVelMax(3.45); // (m/s) m[0].setVariacaoTempo(1000); // (µs per step)
- Evolution algorithm parameters (class DNA in
AI.pde):tamPopulacao(population size),mutacao(rate), fitness weights (pesoVel,pesoDist,pesoEstabilidade), time and distance limits.
PSOFollowLineDeepLearning04.pde: main sketch; loads track, initializes robot, network, PSO, and simulation loopAI.pde(DNA class): population evaluation, fitness calculation, and PSO iteration over network weightsRobot.pde(Movimento class): robot logic, line reading, PID, neural control, odometry, wheel dynamicsDynamic.pde: robot physics (simplified), velocities, integrationDeep_Learning.pde: implementation of Neural_Network and auxiliary matrix operationsAlgebra.pde: matrix/vector utilitiesHeuristic.pde: PSO implementation (population, individuals, pose updates)Interface.pde: top menu, tabs, and slidersControles.pde: Control class (controller parameters/state) and logsBotoes.pde: button/clickable area components used by the menudata/: static assets (images) used by the sketchcontrole/: optional exported logs (velocity, distance, time) when enabled
When enabled (see Control.save_data), the simulator saves time-series logs to controle/:
vel_linear.txtvel_estrategia.txtdist_percorrida.txttempo.txt
- If the window does not open or sound fails, check if the “Sound” library is installed in Processing 4.
- To speed up the simulation, adjust
velTempoinPSOFollowLineDeepLearning04.pde. - If the robot “disappears” from the track (leaves the image), reposition it with the right mouse button.
Distributed under the MIT license. See the LICENSE file for details.
Author: Jakson Almeida
