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.01)
131 {
132 platformUnitPrx->move(data.leftStickY * scaleX,
133 data.leftStickX * scaleY,
134 data.rightStickX * scaleRotation);
135 isGamepadPlatformControlActive = true;
136 }
137 else if (isGamepadPlatformControlActive)
138 {
139 platformUnitPrx->move(0, 0, 0);
140 isGamepadPlatformControlActive = false;
141 }
142
143
144 if (data.leftButton)
145 {
146
147 if (leftHandTime <= 0.0)
148 {
149 leftHandTime = IceUtil::Time::now().toMicroSeconds();
150 }
151 else if ((IceUtil::Time::now().toMicroSeconds() - leftHandTime) > 1000 * 1000)
152 {
153
154 HandUnitInterfacePrx handUnit = getProxy<HandUnitInterfacePrx>("LeftHandUnit");
155 if (handUnit)
156 {
157 std::string shapeName = (leftHandOpen) ? "Close" : "Open";
158 handUnit->setShape(shapeName);
159 leftHandOpen = !leftHandOpen;
160 leftHandTime = 0.0;
161 }
162 }
163 }
164 else
165 {
166 leftHandTime = 0.0;
167 }
168
169 if (data.rightButton)
170 {
171
172 if (rightHandTime <= 0.0)
173 {
174 rightHandTime = IceUtil::Time::now().toMicroSeconds();
175 }
176 else if ((IceUtil::Time::now().toMicroSeconds() - rightHandTime) > 1000 * 1000)
177 {
178 HandUnitInterfacePrx handUnit = getProxy<HandUnitInterfacePrx>("RightHandUnit");
179 if (handUnit)
180 {
181 std::string shapeName = (rightHandOpen) ? "Close" : "Open";
182 handUnit->setShape(shapeName);
183 rightHandOpen = !rightHandOpen;
184 rightHandTime = 0.0;
185 }
186 }
187 }
188 else
189 {
190 rightHandTime = 0.0;
191 }
192
193 if (enableHeartBeat)
194 {
195 heartbeatPlugin->heartbeat();
196 }
197
198 //ARMARX_INFO << "sending targets" << data.leftStickX* scaleX << " " << data.leftStickY* scaleY << " " << data.rightStickX* scaleRotation;
199 }
200
202 {
203 addPlugin(heartbeatPlugin);
204 }
205} // 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.