Skip to content

创建客户应用

本文将指导用户如何使用 Daystar Bot SDK 创建一个属于自己的高层应用例程程序。

利用 Daystar Bot SDK的mc_client_interface 头文件可以使得用户创建各式各样的高层应用,本小节会手把手教学用户使用 mc_client_interface 创建一个 “让Daystar Bot机器人站立并往前行走一段距离” 的高层应用。

查阅 mc_client_interface 接口

mc_client_interface 是 Daystar Bot SDK 提供的接口头文件,通过调用其中高层接口控制 Daystar Bot 机器人执行指定动作。

该头文件位于sdk下载包 rc_sdk/sdk/include/mc_client_interface.h,见于下文

cpp
#ifndef MC_CLIENT_INTERFACE_H
#define MC_CLIENT_INTERFACE_H

#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include "common.h"

using namespace robot_control;

// SDK初始化接口
bool rcClientInterfaceInit(std::string target_ip);

// 高层运动服务接口
bool rcClientInterfaceDriverEnable(robot_control::common::NAV_OR_JOY_MODE from, bool driver_enable);
bool rcClientInterfaceDirectionMovement(robot_control::common::NAV_OR_JOY_MODE from, common::ROBOT_TWIST rc_direct, int64_t time_millis);
bool rcClientInterfaceBodyHighAdjust(robot_control::common::NAV_OR_JOY_MODE from, int scale);
bool rcClientInterfaceSetScene(robot_control::common::NAV_OR_JOY_MODE from, common::SCENE_TYPE scene);
bool rcClientInterfaceDemoControl(common::NAV_OR_JOY_MODE from, int mode, bool enable);
bool rcClientInterfaceSpeedAdjust(robot_control::common::NAV_OR_JOY_MODE from, int scale);
bool rcClientInterfaceDriverSoftEstop(robot_control::common::NAV_OR_JOY_MODE from);
bool rcClientInterfacercResumeSoftEstop(robot_control::common::NAV_OR_JOY_MODE from);
bool rcClientInterfaceSetNavOrJoyControl(common::NAV_OR_JOY_MODE nav_joy);

// 底层运动服务接口
bool rcClientInterfaceClearDriverError(unsigned int mask);
bool rcClientInterfaceReCalibrateAllDriverAndSave(bool is_rough);
// 清除BISS错误接口缺失
bool rcClientInterfaceZeroTorqueSensor(robot_control::common::NAV_OR_JOY_MODE from);


// 设备状态服务接口
bool rcClientInterfaceGetCommonStatus(common::ROBOT_COMMON_STATUS *com_state);
bool rcClientInterfaceGetJointsStatus(common::ROBOT_JOINTS_STATUS *state);

// 底层服务接口
bool rcClientInterfaceGetChargeState(common::JETSON_CHARGE_STATE *charge_state);
bool rcClientInterfaceEnterOrExitCharge(robot_control::common::NAV_OR_JOY_MODE from, bool driver_enable);
bool rcClientInterfaceGetImuData(common::MC_MOTION_IMU_DATA *imu_data);
bool rcClientInterfaceGetImuStreamData(std::vector<common::MC_MOTION_IMU_DATA> *imu_data_list);

// 故障服务接口
bool rcClientInterfaceLogUpdate(std::vector<std::string> &log_list);

// 系统信息服务接口
std::string rcClientInterfaceGetMcVersion(const int& index);
std::string rcClientInterfaceGetRobotName(const int& index);

// 停障服务接口
bool rcClientInterfaceSetGuardian(common::NAV_OR_JOY_MODE from, float velocity_decay_ratio, std::string trigger_source);
bool rcClientInterfaceSetGuardianSwitch(robot_control::common::NAV_OR_JOY_MODE from, bool enable);

#endif // MC_CLIENT_INTERFACE_H

根据前文描述的应用情况,经查阅,用到的函数有

  1. bool rcClientInterfaceInit(std::string target_ip); 初始化SDK
  2. bool rcClientInterfaceDriverEnable(robot_control::common::NAV_OR_JOY_MODE from, bool driver_enable); 上使能
  3. bool rcClientInterfaceSetScene(robot_control::common::NAV_OR_JOY_MODE from, common::SCENE_TYPE scene);设置机器人场景
  4. bool rcClientInterfaceSpeedAdjust(robot_control::common::NAV_OR_JOY_MODE from, int scale);** **设置机器人移动速度系数
  5. bool rcClientInterfaceGetCommonStatus(common::ROBOT_COMMON_STATUS *com_state); 读取机器人状态
  6. rcClientInterfaceDirectionMovement(robot_control::common::NAV_OR_JOY_MODE from, common::ROBOT_TWIST rc_direct, int64_t time_millis); 控制机器人移动

