快速开始
本节将帮助您快速掌握 Python API 的基本结构及其使用方法。
1. 搭建开发环境
Section titled “1. 搭建开发环境”在开始开发前,请先确保已满足以下 Python 开发环境:
- 操作系统:Ubuntu22 及以上。
- Python:3.10。
2. 安装 alphabot 模块
Section titled “2. 安装 alphabot 模块”搭建完 Python 开发环境后,即可通过 pip 工具安装 alphabot 模块。
-
在终端中,进入软件包解压后的
python目录。Terminal window cd python -
通过
pip安装alphabot模块。Terminal window pip install .
3. 了解模块结构
Section titled “3. 了解模块结构”您可以浏览 alphabot 目录下的 .pyi 接口文件,了解接口模块划分。
alphabot 模块提供了多个可直接使用的类,支持通过 from alphabot import 类名 的方式导入。
4. 参考示例程序
Section titled “4. 参考示例程序”参考 examples 下的示例程序,掌握核心接口调用。
例如,Python\examples\arm\arm_factory_example.py 样例程序代码如下所示。
import sysimport osfrom alphabot import ArmFactory, ArmFactoryParam
def main(): """Main function""" # Parameter validation if len(sys.argv) < 2: print(f"Usage: {sys.argv[0]} <configuration_file_path>") print(f"Example: {sys.argv[0]} config/left_arm.json") return 1
config_path = sys.argv[1]
print("===== ArmFactory Sample Program =====")
try: print("\n----- Method 1: Creating a Robotic Arm Using Parameters -----") # Configure robotic arm parameters param = ArmFactoryParam({ "arm_type": "realman", "arm_name": "right_arm", "ip": "192.168.1.18", "port": "8080", "udp_ip": "192.168.1.102", "udp_port": "8089", "udp_cycle": "5" })
# Create robotic arm instance arm1 = ArmFactory.create_arm(param) if not arm1: print("Failed to create robotic arm instance") return 1
print(f"Successfully created robotic arm: {arm1}")
print("\n----- Method 2: Creating a Robotic Arm Using Configuration File -----") # Create robotic arm instance from configuration file arm2 = ArmFactory.create_arm_from_config(config_path) if not arm2: print("Failed to create robotic arm instance from configuration file") return 1
print(f"Successfully created robotic arm from configuration file: {arm2}")
return 0
except Exception as e: print(f"Exception occurred: {e}") return 1
if __name__ == "__main__": sys.exit(main())通过上述示例程序,您可以快速了解如何创建机械臂接口实例,并连接机械臂。其他示例程序也具有类似的结构和调用方式,请参考更多的示例程序进行学习。
接下来以 Python\examples\arm\arm_factory_example.py 文件为例,讲解运行过程。
-
在终端中,进入示例程序所在目录。
Terminal window cd python/examples/arm -
执行
python命令,便可运行程序。本示例需要传入机械臂配置文件路径参数。Terminal window python arm_factory_example.py ../config/left_arm.json
其他示例程序的运行方式与此类似,请参考上述步骤进行操作。
通过本节学习,您已经初步掌握了 Python 接口的结构和使用方法。建议您继续深入学习其他示例程序,并结合接口文档进行开发实践,以更好地理解和应用 Python 接口。