icart_mini_driver_node.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2014, Daiki Maekawa and Robot Design and Control Lab.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  *  * Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  *  * Redistributions in binary form must reproduce the above
00014  *    copyright notice, this list of conditions and the following
00015  *    disclaimer in the documentation and/or other materials provided
00016  *    with the distribution.
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00018  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00021  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00024  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00025  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00026  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00027  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028  * POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 #include <ros/ros.h>
00032 #include <std_msgs/Float64.h>
00033 #include <controller_manager/controller_manager.h>
00034 #include <hardware_interface/joint_command_interface.h>
00035 #include <hardware_interface/joint_state_interface.h>
00036 #include <hardware_interface/robot_hw.h>
00037 #include <realtime_tools/realtime_buffer.h>
00038 
00039 extern "C"{
00040 #include <unistd.h>
00041 #include <math.h>
00042 #include <ypspur.h>
00043 }
00044 
00045 class TFrog : public hardware_interface::RobotHW
00046 {
00047 public:
00048   TFrog(){
00049     if(Spur_init() < 0){
00050       ROS_WARN_STREAM("ERROR : cannot open spur.\n");
00051     }
00052 
00053     YP_set_wheel_vel(13.0, 13.0);
00054     YP_set_wheel_accel(13.0, 13.0);
00055 
00056     pos_[0] = 0.0; pos_[1] = 0.0;
00057     vel_[0] = 0.0; vel_[1] = 0.0;
00058     eff_[0] = 0.0; eff_[1] = 0.0;
00059     cmd_[0] = 0.0; cmd_[1] = 0.0;
00060 
00061     hardware_interface::JointStateHandle state_handle_1("right_wheel_hinge", &pos_[0], &vel_[0], &eff_[0]);
00062     jnt_state_interface_.registerHandle(state_handle_1);
00063 
00064     hardware_interface::JointStateHandle state_handle_2("left_wheel_hinge", &pos_[1], &vel_[1], &eff_[1]);
00065     jnt_state_interface_.registerHandle(state_handle_2);
00066 
00067     registerInterface(&jnt_state_interface_);
00068 
00069     hardware_interface::JointHandle vel_handle_1(jnt_state_interface_.getHandle("right_wheel_hinge"), &cmd_[0]);
00070     jnt_vel_interface_.registerHandle(vel_handle_1);
00071 
00072     hardware_interface::JointHandle vel_handle_2(jnt_state_interface_.getHandle("left_wheel_hinge"), &cmd_[1]);
00073     jnt_vel_interface_.registerHandle(vel_handle_2);
00074 
00075     registerInterface(&jnt_vel_interface_);
00076   }
00077 
00078   ~TFrog(){
00079     Spur_stop();
00080     ros::Duration(1);
00081     //usleep(40000000);
00082     Spur_free();
00083   }
00084 
00085   ros::Time getTime() const {return ros::Time::now();}
00086   ros::Duration getPeriod() const {return ros::Duration(0.01);}
00087   
00088   void reopen(){
00089     YP_wheel_vel(0, 0);
00090     Spur_free();
00091     Spur_init();
00092     YP_set_wheel_vel(13.0, 13.0);
00093     YP_set_wheel_accel(13.0, 13.0);
00094   }
00095 
00096   void read(){
00097     ROS_INFO_STREAM("Commands for joints: " << cmd_[0] << ", " << -cmd_[1]);
00098     int ret = YP_wheel_vel(cmd_[1], -cmd_[0]);
00099   }
00100 
00101   void write(){
00102     double yp_vel[2];
00103     yp_vel[0] = 0;
00104     yp_vel[1] = 0;
00105     YP_get_wheel_vel(&yp_vel[1], &yp_vel[0]);
00106     yp_vel[0] = -yp_vel[0];
00107     //ROS_INFO_STREAM("YPSpur vel: " << yp_vel[0] << ", " << -yp_vel[1]);
00108 
00109     for (unsigned int i = 0; i < 2; ++i)
00110     {
00111       pos_[i] += yp_vel[i] * getPeriod().toSec();
00112       vel_[i] = yp_vel[i];
00113     }
00114   }
00115 
00116 private:
00117   hardware_interface::JointStateInterface    jnt_state_interface_;
00118   hardware_interface::VelocityJointInterface jnt_vel_interface_;
00119   double cmd_[2];
00120   double pos_[2];
00121   double vel_[2];
00122   double eff_[2];
00123 };
00124 
00125 int main(int argc, char **argv)
00126 {
00127   double x, y, theta;
00128   
00129   ros::init(argc, argv, "icart_mini");
00130   ros::NodeHandle nh;
00131     
00132   TFrog robot;
00133   ROS_INFO_STREAM("period: " << robot.getPeriod().toSec());
00134   controller_manager::ControllerManager cm(&robot, nh);
00135 
00136   ros::Rate rate(1.0 / robot.getPeriod().toSec());
00137   ros::AsyncSpinner spinner(1);
00138   spinner.start();
00139 
00140   while(ros::ok())
00141   {
00142     int state = YP_get_error_state();
00143     
00144     if(state == 0){
00145         robot.read();
00146         robot.write();
00147     }else{
00148         ROS_WARN("Disconnected T-frog driver");
00149         robot.reopen();
00150     }
00151     
00152     cm.update(robot.getTime(), robot.getPeriod());
00153     rate.sleep();
00154   }
00155   spinner.stop();
00156 
00157   return 0;
00158 }


icart_mini_driver
Author(s): Daiki Maekawa
autogenerated on Thu Jun 6 2019 21:06:15