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 
30 namespace 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
66  {
67  }
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 
192  if (enableHeartBeat)
193  {
194  auto now = armarx::core::time::dto::DateTime();
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 } // namespace armarx
armarx::plugins::HeartbeatComponentPlugin::heartbeat
void heartbeat()
Sends out a heartbeat using the default config.
Definition: HeartbeatComponentPlugin.cpp:80
armarx::plugins::HeartbeatComponentPlugin::signUp
void signUp(const std::string &channelName="", const std::vector< std::string > &aliases={}, const std::string &description="")
register component to heartbeat
Definition: HeartbeatComponentPlugin.cpp:14
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
DateTime.h
armarx::core::time::toIce
void toIce(dto::ClockType::ClockTypeEnum &dto, const ClockType &bo)
Definition: ice_conversions.cpp:33
Duration.h
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::GamepadControlUnit::onInitComponent
void onInitComponent() override
Definition: GamepadControlUnit.cpp:33
ice_conversions.h
armarx::ManagedIceObject::addPlugin
PluginT * addPlugin(const std::string prefix="", ParamsT &&...params)
Definition: ManagedIceObject.h:182
armarx::GamepadControlUnit::reportGamepadState
void reportGamepadState(const std::string &device, const std::string &name, const GamepadData &data, const TimestampBasePtr &timestamp, const Ice::Current &c) override
Definition: GamepadControlUnit.cpp:87
armarx::GamepadControlUnitPropertyDefinitions
Definition: GamepadControlUnit.h:43
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::GamepadControlUnit::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: GamepadControlUnit.cpp:76
TimestampVariant.h
armarx::GamepadControlUnit::onExitComponent
void onExitComponent() override
Definition: GamepadControlUnit.cpp:70
armarx::GamepadControlUnit::GamepadControlUnit
GamepadControlUnit()
Definition: GamepadControlUnit.cpp:202
armarx::GamepadControlUnit::onDisconnectComponent
void onDisconnectComponent() override
Definition: GamepadControlUnit.cpp:65
armarx::ManagedIceObject::usingTopic
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Definition: ManagedIceObject.cpp:248
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
IceUtil::Handle< class PropertyDefinitionContainer >
GamepadControlUnit.h
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:151
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::core::time::Duration::MilliSeconds
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition: Duration.cpp:55
armarx::GamepadControlUnit::onConnectComponent
void onConnectComponent() override
Definition: GamepadControlUnit.cpp:47