publish_control.cpp
Go to the documentation of this file.
00001 /*
00002 * Unpublished Copyright (c) 2009-2018 AutonomouStuff, LLC, All Rights Reserved.
00003 *
00004 * This file is part of the PACMod ROS 1.0 driver which is released under the MIT license.
00005 * See file LICENSE included with this software or go to https://opensource.org/licenses/MIT for full license details.
00006 */
00007 
00008 #include "publish_control.h"
00009 
00010 using namespace AS::Joystick;
00011 
00012 JoyAxis PublishControl::steering_axis = LEFT_STICK_LR;
00013 float PublishControl::max_rot_rad = MAX_ROT_RAD_DEFAULT;
00014 int PublishControl::vehicle_type = INVALID;
00015 GamepadType PublishControl::controller = LOGITECH_F310;
00016 int PublishControl::board_rev = INVALID;
00017 double PublishControl::max_veh_speed = INVALID;
00018 double PublishControl::accel_scale_val = 1.0;
00019 double PublishControl::brake_scale_val = 1.0;
00020 double PublishControl::steering_max_speed = INVALID;
00021 std::unordered_map<JoyAxis, int, EnumHash> PublishControl::axes;
00022 std::unordered_map<JoyButton, int, EnumHash> PublishControl::btns;
00023 pacmod_msgs::VehicleSpeedRpt::ConstPtr PublishControl::last_speed_rpt = NULL;
00024 bool PublishControl::pacmod_enable;
00025 bool PublishControl::prev_enable = false;
00026 bool PublishControl::local_enable = false;
00027 bool PublishControl::last_pacmod_state = false;
00028 bool PublishControl::accel_0_rcvd = false;
00029 bool PublishControl::brake_0_rcvd = false;
00030 int PublishControl::headlight_state = 0;
00031 bool PublishControl::headlight_state_change = false;
00032 int PublishControl::wiper_state = 0;
00033 
00034 PublishControl::PublishControl()
00035 {
00036   // Subscribe to messages
00037   joy_sub = n.subscribe("joy", 1000, &PublishControl::callback_control, this);
00038   speed_sub = n.subscribe("/pacmod/parsed_tx/vehicle_speed_rpt", 20, &PublishControl::callback_veh_speed);
00039 
00040   // Advertise published messages
00041   enable_pub = n.advertise<std_msgs::Bool>("/pacmod/as_rx/enable", 20);
00042 }
00043 
00044 /*
00045  * Called when a game controller message is received
00046  */
00047 void PublishControl::callback_control(const sensor_msgs::Joy::ConstPtr& msg)
00048 {
00049   try
00050   {
00051     // Only send messages when enabled, or when the state changes between enabled/disabled
00052     check_is_enabled(msg);
00053 
00054     if (local_enable == true || local_enable != prev_enable)
00055     {
00056       // Steering
00057       publish_steering_message(msg);
00058 
00059       // Turn signals
00060       publish_turn_signal_message(msg);
00061 
00062       // Shifting
00063       publish_shifting_message(msg);
00064 
00065       // Accelerator
00066       publish_accelerator_message(msg);
00067 
00068       // Brake
00069       publish_brake_message(msg);
00070 
00071       // Lights and horn
00072       publish_lights_horn_wipers_message(msg);
00073     }
00074 
00075     prev_enable = local_enable;
00076   }
00077   catch (const std::out_of_range& oor)
00078   {
00079     ROS_ERROR("An out-of-range exception was caught. This probably means you selected the wrong controller type.");
00080   }
00081 
00082   last_axes.clear();
00083   last_axes.insert(last_axes.end(), msg->axes.begin(), msg->axes.end());
00084 }
00085 
00086 /*
00087  * Called when the node receives a message from the enable topic
00088  */
00089 void PublishControl::callback_pacmod_enable(const std_msgs::Bool::ConstPtr& msg)
00090 {
00091   if (msg->data == false &&
00092       PublishControl::last_pacmod_state == true)
00093     prev_enable = false;
00094 
00095   enable_mutex.lock();
00096   pacmod_enable = msg->data;
00097   enable_mutex.unlock();
00098 
00099   PublishControl::last_pacmod_state = msg->data;
00100 }
00101 
00102 /*
00103  * Called when the node receives a message from the vehicle speed topic
00104  */
00105 void PublishControl::callback_veh_speed(const pacmod_msgs::VehicleSpeedRpt::ConstPtr& msg)
00106 {
00107   speed_mutex.lock();
00108   last_speed_rpt = msg;
00109   speed_mutex.unlock();
00110 }
00111 
00112 void PublishControl::check_is_enabled(const sensor_msgs::Joy::ConstPtr& msg)
00113 {
00114   bool state_changed = false;
00115 
00116   enable_mutex.lock();
00117   local_enable = pacmod_enable;
00118   enable_mutex.unlock();
00119 
00120   if (controller == HRI_SAFE_REMOTE)
00121   {
00122     // Enable
00123     if (msg->axes[axes[DPAD_UD]] >= 0.9 && !local_enable)
00124     {
00125       std_msgs::Bool bool_pub_msg;
00126       bool_pub_msg.data = true;
00127       local_enable = true;
00128       enable_pub.publish(bool_pub_msg);
00129 
00130       state_changed = true;
00131     }
00132 
00133     // Disable
00134     if (msg->axes[axes[DPAD_UD]] <= -0.9 && local_enable)
00135     {
00136       std_msgs::Bool bool_pub_msg;
00137       bool_pub_msg.data = false;
00138       local_enable = false;
00139       enable_pub.publish(bool_pub_msg);
00140 
00141       state_changed = true;
00142     }
00143   }
00144   else
00145   {
00146     // Enable
00147     if (msg->buttons[btns[START_PLUS]] == BUTTON_DOWN && msg->buttons[btns[BACK_SELECT_MINUS]] == BUTTON_DOWN && !local_enable)
00148     {
00149       std_msgs::Bool bool_pub_msg;
00150       bool_pub_msg.data = true;
00151       local_enable = true;
00152       enable_pub.publish(bool_pub_msg);
00153 
00154       state_changed = true;
00155     }
00156 
00157     // Disable
00158     if (msg->buttons[btns[BACK_SELECT_MINUS]] == BUTTON_DOWN && msg->buttons[btns[START_PLUS]] != BUTTON_DOWN && local_enable)
00159     {
00160       std_msgs::Bool bool_pub_msg;
00161       bool_pub_msg.data = false;
00162       local_enable = false;
00163       enable_pub.publish(bool_pub_msg);
00164 
00165       state_changed = true;
00166     }
00167   }
00168 
00169   if (state_changed)
00170   {
00171     enable_mutex.lock();
00172     pacmod_enable = local_enable;
00173     enable_mutex.unlock();
00174   }
00175 }


pacmod_game_control
Author(s): Joe Driscoll
autogenerated on Thu Jun 6 2019 21:10:24