Creating Your Own Algorithm
If you have a custom algorithm that you would like to run on a SWARM Node, you can do so by following the instructions below. These instructions assume that you are somewhat familiar with the SWARM RDS Client and have a basic understanding of how the system works.
Step 1
First, you will need to create a new folder called user_code in the SWARM-RDS-Client repository if there isn't one already there.
Step 2
Inside of this folder, you need to create a new folder that is exactly the name of the agent (ie. drone, vehicle, etc.) that will be used in the Simulation. For example, if I wanted to create a vehicle called Drone1, then I would create a folder called Drone1 inside of the user_code folder. Your file structure should look as follows:
Step 3
Next, you will create a new python file (.py) that must be the exact name of the Class that you will create. For example, if I had an algorithm called MyAlgorithm, then I would create a file called MyAlgorithm.py inside of the Drone1 folder. Your file structure should look as follows:
Step 4
Now, you will need to create your algorithm. We have provided a template below that you can use to get started. You can also find this template in the SWARMRDS/utilities folder with the file name algorithm_template.py.
There are only two requirements for your algorithm:
1. You must inherit from the Algorithm class that exists in utilities/algorithm_utils.py. Please reference the template File
for how to import this class into your file.
2. You must implement the run method. This method is called every time the SWARM Node is updated. You can use this method to
complete any computation that you want. The return of this method is specific to the settings that you provide (ie. what values you tell the
SWARM Node to publish). You can find more information about the settings here: Settings.
Please refer to the file "SWARMRDS/utilities/data_classes.py" to see what data types are availabe in your algorithm and how to work with them.
Step 5
Now, you will need to configure your SWARM Simulations File so that it can work with PX4. We have provided an example file below and this file is also in the SWARM Developer Client, in the "settings" folder, titled CustomAlgorithmExampleSimulation.json.
Here are some key points of the settings file shown above:
- First, you will notice that we create our algorithm in the module with the name HighLevelBehavior. This will load in our module and handle everything, including receiving and publishing messages.
- Next, you will notice that the Algorithm structure has a field "Level", which is set to 3. This must be set to 3 so that your algorithm will be loaded appropriately.
Step 5
Now, you are ready to run the simulation. Go to the root of the SWARM Developer Client and type in the following information, assuming that you are using the provided CustomAlgorithmExampleSimulation.json file provided in the repo. Note: You do NOT need .json on the end of the file names.
You will see that the simulation file is being validated. Once the system has uploaded your configuration
file, you will see the following output.
The SWARM RDS Server will provide your algorithm with a set of fake
data to validate that your algorithm will actually run. If your algorithm does not run, then you will see
an error message in the console.
About Validation
The SWARM RDS Client will validate your algorithm before submitting it to the SWARM RDS Server. This validation process will ensure that your algorithm will run on the SWARM RDS Server. If you have any errors in your algorithm, then you will see an error message in the console. If you have any warnings, then you will see a warning message in the console. If you have no errors or warnings, then you will see a success message in the console.
Each "validation" run takes an example set of data (ex. a real image) and provides that as part of the **kwargs arguement to the "run" function of your algorithm. Then, the validation system looks at the outputs. The ONLY aspect that is checked about your algorithm is that the function runs and produces the correct number of outputs. It DOES NOT check that right type of outputs (as you could return None if you wanted to). If you see errors becasue of this, then you need to validate your algorithm using our "AlgorithmTester.py" script available for you in the "user_code" folder.
Step 6
You can visualize the simulator ouptut by starting your favorite Web Browser by either typing in "127.0.0.1" if you are using the local edition or clicking "Launch Streamer" on the RDS CLient application. You will see a gray screen, like below. Click on the
screen to start the visualization.
The agent should move along the path defined in "settings/DefaultTrajectory.json".
Step 7 - View the Output
Once the simulation has completed, you can choose to download the data that has been collected by each agent.
Each module has a separate log file that is collected and provided to you for each agent in the system. Simply go to the "data" folder and looks at the "logs" folder to view the output log. Please view the ExampleAlgorithm.py file to see how to log data using our logging system.
Walkthrough
Coming soon, we will have a video describing setup with the GUI application.