This document provides a detailed explanation of the components, methods, and logic implemented in the Drone Fuzzy Control System.
This project implements a fuzzy logic control system for managing the altitude of a drone. It uses fuzzy models, MQTT communication, and a Streamlit interface to allow real-time data visualization and interaction.
src/
├── data/
│ └── posicao_atual.txt # Stores current drone position
├── fuzzy/
│ ├── models/
│ │ └── fuzzy_models.py # Fuzzy models for control system
│ ├── utils/
│ │ └── fuzzy_utils.py # Utility functions for fuzzy logic
│ └── fuzzy_control.py # Implements the FuzzyControl class
├── interface/
│ └── streamlit_app.py # Streamlit app for UI
├── mqtt/
│ └── mqtt_client.py # MQTT client for communication
└── main.py # Main entry point for running the app
File: fuzzy/models/fuzzy_models.py
The FuzzyModels
class defines fuzzy logic models and membership functions.
__init__()
: Initializes fuzzy variablesErro
,dErro
, andPotenciaMotor
.pertinence()
: Defines membership functions for input and output variables.pertinence_error_plot()
: Visualizes membership functions forErro
.pertinence_derror_plot()
: Visualizes membership functions fordErro
.pertinence_potencia_motor_plot()
: Visualizes membership functions forPotenciaMotor
.pertinence_plots()
: Displays all pertinence plots.rules()
: Defines a set of fuzzy control rules.
This class is used to create fuzzy models and define control rules for the drone's altitude management.
File: fuzzy/fuzzy_control.py
The FuzzyControl
class applies fuzzy logic to compute altitude changes based on the current state of the drone.
__init__()
: Initializes the control system usingFuzzyModels
.infer_rules()
: Generates a DataFrame of fuzzy rules for visualization.Subir_e_Descer(Pos_Final)
: Calculates the drone's position using fuzzy logic.set_home()
: Sets the current position as the "home" position.go_to_home()
: Moves the drone to the home position.ligar(altura)
: Takes off the drone to a specified altitude.
This class is responsible for handling the drone's movement logic, computing errors, and managing control rules.
File: interface/streamlit_app.py
The StreamlitApp
class provides a user interface for interacting with the drone control system.
__init__(mqtt_client, fuzzy_control)
: Initializes the app with an MQTT client and fuzzy control object.run()
: Runs the Streamlit app with tabs for sending and visualizing data.send_data_tab()
: Allows users to input altitude and send data to the drone.visualize_data_tab()
: Visualizes pertinence functions and rules in a tabular format.
This class provides an interactive interface for monitoring and controlling the drone.
File: mqtt/mqtt_client.py
The MQTTClient
class handles MQTT communication for publishing and subscribing to topics.
__init__(broker, topics)
: Initializes the MQTT client with a broker and topics.connect()
: Connects to the broker and subscribes to topics.on_message(client, userdata, msg)
: Processes incoming messages.publish(topic, data)
: Publishes data to a specified topic.
This class enables communication between the fuzzy control system and the drone via MQTT.
File: mqtt/monitor_mqtt.py
This script monitors MQTT topics and prints received messages.
on_connect(client, userdata, flags, rc)
: Subscribes to topics upon connection.on_message(client, userdata, message)
: Decodes and displays messages.on_disconnect(client, userdata, rc)
: Attempts to reconnect when disconnected.
File: mqtt/mqtt_test_data.py
This script publishes random test data to MQTT topics for testing.
generate_random_data(topic)
: Generates random data for altitude and error topics.main()
: Publishes data to topics at regular intervals.