跳至主要内容
版本:最新

Python API

入门

Haply 硬件 API 是一个 Python 软件包,为 Haply 硬件提供了一个 Python 接口。该 API 以 C++ 实现,并使用 pybind11 封装。该 API 为 Haply 硬件(包括 Haply Versegrip 和 Haply Inverse3)提供了一个 Python 接口。

安装

Haply Hardware API 是PyPI 上的一个 Python 软件包。要安装该软件包,请使用 pip:

pip install HaplyHardwareAPI

使用方法

Gitlab 示例库中提供了如何使用 Haply 硬件 API 的示例。

下面的代码片段举例说明了如何使用 Haply 硬件 API 连接到 Inverse3 并模拟虚拟物理球:

#!/usr/bin/env python

"""This example demonstrates how to display a haptic ball """

__author__ = "Antoine Weill--Duflos"
__copyright__ = "Copyright 2023, HaplyRobotics"

import HaplyHardwareAPI
import time
import math
connected_devices = HaplyHardwareAPI.detect_inverse3s()
com_stream = HaplyHardwareAPI.SerialStream(connected_devices[0])
inverse3 = HaplyHardwareAPI.Inverse3(com_stream)
response_to_wakeup = inverse3.device_wakeup_dict()
print("connected to device {}".format(response_to_wakeup["device_id"]))
start_time = time.perf_counter()
loop_time = 0.001 # 1ms
forces = [0, 0, 0]


def force_sphere(sphere_center, sphere_radius, device_position, stiffness):
distance = math.sqrt(
sum([(device_position[i] - sphere_center[i])**2 for i in range(3)]))
if distance > sphere_radius:
return [0, 0, 0]
else:
# Compute the normalised direction of the forces
direction = [(device_position[i] - sphere_center[i])/sphere_radius
for i in range(3)]
# Compute the force
force = [direction[i]*(sphere_radius-distance)
* stiffness for i in range(3)]
return force


while True:
position, velocity = inverse3.end_effector_force(forces)
forces = force_sphere([0, -0.14, 0.2], 0.08, position, stiffness=800)
print("position: {}".format(position))
while time.perf_counter() - start_time < loop_time: # wait for loop time to be reached
pass
start_time = time.perf_counter()