31using namespace KITHand;
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);
45 "MAC-Address of the hand to connect to.");
47 "AutomaticReconnectActive",
49 "Whether the hand unit should try to reconnect after the connection is lost.");
53 "Wheather to keep scanning until the hand with the given MAC-Address is found.");
78 _driver = std::make_unique<KITHandCommunicationDriver>();
79 _driver->registerConnectionStateChangedCallback(
87 KITHandCommunicationDriver::ScanMode mode =
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));
128 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() -
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")
198 const std::uint64_t pos = std::clamp(
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();
216 NameValueMap jointValues;
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());
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Property< PropertyType > getProperty(const std::string &name)
HandUnitPropertyDefinitions(std::string prefix)
SingleTypeVariantListPtr shapeNames
List containing the names of all valid shapes.
KITHandUnitPropertyDefinitions(std::string prefix)
void addShapeName(const std::string &name)
void onInitHandUnit() override
void setShape(const std::string &shapeName, const Ice::Current &=Ice::emptyCurrent) override
void onExitHandUnit() override
void connectionStateChangedCallback(const KITHand::State state)
RemoteGui::WidgetPtr buildGui()
virtual armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void addShape(const std::string &name, const std::map< std::string, float > &shape)
void onStartHandUnit() override
void setJointAngles(const NameValueMap &targetJointAngles, const Ice::Current &=Ice::emptyCurrent) override
void processGui(RemoteGui::TabProxy &prx)
NameValueMap getCurrentJointValues(const Ice::Current &=Ice::emptyCurrent) override
virtual std::string getDefaultName() const override
SpamFilterDataPtr deactivateSpam(float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
disables the logging for the current line for the given amount of seconds.
ArmarXManagerPtr getArmarXManager() const
Returns the ArmarX manager used to add and remove components.
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
PropertyDefinition< PropertyType > & defineOptionalProperty(const std::string &name, PropertyType defaultValue, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
PropertyDefinition< PropertyType > & defineRequiredProperty(const std::string &name, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
void createOrUpdateRemoteGuiTab(Ts &&... ts)
bool getButtonClicked(std::string const &name)
ValueProxy< T > getValue(std::string const &name)
Class that offers the main functionality needed to create a statechart.
The Variant class is described here: Variants.
void setString(const std::string &s, const Ice::Current &c=Ice::emptyCurrent) override
Sets the Variant's value to s.
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
#define ARMARX_INFO
The normal logging level.
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
detail::CheckBoxBuilder makeCheckBox(std::string const &name)
detail::FloatSpinBoxBuilder makeFloatSpinBox(std::string const &name)
detail::SimpleGridLayoutBuilder makeSimpleGridLayout(std::string const &name="")
detail::ButtonBuilder makeButton(std::string const &name)
detail::LineEditBuilder makeLineEdit(std::string const &name)
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
SimpleGridLayoutBuilder & addTextLabel(std::string const &text, int colspan)
SimpleGridLayoutBuilder & cols(int n)
SimpleGridLayoutBuilder & addChild(WidgetPtr const &child, int colspan)