Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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
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
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 }