Advanced mavlink-kotlin - Part 4: Example micorservices
We have finally reached the last part of the series. In this part, we will look at some examples of how to use the API
that we have created thus far to implement MAVLink microservices.
I will be implementing them as simple final classes, but in a production grade application, you would want to properly
define your services as interfaces and implement them in a separate class. Also, you should use a dependency injection
framework like Koin or Dagger/Hilt to provide the dependencies to the services, repositories, view models, etc.,
based on the architecture of your application.
TelemetryService
In this class, we have a Flow for the armed state. We are filtering the messages from the FCU for the Heartbeat
messages and mapping them to the respective values by using the Kotlin Coroutines library functions.
We can also break down a bigger service like requestDataStreams into smaller suspend functions to make the code
easier to handle.
CalibrationService
Let's use this example to look at some of the concepts that we have learned in the previous parts of the series.
The withTimeout function is used to set a timeout of 5 seconds for the calibration process.
We are using the receive function to get the GpsRawInt message from the FCU.
If the GPS is not in the correct state, we will throw a MavException with an appropriate message to cancel the
calibration.
We are using the sendCommandLong function to send the calibration command to the FCU.
The throwIfFailure function is used to throw an exception if the command fails.
The CalibrationService can be used as follows.
Conclusion
In this series, we have learned how to create a MAVLink API using Kotlin. We have also learned how to use the API to
implement MAVLink microservices. Though the examples are simple, you can use the concepts to create more complex
services.
I hope you have enjoyed the series and learned something new. Even if you are not working with MAVLink, you can use the
concepts to architect solutions like this for similar systems.