GamepadControlUnit.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package RobotAPI::ArmarXObjects::GamepadControlUnit
17 * @author Simon Ottenhaus ( simon dot ottenhaus at kit dot edu )
18 * @date 2017
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#include "GamepadControlUnit.h"
24
29
30namespace armarx
31{
32 void
34 {
35 ARMARX_INFO << "oninit GamepadControlUnit";
36 usingProxy(getProperty<std::string>("PlatformUnitName").getValue());
37 usingTopic(getProperty<std::string>("GamepadTopicName").getValue());
38 usingProxy("EmergencyStopMaster");
39
40 scaleX = getProperty<float>("ScaleX").getValue();
41 scaleY = getProperty<float>("ScaleY").getValue();
42 scaleRotation = getProperty<float>("ScaleAngle").getValue();
43 ARMARX_INFO << "oninit GamepadControlUnit end";
44 }
45
46 void
48 {
49 ARMARX_INFO << "onConnect GamepadControlUnit";
50 platformUnitPrx = getProxy<PlatformUnitInterfacePrx>(
51 getProperty<std::string>("PlatformUnitName").getValue());
52 emergencyStop = getProxy<EmergencyStopMasterInterfacePrx>("EmergencyStopMaster");
53
54
55 if (enableHeartBeat)
56 {
57 this->heartbeatPlugin->signUp(armarx::core::time::Duration::MilliSeconds(1000),
59 {"Gamepad"},
60 "The GamepadControlUnit");
61 }
62 }
63
64 void
68
69 void
71 {
72 ARMARX_INFO << "exit GamepadControlUnit";
73 }
74
77 {
80 defs->optional(enableHeartBeat,
81 "EnableHeartBeat",
82 "Flag to enable send a heart beat to the robot healh topic");
83 return defs;
84 }
85
86 void
87 GamepadControlUnit::reportGamepadState(const std::string& device,
88 const std::string& name,
89 const GamepadData& data,
90 const TimestampBasePtr& timestamp,
91 const Ice::Current& c)
92 {
93
94
95 // struct GamepadData {
96 // float leftStickX;
97 // float leftStickY;
98 // float rightStickX;
99 // float rightStickY;
100 // float dPadX;
101 // float dPadY;
102 // float leftTrigger;
103 // float rightTrigger;
104 //
105 // bool leftButton;
106 // bool rightButton;
107 // bool backButton;
108 // bool startButton;
109 // bool xButton;
110 // bool yButton;
111 // bool aButton;
112 // bool bButton;
113 // bool theMiddleButton;
114 // bool leftStickButton;
115 // bool rightStickButton;
116 //
117 // };
118
119
120 if (data.leftTrigger > 0)
121 {
122 emergencyStop->setEmergencyStopState(EmergencyStopState::eEmergencyStopActive);
123 return;
124 }
125 else if (data.startButton)
126 {
127 emergencyStop->setEmergencyStopState(EmergencyStopState::eEmergencyStopInactive);
128 }
129 //scales are for the robot axis
130 if (data.rightTrigger > 0)
131 {
132 platformUnitPrx->move(data.leftStickY * scaleX,
133 data.leftStickX * scaleY,
134 data.rightStickX * scaleRotation);
135 }
136 else
137 {
138 platformUnitPrx->move(0, 0, 0);
139 }
140
141
142 if (data.leftButton)
143 {
144
145 if (leftHandTime <= 0.0)
146 {
147 leftHandTime = IceUtil::Time::now().toMicroSeconds();
148 }
149 else if ((IceUtil::Time::now().toMicroSeconds() - leftHandTime) > 1000 * 1000)
150 {
151
152 HandUnitInterfacePrx handUnit = getProxy<HandUnitInterfacePrx>("LeftHandUnit");
153 if (handUnit)
154 {
155 std::string shapeName = (leftHandOpen) ? "Close" : "Open";
156 handUnit->setShape(shapeName);
157 leftHandOpen = !leftHandOpen;
158 leftHandTime = 0.0;
159 }
160 }
161 }
162 else
163 {
164 leftHandTime = 0.0;
165 }
166
167 if (data.rightButton)
168 {
169
170 if (rightHandTime <= 0.0)
171 {
172 rightHandTime = IceUtil::Time::now().toMicroSeconds();
173 }
174 else if ((IceUtil::Time::now().toMicroSeconds() - rightHandTime) > 1000 * 1000)
175 {
176 HandUnitInterfacePrx handUnit = getProxy<HandUnitInterfacePrx>("RightHandUnit");
177 if (handUnit)
178 {
179 std::string shapeName = (rightHandOpen) ? "Close" : "Open";
180 handUnit->setShape(shapeName);
181 rightHandOpen = !rightHandOpen;
182 rightHandTime = 0.0;
183 }
184 }
185 }
186 else
187 {
188 rightHandTime = 0.0;
189 }
190
191 if (enableHeartBeat)
192 {
193 heartbeatPlugin->heartbeat();
194 }
195
196 //ARMARX_INFO << "sending targets" << data.leftStickX* scaleX << " " << data.leftStickY* scaleY << " " << data.rightStickX* scaleRotation;
197 }
198
200 {
201 addPlugin(heartbeatPlugin);
202 }
203} // namespace armarx
std::string timestamp()
constexpr T c
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void reportGamepadState(const std::string &device, const std::string &name, const GamepadData &data, const TimestampBasePtr &timestamp, const Ice::Current &c) override
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
PluginT * addPlugin(const std::string prefix="", ParamsT &&... params)
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition Duration.cpp:48
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.