-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteleop_spacenav.cpp
206 lines (171 loc) · 6.53 KB
/
teleop_spacenav.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Copyright (c) 2008, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Author: Stuart Glaser
* modified by: Jan Heuer, Riccardo Polvara
*/
#include "geometry_msgs/Twist.h"
#include "geometry_msgs/Vector3.h"
#include "ros/node_handle.h"
#include "sensor_msgs/Joy.h"
#include "spnav.h"
#include <ros/ros.h>
#include <stdio.h>
#include "deep_reinforced_landing/SendCommand.h"
#include "std_msgs/Empty.h"
#define FULL_SCALE (350.0)
// Used to scale joystick output to be in [-1, 1]. Estimated from data, and not
// necessarily correct.
int main(int argc, char** argv)
{
ros::init(argc, argv, "ardrone3dnav");
ros::NodeHandle node_handle;
// ros::Publisher offset_pub =
// node_handle.advertise<geometry_msgs::Vector3>("spacenav/offset", 2);
// ros::Publisher rot_offset_pub =
// node_handle.advertise<geometry_msgs::Vector3>("spacenav/rot_offset", 2);
ros::Publisher twist_pub =
node_handle.advertise<geometry_msgs::Twist>("/quadrotor/cmd_vel", 2);
// ros::Publisher joy_pub = node_handle.advertise<joy::Joy>("spacenav/joy",
// 2);
ros::Publisher land_pub =
node_handle.advertise<std_msgs::Empty>("/quadrotor/ardrone/land", 2);
ros::Publisher takeoff_pub =
node_handle.advertise<std_msgs::Empty>("/quadrotor/ardrone/takeoff", 2);
ros::Publisher reset_pub =
node_handle.advertise<std_msgs::Empty>("/quadrotor/ardrone/reset", 2);
ros::ServiceClient drl_client = node_handle.serviceClient<deep_reinforced_landing::SendCommand> ( "/drl/send_command" );
deep_reinforced_landing::SendCommandRequest req;
deep_reinforced_landing::SendCommandResponse res;
if (spnav_open() == -1)
{
ROS_ERROR("Could not open the space navigator device. Did you remember to "
"run spacenavd (as root)?");
return 1;
}
else
ROS_INFO("Properly connected to the space navigator device.");
sensor_msgs::Joy joystick_msg;
joystick_msg.axes.resize(6);
joystick_msg.buttons.resize(2);
spnav_event sev;
int no_motion_count = 0;
bool motion_stale = false;
geometry_msgs::Vector3 offset_msg;
geometry_msgs::Vector3 rot_offset_msg;
geometry_msgs::Twist twist_msg;
std_msgs::Empty empty_msg;
while (node_handle.ok())
{
bool joy_stale = false;
bool queue_empty = false;
// Sleep when the queue is empty.
// If the queue is empty 30 times in a row output zeros.
// Output changes each time a button event happens, or when a motion
// event happens and the queue is empty.
switch (spnav_poll_event(&sev))
{
case 0:
queue_empty = true;
if (++no_motion_count > 30)
{
offset_msg.x = offset_msg.y = offset_msg.z = 0;
rot_offset_msg.x = rot_offset_msg.y = rot_offset_msg.z = 0;
no_motion_count = 0;
motion_stale = true;
}
break;
case SPNAV_EVENT_MOTION:
offset_msg.x = sev.motion.z / FULL_SCALE;
offset_msg.y = -sev.motion.x / FULL_SCALE;
offset_msg.z = sev.motion.y / FULL_SCALE;
rot_offset_msg.x = sev.motion.rz / FULL_SCALE;
rot_offset_msg.y = -sev.motion.rx / FULL_SCALE;
rot_offset_msg.z = sev.motion.ry / FULL_SCALE;
// printf("%lf %lf %lf\n", rot_offset_msg.x, rot_offset_msg.y,
// rot_offset_msg.z);
motion_stale = true;
break;
case SPNAV_EVENT_BUTTON:
// printf("type, press, bnum = <%d, %d, %d>\n", sev.button.type,
// sev.button.press, sev.button.bnum);
// joystick_msg.buttons[sev.button.bnum] = sev.button.press;
if (sev.button.press == 1)
{
switch (sev.button.bnum)
{
case 0:
//takeoff_pub.publish(empty_msg);
req.command = "takeoff";
ros::service::call<>("/drl/send_command", req, res );
ROS_INFO("The drone is taking off!");
break;
case 1:
req.command = "descend";
ros::service::call<>("/drl/send_command", req, res );
//land_pub.publish(empty_msg);
ROS_INFO("The drone is landing");
break;
case 6:
reset_pub.publish(empty_msg);
break;
}
}
joy_stale = true;
break;
default:
ROS_WARN("Unknown message type in spacenav. This should never happen.");
break;
}
if (motion_stale && (queue_empty || joy_stale))
{
// offset_pub.publish(offset_msg);
// rot_offset_pub.publish(rot_offset_msg);
twist_msg.linear = offset_msg;
twist_msg.angular = rot_offset_msg;
twist_pub.publish(twist_msg);
// joystick_msg.axes[0] = offset_msg.x / FULL_SCALE;
// joystick_msg.axes[1] = offset_msg.y / FULL_SCALE;
// joystick_msg.axes[2] = offset_msg.z / FULL_SCALE;
// joystick_msg.axes[3] = rot_offset_msg.x / FULL_SCALE;
// joystick_msg.axes[4] = rot_offset_msg.y / FULL_SCALE;
// joystick_msg.axes[5] = rot_offset_msg.z / FULL_SCALE;
no_motion_count = 0;
motion_stale = false;
joy_stale = true;
}
// if (joy_stale)
//{
// joy_pub.publish(joystick_msg);
//}
if (queue_empty)
usleep(1000);
}
return 0;
}