Tutorial
Work in Progress
2023:11
Valentijn
\\ \\ \\
====== Integrating Bhpatics into Python ======
----
===== How to Integrate bhaptics SDK into Python =====
TLDR: In this tutorial we will be connecting a [[BHaptics Tactosy for arms]]((https://www.bhaptics.com/tactsuit/tactosy-for-arms)) to Python. First connecting to the device using a laptop or computer will be explained after which we can create .tact files in the designer and use python to "play" them on the haptic device.
You can also easily skip ahead to any of the steps using the table of contents on the right
===== Step 0. Materials and Prerequisites =====
* PC or laptop that can run python 3.7
* bhaptics hardware (vest, gloves, etc.)
===== Step 1. Setup of bHaptics Player =====
To be able to connect the bHaptics hardware to a software package as python we must install **bHaptics Player** from the [[https://www.bhaptics.com/software/player|bHaptics player download page]]. Then follow the installer instructions to install the software.
When first opening the bHaptics Player it should give you a short explanation and allow you to connect your hardware.
===== Step 2. Connecting hardware to the player =====
When connecting to hardware for the first time you must manually pair device to the bHaptics Player Software.
In the bottom left of the player you should see a device popup after you turn it on.
{{:playground:screenshot_2023-11-17_162342.png?200|}}
To pair to this device you must **click on the device**
After pairing you can test it by clicking on it and clicking on **ping**.
{{:playground:screenshot_2023-11-17_163016.png?200|}}
After this inital pairing connecting hardware usually goes automatically, when you click on the button on the device the software should automatically see it.
===== Step 3. Create vibration patterns =====
{{ :playground:screenshot_2023-11-18_163525.png?400|}}
To be able to use the bHaptics device from python we need to send it commands to vibrate. These commands originate from a "**tact file**" which we need to create in the [[https://designer.bhaptics.com/|bHaptics Designer]].
(note: you need to have the bHaptics player program running in the background and have the device connected.)
Firstly we will create a new project and select your device that you are using. We are using the armband so will be selecting the 4th icon as shown right.
Next in the designer we can see a split screen where the left device allows you to create patterns for the left device and the same applies for the right side.
To create an effect we must first click on **Create Effect** after which we can click the different parts of the armband to specify movement there.
After creating a pattern you can test it by clicking the play icon in the timeline below.
===== Step 4. Export vibration files =====
To be able to use the vibration pattern that we just designed in the designer we must export a .tact file from the designer using the **export project** button which will download the .tact file of your project. We then need to move this to our python working directory.
===== Step 5. Setup python enviroment =====
In this tutorial we assume that you **already have python installed** version 3.10 is confirmed to be working, but others probabally work too.
Sadly there is not python libary yet that we can easily import using pip, so we will first have to copy the folder called "bhaptics" from [[https://github.com/bhaptics/tact-python]] to our working directory where we will be using python. You can also do this using:
git clone https://github.com/bhaptics/tact-python
We do need to install the dependency websocket-client. We can do this using:
pip install websocket-client
===== Step 6. Create a python script =====
To be able to use the vibration patterns that we created earlier in the bHaptics Studio we will be using python to "play" through these files.
from time import sleep
from bhaptics import haptic_player
# Import the player from the libary (/bhaptics)
player = haptic_player.HapticPlayer()
# Import .tact file
tact_file = "test_project.tact"
# Load the tact file
print("test_haptics")
player.register("test_haptics", tact_file)
sleep(0.3)
# submit (send) the haptics to the device
print("submit test_haptics")
player.submit_registered("test_haptics")
It might happen that the device does not respond when running the script. You can try to restart the bHaptics player software and reconnect the deivce. It is important that the player runs in the background if you run the Python script.
{{page>.playground:css:footer}}