Swarm Simulation

April 2022 - December 2022

Create a simulator for swarm using socket programming (Github link)


Overview

The project aims to create a simulator for swarm robots. The project involves implementing the existing API's functionality by creating a custom API that interfaces with the simulator. The project uses socket programming and client - server architecture to launch several instances of the robot as processes, that can communicate with a central server simulator. This central simulator keeps track of time, dynamic model of the robots, and integrates the world. There is also visualization using pygame that occurs in a seperate process.

Download and run

Clone the repository in a folder. Go inside sim_pkg folder. Run python3 coachbot-simulation.py -fn userfile in sim_pkg folder. The user.py file should be in sim_pkg folder. The user file name should be given without .py extension in the above command.

This should launch the simulation with the mentioned number of robots running the algorithm given in the user program. The simulation environment can be set by setting the variables in config.json:

  1. TIME_ASYNC - 0 to make the robot time synced and 1 to introduce time asynchronous initialization for robots.
  2. NUMBER_OF_ROBOTS - Number of robots to launch
  3. COMM_RANGE - The range of communication (in meters)
  4. PACKET_SUCCESS_PERC - Set the success rate of message packets (values in decimal range from 0 to 1)
  5. REAL_TIME_FACTOR - Maximum allowable real time factor
  6. NUM_OF_MSGS - Maximum number of messages to keep in the buffer
  7. MSG_SIZE - Maximum size of each message in buffer
  8. WIDTH - width of the arena (vertical axis)
  9. LENGTH - length of the arena (horizontal axis length)
  10. USE_INIT_POS - use the initialization python program to initialize position of the robot
  11. SIM_TIME_STEP - Set the simulation time step for each loop cycle
  12. USE_VIS - If 1, then visualizer will be used. If 0, then visualizer will not be used and instead all states and the time are logged automatically to sim.log

Structure

The main files include the following:

  1. simulator.py - The main simulator file. This communicates with all the robots and keeps track of the messages and state of the world.
  2. robot_class.py - Implements the robot API of the real world robots for this simulation with communication protocols set using socket programming
  3. visualization.py - uses pygame to visualize the robots
  4. user.py - user code written by user to run
  5. coachbot-simulation.py - launches all the processes and programs correctly, using the config.json
  6. bootloader.py - launches the user code with the custom coachbot class
Workflow

Current program examples

Tactile setup

Future work

  1. Collision detection currently is done through a brute force solution and can create a bottleneck with large number of robots. A more effecient algorithm for collision detection can speed up the simulation.
  2. The visualization could be a bottleneck with large number of robots. Data is sent from the simulator every 0.05 seconds to the visualizer thread through sockets. It will wait to send all the data. One can increase the time threshold if it is a bottleneck. The visualization code can also be made more efficient by using Sprite and Dirty Sprite and selective update of the screen.

Link for Github

For more, please check this out