Skip to content

Examples

Integration with ROS

In the following example, a basic ROS integration is shown. For the drone environment setup, iq_sim has been employed. Follow the link for a detailed installation tutorial and examples. The chosen environment for this test is the multidrone setup, but this code example should work with any other example.

Python script implementation

The full code for connecting with ROS, reading the quadcopter position, calculating the distance, and estimating the path loss (using the free_space_pl method) is provided below:

#!/usr/bin/env python

import rospy
import numpy as np
from gazebo_msgs.msg import ModelStates
import uav_radio

rospy.init_node('drone_rf_python_script')

class rf_body:
    def __init__(self):
        """
        Reads the table of parameters
        """
        self.calculator = uav_radio.PathLossCalculator(reference_distance=1.0)

    def get_distance_loss(self, d):
        """
        Calculates the forces applied to the quadcopter based on the velocity of the wind and 
        the aerodynamics coefficients.
        Parameters
        ----------
        d : float
            distance in m to the ground station
        Returns
        -------
        float
            returns the spatial loss based on distance
        """
        spatial_loss,_ = self.calculator.free_space_pl(distance=d, frequency=868000000)
        return spatial_loss

    def calc_losses(self, position):
        """
        Calculates the RF losses based on the distance to the ground station
        and the quad attitude.
        Parameters
        ----------
        position : np array [3]
            the position of the copter
        Returns
        -------
        np array [1], np array [1]
            returns the computed directional and space losses
        """

        distance = np.linalg.norm(position) # we calculate the absolute distance between the origin of coordinates and the quadcopter.
        space_loss = self.get_distance_loss(d = distance)

        return np.round(space_loss,3), np.round(distance,3)

class rf():
    """
        Sets the RF model ready for experimentation with the copter.
    """     
    def __init__(self):
        self.frequency = 50.0
        self.rate = rospy.Rate(self.frequency)
        self.sub = rospy.Subscriber("/gazebo/model_states", ModelStates, self.location_callback)
        self.quad_position = np.zeros(3) + 0.01
        self.quad = rf_body()
        self.name = 'drone3'

    def location_callback(self, msg):
        """
        Gets the location of the copter with ROSPY package
        Parameters
        ----------
        msg : gazebo msg
            the gazebo message to be read
        """
        ind = msg.name.index(self.name)
        positionObj = msg.pose[ind].position
        self.quad_position = np.array([positionObj.x, positionObj.y, positionObj.z])

    def rf_signal(self):
        """
        Main point of the algorithm, runs the hexacopter pose, calculates the rf gain,
        and sends it to a node or prints it.
        """

        distance_loss, distance = self.quad.calc_losses(position = self.quad_position)

        print("--------------------------------")
        print("\nDistance: ", distance, "[m]")   
        print("\nPath Loss :", distance_loss, " [dB]")
        self.rate.sleep()

if __name__ == "__main__":
    radio = rf()
    while True:
        radio.rf_signal()         

Running the example

To run the example, we recommend saving the provided code in a new Python file, inside the scripts directory where the iq_sim workspace has been installed. To run this, open a new terminal inside the folder containing the script and type:

python your_script_name.py
The ROS node should start and continuously print the free space path loss value over the terminal.