31 using namespace KITHand;
35 KITHandUnitPropertyDefinitions::KITHandUnitPropertyDefinitions(std::string prefix) :
38 defineOptionalProperty<KITHandCommunicationDriver::ScanMode>(
40 KITHandCommunicationDriver::ScanMode::Bluetooth,
41 "Type of the connection to the hand, either Bluetooth or Serial")
42 .map(
"Bluetooth", KITHandCommunicationDriver::ScanMode::Bluetooth)
43 .map(
"Serial", KITHandCommunicationDriver::ScanMode::Serial);
44 defineRequiredProperty<std::string>(
"MAC-Address",
45 "MAC-Address of the hand to connect to.");
46 defineOptionalProperty<bool>(
47 "AutomaticReconnectActive",
49 "Whether the hand unit should try to reconnect after the connection is lost.");
50 defineOptionalProperty<bool>(
53 "Wheather to keep scanning until the hand with the given MAC-Address is found.");
54 defineOptionalProperty<int>(
"ScanTimeout", 5,
"Timeout for scanning repeatedly.");
78 _driver = std::make_unique<KITHandCommunicationDriver>();
79 _driver->registerConnectionStateChangedCallback(
86 std::string macAddress = getProperty<std::string>(
"MAC-Address");
87 KITHandCommunicationDriver::ScanMode mode =
88 getProperty<KITHandCommunicationDriver::ScanMode>(
"ConnectionType").getValue();
90 auto start = std::chrono::system_clock::now();
93 std::vector<HandDevice> devices = _driver->scanForDevices(mode);
94 for (HandDevice& d : devices)
96 if (mode == KITHandCommunicationDriver::ScanMode::Bluetooth)
98 ARMARX_INFO <<
"Found device with MAC-Address: " << d.macAdress;
100 else if (mode == KITHandCommunicationDriver::ScanMode::Serial)
102 ARMARX_INFO <<
"Found device at serial port: " << d.serialDeviceFile;
108 if (d.macAdress == macAddress ||
109 (mode == KITHandCommunicationDriver::ScanMode::Serial))
111 d.hardwareTarget = mode == KITHandCommunicationDriver::ScanMode::Bluetooth
112 ? KITHand::HardwareTarget::Bluetooth
113 : KITHand::HardwareTarget::Serial;
115 while (!_driver->connected())
117 std::this_thread::sleep_for(std::chrono::milliseconds(100));
127 }
while (getProperty<bool>(
"ScanUntilHandFound").getValue() && !found &&
128 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() -
130 .count() < getProperty<int>(
"ScanTimeout").getValue());
132 if (mode == KITHandCommunicationDriver::ScanMode::Bluetooth)
134 ARMARX_WARNING <<
"Could not find hand with the given MAC-Address: " << macAddress
135 <<
" Shutting down this KITHandUnit.";
140 <<
"Could not find hand ove serial ports. Shutting down this KITHHandUnit";
149 _driver->disconnect();
156 if (std::regex_match(shapeName, std::regex{
"[gG](0|[1-9][0-9]*)"}) &&
157 _driver->getCurrentConnectedDevice()->abilities.receivesAdditionalGraspCommands)
159 _driver->sendGrasp(std::stoul(shapeName.substr(1)));
161 else if (shapeName ==
"Open")
163 _driver->sendGrasp(0);
165 else if (shapeName ==
"Close")
167 _driver->sendGrasp(1);
169 else if (!_shapes.count(shapeName))
171 ARMARX_WARNING <<
"Unknown shape name '" << shapeName <<
"'\nKnown shapes: " << _shapes;
184 for (
const std::pair<std::string, float>& pair : targetJointAngles)
186 if (pair.first ==
"Fingers")
188 const std::uint64_t pos =
189 std::clamp(
static_cast<std::uint64_t
>(pair.second *
190 KITHand::ControlOptions::maxPosFingers),
191 static_cast<std::uint64_t
>(0),
192 KITHand::ControlOptions::maxPosFingers);
194 _driver->sendFingersPosition(pos);
196 else if (pair.first ==
"Thumb")
199 static_cast<std::uint64_t
>(pair.second * KITHand::ControlOptions::maxPosThumb),
200 static_cast<std::uint64_t
>(0),
201 KITHand::ControlOptions::maxPosThumb);
203 _driver->sendThumbPosition(pos);
207 ARMARX_WARNING <<
"Invalid HandJointName '" << pair.first <<
"', ignoring.";
209 _driver->waitForCommunicationMedium();
217 jointValues[
"Fingers"] =
218 _driver->getFingersPos() * 1.f / KITHand::ControlOptions::maxPosFingers;
219 jointValues[
"Thumb"] = _driver->getThumbPos() * 1.f / KITHand::ControlOptions::maxPosThumb;
226 _shapes[name] = shape;
248 if (state == State::DeviceLost)
282 const auto fingers = prx.
getValue<
float>(
"Fingers").get();
283 const auto thumb = prx.
getValue<
float>(
"Thumb").get();
284 ARMARX_INFO <<
"setting Fingers " << fingers <<
" and Thumb " << thumb;
289 _driver->sendRaw(prx.
getValue<std::string>(
"Raw").get());