00001 /*!@file AppDevices/test-armSim.C Test the robot arm simulator */ 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-armSim.C $ 00035 // $Id: test-armSim.C 10794 2009-02-08 06:21:09Z itti $ 00036 // 00037 00038 00039 #include "Image/Layout.H" 00040 #include "Image/MatrixOps.H" 00041 #include "GUI/ImageDisplayStream.H" 00042 #include "GUI/XWinManaged.H" 00043 #include "Component/ModelManager.H" 00044 #include "Raster/GenericFrame.H" 00045 #include "Media/FrameSeries.H" 00046 #include "Transport/FrameInfo.H" 00047 #include "Devices/JoyStick.H" 00048 #include "Devices/Scorbot.H" 00049 #include "ArmControl/ArmSim.H" 00050 #include <stdio.h> 00051 #include <stdlib.h> 00052 #include <ode/ode.h> 00053 #include <signal.h> 00054 00055 #define KEY_UP 98 00056 #define KEY_DOWN 104 00057 #define KEY_LEFT 100 00058 #define KEY_RIGHT 102 00059 00060 00061 int getKey(nub::ref<OutputFrameSeries> &ofs) 00062 { 00063 const nub::soft_ref<ImageDisplayStream> ids = 00064 ofs->findFrameDestType<ImageDisplayStream>(); 00065 00066 const rutz::shared_ptr<XWinManaged> uiwin = 00067 ids.is_valid() 00068 ? ids->getWindow("ArmSiminput") 00069 : rutz::shared_ptr<XWinManaged>(); 00070 return uiwin->getLastKeyPress(); 00071 } 00072 00073 Point2D<int> getClick(nub::ref<OutputFrameSeries> &ofs) 00074 { 00075 const nub::soft_ref<ImageDisplayStream> ids = 00076 ofs->findFrameDestType<ImageDisplayStream>(); 00077 00078 const rutz::shared_ptr<XWinManaged> uiwin = 00079 ids.is_valid() 00080 ? ids->getWindow("ArmSiminput") 00081 : rutz::shared_ptr<XWinManaged>(); 00082 return uiwin->getLastMouseClick(); 00083 } 00084 00085 00086 int main(int argc, char *argv[]) 00087 { 00088 // Instantiate a ModelManager: 00089 ModelManager manager("Test Model for armSim"); 00090 00091 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager)); 00092 manager.addSubComponent(ofs); 00093 00094 nub::soft_ref<ArmSim> armSim ((new ArmSim(manager,"ArmSim","ArmSim",21,14,22.5+10,22.5+10)));// length in CM 00095 manager.addSubComponent(armSim); 00096 00097 // Parse command-line: 00098 if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1); 00099 00100 // let's get all our ModelComponent instances started: 00101 manager.start(); 00102 00103 int speed = 75; 00104 while(1){ 00105 armSim->simLoop(); 00106 00107 Image<PixRGB<byte> > armCam = flipVertic(armSim->getFrame(-1)); 00108 00109 ofs->writeRGB(armCam, "ArmSiminput", FrameInfo("ArmSiminput", SRC_POS)); 00110 00111 int key = getKey(ofs); 00112 double pos[]={-0.3,0,0.1}; 00113 if (key != -1) 00114 { 00115 switch(key) 00116 { 00117 case 10: //1 00118 armSim->setMotor(ArmSim::BASE, speed); 00119 break; 00120 case 24: //q 00121 armSim->setMotor(ArmSim::BASE, -1*speed); 00122 break; 00123 case 11: //2 00124 armSim->setMotor(ArmSim::SHOLDER, -1*speed); 00125 break; 00126 case 25: //w 00127 armSim->setMotor(ArmSim::SHOLDER, speed); 00128 break; 00129 case 12: //3 00130 armSim->setMotor(ArmSim::ELBOW, speed); 00131 break; 00132 case 26: //e 00133 armSim->setMotor(ArmSim::ELBOW, -1*speed); 00134 break; 00135 case 13: //4 00136 armSim->setMotor(ArmSim::WRIST1, speed); 00137 break; 00138 case 27: //r 00139 armSim->setMotor(ArmSim::WRIST1, -1*speed); 00140 break; 00141 case 14: //5 00142 armSim->setMotor(ArmSim::WRIST2, speed); 00143 break; 00144 case 28: //t 00145 armSim->setMotor(ArmSim::WRIST2, -1*speed); 00146 break; 00147 case 15: //6 00148 armSim->setMotor(ArmSim::GRIPPER, speed); 00149 break; 00150 case 29: //y 00151 armSim->setMotor(ArmSim::GRIPPER, -1*speed); 00152 break; 00153 case 65: //space 00154 armSim->stopAllMotors(); 00155 break; 00156 case 33: //p 00157 //showEncoders = !showEncoders; 00158 //rawWrist = !rawWrist; 00159 break; 00160 case 58: //m 00161 //showMS = !showMS; 00162 break; 00163 case 43: //h 00164 //home(ofs); 00165 break; 00166 case 32://o Add an object 00167 armSim->addObject(ArmSim::BOX,pos); 00168 break; 00169 00170 case KEY_UP: speed += 1; break; 00171 case KEY_DOWN: speed -= 1; break; 00172 00173 } 00174 00175 if (speed < 0) speed = 0; 00176 if (speed > 100) speed = 100; 00177 00178 00179 LINFO("Key = %i", key); 00180 } 00181 00182 Point2D<int> clickLoc = getClick(ofs); 00183 if (clickLoc.isValid()) 00184 { 00185 double loc[3]; 00186 armSim->getObjLoc(clickLoc.i, clickLoc.j, loc); 00187 LINFO("Loc %f,%f,%f", loc[0], loc[1], loc[2]); 00188 } 00189 } 00190 return 0; 00191 00192 }