Skip to main content

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 Your Own Transform

Before getting started, create a Team if you haven't already.

  1. Go to FlexLang

  2. Create a Package by clicking the New button in the upper left. Choose a name and ensure you select your team from the drop down

  3. Click Create Package

  4. Create a new file by hovering over your package in the left side file viewer, clicking the ... icon, and then New File

  5. Enter the name "Transform.flex" and click Create File

  6. 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 message PositionMetric represents values in radians and meters. The transform US_to_Metric defines the relationship between these messages, taking PositionUS as input and outputting PositionMetric. A return keyword doesn't need to be included because the last expression of a transform is implicitly returned.

  7. Save the file by clicking the gear button in the upper-right and selecting Save File or alternatively pressing the hot-key Ctrl + S / Cmd + S.

  8. Publish your new package by selecting the ... button next to your package name in the left panel and then choosing Publish Package.

Step 2: Build a Component-Based Design Using the New Transform

For a refresher on building a component-based design, go to Visualize a Project Design.

  1. Go to Projects and create a new Project
  2. Add two components and update their names to be:
    • PositionUS
    • PositionMetric
  3. Click Add Transform and drop it between the two components
  4. Define the connection by clicking on PositionUS first, then PositionMetric
  5. For the Transform's Input Message, select your new package and message PositionUS
  6. For the Transform's Output Message, select the same package and message PositionMetric
  7. Click Apply Transform. This is your Flex Transform!

Step 3: Generate Transform Code

  1. Click Build in your project
  2. Select the Transform item in the list
  3. Choose Generate Transform in its dropdown menu
  4. Click Build
  5. Download the generated code. Extract and open the package.
  6. Navigate to the transforms/gen folder. You should see files called Transform.cpp and my_transform.cpp. These were generated as a result of the transform that you defined in your .flex file.
  7. Open Transform.cpp. Take a look at the file and see that there is a C++ function called "US_to_Metric" that represents your Flex transform. The my_transform.cpp file can be used to execute the "US_to_Metric" function.

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.


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 code with these user-defined Flex packages.

Check out Discuss, our Tangram Flex community and find out more about Flex Authorship in Tangram Pro™.