创建应用文件

接下来集成sdk创建你的应用工程。

以下叙述假设工作路径为 /home/daystar/sdk_test_project

依次输入下列命令。把sdk so库文件和 .h 头文件拷贝到应用工程对应目录下(根据使用的开发平台相应选择sdk arm或者x86 的so库文件),创建app_test.cpp文件,编写调用sdk接口和应用逻辑。

bash
cd /home/daystar/sdk_test_project
mkdir include src libs
cp /sdk/include/* include/
cp /sdk/lib/arm/* libs/
cd src
touch app_test.cpp

接下来让我们编辑 app_test.cpp

  1. 包含头文件:
plain
#include "include/mc_client_interface.h"
  1. 调用SDK函数实现应用逻辑
cpp
// 初始化
rcClientInterfaceInit("192.168.100.103");
common::NAV_OR_JOY_MODE mode = common::NAV_OR_JOY_MODE::joy_control;
// 上使能
rcClientInterfaceDriverEnable(mode, true);
// 设置行走步态
common::SCENE_TYPE scene = common::WALKING;
rcClientInterfaceSetScene(mode, scene);
// 设置行走最大速度系数(1-100)
float max_speed_factor = 1;  
rcClientInterfaceSpeedAdjust(mode, max_speed_factor);
// 获取机器人状态
common::ROBOT_COMMON_STATUS status;
rcClientInterfaceGetCommonStatus(&status);
// 设置以0.5m/s的速度往前行走
common::ROBOT_TWIST direction = {{0.5, 0.0, 0.0}, {0.0, 0.0, 0.0}};
rcClientInterfaceDirectionMovement(mode, direction, status.heartbeat);

完整代码为

cpp
#include "mc_client_interface.h"
#include <unistd.h>

int main(int argc, char **argv)
{
    common::NAV_OR_JOY_MODE mode = common::NAV_OR_JOY_MODE::joy_control;
    // 初始化
    rcClientInterfaceInit("192.168.100.103");
    // 上使能
    rcClientInterfaceDriverEnable(mode, true);

    // 设置行走场景
    common::SCENE_TYPE scene = common::WALKING;
    rcClientInterfaceSetScene(mode, scene);
    usleep(5000000); // 等待5秒,趴下切换到行走

    // 设置行走最大速度系数(1-100)
    float max_speed_factor = 1; 
    rcClientInterfaceSpeedAdjust(mode, max_speed_factor);

    // 控制机器人以0.5m/s的速度往前行走1秒,推荐频率100ms发送一次
    common::ROBOT_COMMON_STATUS status;
    common::ROBOT_TWIST direction = {{0.5, 0.0, 0.0}, {0.0, 0.0, 0.0}};
    int32_t count = 10;
    for (size_t i = 0; i < count; i++)
    {
        rcClientInterfaceGetCommonStatus(&status);
        rcClientInterfaceDirectionMovement(mode, direction, status.heartbeat);
        usleep(100000);
    }
    return 0;
}

编译应用文件

当完成应用逻辑的编写后,接下来开始编译应用文件。

sdk_test_project/CMakeLists.txt 文件添加编译命令, 将刚才写的例程加入以生成对应可执行文件,然后保存文件即可。

cpp
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
    set(MC_CLIENT_LIB ${CMAKE_SOURCE_DIR}/libs/arm/libmc_client.so) # for rc_sdk
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
    set(MC_CLIENT_LIB ${CMAKE_SOURCE_DIR}/libs/x86_64/libmc_client.so) # for rc_sdk
endif()

# Add executable
add_executable(app_test 
     src/app_test.cpp)

target_include_directories(app_test PRIVATE
     include
)

target_link_libraries(app_test PRIVATE ${MC_CLIENT_LIB})

最后编译例程运行

bash
cd /home/daystar/sdk_test_project
mkdir build
cd build
cmake ..
make

运行应用

连接机器人网络(例如IS_Plus_001),确保开发设备跟机器人在同一个局域网内,打开一个终端,并依次执行以下命令以运行应用。

bash
cd /home/daystar/sdk_test_project/build
sudo ./app_test

注意

该应用会将控制命令经由运控服务转发至 Daystar Bot 机器人,故运行该应用前请确保机器人处于正常运行状态,具体请查阅《App绑定》篇。