cirkit_unit03_hw.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <sensor_msgs/JointState.h>
00003 #include <geometry_msgs/Twist.h>
00004 
00005 #include <controller_manager/controller_manager.h>
00006 #include <hardware_interface/joint_command_interface.h>
00007 #include <hardware_interface/joint_state_interface.h>
00008 #include <hardware_interface/robot_hw.h>
00009 
00010 #include "ixis_imcs01_driver/ixis_imcs01_driver.h"
00011 
00012 enum VIRTUAL_JOINT_IND {
00013   VIRTUAL_JOINT_IND_RIGHT_REAR        = 0,
00014   VIRTUAL_JOINT_IND_LEFT_REAR         = 1,
00015   VIRTUAL_JOINT_IND_RIGHT_FRONT       = 2,
00016   VIRTUAL_JOINT_IND_LEFT_FRONT        = 3,
00017   VIRTUAL_JOINT_IND_RIGHT_FRONT_STEER = 4,
00018   VIRTUAL_JOINT_IND_LEFT_FRONT_STEER  = 5
00019 };
00020 
00021 class CirkitUnit03HardwareInterface
00022   : public hardware_interface::RobotHW
00023 {
00024 public:
00025   CirkitUnit03HardwareInterface(const std::string& imcs01_port, const ros::NodeHandle& nh);
00026   ros::Time getTime() const { return ros::Time::now(); }
00027   ros::Duration getPeriod() const {return ros::Duration(0.01); }
00028   void read();
00029   void write();
00030 protected:
00031   void publishSteer(double angle_cmd);
00032   void registerVirtualJointState(std::vector<double> &virtual_wheels_pos_,
00033                                  std::vector<double> &virtual_wheels_vel_,
00034                                  std::vector<double> &virtual_wheels_eff_,
00035                                  std::vector<std::string> &virtual_wheels_names);
00036   ros::NodeHandle nh_;
00037   //hardware_interface::JointStateInterface front_steer_jnt_state_interface_;
00038   hardware_interface::PositionJointInterface front_steer_jnt_pos_cmd_interface_;
00039   double front_steer_pos_;
00040   double front_steer_vel_;
00041   double front_steer_eff_;
00042   double front_steer_pos_cmd_;
00043 
00044   //hardware_interface::JointStateInterface rear_wheel_jnt_state_interface_;
00045   hardware_interface::VelocityJointInterface rear_wheel_jnt_vel_cmd_interface_;
00046   double rear_wheel_pos_;
00047   double rear_wheel_vel_;
00048   double rear_wheel_eff_;
00049   double rear_wheel_vel_cmd_;
00050 
00051   hardware_interface::JointStateInterface joint_state_interface_;
00052 
00053   std::vector<double> virtual_wheels_pos_;
00054   std::vector<double> virtual_wheels_vel_;
00055   std::vector<double> virtual_wheels_eff_;
00056 
00057   ros::Publisher steer_cmd_publisher_;
00058   IxisImcs01Driver ixis_imcs01_driver_;
00059 };
00060 
00061 CirkitUnit03HardwareInterface::CirkitUnit03HardwareInterface(const std::string& imcs01_port, const ros::NodeHandle& nh)
00062   : nh_(nh),
00063     ixis_imcs01_driver_(imcs01_port),
00064     steer_cmd_publisher_(nh_.advertise<geometry_msgs::Twist>("/steer_ctrl", 1))
00065 {
00066   ros::NodeHandle n("~");
00067   std::string front_steer_joint_name("front_steer_joint"); // for steer_drive_ros
00068   std::string rear_wheel_joint_name("rear_wheel_joint"); // for steer_drive_ros
00069   std::vector<std::string> virtual_wheels_names;
00070   virtual_wheels_names.push_back("base_to_right_rear_wheel");
00071   virtual_wheels_names.push_back("base_to_left_rear_wheel");
00072   virtual_wheels_names.push_back("base_to_right_front_wheel");
00073   virtual_wheels_names.push_back("base_to_left_front_wheel");
00074   virtual_wheels_names.push_back("base_to_right_front_steer");
00075   virtual_wheels_names.push_back("base_to_left_front_steer");
00076   // front right, front left, rear right, rear left and steers = 6
00077   virtual_wheels_vel_.resize(6);
00078   virtual_wheels_pos_.resize(6);
00079   virtual_wheels_eff_.resize(6);
00080   
00081   hardware_interface::JointStateHandle front_steer_state_handle(front_steer_joint_name, &front_steer_pos_, &front_steer_vel_, &front_steer_eff_);
00082   joint_state_interface_.registerHandle(front_steer_state_handle);
00083   hardware_interface::JointHandle front_steer_pos_cmd_handle(joint_state_interface_.getHandle(front_steer_joint_name), &front_steer_pos_cmd_);
00084   front_steer_jnt_pos_cmd_interface_.registerHandle(front_steer_pos_cmd_handle);
00085 
00086   hardware_interface::JointStateHandle rear_wheel_state_handle(rear_wheel_joint_name, &rear_wheel_pos_, &rear_wheel_vel_, &rear_wheel_eff_);
00087   joint_state_interface_.registerHandle(rear_wheel_state_handle);
00088   hardware_interface::JointHandle rear_wheel_vel_cmd_handle(joint_state_interface_.getHandle(rear_wheel_joint_name), &rear_wheel_vel_cmd_);
00089   rear_wheel_jnt_vel_cmd_interface_.registerHandle(rear_wheel_vel_cmd_handle);
00090 
00091   this->registerVirtualJointState(virtual_wheels_pos_, virtual_wheels_vel_, virtual_wheels_eff_,
00092                                   virtual_wheels_names);
00093   
00094   registerInterface(&front_steer_jnt_pos_cmd_interface_);
00095   registerInterface(&rear_wheel_jnt_vel_cmd_interface_);
00096   registerInterface(&joint_state_interface_);
00097 }
00098 
00099 void CirkitUnit03HardwareInterface::registerVirtualJointState(std::vector<double> &virtual_wheels_pos_,
00100                                                               std::vector<double> &virtual_wheels_vel_,
00101                                                               std::vector<double> &virtual_wheels_eff_,
00102                                                               std::vector<std::string> &virtual_wheels_names)
00103 {
00104  for (int i = 0; i < 6; ++i) {
00105    hardware_interface::JointStateHandle state_handle(virtual_wheels_names[i], &virtual_wheels_pos_[i], &virtual_wheels_vel_[i], &virtual_wheels_eff_[i]);
00106    joint_state_interface_.registerHandle(state_handle);
00107  }
00108 }
00109 
00110 void CirkitUnit03HardwareInterface::read()
00111 {
00112   ixis_imcs01_driver_.update(); // reading from imcs01.
00113   sensor_msgs::JointState joints_state = ixis_imcs01_driver_.getJointState();
00114   front_steer_pos_ = joints_state.position[JOINT_INDEX_FRONT];
00115   rear_wheel_pos_ = (joints_state.position[JOINT_INDEX_REAR_RIGHT]
00116                      + joints_state.position[JOINT_INDEX_REAR_LEFT]) / 2.0;
00117   rear_wheel_vel_ = (joints_state.velocity[JOINT_INDEX_REAR_RIGHT]
00118                      + joints_state.velocity[JOINT_INDEX_REAR_LEFT]) / 2.0;
00119   const double h = 0.75;
00120   const double w = 0.28;
00121   virtual_wheels_vel_[VIRTUAL_JOINT_IND_RIGHT_REAR]  = joints_state.velocity[JOINT_INDEX_REAR_RIGHT];
00122   virtual_wheels_pos_[VIRTUAL_JOINT_IND_RIGHT_REAR]  = joints_state.position[JOINT_INDEX_REAR_RIGHT];
00123   virtual_wheels_vel_[VIRTUAL_JOINT_IND_LEFT_REAR]   = joints_state.velocity[JOINT_INDEX_REAR_LEFT];
00124   virtual_wheels_pos_[VIRTUAL_JOINT_IND_LEFT_REAR]   = joints_state.position[JOINT_INDEX_REAR_LEFT];
00125   virtual_wheels_vel_[VIRTUAL_JOINT_IND_RIGHT_FRONT] = virtual_wheels_vel_[VIRTUAL_JOINT_IND_RIGHT_REAR];
00126   virtual_wheels_pos_[VIRTUAL_JOINT_IND_RIGHT_FRONT] = virtual_wheels_pos_[VIRTUAL_JOINT_IND_RIGHT_REAR];
00127   virtual_wheels_vel_[VIRTUAL_JOINT_IND_LEFT_FRONT]  = virtual_wheels_vel_[VIRTUAL_JOINT_IND_LEFT_REAR];
00128   virtual_wheels_pos_[VIRTUAL_JOINT_IND_LEFT_FRONT]  = virtual_wheels_pos_[VIRTUAL_JOINT_IND_LEFT_REAR];
00129   virtual_wheels_pos_[VIRTUAL_JOINT_IND_RIGHT_FRONT_STEER] = atan2(2.0*h*tan(front_steer_pos_),
00130                                                                    2*h + w/2.0*tan(front_steer_pos_));
00131   virtual_wheels_pos_[VIRTUAL_JOINT_IND_LEFT_FRONT_STEER]  = atan2(2.0*h*tan(front_steer_pos_),
00132                                                                    2*h - w/2.0*tan(front_steer_pos_));
00133 }
00134 
00135 void CirkitUnit03HardwareInterface::write()
00136 {
00137   ixis_imcs01_driver_.controlRearWheel(rear_wheel_vel_cmd_);
00138   this->publishSteer(front_steer_pos_cmd_);
00139 }
00140 
00141 void CirkitUnit03HardwareInterface::publishSteer(double angle_cmd)
00142 {
00143   double limited_angle = angle_cmd * 180.0 / M_PI;
00144   limited_angle = MAX(limited_angle, -60.0);
00145   limited_angle = MIN(limited_angle, 60);
00146   geometry_msgs::Twist steer;
00147   double angle_diff = limited_angle - (front_steer_pos_*180.0/M_PI); // angle_diff assume [deg]
00148   if(angle_diff > 0){
00149     steer.angular.z = 1;
00150     steer.angular.x = fabs(angle_diff);
00151   }else if(angle_diff < 0){
00152     steer.angular.z = -1;
00153     steer.angular.x = fabs(angle_diff);
00154   }else{
00155     steer.angular.z = 0;
00156     steer.angular.x = 0;
00157   }
00158   steer_cmd_publisher_.publish(steer);
00159 }
00160 
00161 int main(int argc, char** argv)
00162 {
00163   ros::init(argc, argv, "cirkit_unit03_control");
00164 
00165   ros::NodeHandle nh;
00166   std::string imcs01_port("/dev/urbtc0");
00167   CirkitUnit03HardwareInterface cirkit_unit03_hardware_interface(imcs01_port, nh);
00168   controller_manager::ControllerManager cm(&cirkit_unit03_hardware_interface, nh);
00169 
00170   ros::Rate rate(1.0 / cirkit_unit03_hardware_interface.getPeriod().toSec());
00171   ros::AsyncSpinner spinner(1);
00172   spinner.start();
00173 
00174   while(ros::ok()){
00175     ros::Time now = cirkit_unit03_hardware_interface.getTime();
00176     ros::Duration dt = cirkit_unit03_hardware_interface.getPeriod();
00177 
00178     cirkit_unit03_hardware_interface.read();
00179     cm.update(now, dt);
00180     cirkit_unit03_hardware_interface.write();
00181     rate.sleep();
00182   }
00183   spinner.stop();
00184 
00185   return 0;
00186 }


cirkit_unit03_base
Author(s): CIR-KIT
autogenerated on Thu Jun 6 2019 21:08:13