AWS Robotics Blog

Introducing Log-based Simulation for AWS RoboMaker

This blog uses colcon bundle for robot and simulation applications. AWS RoboMaker now only supports containers to make it easy for you to bring and run your own simulations and applications. To follow along with this blog post, see our updated blog on Preparing ROS application and simulation application containers for AWS RoboMaker.

Introduction

Roboticists test their robot applications in simulations to understand how applications behave in different environments. This helps roboticists debug their applications and develop resilient robots. Recently, AWS launched log-based simulation in AWS RoboMaker. Log-based simulation eases the burden of building and managing infrastructure for log-based simulations.

In the world of Robot Operating Systems (ROS), a ROS bag is a file containing the serialized exchange of data between robot nodes in a system over a specific period of time. Using pre-recorded ROS bag files, often from a physical robot running in the real-world, to run simulations is called log-based simulation. It replaces the virtual simulators (such as Gazebo) with ROS bags as the input to ROS nodes. With log-based simulations, your ROS nodes read played-back data from ROS bags instead of relying on a virtual simulator.

Log-based simulation helps you test repeating scenarios and avoid software regressions. With log-based simulation support for AWS RoboMaker, you can run multiple log-based simulations in parallel without any infrastructure provisioning or management, accelerating your robot application development cycle and saving development costs.

AWS has launched two additional features:

  • Event-driven simulation job termination helps you terminate a simulation job programmatically based on an event. For example, you can programmatically terminate a simulation job after your simulated robot succeeds or fails at a defined task without waiting for the entire simulation job to complete. This feature allows you to terminate a simulation job promptly after you’ve finished your test and helps reduce costs.
  • Simulation event tagging lets you add tags to your simulation job. A tag is a key-value pair that simplifies managing, searching for, and filtering AWS RoboMaker simulation jobs. When you run a simulation job in AWS RoboMaker, you can tag any events of interest during the simulation execution. For example, you can add tags for events like when your simulated robot collides with an obstacle, successfully completes a task, or fails a task.

All these new features combine to simplify running automated simulations for use cases like regression testing.

In this post, we demonstrate how to run a sample log-based simulation job as a regression test in AWS RoboMaker.

This sample application showcases the practical example of a robot that has voice controls via Amazon Lex, and can speak up if it encounters trouble using Amazon Polly. The simulation application consumes messages published from a ROS bag, which contains commands to make a robot move forward at 5 m/s.

The test determines how the robot responds to different commands when the simulation plays back the ROS bag messages. Later, when you create and run your own log-based simulations in AWS RoboMaker, replace the provided artifacts with your own.

Running a log-based simulation in AWS RoboMaker

Running a log-based simulation in AWS RoboMaker involves the following steps:

  1. Create resources with an AWS CloudFormation stack.
  2. Create an IAM role for the simulation job.
  3. Create a robot application in AWS RoboMaker.
  4. Create a simulation application in AWS RoboMaker.
  5. Create a simulation job in AWS RoboMaker.
  6. Check the simulation job result.
  7. Delete resources we created for running this tutorial.

Prerequisites
The robot application uses Amazon Lex for building conversations in both audio and text formats. It also uses Amazon Polly to generate audio output when errors occur, so users are notified. It uses AWS Lambda and the AWS SDK in C++ to set up some of the required resources with proper access permissions. In this post, we use the US West (Oregon) AWS Region.

Step 1: Create resources with a CloudFormation stack.

  1. In the AWS CloudFormation console, choose Create stack.
  2. For S3 URL, enter the following value and click Next: https://aws-robomaker-samples-us-west-2-fa07e396efe2.s3-us-west-2.amazonaws.com/voice-interaction/kinetic/gazebo7/1.2.0.268/0.4.11/voice-interaction.template
  3. Fill in the stack name as RoboMakerLogBasedSimulationBlog.
  4. Fill in parameters with the following values and click Next:
    1. LambdaFunctionS3Key: voice-interaction/kinetic/gazebo7/1.2.0.268/0.4.11/voice-interaction-lambda.zip
    2. SourceBucket: aws-robomaker-samples-us-west-2-fa07e396efe2
    3. LexImportBotS3Key: voice-interaction/kinetic/gazebo7/1.2.0.268/0.4.11/assets/lex-bot.zip
    4. InRoboMaker: false
  5. Click Next on Configure stack options page.
  6. The final Review page has a check box at the bottom for acknowledging that AWS CloudFormation might create IAM resources, check the box and click Create Stack.
  7. Wait until the stack is successfully created, then proceed to the next step.

