Skip to content

Getting Started

This section will help you quickly master the basic structure and usage of the C++ API.

Before you begin development, ensure your C++ environment meets the following requirements:

  • Operating System: Ubuntu 22 or later.
  • Compiler: GCC 11 or later.
  • C++ Language Standard: ISO C++17.

After obtaining and extracting the AlphaBotCore SDK package, navigate to the C++ directory. The overall file organization structure is as shown below.

  • Directoryc++
    • Directoryexamples # Sample programs
      • Makefile # For compilation
    • Directoryinclude # Header files
      • Directorybase # Basic data types
        • abc_actuator_types.h
        • abc_arm_types.h
        • abc_base.h
        • abc_chassis_types.h
        • abc_ee_types.h
        • abc_sensor_types.h
        • abc_torso_types.h
        • abc_version.h
      • Directorycommon # Common data types
        • abc_common_types.h
        • abc_core.h
        • abc_define.h
      • Directorycore
        • Directoryalgo
          • Directorycontrol
            • admittance_control.h # Admittance control API
          • Directorykinematics # Kinematics APIs
            • arm_kinematics.h
            • collision.h
            • kinematics.h
            • torso_kinematics.h
      • Directorydrivers
        • Directoryarm # Robotic arm APIs
          • arm_factory.h
          • arm_interface.h
        • Directorychassis # Chassis APIs
          • chassis_factory.h
          • chassis_interface.h
        • Directoryhead # Head APIs
          • head_factory.h
          • head_interface.h
        • Directorysensors # Sensors APIs
          • camera_data.h
          • depth_camera_interface.h
          • sensor_factory.h
          • sensor_interface.h
        • Directorytorso # Torso APIs
          • torso_factory.h
          • torso_interface.h
        • Directorytraj # Trajectory planner APIs
          • poly5_planner.h
          • trajectory.h
          • trajectory_planner.h
        • Directorysensor # Sensor Synchronizer APIs
          • sensor_emergency_stop_monitor.h
          • sensor_synchronizer.h
    • Directorylib # Shared Libraries
      • libalphabot_algo.so
      • libalphabot_drivers_chassis.so
      • libalphabot_drivers.so
      • libalphabot_sensor.so
      • libapi_cpp.so
      • librealsense2.so

Refer to the example programs under the examples directory to learn how to call core interfaces.

For example, the sample code in c++\examples\arm\arm_factory_example.cpp is shown below.

#include <iostream>
#include <unordered_map>
#include "drivers/arm/arm_factory.h"
using namespace alphabot;
int main(int argc, char *argv[])
{
// Check parameter
if (argc < 2)
{
std::cerr << "Usage: " << argv[0] << " <config_file_path>" << std::endl;
std::cerr << "Example: " << argv[0] << " config/left_arm.json" << std::endl;
return 1;
}
const std::string config_path = argv[1];
std::cout << "===== ArmFactory Example Program =====" << std::endl;
try
{
// Create a robotic arm factory instance
ArmFactory factory;
std::cout << "\n----- Method 1: Create a Robotic Arm Using Parameters -----" << std::endl;
// Configure robotic arm parameters
std::unordered_map<std::string, std::string> params = {
{"ip", "192.168.1.18"},
{"port", "8080"},
{"udp_ip", "192.168.1.102"},
{"udp_port", "8089"},
{"udp_cycle", "5"}};
// Create a robotic arm instance
auto arm1 = factory.create_arm("realman", "right_arm", params);
if (!arm1)
{
std::cerr << "Failed to create robotic arm instance" << std::endl;
return 1;
}
// Connect to the robotic arm
std::cout << "Connecting to robotic arm..." << std::endl;
if (arm1->connect() != AbcErrorCode::SUCCESS)
{
std::cerr << "Failed to connect to robotic arm" << std::endl;
return 1;
}
std::cout << "Robotic arm connected successfully" << std::endl;
std::cout << "\n----- Method 2: Create a Robotic Arm Using Configuration File -----" << std::endl;
// Create a robotic arm instance from configuration file
auto arm2 = factory.create_arm_from_config(config_path);
if (!arm2)
{
std::cerr << "Failed to create robotic arm instance from configuration file" << std::endl;
return 1;
}
// Connect to the robotic arm
std::cout << "Connecting to robotic arm..." << std::endl;
if (arm2->connect() != AbcErrorCode::SUCCESS)
{
std::cerr << "Failed to connect to robotic arm" << std::endl;
return 1;
}
std::cout << "Robotic arm connected successfully" << std::endl;
// Disconnect
arm1->disconnect();
std::cout << "Robotic arm disconnected" << std::endl;
// Disconnect
arm2->disconnect();
std::cout << "Robotic arm disconnected" << std::endl;
}
catch (const std::exception &e)
{
std::cerr << "Exception occurred: " << e.what() << std::endl;
return 1;
}
std::cout << "\n===== Example Program Ended =====" << std::endl;
return 0;
}

Through the above example program, you can quickly learn how to create an instance of the robotic arm interface and connect to the robotic arm. Other example programs follow similar structures and calling conventions. Please refer to more example programs for further learning.

To simplify C++ program compilation, the AlphaBotCore SDK provides a Makefile in the c++\examples directory. This Makefile automatically detects and compiles new .cpp files without manual modification.

The following uses c++\examples\arm\arm_factory_example.cpp as an example to explain the compilation and execution process in detail.

  1. Open a terminal and navigate to the directory containing the Makefile.

    Terminal window
    cd c++/examples
  2. Run the make command to compile. After successful compilation, all executable files will be output to the bin/ directory.

    Terminal window
    make
  3. After successful compilation, navigate to the directory containing the executable files.

    Terminal window
    cd bin/arm
  4. Run the example program. This example requires passing the robotic arm configuration file path as a parameter.

    Terminal window
    ./arm_factory_example ../../config/left_arm.json

The compilation and execution steps for other example programs are similar; follow the steps above.

Through this section, you have learned the basic structure and usage of the C++ interface. We recommend further exploring other example programs and developing with reference to the C++ API documentation.