Reading information from MARA

Source code

NOTE: Work in progress. We're continuously improving the documentation, stay tuned for updates

This section will explain how to fetch information MARA with a Python script and C++.

#!/usr/bin/python3

# ROS 2.0
import rclpy

# HRIM
from hrim_actuator_rotaryservo_msgs.msg import GoalRotaryServo

def main(args=None):
    rclpy.init(args=args)

    node = rclpy.create_node('mara_minimal_subscriber')

    subscription = node.create_subscription(
        GoalRotaryServo, '/hros_actuation_servomotor_000000000001/state', lambda msg: node.get_logger().info('Position:' + str(msg.position)))
    subscription  # prevent unused variable warning

    rclpy.spin(node)

    node.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()

When we program with Python is as simple as create a script and running it. But in C++ we need to go to process of compilation. For this we need to create a following directory structure:

| mara_minimal_subscriber
| -- CMakeLists.txt
| -- package.xml
| -- src/
| -- src/mara_minimal_subscriber.cpp

#include <iostream>
#include "rclcpp/rclcpp.hpp"
#include "hrim_actuator_rotaryservo_msgs/msg/state_rotary_servo.hpp"

class MinimalSubscriber : public rclcpp::Node
{
public:
  MinimalSubscriber()
  : Node("mara_minimal_subscriber")
  {
    subscription_ = this->create_subscription<hrim_actuator_rotaryservo_msgs::msg::StateRotaryServo>(
      "/hros_actuation_servomotor_000000000001/state",
      [this](hrim_actuator_rotaryservo_msgs::msg::StateRotaryServo::UniquePtr msg) {
      RCLCPP_INFO(this->get_logger(), "Position: %.5f", msg->position);
    });
  }

private:
  rclcpp::Subscription<hrim_actuator_rotaryservo_msgs::msg::StateRotaryServo>::SharedPtr subscription_;
};

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  rclcpp::spin(std::make_shared<MinimalSubscriber>());
  rclcpp::shutdown();
  return 0;
}

For the compilation process we need to create a package.xml which help colcon or ament to understand which is the order in the compilation chain between others matters

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="2">
  <name>mara_minimal_subscriber</name>
  <version>0.0.1</version>
  <description>MARA minimal subscriber node</description>
  <maintainer email="alex@erlerobotics.com">Alejandro Hernandez</maintainer>
  <license>Apache License 2.0</license>
  <author>Alejandro Hernandez</author>

  <buildtool_depend>ament_cmake</buildtool_depend>

  <build_depend>rclcpp</build_depend>
  <build_depend>hrim_actuator_rotaryservo_msgs</build_depend>

  <exec_depend>rclcpp</exec_depend>
  <exec_depend>hrim_actuator_rotaryservo_msgs</exec_depend>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>

CMakeLists.txt is the file that colcon or ament are going to load to compile the sources of this project. It's important to include rclcpp and hrim_actuator_rotaryservo_msgs which contains the messages:

cmake_minimum_required(VERSION 3.5)
project(mara_minimal_subscriber)

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(hrim_actuator_rotaryservo_msgs REQUIRED)

add_executable(mara_minimal_subscriber src/mara_minimal_subscriber.cpp)
ament_target_dependencies(mara_minimal_subscriber rclcpp hrim_actuator_rotaryservo_msgs)

install(TARGETS
  mara_minimal_subscriber
  DESTINATION lib/${PROJECT_NAME}
)

ament_package()