Skip to content

Commit 20853cf

Browse files
th-skamepernod
andauthored
[src] Add option to support the wireless grip version with 3 buttons (#28)
NEEDLE-11 #done #time 2h --------- * [src] Add option to support the wireless grip version with 3 buttons * [src] Fix several bugs in the integration of the wireless stylus --------- Co-authored-by: epernod <erik.pernod@gmail.com>
1 parent 869f2f3 commit 20853cf

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

src/SofaHaplyRobotics/Haply_Inverse3Controller.cpp

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ namespace sofa::HaplyRobotics
4242
// Static constants
4343
const std::string Haply_Inverse3Controller::inverseKey_ = "inverse3";
4444
const std::string Haply_Inverse3Controller::deviceIdKey_ = "device_id";
45-
const std::string Haply_Inverse3Controller::gripIdKey_ = "verse_grip";
45+
const std::string Haply_Inverse3Controller::gripIdKey_ = "verse_grip"; // Refered as "VerseGrip Quill" on Haply developer doc.
46+
const std::string Haply_Inverse3Controller::wirelessGripIdKey_ = "wireless_verse_grip"; // Refered as "VerseGrip Stylus" on Haply developer doc.
4647

4748

4849
using namespace sofa::helper::system::thread;
@@ -59,7 +60,9 @@ Haply_Inverse3Controller::Haply_Inverse3Controller()
5960
, d_orientationBase(initData(&d_orientationBase, Quat(0, 0, 0, 1), "orientationBase", "Orientation of the device base in the SOFA scene world coordinates"))
6061
, d_scale(initData(&d_scale, 1.0, "scale", "Default scale applied to the Device coordinates"))
6162

62-
, d_handleButton(initData(&d_handleButton, false, "handleButton", "Bool value showing if handle button is pressed"))
63+
, d_handleButtonA(initData(&d_handleButtonA, false, "handleButtonA", "Bool value returning if VerseGrip Stylus first button (resp. VerseGrip Quill single button) is pressed."))
64+
, d_handleButtonB(initData(&d_handleButtonB, false, "handleButtonB", "Bool value returning if VerseGrip Stylus second button is pressed"))
65+
, d_handleButtonC(initData(&d_handleButtonC, false, "handleButtonC", "Bool value returning if VerseGrip Stylus calibrate button is pressed"))
6366
, d_posDevice(initData(&d_posDevice, "positionDevice", "position of the device end-effector in SOFA frame"))
6467
, d_rawForceDevice(initData(&d_rawForceDevice, "rawForceDevice", "For debug: raw values sent to the device in the device frame"))
6568
, d_dampingForce(initData(&d_dampingForce, 0.0001, "damping", "Default damping applied to the force feedback"))
@@ -73,10 +76,17 @@ Haply_Inverse3Controller::Haply_Inverse3Controller()
7376
d_hapticIdentity.setGroup("Infos");
7477

7578
d_posDevice.setReadOnly(true);
76-
d_handleButton.setReadOnly(true);
79+
d_handleButtonA.setReadOnly(true);
80+
d_handleButtonB.setReadOnly(true);
81+
d_handleButtonC.setReadOnly(true);
82+
this->addAlias(&this->d_handleButtonA, "handleButton");
7783
d_rawForceDevice.setReadOnly(true);
84+
7885
d_posDevice.setGroup("Device Status");
79-
d_handleButton.setGroup("Device Status");
86+
d_handleButtonA.setGroup("Device Status");
87+
d_handleButtonB.setGroup("Device Status");
88+
d_handleButtonC.setGroup("Device Status");
89+
8090
d_rawForceDevice.setGroup("Device Status");
8191
}
8292

@@ -317,7 +327,7 @@ void Haply_Inverse3Controller::HapticsHandling(const std::string& msg)
317327
}
318328
}
319329

