JoystickDemo.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2018-2019 New Eagle 
00005  *  Copyright (c) 2015-2018, Dataspeed Inc.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of Dataspeed Inc. nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *********************************************************************/
00035 
00036 #include "JoystickDemo.h"
00037 
00038 namespace joystick_demo
00039 {
00040 
00041 JoystickDemo::JoystickDemo(ros::NodeHandle &node, ros::NodeHandle &priv_nh) : counter_(0)
00042 {
00043   joy_.axes.resize(AXIS_COUNT, 0);
00044   joy_.buttons.resize(BTN_COUNT, 0);
00045 
00046   ignore_ = false;
00047   enable_ = true;
00048   count_ = false;
00049   svel_ = 0.0;
00050   priv_nh.getParam("ignore", ignore_);
00051   priv_nh.getParam("enable", enable_);
00052   priv_nh.getParam("count", count_);
00053   priv_nh.getParam("svel", svel_);
00054 
00055   sub_joy_ = node.subscribe("/joy", 1, &JoystickDemo::recvJoy, this);
00056 
00057   data_.brake_joy = 0.0;
00058   data_.gear_cmd = dbw_pacifica_msgs::Gear::NONE;
00059   data_.steering_joy = 0.0;
00060   data_.steering_mult = false;
00061   data_.accelerator_pedal_joy = 0.0;
00062   data_.turn_signal_cmd = dbw_pacifica_msgs::TurnSignal::NONE;
00063   data_.joy_accelerator_pedal_valid = false;
00064   data_.joy_brake_valid = false;
00065   data_.accel_decel_limits = 3;
00066 
00067   pub_accelerator_pedal_ = node.advertise<dbw_pacifica_msgs::AcceleratorPedalCmd>("accelerator_pedal_cmd", 1);
00068   pub_brake_ = node.advertise<dbw_pacifica_msgs::BrakeCmd>("brake_cmd", 1);
00069   pub_misc_ = node.advertise<dbw_pacifica_msgs::MiscCmd>("misc_cmd", 1);
00070   pub_steering_ = node.advertise<dbw_pacifica_msgs::SteeringCmd>("steering_cmd", 1);
00071   pub_global_enable_ = node.advertise<dbw_pacifica_msgs::GlobalEnableCmd>("global_enable_cmd", 1);
00072   pub_gear_ = node.advertise<dbw_pacifica_msgs::GearCmd>("gear_cmd", 1);
00073   if (enable_) {
00074     pub_enable_ = node.advertise<std_msgs::Empty>("enable", 1);
00075     pub_disable_ = node.advertise<std_msgs::Empty>("disable", 1);
00076   }
00077 
00078   timer_ = node.createTimer(ros::Duration(0.02), &JoystickDemo::cmdCallback, this);
00079 }
00080 
00081 void JoystickDemo::cmdCallback(const ros::TimerEvent& event)
00082 {
00083   // Detect joy timeouts and reset
00084   if (event.current_real - data_.stamp > ros::Duration(0.1)) {
00085     data_.joy_accelerator_pedal_valid = false;
00086     data_.joy_brake_valid = false;
00087     return;
00088   }
00089 
00090   // Optional watchdog counter
00091   if (count_) {
00092     counter_++;
00093     if (counter_ > 15)
00094     {
00095       counter_ = 0;
00096     }
00097   }
00098 
00099   // Accelerator Pedal
00100   dbw_pacifica_msgs::AcceleratorPedalCmd accelerator_pedal_msg;
00101   accelerator_pedal_msg.enable = true;
00102   accelerator_pedal_msg.ignore = ignore_;
00103   accelerator_pedal_msg.rolling_counter = counter_;
00104   //accelerator_pedal_msg.pedal_cmd = data_.accelerator_pedal_joy * 100;
00105   accelerator_pedal_msg.speed_cmd = data_.accelerator_pedal_joy; // * 10 * 0.44704;
00106   accelerator_pedal_msg.road_slope = 0;
00107   accelerator_pedal_msg.accel_limit = data_.accel_decel_limits;
00108   accelerator_pedal_msg.control_type.value = dbw_pacifica_msgs::ActuatorControlMode::closed_loop_vehicle;
00109   pub_accelerator_pedal_.publish(accelerator_pedal_msg);
00110 
00111   // Brake
00112   dbw_pacifica_msgs::BrakeCmd brake_msg;
00113   brake_msg.enable = true;
00114   brake_msg.rolling_counter = counter_;
00115   brake_msg.pedal_cmd = data_.brake_joy * 100;
00116   brake_msg.control_type.value = dbw_pacifica_msgs::ActuatorControlMode::closed_loop_vehicle;
00117   brake_msg.decel_limit = data_.accel_decel_limits;
00118   pub_brake_.publish(brake_msg);
00119 
00120   // Steering
00121   dbw_pacifica_msgs::SteeringCmd steering_msg;
00122   steering_msg.enable = true;
00123   steering_msg.ignore = ignore_;
00124   steering_msg.rolling_counter = counter_;
00125   steering_msg.angle_cmd = data_.steering_joy;
00126   steering_msg.angle_velocity = svel_;
00127   steering_msg.control_type.value = dbw_pacifica_msgs::ActuatorControlMode::closed_loop_actuator;
00128   if (!data_.steering_mult) {
00129     steering_msg.angle_cmd *= 0.5;
00130   }
00131   pub_steering_.publish(steering_msg);
00132 
00133   // Gear
00134   dbw_pacifica_msgs::GearCmd gear_msg;
00135   gear_msg.cmd.gear = data_.gear_cmd;
00136   gear_msg.enable = true;
00137   gear_msg.rolling_counter = counter_;
00138   pub_gear_.publish(gear_msg);
00139 
00140   // Turn signal
00141   dbw_pacifica_msgs::MiscCmd misc_msg;
00142   misc_msg.cmd.value = data_.turn_signal_cmd;
00143   misc_msg.rolling_counter = counter_;
00144   pub_misc_.publish(misc_msg);
00145 
00146   dbw_pacifica_msgs::GlobalEnableCmd globalEnable_msg;
00147   globalEnable_msg.global_enable = true;
00148   globalEnable_msg.enable_joystick_limits = true;
00149   globalEnable_msg.rolling_counter = counter_;
00150   pub_global_enable_.publish(globalEnable_msg);
00151 }
00152 
00153 void JoystickDemo::recvJoy(const sensor_msgs::Joy::ConstPtr& msg)
00154 {
00155   // Check for expected sizes
00156   if (msg->axes.size() != (size_t)AXIS_COUNT) {
00157     ROS_ERROR("Expected %zu joy axis count, received %zu", (size_t)AXIS_COUNT, msg->axes.size());
00158     return;
00159   }
00160   if (msg->buttons.size() != (size_t)BTN_COUNT) {
00161     ROS_ERROR("Expected %zu joy button count, received %zu", (size_t)BTN_COUNT, msg->buttons.size());
00162     return;
00163   }
00164 
00165   // Handle joystick startup
00166   if (msg->axes[AXIS_ACCELERATOR_PEDAL] != 0.0) {
00167     data_.joy_accelerator_pedal_valid = true;
00168   }
00169   if (msg->axes[AXIS_BRAKE] != 0.0) {
00170     data_.joy_brake_valid = true;
00171   }
00172 
00173   data_.accel_decel_limits = 3;
00174 
00175   // Accelerator pedal
00176   if (data_.joy_accelerator_pedal_valid) {
00177     //data_.accelerator_pedal_joy = 0.5 - 0.5 * msg->axes[AXIS_ACCELERATOR_PEDAL];
00178     if (msg->axes[AXIS_ACCELERATOR_PEDAL] < 0.0) {
00179       data_.accel_decel_limits = 0;
00180     }
00181   }
00182 
00183   // Brake
00184   if (data_.joy_brake_valid) {
00185     //data_.brake_joy = 0; //0.5 - 0.5 * msg->axes[AXIS_BRAKE];
00186     if (msg->axes[AXIS_BRAKE] < 0.0) {
00187       data_.accelerator_pedal_joy = 0;
00188     }
00189   }
00190 
00191   // Gear
00192   if (msg->buttons[BTN_PARK]) {
00193     data_.gear_cmd = dbw_pacifica_msgs::Gear::PARK;
00194   } else if (msg->buttons[BTN_REVERSE]) {
00195     data_.gear_cmd = dbw_pacifica_msgs::Gear::REVERSE;
00196   } else if (msg->buttons[BTN_DRIVE]) {
00197     data_.gear_cmd = dbw_pacifica_msgs::Gear::DRIVE;
00198   } else if (msg->buttons[BTN_NEUTRAL]) {
00199     data_.gear_cmd = dbw_pacifica_msgs::Gear::NEUTRAL;
00200   } else {
00201     data_.gear_cmd = dbw_pacifica_msgs::Gear::NONE;
00202   }
00203 
00204   // Steering
00205   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]);
00206   data_.steering_mult = msg->buttons[BTN_STEER_MULT_1] || msg->buttons[BTN_STEER_MULT_2];
00207 
00208   // Speed increment
00209   if (msg->axes[7] != joy_.axes[7]) {
00210 
00211         if (msg->axes[7] < -0.5) {
00212           data_.accelerator_pedal_joy -= 0.44704;
00213           if (data_.accelerator_pedal_joy < 0)
00214           {
00215             data_.accelerator_pedal_joy = 0;
00216           }
00217         } else if (msg->axes[7] > 0.5) {
00218           data_.accelerator_pedal_joy += 0.44704;
00219           if (data_.accelerator_pedal_joy > 4.4704)
00220           {
00221             data_.accelerator_pedal_joy = 4.4704;
00222           }
00223         }
00224     }
00225 
00226   // Turn signal
00227   if (msg->axes[AXIS_TURN_SIG] != joy_.axes[AXIS_TURN_SIG]) {
00228     switch (data_.turn_signal_cmd) {
00229       case dbw_pacifica_msgs::TurnSignal::NONE:
00230         if (msg->axes[AXIS_TURN_SIG] < -0.5) {
00231           data_.turn_signal_cmd = dbw_pacifica_msgs::TurnSignal::RIGHT;
00232         } else if (msg->axes[AXIS_TURN_SIG] > 0.5) {
00233           data_.turn_signal_cmd = dbw_pacifica_msgs::TurnSignal::LEFT;
00234         }
00235         break;
00236       case dbw_pacifica_msgs::TurnSignal::LEFT:
00237         if (msg->axes[AXIS_TURN_SIG] < -0.5) {
00238           data_.turn_signal_cmd = dbw_pacifica_msgs::TurnSignal::RIGHT;
00239         } else if (msg->axes[AXIS_TURN_SIG] > 0.5) {
00240           data_.turn_signal_cmd = dbw_pacifica_msgs::TurnSignal::NONE;
00241         }
00242         break;
00243       case dbw_pacifica_msgs::TurnSignal::RIGHT:
00244         if (msg->axes[AXIS_TURN_SIG] < -0.5) {
00245           data_.turn_signal_cmd = dbw_pacifica_msgs::TurnSignal::NONE;
00246         } else if (msg->axes[AXIS_TURN_SIG] > 0.5) {
00247           data_.turn_signal_cmd = dbw_pacifica_msgs::TurnSignal::LEFT;
00248         }
00249         break;
00250     }
00251   }
00252 
00253   // Optional enable and disable buttons
00254   if (enable_) {
00255     const std_msgs::Empty empty;
00256     if (msg->buttons[BTN_ENABLE]) {
00257       pub_enable_.publish(empty);
00258     }
00259     if (msg->buttons[BTN_DISABLE]) {
00260       pub_disable_.publish(empty);
00261     }
00262   }
00263 
00264   data_.stamp = ros::Time::now();
00265   joy_ = *msg;
00266 }
00267 
00268 }


dbw_pacifica_joystick_speed_demo
Author(s): Jane Doe
autogenerated on Mon Jun 24 2019 19:18:35