mh5l_traj.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * ROS wrapper for continous trajectory point streaming using FS100 controller mh5l robot
00004  * Author: Asger Winther-Jørgensen (awijor@elektro.dtu.dk)
00005  *
00006  * Software License Agreement (LGPL License)
00007  *
00008  *  Copyright (c) 2014, Technical University of Denmark
00009  *  All rights reserved.
00010  
00011  *  This program is free software; you can redistribute it and/or modify  
00012  *  it under the terms of the GNU Lesser General Public License as        
00013  *  published by the Free Software Foundation; either version 2 of the    
00014  *  License, or (at your option) any later version.                       
00015  *                                                                        
00016  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00017  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00018  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00019  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00020  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00022  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00024  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00026  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  *  POSSIBILITY OF SUCH DAMAGE.
00028  *********************************************************************/
00029 
00030 
00031 #include "mh5l_traj.h"
00032 
00033 //assign memory for robot state class
00034 Fs100State *robotState;//("192.168.1.10");
00035 float joint_pos[6];
00036 
00037 //assign memory for robot commander class
00038 Fs100Cmd *robotCommander;//("192.168.1.10");
00039 
00040 //assign memory for ros subscribers, sevices, etc...
00041 ros::Subscriber *sub_target;
00042 ros::ServiceServer *get_reset_srv;
00043 ros::Publisher *pub_joints;
00044 int retry = 1;
00045 
00046 void joint_target_cb(const trajectory_msgs::JointTrajectoryPoint& input){
00047     float temp_pos[6],temp_vel[6],temp_time;
00048     for(int i=0;i<6;i++){
00049         temp_pos[i] = input.positions[i];
00050         temp_vel[i] = input.velocities[i];
00051         temp_time = input.time_from_start.toSec();
00052     }    
00053     robotCommander->addPointToQueue(temp_pos,temp_vel,temp_time);
00054 }
00055 
00056 bool reset_robot_trajectory(fs100_motoman::setReset::Request &req, fs100_motoman::setReset::Response &res){
00057     bool result;
00058     retry = 1;
00059     result = robotCommander->resetTrajectory(&retry);
00060     res.success = true;
00061     return true;
00062 }
00063 
00064 void *get_data_func(void *arg){
00065     sensor_msgs::JointState js;
00066     js.position.resize(6);
00067     while( *((bool *) arg)){
00068         while(!robotState->getJointsUpdated(joint_pos));
00069         for(int i=0;i<6;i++){
00070             js.position[i] = joint_pos[i];
00071         }
00072         js.header.stamp = ros::Time::now();
00073         pub_joints->publish(js);
00074     }
00075     
00076 }
00077 
00078 
00079 
00080 int main(int argc, char **argv){
00081     char* robot_ip;
00082     ROS_INFO("argc %d",argc);
00083     if (argc > 1)
00084     {
00085         robot_ip = argv[1];
00086     }
00087     else
00088     {
00089         ROS_ERROR("invalid number of arguments, expected 1 (robot IP)");
00090         return 1;
00091     }
00092 
00093 
00094 
00095     robotState = new Fs100State (robot_ip);
00096     robotCommander = new Fs100Cmd (robot_ip);
00097     //setup ROS
00098     ros::init(argc, argv, "fs100_motoman");
00099     ros::NodeHandle nh("~mh5l");
00100         
00101     sub_target = new ros::Subscriber;
00102     *sub_target = nh.subscribe("joint_target", 1, &joint_target_cb); //joint target topic
00103     
00104     get_reset_srv = new ros::ServiceServer;
00105     *get_reset_srv = nh.advertiseService("reset_trajectory",&reset_robot_trajectory); //reset trajectory service
00106     
00107     pub_joints = new ros::Publisher;
00108     *pub_joints = nh.advertise<sensor_msgs::JointState> ("joint_state", 1);
00109     float pos[6],current_pos[6];
00110     float vel[6];
00111     bool run_thread = true;
00112     pthread_t getthreadID;
00113     
00114 
00115     
00116     //create state class instance
00117 
00118     if(robotState->init())
00119     {
00120         ROS_ERROR("Could not open socket");
00121         return 1;
00122     }
00123     if(robotState->makeConnect())
00124     {
00125         ROS_ERROR("Could not connect to robot controller state server");
00126         return 1;
00127     }
00128     
00129     
00130     
00131     //create command class instance
00132 
00133     if(robotCommander->init())
00134     {
00135         ROS_ERROR("Could not open socket");
00136         return 1;
00137     }
00138     if(robotCommander->makeConnect())
00139     {
00140         ROS_ERROR("Could not connect to robot controller motion server");
00141         return 1;
00142     }
00143     
00144     
00145     
00146     ros::Duration(2.0).sleep();
00147     if (robotCommander->start(1)){
00148         ROS_ERROR("Could not start trajectory on fs100, aborting");
00149         robotState->pgmClose();
00150         robotCommander->pgmClose();
00151         ros::shutdown();
00152         return 1;
00153     }
00154 
00155         
00156     
00157         
00158     
00159     pthread_create(&getthreadID,NULL,get_data_func,&run_thread);
00160     ROS_INFO("Ready to recieve trajectory points");
00161     while(nh.ok()){
00162         ros::spinOnce();
00163     }
00164     run_thread = false;
00165     pthread_join(getthreadID,NULL);
00166     robotState->pgmClose();
00167     robotCommander->pgmClose();
00168 
00169     
00170 
00171     ros::shutdown(); 
00172     return 0;
00173 }
00174 


fs100_motoman
Author(s): Asger Winther-Jørgensen (Technical Univeristy of Denmark)
autogenerated on Fri Aug 28 2015 10:42:16