joy.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Fadri Furrer, ASL, ETH Zurich, Switzerland
3  * Copyright 2015 Michael Burri, ASL, ETH Zurich, Switzerland
4  * Copyright 2015 Mina Kamel, ASL, ETH Zurich, Switzerland
5  * Copyright 2015 Janosch Nikolic, ASL, ETH Zurich, Switzerland
6  * Copyright 2015 Markus Achtelik, ASL, ETH Zurich, Switzerland
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 
23 
25 
27  ros::NodeHandle nh;
28  ros::NodeHandle pnh("~");
29  ctrl_pub_ = nh_.advertise<mav_msgs::RollPitchYawrateThrust> (
31 
32  control_msg_.roll = 0;
33  control_msg_.pitch = 0;
34  control_msg_.yaw_rate = 0;
35  control_msg_.thrust.x = 0;
36  control_msg_.thrust.y = 0;
37  control_msg_.thrust.z = 0;
38  current_yaw_vel_ = 0;
39 
40  pnh.param("axis_roll_", axes_.roll, 0);
41  pnh.param("axis_pitch_", axes_.pitch, 1);
42  pnh.param("axis_thrust_", axes_.thrust, 2);
43 
44  pnh.param("axis_direction_roll", axes_.roll_direction, -1);
45  pnh.param("axis_direction_pitch", axes_.pitch_direction, 1);
46  pnh.param("axis_direction_thrust", axes_.thrust_direction, 1);
47 
48  pnh.param("max_v_xy", max_.v_xy, 1.0); // [m/s]
49  pnh.param("max_roll", max_.roll, 10.0 * M_PI / 180.0); // [rad]
50  pnh.param("max_pitch", max_.pitch, 10.0 * M_PI / 180.0); // [rad]
51  pnh.param("max_yaw_rate", max_.rate_yaw, 45.0 * M_PI / 180.0); // [rad/s]
52  pnh.param("max_thrust", max_.thrust, 30.0); // [N]
53 
54  pnh.param("v_yaw_step", v_yaw_step_, 0.05); // [rad/s]
55 
56  pnh.param("is_fixed_wing", is_fixed_wing_, false);
57 
58  pnh.param("button_yaw_left_", buttons_.yaw_left, 3);
59  pnh.param("button_yaw_right_", buttons_.yaw_right, 4);
60  pnh.param("button_ctrl_enable_", buttons_.ctrl_enable, 5);
61  pnh.param("button_ctrl_mode_", buttons_.ctrl_mode, 10);
62  pnh.param("button_takeoff_", buttons_.takeoff, 7);
63  pnh.param("button_land_", buttons_.land, 8);
64 
66  joy_sub_ = nh_.subscribe("joy", 10, &Joy::JoyCallback, this);
67 }
68 
69 void Joy::StopMav() {
70  control_msg_.roll = 0;
71  control_msg_.pitch = 0;
72  control_msg_.yaw_rate = 0;
73  control_msg_.thrust.x = 0;
74  control_msg_.thrust.y = 0;
75  control_msg_.thrust.z = 0;
76 }
77 
78 void Joy::JoyCallback(const sensor_msgs::JoyConstPtr& msg) {
79  current_joy_ = *msg;
80  control_msg_.roll = msg->axes[axes_.roll] * max_.roll * axes_.roll_direction;
81  control_msg_.pitch = msg->axes[axes_.pitch] * max_.pitch * axes_.pitch_direction;
82 
83  if (msg->buttons[buttons_.yaw_left]) {
85  }
86  else if (msg->buttons[buttons_.yaw_right]) {
88  }
89  else {
90  current_yaw_vel_ = 0;
91  }
92  control_msg_.yaw_rate = current_yaw_vel_;
93 
94  if (is_fixed_wing_) {
95  double thrust = msg->axes[axes_.thrust] * axes_.thrust_direction;
96  control_msg_.thrust.x = (thrust >= 0.0) ? thrust : 0.0;
97  }
98  else {
99  control_msg_.thrust.z = (msg->axes[axes_.thrust] + 1) * max_.thrust / 2.0 * axes_.thrust_direction;
100  }
101 
102  ros::Time update_time = ros::Time::now();
103  control_msg_.header.stamp = update_time;
104  control_msg_.header.frame_id = "rotors_joy_frame";
105  Publish();
106 }
107 
108 void Joy::Publish() {
110 }
111 
112 int main(int argc, char** argv) {
113  ros::init(argc, argv, "rotors_joy_interface");
114  Joy joy;
115 
116  ros::spin();
117 
118  return 0;
119 }
int main(int argc, char **argv)
Definition: joy.cpp:112
Definition: joy.h:56
double current_yaw_vel_
Definition: joy.h:75
bool is_fixed_wing_
Definition: joy.h:78
double v_yaw_step_
Definition: joy.h:76
int pitch
Definition: joy.h:32
sensor_msgs::Joy current_joy_
Definition: joy.h:71
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
Buttons buttons_
Definition: joy.h:67
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
static constexpr char COMMAND_ROLL_PITCH_YAWRATE_THRUST[]
int land
Definition: joy.h:41
mav_msgs::RollPitchYawrateThrust control_msg_
Definition: joy.h:69
void Publish()
Definition: joy.cpp:108
int yaw_left
Definition: joy.h:44
bool param(const std::string &param_name, T &param_val, const T &default_val) const
void publish(const boost::shared_ptr< M > &message) const
int ctrl_mode
Definition: joy.h:43
int thrust
Definition: joy.h:33
double v_xy
Definition: joy.h:49
void JoyCallback(const sensor_msgs::JoyConstPtr &msg)
Definition: joy.cpp:78
ros::Publisher ctrl_pub_
Definition: joy.h:61
double roll
Definition: joy.h:50
ros::NodeHandle nh_
Definition: joy.h:60
int yaw_right
Definition: joy.h:45
double pitch
Definition: joy.h:51
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ROSCPP_DECL void spin()
int takeoff
Definition: joy.h:40
ros::Subscriber joy_sub_
Definition: joy.h:62
Max max_
Definition: joy.h:73
void StopMav()
Definition: joy.cpp:69
double thrust
Definition: joy.h:53
int pitch_direction
Definition: joy.h:35
int roll
Definition: joy.h:31
int ctrl_enable
Definition: joy.h:42
Axes axes_
Definition: joy.h:66
const std::string & getNamespace() const
double rate_yaw
Definition: joy.h:52
Joy()
Definition: joy.cpp:26
std::string namespace_
Definition: joy.h:64
static Time now()
int thrust_direction
Definition: joy.h:36
int roll_direction
Definition: joy.h:34


rotors_joy_interface
Author(s): Fadri Furrer, Michael Burri, Mina Kamel, Janosch Nikolic, Markus Achtelik
autogenerated on Mon Feb 28 2022 23:39:18