Step 2: Create an IAM role for the simulation job.
Next, create an IAM role that grants access to the AWS resources that you just created. Later, you attach the role to the simulation job.

  1. In the IAM console, choose Roles, Create role.
  2. For Choose the service that will use this role, choose AWS RoboMaker and click Next.
  3. For Select your use case, choose RoboMaker – Simulation. Click Next: Permissions.
  4. Check AdministratorAccess for admin access to all AWS services and click Next: Tags and then Next: Review.
  5. Fill in the Role name as RoboMakerLogBasedSimulationBlogRole, and click Create role.

Step 3: Create an AWS RoboMaker robot application.
Create a robot application in AWS RoboMaker. For this example, use a pre-built robot application that uses Amazon Lex to have conservations to act on commands and Amazon Polly to synthesize speech when errors occur. You can find the source code in the robot_ws directory of the aws-robotics GitHub repo.

To use your own robot application in this example, replace the S3 link with the location of your robot application bundle. For more information, see Creating a Robot Application.

  1. In the AWS RoboMaker console, choose Robot applications, Create robot application.
  2. Fill in the Name as LogBasedSimulationRobotApplication.
  3. Select ROS Kinetic
  4. Under Sources, for X86_64 source file, enter the following location: s3://aws-robomaker-samples-us-west-2-fa07e396efe2/voice-interaction/kinetic/gazebo7/1.2.0.268/0.4.11/robot_ws.tar
  5. Click Create.

Step 4: Create a simulation application in AWS RoboMaker.
Next, create a log-based simulation application in AWS RoboMaker. The log-based simulation application plays three ROS bag files and runs a test, whether or not it recognizes people. It adds a key status tag and value based on the test results, and then it terminates the simulation job.

You can check out the source code in the log_based_simulation_ws directory in aws-robotics. The following code example shows an example client program that calls the AWS RoboMaker simulation job tagging service:

from robomaker_simulation_msgs.msg import Tag
from robomaker_simulation_msgs.srv import Cancel, AddTags, ListTags

def add_tags(tags):
    requestAddTags = rospy.ServiceProxy('/robomaker/job/add_tags', AddTags)
    response = requestAddTags(tags)
    if not response.success:
        rospy.logerr("AddTags request failed for tags (%s): %s", tags, response.message)

The following code example shows an example client program that calls the cancel job service:

def cancel_job():
    requestCancel = rospy.ServiceProxy('/robomaker/job/cancel', Cancel)
    response = requestCancel()
    if response.success:
        global IS_CANCELLED
        IS_CANCELLED = True
        rospy.loginfo("Successfully request cancel job")
    else:
        rospy.logerr("Cancel request failed: %s", response.message)

If the robot application move forward at 5 m/s as instructed, the following code example tags the simulation with a status passed. If the robot application does not act based on the command it receives, it applies the status tag timeout. This example sets the timeout limit to 5 minutes:

def has_changed_velocity(cmd_vel):
    """
        Pass the test if the command velocity on the robot changes
        and the x coordinate of the linear equals 5 (move forward at 5m/s)
    """
    if IS_CANCELLED:
        return

    rospy.loginfo("Test is checking if command velocity has changed.")
    if not IS_CANCELLED and cmd_vel.linear.x == X_CMD_VELOCITY:
        rospy.loginfo(
            "Command velocity changed on the robot, test passed and cancelling job")
        add_tags([Tag(key="status", value="passed")])
        cancel_job()


def timeout_test(timeout):
    """
        Cancel the test if it times out. The timeout is based on the
        /clock topic to simulate playback taking too long. Use the
        RoboMaker simulation job duration to timeout based on a
        wallclock duration.
    """
    rospy.loginfo("Test timeout called")
    if not IS_CANCELLED:
        rospy.loginfo("Test timed out, cancelling job")
        add_tags([Tag(key="status", value="timeout cancelled job")])
        cancel_job()
    else:
        rospy.loginfo("Test timed out, job already cancelled")
        add_tags([Tag(key="status", value="timeout job already cancelled")])

This post uses a pre-built log-based simulation application bundle. To develop your own robot application, replace the S3 link with your artifact’s location and the sample ROS bag’s location with your ROS bags’ location. For more information, see Creating a Simulation Application.

  1. In the AWS RoboMaker console, choose Simulation applications, Create simulation application.
  2. Fill in the name as LogBasedSimulationSimApplication.
  3. For Robot Software Suite, choose ROS Kinetic.
  4. For Simulation Software Suite, choose RosbagPlay.
  5. Under Sources, for X86_64 source file, enter the following location: s3://aws-robomaker-sample-resources/voice-interaction-log-based-sim-bundle/log_based_sim.tar
  6. Click Create.

Step 5: Create a simulation job in AWS RoboMaker.
With your resources set up and configured, you now create a simulation job. When the simulation job starts, both the robot application and simulation application call roslaunch to launch your applications.

As mentioned previously, the log-based simulation application plays ROS bag files and runs a test. It adds a tag with the key “status” and the value being the status of the test, and then it terminates the simulation job. The job automatically ends after some time.

