JoystickDemo.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2018-2019 New Eagle
5  * Copyright (c) 2015-2018, Dataspeed Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Dataspeed Inc. nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
36 #include "JoystickDemo.h"
37 
38 namespace joystick_demo
39 {
40 
42 {
43  joy_.axes.resize(AXIS_COUNT, 0);
44  joy_.buttons.resize(BTN_COUNT, 0);
45 
46  ignore_ = false;
47  enable_ = true;
48  count_ = false;
49  svel_ = 0.0;
50  priv_nh.getParam("ignore", ignore_);
51  priv_nh.getParam("enable", enable_);
52  priv_nh.getParam("count", count_);
53  priv_nh.getParam("svel", svel_);
54 
55  sub_joy_ = node.subscribe("/joy", 1, &JoystickDemo::recvJoy, this);
56 
57  data_.brake_joy = 0.0;
58  data_.gear_cmd = dbw_pacifica_msgs::Gear::NONE;
59  data_.steering_joy = 0.0;
60  data_.steering_mult = false;
62  data_.turn_signal_cmd = dbw_pacifica_msgs::TurnSignal::NONE;
64  data_.joy_brake_valid = false;
66 
67  pub_accelerator_pedal_ = node.advertise<dbw_pacifica_msgs::AcceleratorPedalCmd>("accelerator_pedal_cmd", 1);
68  pub_brake_ = node.advertise<dbw_pacifica_msgs::BrakeCmd>("brake_cmd", 1);
69  pub_misc_ = node.advertise<dbw_pacifica_msgs::MiscCmd>("misc_cmd", 1);
70  pub_steering_ = node.advertise<dbw_pacifica_msgs::SteeringCmd>("steering_cmd", 1);
71  pub_global_enable_ = node.advertise<dbw_pacifica_msgs::GlobalEnableCmd>("global_enable_cmd", 1);
72  pub_gear_ = node.advertise<dbw_pacifica_msgs::GearCmd>("gear_cmd", 1);
73  if (enable_) {
74  pub_enable_ = node.advertise<std_msgs::Empty>("enable", 1);
75  pub_disable_ = node.advertise<std_msgs::Empty>("disable", 1);
76  }
77 
79 }
80 
82 {
83  // Detect joy timeouts and reset
84  if (event.current_real - data_.stamp > ros::Duration(0.1)) {
86  data_.joy_brake_valid = false;
87  return;
88  }
89 
90  // Optional watchdog counter
91  if (count_) {
92  counter_++;
93  if (counter_ > 15)
94  {
95  counter_ = 0;
96  }
97  }
98 
99  // Accelerator Pedal
100  dbw_pacifica_msgs::AcceleratorPedalCmd accelerator_pedal_msg;
101  accelerator_pedal_msg.enable = true;
102  accelerator_pedal_msg.ignore = ignore_;
103  accelerator_pedal_msg.rolling_counter = counter_;
104  //accelerator_pedal_msg.pedal_cmd = data_.accelerator_pedal_joy * 100;
105  accelerator_pedal_msg.speed_cmd = data_.accelerator_pedal_joy; // * 10 * 0.44704;
106  accelerator_pedal_msg.road_slope = 0;
107  accelerator_pedal_msg.accel_limit = data_.accel_decel_limits;
108  accelerator_pedal_msg.control_type.value = dbw_pacifica_msgs::ActuatorControlMode::closed_loop_vehicle;
109  pub_accelerator_pedal_.publish(accelerator_pedal_msg);
110 
111  // Brake
112  dbw_pacifica_msgs::BrakeCmd brake_msg;
113  brake_msg.enable = true;
114  brake_msg.rolling_counter = counter_;
115  brake_msg.pedal_cmd = data_.brake_joy * 100;
116  brake_msg.control_type.value = dbw_pacifica_msgs::ActuatorControlMode::closed_loop_vehicle;
117  brake_msg.decel_limit = data_.accel_decel_limits;
118  pub_brake_.publish(brake_msg);
119 
120  // Steering
121  dbw_pacifica_msgs::SteeringCmd steering_msg;
122  steering_msg.enable = true;
123  steering_msg.ignore = ignore_;
124  steering_msg.rolling_counter = counter_;
125  steering_msg.angle_cmd = data_.steering_joy;
126  steering_msg.angle_velocity = svel_;
127  steering_msg.control_type.value = dbw_pacifica_msgs::ActuatorControlMode::closed_loop_actuator;
128  if (!data_.steering_mult) {
129  steering_msg.angle_cmd *= 0.5;
130  }
131  pub_steering_.publish(steering_msg);
132 
133  // Gear
134  dbw_pacifica_msgs::GearCmd gear_msg;
135  gear_msg.cmd.gear = data_.gear_cmd;
136  gear_msg.enable = true;
137  gear_msg.rolling_counter = counter_;
138  pub_gear_.publish(gear_msg);
139 
140  // Turn signal
141  dbw_pacifica_msgs::MiscCmd misc_msg;
142  misc_msg.cmd.value = data_.turn_signal_cmd;
143  misc_msg.rolling_counter = counter_;
144  pub_misc_.publish(misc_msg);
145 
146  dbw_pacifica_msgs::GlobalEnableCmd globalEnable_msg;
147  globalEnable_msg.global_enable = true;
148  globalEnable_msg.enable_joystick_limits = true;
149  globalEnable_msg.rolling_counter = counter_;
150  pub_global_enable_.publish(globalEnable_msg);
151 }
152 
153 void JoystickDemo::recvJoy(const sensor_msgs::Joy::ConstPtr& msg)
154 {
155  // Check for expected sizes
156  if (msg->axes.size() != (size_t)AXIS_COUNT) {
157  ROS_ERROR("Expected %zu joy axis count, received %zu", (size_t)AXIS_COUNT, msg->axes.size());
158  return;
159  }
160  if (msg->buttons.size() != (size_t)BTN_COUNT) {
161  ROS_ERROR("Expected %zu joy button count, received %zu", (size_t)BTN_COUNT, msg->buttons.size());
162  return;
163  }
164 
165  // Handle joystick startup
166  if (msg->axes[AXIS_ACCELERATOR_PEDAL] != 0.0) {
168  }
169  if (msg->axes[AXIS_BRAKE] != 0.0) {
170  data_.joy_brake_valid = true;
171  }
172 
174 
175  // Accelerator pedal
177  //data_.accelerator_pedal_joy = 0.5 - 0.5 * msg->axes[AXIS_ACCELERATOR_PEDAL];
178  if (msg->axes[AXIS_ACCELERATOR_PEDAL] < 0.0) {
180  }
181  }
182 
183  // Brake
184  if (data_.joy_brake_valid) {
185  //data_.brake_joy = 0; //0.5 - 0.5 * msg->axes[AXIS_BRAKE];
186  if (msg->axes[AXIS_BRAKE] < 0.0) {
188  }
189  }
190 
191  // Gear
192  if (msg->buttons[BTN_PARK]) {
193  data_.gear_cmd = dbw_pacifica_msgs::Gear::PARK;
194  } else if (msg->buttons[BTN_REVERSE]) {
195  data_.gear_cmd = dbw_pacifica_msgs::Gear::REVERSE;
196  } else if (msg->buttons[BTN_DRIVE]) {
197  data_.gear_cmd = dbw_pacifica_msgs::Gear::DRIVE;
198  } else if (msg->buttons[BTN_NEUTRAL]) {
199  data_.gear_cmd = dbw_pacifica_msgs::Gear::NEUTRAL;
200  } else {
201  data_.gear_cmd = dbw_pacifica_msgs::Gear::NONE;
202  }
203 
204  // Steering
205  data_.steering_joy = 470.0 * M_PI / 180.0 * ((fabs(msg->axes[AXIS_STEER_1]) > fabs(msg->axes[AXIS_STEER_2])) ? msg->axes[AXIS_STEER_1] : msg->axes[AXIS_STEER_2]);
206  data_.steering_mult = msg->buttons[BTN_STEER_MULT_1] || msg->buttons[BTN_STEER_MULT_2];
207 
208  // Speed increment
209  if (msg->axes[7] != joy_.axes[7]) {
210 
211  if (msg->axes[7] < -0.5) {
212  data_.accelerator_pedal_joy -= 0.44704;
214  {
216  }
217  } else if (msg->axes[7] > 0.5) {
218  data_.accelerator_pedal_joy += 0.44704;
219  if (data_.accelerator_pedal_joy > 4.4704)
220  {
221  data_.accelerator_pedal_joy = 4.4704;
222  }
223  }
224  }
225 
226  // Turn signal
227  if (msg->axes[AXIS_TURN_SIG] != joy_.axes[AXIS_TURN_SIG]) {
228  switch (data_.turn_signal_cmd) {
229  case dbw_pacifica_msgs::TurnSignal::NONE:
230  if (msg->axes[AXIS_TURN_SIG] < -0.5) {
231  data_.turn_signal_cmd = dbw_pacifica_msgs::TurnSignal::RIGHT;
232  } else if (msg->axes[AXIS_TURN_SIG] > 0.5) {
233  data_.turn_signal_cmd = dbw_pacifica_msgs::TurnSignal::LEFT;
234  }
235  break;
236  case dbw_pacifica_msgs::TurnSignal::LEFT:
237  if (msg->axes[AXIS_TURN_SIG] < -0.5) {
238  data_.turn_signal_cmd = dbw_pacifica_msgs::TurnSignal::RIGHT;
239  } else if (msg->axes[AXIS_TURN_SIG] > 0.5) {
240  data_.turn_signal_cmd = dbw_pacifica_msgs::TurnSignal::NONE;
241  }
242  break;
243  case dbw_pacifica_msgs::TurnSignal::RIGHT:
244  if (msg->axes[AXIS_TURN_SIG] < -0.5) {
245  data_.turn_signal_cmd = dbw_pacifica_msgs::TurnSignal::NONE;
246  } else if (msg->axes[AXIS_TURN_SIG] > 0.5) {
247  data_.turn_signal_cmd = dbw_pacifica_msgs::TurnSignal::LEFT;
248  }
249  break;
250  }
251  }
252 
253  // Optional enable and disable buttons
254  if (enable_) {
255  const std_msgs::Empty empty;
256  if (msg->buttons[BTN_ENABLE]) {
257  pub_enable_.publish(empty);
258  }
259  if (msg->buttons[BTN_DISABLE]) {
260  pub_disable_.publish(empty);
261  }
262  }
263 
265  joy_ = *msg;
266 }
267 
268 }
ros::Publisher pub_enable_
Definition: JoystickDemo.h:80
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
JoystickDemo(ros::NodeHandle &node, ros::NodeHandle &priv_nh)
ros::Publisher pub_global_enable_
Definition: JoystickDemo.h:82
JoystickDataStruct data_
Definition: JoystickDemo.h:91
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
ros::Subscriber sub_joy_
Definition: JoystickDemo.h:74
ros::Publisher pub_steering_
Definition: JoystickDemo.h:77
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool getParam(const std::string &key, std::string &s) const
void recvJoy(const sensor_msgs::Joy::ConstPtr &msg)
static Time now()
void cmdCallback(const ros::TimerEvent &event)
ros::Publisher pub_accelerator_pedal_
Definition: JoystickDemo.h:75
ros::Publisher pub_disable_
Definition: JoystickDemo.h:81
#define ROS_ERROR(...)


dbw_pacifica_joystick_speed_demo
Author(s): Jane Doe
autogenerated on Fri Mar 20 2020 03:31:43