39 #include "geometry_msgs/Vector3.h" 40 #include "geometry_msgs/Twist.h" 41 #include "sensor_msgs/Joy.h" 52 if (param.size() == 0)
59 if (param.size() == 3)
63 if (param.size() == 1)
65 param.push_back(param[0]);
66 param.push_back(param[0]);
72 int main(
int argc,
char **argv)
85 private_nh.param<
double>(
"full_scale", full_scale, 512);
86 if (full_scale < 1e-10)
92 std::vector<double> linear_scale;
93 std::vector<double> angular_scale;
94 private_nh.param<std::vector<double> >(
"linear_scale", linear_scale, std::vector<double>(3, 1));
95 private_nh.param<std::vector<double> >(
"angular_scale", angular_scale, std::vector<double>(3, 1));
99 ROS_ERROR_STREAM(
"Parameter " << private_nh.getNamespace() <<
"/linear_scale must have either one or three components.");
104 ROS_ERROR_STREAM(
"Parameter " << private_nh.getNamespace() <<
"/angular_scale must have either one or three components.");
107 ROS_DEBUG(
"linear_scale: %.3f %.3f %.3f", linear_scale[0], linear_scale[1], linear_scale[2]);
108 ROS_DEBUG(
"angular_scale: %.3f %.3f %.3f", angular_scale[0], angular_scale[1], angular_scale[2]);
110 if (spnav_open() == -1)
112 ROS_ERROR(
"Could not open the space navigator device. Did you remember to run spacenavd (as root)?");
119 int static_count_threshold = 30;
123 bool zero_when_static =
true;
129 double static_trans_deadband = 0.1;
130 double static_rot_deadband = 0.1;
134 sensor_msgs::Joy joystick_msg;
135 joystick_msg.axes.resize(6);
136 joystick_msg.buttons.resize(2);
139 int no_motion_count = 0;
140 bool motion_stale =
false;
141 double normed_vx = 0;
142 double normed_vy = 0;
143 double normed_vz = 0;
144 double normed_wx = 0;
145 double normed_wy = 0;
146 double normed_wz = 0;
147 while (node_handle.
ok())
149 bool joy_stale =
false;
150 bool queue_empty =
false;
157 switch (spnav_poll_event(&sev))
161 if (++no_motion_count > static_count_threshold)
163 if ( zero_when_static ||
164 ( fabs(normed_vx) < static_trans_deadband &&
165 fabs(normed_vy) < static_trans_deadband &&
166 fabs(normed_vz) < static_trans_deadband)
169 normed_vx = normed_vy = normed_vz = 0;
172 if ( zero_when_static ||
173 ( fabs(normed_wx) < static_rot_deadband &&
174 fabs(normed_wy) < static_rot_deadband &&
175 fabs(normed_wz) < static_rot_deadband )
178 normed_wx = normed_wy = normed_wz = 0;
186 case SPNAV_EVENT_MOTION:
187 normed_vx = sev.motion.z / full_scale;
188 normed_vy = -sev.motion.x / full_scale;
189 normed_vz = sev.motion.y / full_scale;
191 normed_wx = sev.motion.rz / full_scale;
192 normed_wy = -sev.motion.rx / full_scale;
193 normed_wz = sev.motion.ry / full_scale;
198 case SPNAV_EVENT_BUTTON:
200 joystick_msg.buttons[sev.button.bnum] = sev.button.press;
206 ROS_WARN(
"Unknown message type in spacenav. This should never happen.");
210 if (motion_stale && (queue_empty || joy_stale))
213 geometry_msgs::Vector3 offset_msg;
214 offset_msg.x = normed_vx * linear_scale[0];
215 offset_msg.y = normed_vy * linear_scale[1];
216 offset_msg.z = normed_vz * linear_scale[2];
217 offset_pub.
publish(offset_msg);
218 geometry_msgs::Vector3 rot_offset_msg;
219 rot_offset_msg.x = normed_wx * angular_scale[0];
220 rot_offset_msg.y = normed_wy * angular_scale[1];
221 rot_offset_msg.z = normed_wz * angular_scale[2];
222 rot_offset_pub.publish(rot_offset_msg);
224 geometry_msgs::Twist twist_msg;
225 twist_msg.linear = offset_msg;
226 twist_msg.angular = rot_offset_msg;
227 twist_pub.publish(twist_msg);
230 joystick_msg.axes[0] = normed_vx;
231 joystick_msg.axes[1] = normed_vy;
232 joystick_msg.axes[2] = normed_vz;
233 joystick_msg.axes[3] = normed_wx;
234 joystick_msg.axes[4] = normed_wy;
235 joystick_msg.axes[5] = normed_wz;
238 motion_stale =
false;
244 joy_pub.publish(joystick_msg);
void publish(const boost::shared_ptr< M > &message) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int main(int argc, char **argv)
ROSCPP_DECL bool get(const std::string &key, std::string &s)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define ROS_ERROR_STREAM(args)
bool ensureThreeComponents(std::vector< double > ¶m)