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
30
31namespace armarx
32{
33 void
35 {
36 ARMARX_INFO << "oninit GamepadControlUnit";
37 usingProxy(getProperty<std::string>("PlatformUnitName").getValue());
38 usingTopic(getProperty<std::string>("GamepadTopicName").getValue());
39 usingProxy("EmergencyStopMaster");
40
41 scaleX = getProperty<float>("ScaleX").getValue();
42 scaleY = getProperty<float>("ScaleY").getValue();
43 scaleRotation = getProperty<float>("ScaleAngle").getValue();
44 ARMARX_INFO << "oninit GamepadControlUnit end";
45 }
46
47 void
49 {
50 ARMARX_INFO << "onConnect GamepadControlUnit";
51 platformUnitPrx = getProxy<PlatformUnitInterfacePrx>(
52 getProperty<std::string>("PlatformUnitName").getValue());
53 emergencyStop = getProxy<EmergencyStopMasterInterfacePrx>("EmergencyStopMaster");
54
55
56 if (enableHeartBeat)
57 {
58 this->heartbeatPlugin->signUp(armarx::core::time::Duration::MilliSeconds(1000),
60 {"Gamepad"},
61 "The GamepadControlUnit");
62 }
63 }
64
65 void
69
70 void
72 {
73 ARMARX_INFO << "exit GamepadControlUnit";
74 }
75
78 {
81 defs->optional(enableHeartBeat,
82 "EnableHeartBeat",
83 "Flag to enable send a heart beat to the robot healh topic");
84 return defs;
85 }
86
87 void
88 GamepadControlUnit::reportGamepadState(const std::string& device,
89 const std::string& name,
90 const GamepadData& data,
91 const TimestampBasePtr& timestamp,
92 const Ice::Current& c)
93 {
94
95
96 // struct GamepadData {
97 // float leftStickX;
98 // float leftStickY;
99 // float rightStickX;
100 // float rightStickY;
101 // float dPadX;
102 // float dPadY;
103 // float leftTrigger;
104 // float rightTrigger;
105 //
106 // bool leftButton;
107 // bool rightButton;
108 // bool backButton;
109 // bool startButton;
110 // bool xButton;
111 // bool yButton;
112 // bool aButton;
113 // bool bButton;
114 // bool theMiddleButton;
115 // bool leftStickButton;
116 // bool rightStickButton;
117 //
118 // };
119
120
121 if (data.leftTrigger > 0)
122 {
123 emergencyStop->setEmergencyStopState(EmergencyStopState::eEmergencyStopActive);
124 return;
125 }
126 else if (data.startButton)
127 {
128 emergencyStop->setEmergencyStopState(EmergencyStopState::eEmergencyStopInactive);
129 }
130 //scales are for the robot axis
131 if (data.rightTrigger > 0.01)
132 {
133 platformUnitPrx->move(data.leftStickY * scaleX,
134 data.leftStickX * scaleY,
135 data.rightStickX * scaleRotation);
136 isGamepadPlatformControlActive = true;
137 }
138 else if (isGamepadPlatformControlActive)
139 {
140 platformUnitPrx->move(0, 0, 0);
141 isGamepadPlatformControlActive = false;
142 }
143
144
145 if (data.leftButton)
146 {
147
148 if (leftHandTime <= 0.0)
149 {
150 leftHandTime = IceUtil::Time::now().toMicroSeconds();
151 }
152 else if ((IceUtil::Time::now().toMicroSeconds() - leftHandTime) > 1000 * 1000)
153 {
154
155 HandUnitInterfacePrx handUnit = getProxy<HandUnitInterfacePrx>("LeftHandUnit");
156 if (handUnit)
157 {
158 std::string shapeName = (leftHandOpen) ? "Close" : "Open";
159 handUnit->setShape(shapeName);
160 leftHandOpen = !leftHandOpen;
161 leftHandTime = 0.0;
162 }
163 }
164 }
165 else
166 {
167 leftHandTime = 0.0;
168 }
169
170 if (data.rightButton)
171 {
172
173 if (rightHandTime <= 0.0)
174 {
175 rightHandTime = IceUtil::Time::now().toMicroSeconds();
176 }
177 else if ((IceUtil::Time::now().toMicroSeconds() - rightHandTime) > 1000 * 1000)
178 {
179 HandUnitInterfacePrx handUnit = getProxy<HandUnitInterfacePrx>("RightHandUnit");
180 if (handUnit)
181 {
182 std::string shapeName = (rightHandOpen) ? "Close" : "Open";
183 handUnit->setShape(shapeName);
184 rightHandOpen = !rightHandOpen;
185 rightHandTime = 0.0;
186 }
187 }
188 }
189 else
190 {
191 rightHandTime = 0.0;
192 }
193
194 if (enableHeartBeat)
195 {
196 heartbeatPlugin->heartbeat();
197 }
198
199 //ARMARX_INFO << "sending targets" << data.leftStickX* scaleX << " " << data.leftStickY* scaleY << " " << data.rightStickX* scaleRotation;
200 }
201
203 {
204 addPlugin(heartbeatPlugin);
205 }
206
208} // namespace armarx
std::string timestamp()
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
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)
Brief description of class GamepadControlUnit.
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.