youbot_teleop_arm.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 * Copyright (c) 2011
00003 * Locomotec
00004 *
00005 * Author:
00006 * Sebastian Blumenthal
00007 *
00008 *
00009 * This software is published under a dual-license: GNU Lesser General Public
00010 * License LGPL 2.1 and BSD license. The dual-license implies that users of this
00011 * code may choose which terms they prefer.
00012 *
00013 * Redistribution and use in source and binary forms, with or without
00014 * modification, are permitted provided that the following conditions are met:
00015 *
00016 * * Redistributions of source code must retain the above copyright
00017 * notice, this list of conditions and the following disclaimer.
00018 * * Redistributions in binary form must reproduce the above copyright
00019 * notice, this list of conditions and the following disclaimer in the
00020 * documentation and/or other materials provided with the distribution.
00021 * * Neither the name of Locomotec nor the names of its
00022 * contributors may be used to endorse or promote products derived from
00023 * this software without specific prior written permission.
00024 *
00025 * This program is free software: you can redistribute it and/or modify
00026 * it under the terms of the GNU Lesser General Public License LGPL as
00027 * published by the Free Software Foundation, either version 2.1 of the
00028 * License, or (at your option) any later version or the BSD license.
00029 *
00030 * This program is distributed in the hope that it will be useful,
00031 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00032 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00033 * GNU Lesser General Public License LGPL and the BSD license for more details.
00034 *
00035 * You should have received a copy of the GNU Lesser General Public
00036 * License LGPL and BSD license along with this program.
00037 *
00038 ******************************************************************************/
00039 
00040 #include <iostream>
00041 #include <assert.h>
00042 
00043 #include "ros/ros.h"
00044 #include "trajectory_msgs/JointTrajectory.h"
00045 #include "brics_actuator/CartesianWrench.h"
00046 
00047 #include <boost/units/io.hpp>
00048 
00049 #include <boost/units/systems/angle/degrees.hpp>
00050 #include <boost/units/conversion.hpp>
00051 
00052 #include <math.h>
00053 
00054 #include "ros/ros.h"
00055 #include "brics_actuator/JointPositions.h"
00056 #include "sensor_msgs/JointState.h"
00057 
00058 #include <boost/units/systems/si/length.hpp>
00059 #include <boost/units/systems/si/plane_angle.hpp>
00060 #include <boost/units/io.hpp>
00061 
00062 #include <boost/units/systems/angle/degrees.hpp>
00063 #include <boost/units/conversion.hpp>
00064 
00065 #include <stdio.h>
00066 #include <unistd.h>
00067 #include <termios.h>
00068 #include "std_msgs/String.h"
00069 
00070 double joint[5];
00071 double lastJoint[5];            
00072 double gripperr = 0;
00073 double gripperl = 0;
00074 
00075 double jointMax[] = {5.840139, 2.617989, -0.0157081, 3.42919, 5.641589};
00076 double jointMin[] = {0.01006921, 0.01006921, -5.0264, 0.0221391, 0.11062};
00077 double gripperMax = 0.0115;
00078 double gripperMin = 0;
00079 
00080 double jointDelta[5];
00081 double gripperDelta = (gripperMax - gripperMin) * 0.02; 
00082 
00083 double jointHome[] = {0.01007,0.01007,-0.15709,0.02214,0.1107};
00084 double jointCamera[] = {3.0,0.5,-0.9,0.1,3.0};
00085 double jointObject[] = {3.04171,0.63597,-1.017845,0.36284,2.876194};
00086 double jointGrasp[] = {3.04171,2.04427,-1.5189129,2.5434289757,2.8761944};
00087 double jointInitialize[] = {0.01007,.635971,-1.91989,1.04424,2.87619};
00088 
00089 using namespace std;
00090 /*
00091 bool commandMatch()
00092 {
00093         cout << "Comparing values..." << endl;  
00094         
00095         bool match = false;
00096         int matchCounter;
00097 
00098         matchCounter = 0;
00099 
00100         for(int i = 0; i < 5; i++)
00101         {
00102                 if(     (joint[i] >= lastJoint[i] - jointDelta[i]) && 
00103                         (joint[i] <= lastJoint[i] + jointDelta[i])      )
00104                 {
00105 
00106                         matchCounter++;
00107                         //cout << "matchCounter: " << matchCounter << endl;
00108                 }
00109         }       
00110 
00111         if(matchCounter == 5) match = true;
00112 
00113         return match;
00114 }
00115 */
00116 void position_listener(const sensor_msgs::JointState::ConstPtr& msg)
00117 {
00118         if(msg->name[0] == "arm_joint_1")
00119         {
00120                 joint[0] = msg->position[0];
00121                 joint[1] = msg->position[1];
00122                 joint[2] = msg->position[2];
00123                 joint[3] = msg->position[3];
00124                 joint[4] = msg->position[4];
00125 
00126                 gripperl = msg->position[5];
00127                 gripperr = msg->position[6];
00128         }
00129 }
00130 
00131 char getch(void)
00132 {
00133         char ch;
00134         struct termios oldt;
00135         struct termios newt;
00136         tcgetattr(STDIN_FILENO, &oldt); /*store old settings */
00137         newt = oldt; /* copy old settings to new settings */
00138         newt.c_lflag &= ~(ICANON | ECHO); /* make one change to old settings in new settings */
00139         tcsetattr(STDIN_FILENO, TCSANOW, &newt); /*apply the new settings immediatly */
00140         ch = getchar(); /* standard getchar call */
00141         tcsetattr(STDIN_FILENO, TCSANOW, &oldt); /*reapply the old settings */
00142         return ch; /*return received char */
00143 }
00144 
00145 int main(int argc, char **argv) {
00146 
00147         ros::init(argc, argv, "youbot_arm_control_test");
00148         ros::NodeHandle n;
00149         ros::Publisher armPositionsPublisher;
00150         ros::Publisher gripperPositionPublisher;
00151         ros::Subscriber armPositionSubscriber;
00152 
00153         armPositionsPublisher = n.advertise<brics_actuator::JointPositions > ("arm_1/arm_controller/position_command", 1);
00154         gripperPositionPublisher = n.advertise<brics_actuator::JointPositions > ("arm_1/gripper_controller/position_command", 1);
00155         armPositionSubscriber = n.subscribe("joint_states", 1000, position_listener);
00156 
00157         std::fill_n(joint, 5, 0);
00158         gripperl = 0;
00159         gripperr = 0;
00160 
00161         ros::Rate rate(10); //Hz
00162         int readValue;
00163         static const int numberOfArmJoints = 5;
00164         static const int numberOfGripperJoints = 2;
00165 
00166         brics_actuator::JointPositions command;
00167         vector <brics_actuator::JointValue> armJointPositions;
00168         vector <brics_actuator::JointValue> gripperJointPositions;
00169 
00170         armJointPositions.resize(numberOfArmJoints); //TODO:change that
00171         gripperJointPositions.resize(numberOfGripperJoints);
00172 
00173         std::stringstream jointName;
00174 
00175         char keyboardInput = '0';
00176 
00177         for(int i = 0; i < numberOfArmJoints; i++)
00178         {
00179                 jointDelta[i] = (jointMax[i] - jointMin[i]) * 0.02;
00180         }       
00181 
00182         ros::spinOnce();
00183         
00184         for(int i = 0; i < numberOfArmJoints; i++)
00185         {
00186                 lastJoint[i] = joint[i];
00187         }
00188         
00189         while(n.ok())
00190         {
00191                 ros::spinOnce();
00192 
00193                 keyboardInput = '0';
00194                 readValue = 0;
00195 
00196                 cout << "Please provide an arm joint or the gripper to operate on.\n\tType number between 1 and 5, to control joints 1 - 5.\n\tOr hit 6 to go to a preprogrammed position.\n\tOr hit 9 to quit:" << endl;
00197                 cin >> readValue;
00198 
00199                 if((readValue >= 1)&&(readValue <= 5))
00200                 {
00201                         jointName.str("");
00202                         jointName << "arm_joint_" << readValue;
00203 
00204                         cout << "Selected arm joint " << readValue << endl;
00205                 }
00206 
00207                 if(readValue == 6)
00208                 {
00209                         cout << "Select a position:" << endl << "\t1.\tHome" << endl << "\t2.\tCamera View" << endl << "\t3.\tCamera Initialization" << endl << "\t4.\tObject" << endl << "\t5.\tGrasp" << endl << "\t6.\tReturn to joint select" << endl << "\t9.\tExit" << endl;
00210 
00211                         int input = 0;
00212                         cin >> input;
00213 
00214                         if((input >= 1)&&(input <= 5))
00215                         {       /*
00216                                 for(int i = 0; i < numberOfArmJoints; i++)
00217                                 {
00218 
00219                                         jointName.str("");
00220                                         jointName << "arm_joint_" << (i + 1);
00221 
00222                                         armJointPositions[i].joint_uri = jointName.str();
00223                                         if(input == 1)
00224                                         {
00225                                                 armJointPositions[i].value = jointHome[i];
00226                                                 joint[i] = jointHome[i];
00227                                         }
00228                                         if(input == 2)
00229                                         {
00230                                                 armJointPositions[i].value = jointHome[i];
00231                                                 joint[i] = jointCamera[i];
00232                                         }
00233                                         if(input == 3)
00234                                         {
00235                                                 armJointPositions[i].value = jointHome[i];
00236                                                 joint[i] = jointInitialize[i];
00237                                         }
00238                                         if(input == 4)
00239                                         {
00240                                                 armJointPositions[i].value = jointHome[i];
00241                                                 joint[i] = jointObject[i];
00242                                         }
00243                                         if(input == 5)
00244                                         {
00245                                                 armJointPositions[i].value = jointHome[i];
00246                                                 joint[i] = jointGrasp[i];
00247                                         }
00248 
00249                                         armJointPositions[i].unit = boost::units::to_string(boost::units::si::radians);
00250 
00251                                         lastJoint[i] = joint[i];
00252                                 }*/
00253                                 
00254                                 if(input == 1)
00255                                 {
00256                                         for(int i = 0; i < numberOfArmJoints; i++)
00257                                         {
00258 
00259                                                 jointName.str("");
00260                                                 jointName << "arm_joint_" << (i + 1);
00261 
00262                                                 armJointPositions[i].joint_uri = jointName.str();
00263                                                 armJointPositions[i].value = jointHome[i];
00264 
00265                                                 armJointPositions[i].unit = boost::units::to_string(boost::units::si::radians);
00266 
00267                                                 joint[i] = jointHome[i];
00268                                                 lastJoint[i] = joint[i];
00269                                                 //cout << jointName << " " << joint[i] << endl;
00270                                         }
00271                                 }
00272                                 if(input == 2)
00273                                 {
00274                                         for(int i = 0; i < numberOfArmJoints; i++)
00275                                         {
00276 
00277                                                 jointName.str("");
00278                                                 jointName << "arm_joint_" << (i + 1);
00279 
00280                                                 armJointPositions[i].joint_uri = jointName.str();
00281                                                 armJointPositions[i].value = jointCamera[i];
00282 
00283                                                 armJointPositions[i].unit = boost::units::to_string(boost::units::si::radians);
00284 
00285                                                 joint[i] = jointCamera[i];
00286                                                 lastJoint[i] = joint[i];
00287                                                 //cout << jointName << " " << joint[i] << endl;
00288                                         }
00289                                 }
00290                                 if(input == 3)
00291                                 {
00292                                         for(int i = 0; i < numberOfArmJoints; i++)
00293                                         {
00294 
00295                                                 jointName.str("");
00296                                                 jointName << "arm_joint_" << (i + 1);
00297 
00298                                                 armJointPositions[i].joint_uri = jointName.str();
00299                                                 armJointPositions[i].value = jointInitialize[i];
00300 
00301                                                 armJointPositions[i].unit = boost::units::to_string(boost::units::si::radians);
00302 
00303                                                 joint[i] = jointObject[i];
00304                                                 lastJoint[i] = joint[i];
00305                                                 //cout << jointName << " " << joint[i] << endl;
00306                                         }
00307                                 }
00308                                 if(input == 4)
00309                                 {
00310                                         for(int i = 0; i < numberOfArmJoints; i++)
00311                                         {
00312 
00313                                                 jointName.str("");
00314                                                 jointName << "arm_joint_" << (i + 1);
00315 
00316                                                 armJointPositions[i].joint_uri = jointName.str();
00317                                                 armJointPositions[i].value = jointObject[i];
00318 
00319                                                 armJointPositions[i].unit = boost::units::to_string(boost::units::si::radians);
00320 
00321                                                 joint[i] = jointObject[i];
00322                                                 lastJoint[i] = joint[i];
00323                                                 //cout << jointName << " " << joint[i] << endl;
00324                                         }
00325                                 }
00326                                 if(input == 5)
00327                                 {
00328                                         for(int i = 0; i < numberOfArmJoints; i++)
00329                                         {
00330 
00331                                                 jointName.str("");
00332                                                 jointName << "arm_joint_" << (i + 1);
00333 
00334                                                 armJointPositions[i].joint_uri = jointName.str();
00335                                                 armJointPositions[i].value = jointGrasp[i];
00336 
00337                                                 armJointPositions[i].unit = boost::units::to_string(boost::units::si::radians);
00338 
00339                                                 joint[i] = jointGrasp[i];
00340                                                 lastJoint[i] = joint[i];
00341                                                 //cout << "joint " << i << " " << joint[i] << endl;
00342                                         }
00343                                 }
00344                 
00345                                 gripperJointPositions[0].joint_uri = "gripper_finger_joint_l";
00346                                 gripperJointPositions[0].value = 0.01;
00347                                 gripperJointPositions[0].unit = boost::units::to_string(boost::units::si::meter);
00348 
00349                                 gripperJointPositions[1].joint_uri = "gripper_finger_joint_r";
00350                                 gripperJointPositions[1].value = 0.01;
00351                                 gripperJointPositions[1].unit = boost::units::to_string(boost::units::si::meter);
00352 
00353 
00354                                 cout << "Going to joint command: " << joint[0] << " " <<
00355                                                 joint[1] << " " <<
00356                                                 joint[2] << " " <<
00357                                                 joint[3] << " " <<
00358                                                 joint[4] << " " << endl;
00359 
00360                                 command.positions = armJointPositions;
00361                                 armPositionsPublisher.publish(command);
00362 
00363                                 command.positions = gripperJointPositions;
00364                                 gripperPositionPublisher.publish(command);
00365 
00366                         }
00367 
00368                         if(input == 6)
00369                         {
00370                                 cout << "Returning..." << endl;
00371                                 continue;
00372                         }
00373 
00374                         if(input == 9)
00375                         {
00376                                 cout << "Exiting..." << endl;
00377                                 break;
00378                         }
00379 
00380 
00381                         continue;
00382                 }
00383 
00384                 if(readValue == 9)
00385                 {
00386                         cout << "Exiting..." << endl;
00387                         break;
00388                 }
00389 
00390 
00391                 cout << "Hit w/s to increase/decrease joint angle." << endl;
00392                 cout << "If you want to stop manipulating this joint, hit 'm' to switch to new joint or exit." << endl;
00393                 cout << "Hit o/l to open/close gripper." << endl;
00394                 cout << "To quit, hit Control + C and then 'm'." << endl;
00395 
00396                 keyboardInput = '0';
00397         
00398                 while((keyboardInput != 'm')&&ros::ok())
00399                 {       
00400                         //ros::spinOnce();
00401                         /*
00402                         if(commandMatch())
00403                         {*/
00404 
00405                         cout << "Going to joint command: " << joint[0] << " " <<
00406                                         joint[1] << " " <<
00407                                         joint[2] << " " <<
00408                                         joint[3] << " " <<
00409                                         joint[4] << " " << endl;
00410 
00411                         keyboardInput = getch();
00412                 
00413                         if (keyboardInput == 'w')
00414                                 joint[readValue - 1] += jointDelta[readValue - 1];
00415                         if (keyboardInput == 's') 
00416                                 joint[readValue - 1] -= jointDelta[readValue - 1];                      
00417                         if(keyboardInput == 'o')
00418                         {
00419                                 gripperl += gripperDelta;
00420                                 gripperr += gripperDelta;
00421                         }
00422                         if(keyboardInput == 'l')
00423                         {
00424                                 gripperl -= gripperDelta;
00425                                 gripperr -= gripperDelta;
00426                         }
00427 
00428                         if(keyboardInput != '0')
00429                         {
00430 
00431                                 for(int i = 0; i < numberOfArmJoints; i++)
00432                                 {
00433 
00434                                         jointName.str("");
00435                                         jointName << "arm_joint_" << (i + 1);
00436 
00437                                         if(joint[i] < jointMin[i])
00438                                                 joint[i] = jointMin[i];
00439                                         if(joint[i] > jointMax[i])
00440                                                 joint[i] = jointMax[i];
00441 
00442                                         armJointPositions[i].joint_uri = jointName.str();
00443                                         armJointPositions[i].value = joint[i];
00444 
00445                                         armJointPositions[i].unit = boost::units::to_string(boost::units::si::radians);
00446                                 }
00447 
00448                                 if(gripperl < gripperMin)
00449                                         gripperl = gripperMin;
00450                                 if(gripperr < gripperMin)
00451                                         gripperr = gripperMin;
00452                                 if(gripperl > gripperMax)
00453                                         gripperl = gripperMax;
00454                                 if(gripperr > gripperMax)
00455                                         gripperr = gripperMax;
00456 
00457                                 gripperJointPositions[0].joint_uri = "gripper_finger_joint_l";
00458                                 gripperJointPositions[0].value = gripperl;
00459                                 gripperJointPositions[0].unit = boost::units::to_string(boost::units::si::meter);
00460 
00461                                 gripperJointPositions[1].joint_uri = "gripper_finger_joint_r";
00462                                 gripperJointPositions[1].value = gripperr;
00463                                 gripperJointPositions[1].unit = boost::units::to_string(boost::units::si::meter);
00464 
00465                                 command.positions = armJointPositions;
00466                                 armPositionsPublisher.publish(command);
00467 
00468                                 command.positions = gripperJointPositions;
00469                                 gripperPositionPublisher.publish(command);
00470 
00471                                 for(int i = 0; i <numberOfArmJoints; i++)
00472                                 {
00473                                         lastJoint[i] = joint[i];
00474                                 }
00475 
00476                                 cout << "Joint position given was: " << joint[readValue - 1] << endl;
00477                         }
00478                 }
00479 
00480                 rate.sleep();
00481         }
00482 
00483         return 0;
00484 }
00485 
00486 /* EOF */


youbot_teleop
Author(s): Russell Toris , Graylin Trevor Jay
autogenerated on Mon Oct 6 2014 09:08:50