Demo (C, C++):
Robotic arm force-position hybrid control Demo 1. Project introduction
This project demonstrates the use of the force-position hybrid control function of the RM65-6F robotic arm to ensure that the end effector's contact force remains constant during Cartesian space trajectory planning. It is built with CMake and utilizes the C language development package for the robotic arm provided by RealMan.
2. Code structure
RMDemo_ForceControl/
├── build/ # Output directory generated by CMake build
├── include/ # Custom header file storage directory
├── Robotic_Arm/ # RealMan robotic arm secondary development package
│ ├── include/
│ │ ├── rm_define.h # Header file of the robotic arm secondary development package, containing defined data types and structures
│ │ └── rm_interface.h # Header file of the robotic arm secondary development package, declaring all operation interfaces of the robotic arm
│ └── lib/
│ ├── api_c.dll # API library for Windows 64bit
│ ├── api_c.lib # API library for Windows 64bit
│ └── libapi_c.so # API library for Linux x86
├── src/ # Source file storage directory
│ └── main.c # Source file of main functions
├── run.bat # Windows quick run script
├── run.sh # linux quick run script
├── CMakeLists.txt # CMake configuration file of the project
└── README.md # Project description document
3. Project download
Download RM_API2
locally via the link: development package download. Then, navigate to the RM_API2\Demo\RMDemo_C
directory, where you will find RMDemo_ForceControl.
4. Environment configuration
Required environment and dependencies for running in Windows and Linux environments:
Item | Linux | Windows |
---|---|---|
System architecture | x86 architecture | - |
Compiler | GCC 7.5 or higher | MSVC2015 or higher 64bit |
CMake version | 3.10 or higher | 3.10 or higher |
Specific dependency | RMAPI Linux version library (located in the Robotic_Arm/lib directory) | RMAPI Windows version library (located in the Robotic_Arm/lib directory) |
Linux configuration
1. Compiler (GCC) In most Linux distributions, GCC is installed by default, but the version may not be the latest. If a specific version of GCC (such as 7.5 or higher) is required, it can be installed via the package manager. For example, on Ubuntu, you can use the following commands to install or update GCC:
# Check GCC version
gcc --version
sudo apt update
sudo apt install gcc-7 g++-7
Note: If the GCC version installed by default on the system meets or exceeds the required version, no additional installation is necessary.
2. CMake CMake can also be installed through the package manager in most Linux distributions. For example, on Ubuntu:
sudo apt update
sudo apt install cmake
# Check CMake version
cmake --version
Windows configuration
Compiler (MSVC2015 or higher): The MSVC (Microsoft Visual C++) compiler is typically installed with Visual Studio. You can install it by following these steps:
- Visit the Visual Studio official website to download and install Visual Studio.
- During installation, select the "Desktop development with C++" workload, which will include the MSVC compiler.
- Select additional components as needed, such as CMake (if not already installed).
- After installation, open the Visual Studio command prompt (available in the start menu) and enter the
cl
command to check if the MSVC compiler is installed successfully.
CMake: If CMake was not included during the Visual Studio installation, you can download and install CMake separately.
- Visit the CMake official website to download the installer for Windows.
- Run the installer and follow the on-screen instructions to complete the installation.
- After installation, add the CMake bin directory to the system's PATH environment variable (this is typically asked during installation).
- Open the command prompt or PowerShell and enter
cmake --version
to check if CMake has been installed successfully.
5. User guide
5.1 Quick run
Follow these steps to quickly run the code:
Configuration of the IP address of the robotic arm: Open the
main.c
file and modify the parameters of therobot_ip_address
in themain
function to the current IP address of the robotic arm. The default IP address is"192.168.1.18"
.Cconst char *robot_ip_address = "192.168.1.18"; int robot_port = 8080; rm_robot_handle *robot_handle = rm_create_robot_arm(robot_ip_address, robot_port);
Running via linux command line: Navigate to the
RMDemo_ForceControl
directory in the terminal, and enter the following command to run the C program:bashchmod +x run.sh ./run.sh
The running result is as follows:
Running on Windows: double-click the run.bat script to run The running result is as follows:
After running the script, the trajectory is shown in the following image:
5.2 Description of key codes
The following are the main functions of the main.c
:
Connect the robotic arm Connect the robotic arm to the specified IP address and port.
Crm_robot_handle *robot_handle = rm_create_robot_arm(robot_ip_address, robot_port);
Get the API version Get and display the API version.
Cchar *api_version = rm_api_version(); printf("API Version: %s.\n", api_version);
Move the joints to the initial pose Call
movej
to move the joints to their zero positions, and then callmovej_p
to move to the initial pose:[x, y, z, rx, ry, rz]
are[0.3, 0, 0.4, 3.141, 0, 0]
Cfloat joint_angles_start[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; rm_movej(robot_handle, joint_angles_start, 20, 0, 1, 1); rm_pose_t start_pose = { {0.3, 0, 0.4},{0, 0, 0}, {3.141, 0, 0} }; rm_movej_p(robot_handle, start_pose, 20, 0, 1, 0);
Enable the force control mode Enable the force-position hybrid control mode, set to use 6-DoF force in the work frame, and apply a constant 5 N force in the downward direction along the Z-axis
Crm_set_force_position(robot_handle, 1, 0, 2, -5);
Perform the Cartesian space motion Continuously call
movel
for linear motion between the initial pose[0.3, 0, 0.4, 3.141, 0, 0]
and the target pose[0.2, 0, 0.4, 3.141, 0, 0]
, with motion along the X-axis and a 2-second pause between each trajectory. During the robotic arm's movement, the end effector in contact with the 6-DoF force experiences a constant force along the backward Z-axis.Crm_pose_t target_pose = { {0.2, 0, 0.4},{0, 0, 0}, {3.141, 0, 0} }; result = rm_movel(robot_handle, target_pose, 60, 0, 0, 1); if (check_result(result, "Failed to perform rm_movel motion to target_pose") != 0) { return -1; } SLEEP_S(2); // Move back to the starting position result = rm_movel(robot_handle, start_pose, 50, 0, 0, 1); if (check_result(result, "Failed to perform rm_movel motion to start_pose") != 0) { return -1; }
Disable the force control mode Disable the force-position hybrid control mode after completion.
Crm_stop_force_position(robot_handle);
6. License information
- This project is subject to the MIT license.