1. Copy the example ROS bag files into your own S3 bucket

  • Open up a terminal and configure AWS credentials in your terminal environment.
  • Create an S3 bucket to contain three pre-processed sample ROS bag files:
    • export LOG_BASED_SIM_TUTORIAL_BUCKET=<unique_name>
    • aws s3api create-bucket --bucket robomaker-lbs-$LOG_BASED_SIM_TUTORIAL_BUCKET --region us-west-2 --create-bucket-configuration LocationConstraint=us-west-2
  • Copy the preprocessed, sample ROS bag files into the bucket that you created:
    • aws s3 cp s3://aws-robomaker-sample-resources/rosbags/voice_interaction_log_based_sim_bag_1.bag s3://robomaker-lbs-$LOG_BASED_SIM_TUTORIAL_BUCKET/rosbags/voice_interaction_log_based_sim_bag_1.bag
    • aws s3 cp s3://aws-robomaker-sample-resources/rosbags/voice_interaction_log_based_sim_bag_2.bag s3://robomaker-lbs- $LOG_BASED_SIM_TUTORIAL_BUCKET/rosbags/voice_interaction_log_based_sim_bag_2.bag
    • aws s3 cp s3://aws-robomaker-sample-resources/rosbags/voice_interaction_log_based_sim_bag_3.bag s3://robomaker-lbs-$LOG_BASED_SIM_TUTORIAL_BUCKET/rosbags/voice_interaction_log_based_sim_bag_3.bag

2. In the AWS RoboMaker console, choose Simulation jobs, Create simulation job in the upper right corner.

3. On the Configure simulation page

  • Simulation job duration: 30 Minutes
  • Robot Software Suite: ROS Kinetic
  • IAM Role: RoboMakerLogBasedSimulationBlogRole

4. Output destination is optional. Set this if you would like access to your Simulation Job logs.

5. In Networking section, select the default VPC, it auto-selects a security group for you. Select any two subnets available.

6. Click Next.

7. On the Specify robot application page, select LogBasedSimulationRobotApplication as the Robot application.

8. Under Robot application configuration, fill in the following settings:

  • Launch package name: voice_interaction_robot
  • Launch file: voice_interaction.launch

9. Click Next.

10. On the Specify simulation application page, select LogBasedSimulationSimApplication as the Simulation application.

11. Under Simulation application configuration, fill in the following settings:

  • Launch package name: voice_interaction_log_based_simulation
  • Launch file: play_unpaused.launch

12. Under data source configuration, for ROS bag group name, enter aws_sample_apps_resources.

13. Find and select the S3 folder robomaker-lbs-<unique_name> Go into the folder titled rosbags and select all three ROS bags.

14. Click Next.

15. Click Create.

Step 6: Check the simulation job result.
Wait until the simulation job terminates—this can take up to 5 minutes. During this time, the robot application receives command sent by the ROS bags and uses Amazon Lex to understand the commands. A client runs in the simulation, checking if the robot responds to the commands.

  • If the simulation application detects that there is a change in the velocity of the robot, the client cancels the simulation job from within the simulation application and tags the simulation job with a status passed.
  • If the simulation application does NOT detect a change in the velocity of the robot in 5 minutes, the client cancels the simulation due to time out and tags the job with a status timeout.

Regardless of the tag that the client applies, you should see the simulation job terminate. In the Tags section of the simulation job, you can see a key-value pair: status-passed—this indicates that the robot recognized a person during the simulation.

The AWS RoboMaker client terminates the simulation job, then checks the output of the simulation job. If the simulation job gives the correct output, the client applies the key tag status and the value passed. Otherwise, the client is tagged with key status and value timeout.

Step 7: Resources clean up.
In Step 1., we created resources for running the example application in this tutorial. Now, we are going to delete these resources.

In the AWS Management Console, go to CloudFormation. Delete stack named RoboMakerLogBasedSimulationBlog.

Conclusion
This post demonstrated how to set up infrastructure to test your robot application by using the new log-based simulation, event-driven simulation job termination, and simulation event tagging features in AWS RoboMaker. We hope you find this post useful. We always look for your feedback and comments on the new ways that you discover to make the most of AWS features.

Abby Xu

Abby Xu

Abby Xu is a software development engineer for AWS RoboMaker. She is an open-source enthusiast and is actively making contributions to the open-source community. In her spare time, she hangs out with her dog and her three cats, maintains the automated greenhouse system she’s built in her apartment.

Samuel Gundry

Samuel Gundry

Sam Gundry is a robotic simulation engineer who eschews hardware in the real-world and prefers to build the tools and services for creating content and high-fidelity virtual worlds in software. He lives in Seattle where he ponders on how to simulate sunshine.