Author Transforms with Flex
The previous tutorials leveraged message-to-message transforms that are included with Tangram Pro™. But, if a component software interface needs a transform that is currently not included, a Tangram Pro™ user can define their own with Flex. In the broadest terms, a transform is like a function that accepts a data structure as input, modifies its values, and outputs a new data structure.
Flex Language Documentation is available at Flex Docs. For this tutorial, example Flex code is provided.
You'll learn how to:
- Add support for your own transform using the Flex Code Editor
Step 1: Create a Flex Package
- Go to FlexLang in the top navigation bar
- Click on Package Manager and then the blue New button
- Enter a name for your package using the
Namespace::PackageName
format (e.g.Tutorial::Transforms
), and choose an owner - Click Create Package and wait for the Flex environment to load
- Create a new file by hovering over your package in the left side file viewer, clicking the
File
icon (the left icon next to your package) - Enter the name "MyTransform.flex" and hit enter
- Paste the following code below the module name:
message struct PositionUS {
latitudeDegrees : float64;
longitudeDegrees : float64;
altitudeFeet : float32;
}
message struct PositionMetric {
latitudeRadians : float64;
longitudeRadians : float64;
altitudeMeters : float32;
}
transform US_to_Metric(i : PositionUS) -> PositionMetric {
let pi : float64 = 3.14159265359;
PositionMetric {
latitudeRadians = pi * (i.latitudeDegrees / 180.0);
longitudeRadians = pi * (i.longitudeDegrees / 180.0);
altitudeMeters = i.altitudeFeet / 3.28084;
};
}
The first message
PositionUS
represents values in degrees and feet. The second messagePositionMetric
represents values in radians and meters. The transformUS_to_Metric
defines the relationship between these messages, takingPositionUS
as input and outputtingPositionMetric
. Areturn
keyword doesn't need to be included because the last expression of a transform is implicitly returned.
- Save the file by pressing the hot-key
Ctrl + S
/Cmd + S
. - Create a release of your new package by clicking the Create Release checkmark button above your package name in the left panel and then choosing Create Release r1 at the bottom right
Step 2: Use Transform in a Design
For a refresher on building a component-based design, go to Visualize a Project Design.
- Create a new project
- Add two components and update their names to be:
PositionUS
PositionMetric
- Add a transform object and place it anywhere
- Define the connection by clicking on
PositionUS
first, and thenPositionMetric
- For the transform's
Input Message
, select your custom package and messagePositionUS
- For the transform's
Output Message
, select your custom package and messagePositionMetric
- Click the Apply Transform button. This is your Flex Transform!
Step 3: Generate Transform Code
- Click Build in your project
- Select the Transform item in the list
- Choose Generate Transform in its dropdown menu
- Click Build
- Download the generated code. Extract and open the folder.
- Open the
gen
folder. You should see a file calledMyTransform.cpp
. This was generated as a result of the transform that you defined in your custom Flex package. - Open the
MyTransform.cpp
file. Take a look at the code and see that there is a C++ function calledUS_to_Metric
. This is your Flex transform function transpiled into C++.
Further Exploration
- Go to FlexLang, view the Package Manager, and open the package called
Transforms::LmcpV3MavlinkV23::v1
. Review each of its Flex files to see additional transform examples. These transforms convert LMCP messages to MAVLink messages. - Open your Flex file, and add another transform to convert in the other direction "Metric_to_US". This time converting radians to degrees, and meters to feet. Publish the changed Flex specification. Update your components to use the new version and change the In/Out directions. Delete the transform between the components, and reconnect them in the other direction. Generate Transform code and see how the changes you made to Flex affect the resulting C++ function.
Cheers!
By completing this tutorial you learned how to:
- Use Tangram Pro™ to define and use a custom transform definition to build component-based systems.
- Generate transform code from a custom Flex package.