320-
if (data.contains(gripIdKey_))
330+
if (data.contains(gripIdKey_) && !data[gripIdKey_].empty())
321331
{
322332
// example of grip data
323333
// grip_id: 61576 -> {"button":false, "hall" : 1, "orientation" : {"w":0.71655273, "x" : 0.19647217, "y" : 0.6290283, "z" : -0.22833252},
@@ -336,9 +346,34 @@ void Haply_Inverse3Controller::HapticsHandling(const std::string& msg)
336346
m_hapticData.orientation[1] = qy;
337347
m_hapticData.orientation[2] = qz;
338348
m_hapticData.orientation[3] = qw;
339-
m_hapticData.buttonStatus = state["button"].get<bool>();
349+
m_hapticData.buttonA = state["button"].get<bool>();
350+
m_hapticData.buttonB = false;
351+
m_hapticData.buttonC = false;
340352
}
341353
}
354+
else if (data.contains(wirelessGripIdKey_) && !data[wirelessGripIdKey_].empty())
355+
{
356+
// example of wireless grip data
357+
// {"device_id":"1534","state":{"battery_level":0.55833346,"battery_voltage":3.935,"buttons":{"a":false,"b":false,"c":false},"hall":18,"orientation":{"w":-0.02947998, "x":-0.22390747, "y" : 0.65689087, "z" : 0.71691895},
358+
// "transform":{"position":{"x":0, "y" : 0, "z" : 0}, "rotation" : {"w":1, "x" : 0, "y" : 0, "z" : 0}, "scale" : {"x":1, "y" : 1, "z":1}}},"status":{"awake":true,"connected":true,"ready":true}}
359+
for (auto& el : data[wirelessGripIdKey_])
360+
{
361+
const json& state = el["state"];
362+
363+
float qx = state["orientation"]["x"].get<float>();
364+
float qy = state["orientation"]["y"].get<float>();
365+
float qz = state["orientation"]["z"].get<float>();
366+
float qw = state["orientation"]["w"].get<float>();
367+
368+
m_hapticData.orientation[0] = qx;
369+
m_hapticData.orientation[1] = qy;
370+
m_hapticData.orientation[2] = qz;
371+
m_hapticData.orientation[3] = qw;
372+
m_hapticData.buttonA = state["buttons"]["a"].get<bool>();
373+
m_hapticData.buttonB = state["buttons"]["b"].get<bool>();
374+
m_hapticData.buttonC = state["buttons"]["c"].get<bool>();
375+
}
376+
}
342377

343378
m_ws->send(request.dump());
344379

@@ -405,7 +440,9 @@ void Haply_Inverse3Controller::simulation_updatePosition()
405440
Vec3 position = { m_simuData.position[0], m_simuData.position[1], m_simuData.position[2] };
406441

407442
// Update Data on button status
408-
d_handleButton.setValue(m_simuData.buttonStatus);
443+
d_handleButtonA.setValue(m_simuData.buttonA);
444+
d_handleButtonB.setValue(m_simuData.buttonB);
445+
d_handleButtonC.setValue(m_simuData.buttonC);
409446

410447
Quat ori = { m_simuData.orientation[0], m_simuData.orientation[1], m_simuData.orientation[2], m_simuData.orientation[3] };
411448

@@ -454,7 +491,7 @@ void Haply_Inverse3Controller::draw(const sofa::core::visual::VisualParams* vpar
454491

455492
// Debug: Draw force feedback vector
456493
sofa::type::RGBAColor color4(1.0f, 0.0, 0.0f, 1.0);
457-
if (d_handleButton.getValue())
494+
if (d_handleButtonA.getValue())
458495
{
459496
color4 = sofa::type::RGBAColor(0.0f, 1.0, 0.0f, 1.0);
460497
}

src/SofaHaplyRobotics/Haply_Inverse3Controller.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ class SOFA_HAPLYROBOTICS_API Haply_Inverse3Controller : public Controller
105105
Data<SReal> d_scale; ///< Default scale applied to the device Coordinates
106106

107107
/// Output Data
108-
Data<bool> d_handleButton; ///< Bool value showing if handle button is pressed
108+
Data<bool> d_handleButtonA; ///< Bool value showing if First button is pressed
109+
Data<bool> d_handleButtonB; ///< Bool value showing if Second button is pressed
110+
Data<bool> d_handleButtonC; ///< Bool value showing if Third button is pressed
109111
Data<Coord> d_posDevice; ///< position of the device end-effector in SOFA Frame. Take into account @sa d_positionBase, @sa d_orientationBase and @sa d_scale
110112
Data<Vec3> d_rawForceDevice; ///< For debug: raw values sent to the device in the device frame
111113
Data<SReal> d_dampingForce; ///< Damping value, it is a factor applied to the velocity and substracted to force feedback to avoid oscillations.
@@ -127,7 +129,9 @@ class SOFA_HAPLYROBOTICS_API Haply_Inverse3Controller : public Controller
127129
float position[3]; // raw position of the end-effector
128130
float orientation[4]; // raw quaternion of the handle
129131
float force[3]; // debug raw force vector sent to the device
130-
bool buttonStatus; // button press status
132+
bool buttonA; // button A press status
133+
bool buttonB; // button B press status
134+
bool buttonC; // button C press status
131135
};
132136

133137
/// Data belonging to the haptic thread only
@@ -163,6 +167,7 @@ class SOFA_HAPLYROBOTICS_API Haply_Inverse3Controller : public Controller
163167
static const std::string inverseKey_;
164168
static const std::string deviceIdKey_;
165169
static const std::string gripIdKey_;
170+
static const std::string wirelessGripIdKey_;
166171
};
167172

168173
} // namespace sofa::HaplyRobotics

0 commit comments

Comments
 (0)