# FlockAI: A Testing Suite for ML-Driven Drone Applications
## 4 Modeling and Implementation
### Drone Configuration
```
1 fromflockai.drones.controllersimportFlockAIController
2...
3 classDJIMavic2(FlockAIController);
4
5 def_init_(self,name=\"DJIMavic2\",...):
6 self.drone=configDrone(name,...)
7...
8
9 defconfigDrone(name,...):
10 mators=[
11(MotorDevice.CAMERA,\"cameraroll\",AircraftAxis.ROLL),
12(MotorDevice.CAMERA,\"camerapitch\",AircraftAxis.PITCH),
13(MotorDevice.CAMERA,\"camerayaw\",AircraftAxis.YAW),
14(MotorDevice.PROPELLER,\"FLDropeller\",Relative2 DPosition(1,-1)),
15(MotorDevice.PROPELLER,\"FRpropylene\",Relative2 DPosition(1,1)),
16(MotorDevice.PROPELLER,\"RLDropeller\",Relative2 DPosition(-1,-1)),
17(MotorDevice.PROPELLER,\"RRpropeller\",Relative2 DPosition(-1,1)),
18]
19 sensors=[
20(EnableableDevice.CAMERA,\"camera\"),
21(EnableableDevice.GPS,\"gps\"),
22(EnableableDevice.INERTIAL_UNIT,\"inertial\"),
23(EnableableDevice.COMPASS,\"compass\"),
24(EnableableDevice.GMO,\"ggy\")
25(EnableableDevice.BATTERN_ESNOR,\"battery\"),
26(EnableableDevice.WIFIRECEIVER,\"receiver\"),
27...
28]
29 mavic2=FlockAIDrone(motors,sensors)
30 mavic2.weight=0.907#inkg
31 mavic2.battery=3850#inmAh
32 mavic2.storage=8000#inHS
33...
34 returnmavic2
``````
1 fromflockai.drones.controllersimportDJIMavic2
2 fromflockai.drones.pilotsimportBoundingBoxZigZagPilot
3 fromflockai.drones.monitoringimportFlightProbe,EnergyProbe
4 fromflockai.drones.sensorsimportTempSensor
5 fromClassifiersimportCrowdDetectionClassifier
6...
7 defMybronSLEExperiment():
8 autopilot=BoundingBoxZigZagPilot(...)
9 probes=[FlightProbe(..),EnergyProbe(..)]
10
11 mydrone=DJIMavic2(pilot-autopilot,probes=probes)
12 mydrone.getBattery().setCapacity(4500)
13
14 temp=TempSensor(trange=(20,32),dist=gaussian)
15 mydrone.addSensor(temp)
16
17 crowdetect=CrowdDetectionClassifier()
18 crowdetect.load_model('.../cnn_cd.bin')
19 mydrone.enableML(algo=crowdetect,inf_period=5,...)
20
21 mydrone.activate()#startsimulation
22 mydrone.takeoff()
23
24 whilemydrone.fliping():
25 img=mydrone.getCameraFeed(...)
26 loc=mydrone.getGPScoordinates(...)
27 testdata=(img,loc)
28 pred=mydrone.classifier.inference(data=testdata,...)
29#do somethingwithpred...andoutputenergyandbatterylevel
30 eTotal=mydrone.getProbe(\"EnergyProbe\").values().get(\"total\")
31 battery=mydrone.battery.getLevel()
32 print(eTotal,battery)
33...
34 out=mydrone.simulation.exportLogs(format_type=pandas_df)
35#dostufftooutput
```

Listing 2: Exemplary FlockAI Experiment Blueprint

### ML Algorithm Deployment

Once a drone is configured and ready to fly in a robotics simulator, new challenges arise for the ML practitioner and must be dealt with. These, at best, are three-fold: (i) a training model must be on-boarded to the drone; (ii) runtime data from the drone's sensors (e.g., camera) must be periodically (or on-demand) made available and wrangled to meet a certain format; and (iii) an algorithm must be configured so that ML inference is performed at runtime based on the harvested data and the deployed training model. FlockAI attempts to overcome the burden when faced with the aforementioned challenges by simplifying the deployment of ML models, easing the extraction of sensing data, the passing of the data to the inference algorithm and finally performing inference and formatting the response for subsequent use. Most importantly, FlockAI features a number of class abstractions for ML. Continuing on the running example from the previous listings, Listing 3 shows an example of how a user can extend the FlockAIClassifier class so their ML solution will be deployed with FlockAI. In its most simplistic form, the classifier provides the means to load a fixed training model and apply the inference algorithm. The training model that will be deployed on the (simulated) drone is given in binary format. This format is one of the prominent export formats used in frameworks such as TensorFlow to enable interoperable exchange of ML trained models. Hence, no additional effort is required for a ML model to be deployed through FlockAI.