00001 /*!@file ArmControl/test-ArmController.C test the arm controller */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Lior Elazary <elazary@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/ArmControl/test-ArmController.C $ 00035 // $Id: test-ArmController.C 10794 2009-02-08 06:21:09Z itti $ 00036 // 00037 00038 // 00039 #include "Image/Layout.H" 00040 #include "Image/MatrixOps.H" 00041 #include "Component/ModelManager.H" 00042 #include "Devices/DeviceOpts.H" 00043 #include "GUI/GeneralGUI.H" 00044 #include "ArmControl/ArmController.H" 00045 #include "ArmControl/ArmSim.H" 00046 #include "GUI/XWinManaged.H" 00047 #include "GUI/ImageDisplayStream.H" 00048 #include "Raster/GenericFrame.H" 00049 #include "Media/FrameSeries.H" 00050 #include "ArmControl/RobotArm.H" 00051 #include "Transport/FrameInfo.H" 00052 00053 00054 #include <unistd.h> 00055 #include <stdio.h> 00056 #include <signal.h> 00057 00058 #define KEY_UP 98 00059 #define KEY_DOWN 104 00060 #define KEY_LEFT 100 00061 #define KEY_RIGHT 102 00062 00063 #define ARMSIM 00064 00065 //! Signal handler (e.g., for control-C) 00066 void terminate(int s) 00067 { 00068 LERROR("*** INTERRUPT ***"); 00069 exit(0); 00070 } 00071 00072 int getKey(nub::soft_ref<OutputFrameSeries> &ofs) 00073 { 00074 const nub::soft_ref<ImageDisplayStream> ids = 00075 ofs->findFrameDestType<ImageDisplayStream>(); 00076 00077 const rutz::shared_ptr<XWinManaged> uiwin = 00078 ids.is_valid() 00079 ? ids->getWindow("GUIDisplay") 00080 : rutz::shared_ptr<XWinManaged>(); 00081 return uiwin->getLastKeyPress(); 00082 } 00083 00084 void recordCmd(nub::soft_ref<ArmController> &armCtrl, std::vector<ArmController::JointPos> &jointPositions) 00085 { 00086 00087 ArmController::JointPos jointPos = armCtrl->getJointPos(); 00088 LINFO("Recording Pos: %i %i %i %i %i %i", 00089 jointPos.base, 00090 jointPos.sholder, 00091 jointPos.elbow, 00092 jointPos.wrist1, 00093 jointPos.wrist2, 00094 jointPos.gripper); 00095 jointPositions.push_back(jointPos); 00096 00097 } 00098 00099 void playCmd(nub::soft_ref<ArmController> &armCtrl, std::vector<ArmController::JointPos> &jointPositions) 00100 { 00101 for(uint i=0; i<jointPositions.size(); i++) 00102 { 00103 ArmController::JointPos jointPos = jointPositions[i]; 00104 LINFO("Playing Pos: %i %i %i %i %i %i", 00105 jointPos.base, 00106 jointPos.sholder, 00107 jointPos.elbow, 00108 jointPos.wrist1, 00109 jointPos.wrist2, 00110 jointPos.gripper); 00111 armCtrl->setJointPos(jointPos); 00112 } 00113 } 00114 00115 00116 00117 00118 00119 int main(int argc, const char **argv) 00120 { 00121 // Instantiate a ModelManager: 00122 ModelManager *mgr = new ModelManager("Test Arm Controller"); 00123 00124 // nub::soft_ref<InputFrameSeries> ifs(new InputFrameSeries(*mgr)); 00125 // mgr->addSubComponent(ifs); 00126 00127 nub::soft_ref<OutputFrameSeries> ofs(new OutputFrameSeries(*mgr)); 00128 mgr->addSubComponent(ofs); 00129 00130 // nub::soft_ref<ArmSim> robotArm(new ArmSim(*mgr)); 00131 nub::soft_ref<Scorbot> robotArm(new Scorbot(*mgr,"Scorbot", "Scorbot", "/dev/ttyUSB0")); 00132 00133 nub::soft_ref<ArmController> armController(new ArmController(*mgr, 00134 "ArmController", "ArmController", robotArm)); 00135 mgr->addSubComponent(armController); 00136 00137 nub::soft_ref<GeneralGUI> armGUI(new GeneralGUI(*mgr)); 00138 mgr->addSubComponent(armGUI); 00139 00140 mgr->exportOptions(MC_RECURSE); 00141 00142 00143 // Parse command-line: 00144 if (mgr->parseCommandLine(argc, argv, "", 0, 0) == false) return(1); 00145 00146 // catch signals and redirect them to terminate for clean exit: 00147 signal(SIGHUP, terminate); signal(SIGINT, terminate); 00148 signal(SIGQUIT, terminate); signal(SIGTERM, terminate); 00149 signal(SIGALRM, terminate); 00150 00151 // let's get all our ModelComponent instances started: 00152 mgr->start(); 00153 00154 //start the gui thread 00155 armGUI->startThread(ofs); 00156 sleep(1); 00157 //setup gui for various objects 00158 armGUI->setupGUI(armController.get(), true); 00159 00160 //Main GUI Window 00161 00162 00163 armGUI->addMeter(armController->getMotor_Base_Ptr(), 00164 "Motor_Base", -100, PixRGB<byte>(0, 255, 0)); 00165 armGUI->addMeter(armController->getMotor_Sholder_Ptr(), 00166 "Motor_Sholder", -100, PixRGB<byte>(0, 255, 0)); 00167 armGUI->addMeter(armController->getMotor_Elbow_Ptr(), 00168 "Motor_Elbow", -100, PixRGB<byte>(0, 255, 0)); 00169 armGUI->addMeter(armController->getMotor_Wrist1_Ptr(), 00170 "Motor_Wrist1", -100, PixRGB<byte>(0, 255, 0)); 00171 armGUI->addMeter(armController->getMotor_Wrist2_Ptr(), 00172 "Motor_Wrist2", -100, PixRGB<byte>(0, 255, 0)); 00173 00174 00175 armGUI->addImage(armController->getPIDImagePtr()); 00176 00177 //set The min-max joint pos 00178 ArmController::JointPos jointPos; 00179 jointPos.base = 8000; 00180 jointPos.sholder = 5000; 00181 jointPos.elbow = 5000; 00182 jointPos.wrist1 = 2000; 00183 jointPos.wrist2 = 2000; 00184 //jointPos.base = 3000; 00185 //jointPos.sholder = 3000; 00186 //jointPos.elbow = 2000; 00187 //jointPos.wrist1 = 0; 00188 //jointPos.wrist2 = 0; 00189 armController->setMaxJointPos(jointPos); 00190 00191 jointPos.base = -8000; 00192 jointPos.sholder = -1500; 00193 jointPos.elbow = -3000; 00194 jointPos.wrist1 = -2000; 00195 jointPos.wrist2 = -2000; 00196 armController->setMinJointPos(jointPos); 00197 00198 00199 00200 int speed = 75; 00201 std::vector<ArmController::JointPos> jointPositions; 00202 armController->setMotorsOn(true); 00203 armController->setPidOn(true); 00204 while(1) { 00205 //armController->setBasePos(1000, false); 00206 //sleep(6); 00207 //armController->setBasePos(0, false); 00208 //sleep(6); 00209 if(0){//if it's sim 00210 robotArm->simLoop(); 00211 Image<PixRGB<byte> > armCam = flipVertic(robotArm->getFrame(-1)); 00212 ofs->writeRGB(armCam, "ArmSiminput", FrameInfo("ArmSiminput", SRC_POS)); 00213 } 00214 int key = -1; //getKey(ofs); 00215 if (key != -1) 00216 { 00217 switch(key) 00218 { 00219 00220 //Controll arm 00221 case 10: //1 00222 armController->setBasePos(10, true); 00223 break; 00224 case 24: //q 00225 armController->setBasePos(-10, true); 00226 break; 00227 case 11: //2 00228 armController->setSholderPos(-10, true); 00229 break; 00230 case 25: //w 00231 armController->setSholderPos(10, true); 00232 break; 00233 case 12: //3 00234 armController->setElbowPos(10, true); 00235 break; 00236 case 26: //e 00237 armController->setElbowPos(-10, true); 00238 break; 00239 case 13: //4 00240 armController->setWrist1Pos(10, true); 00241 armController->setWrist2Pos(10, true); 00242 break; 00243 case 27: //r 00244 armController->setWrist1Pos(-10, true); 00245 armController->setWrist2Pos(-10, true); 00246 break; 00247 case 14: //5 00248 armController->setWrist1Pos(10, true); 00249 armController->setWrist2Pos(-10, true); 00250 break; 00251 case 28: //t 00252 armController->setWrist1Pos(-10, true); 00253 armController->setWrist2Pos(10, true); 00254 break; 00255 case 15: //6 00256 armController->setGripper(0); 00257 break; 00258 case 29: //y 00259 armController->setGripper(1); 00260 break; 00261 case 65: //space 00262 armController->killMotors(); 00263 break; 00264 00265 //Record and play joint positions 00266 case 54: //c record position 00267 recordCmd(armController, jointPositions); 00268 break; 00269 case 46: //l play back pos 00270 playCmd(armController, jointPositions); 00271 break; 00272 case 56: //b clear joint pos 00273 LINFO("Clearing joint positions"); 00274 jointPositions.clear(); 00275 break; 00276 00277 //Change speed 00278 case KEY_UP: 00279 speed += 1; 00280 break; 00281 case KEY_DOWN: 00282 speed -= 1; 00283 break; 00284 00285 } 00286 00287 if (speed < 0) speed = 0; 00288 if (speed > 100) speed = 100; 00289 00290 LINFO("Key = %i", key); 00291 } 00292 00293 usleep(1000); 00294 }//End While(1) 00295 00296 // stop all our ModelComponents 00297 mgr->stop(); 00298 00299 // all done! 00300 return 0; 00301 } 00302