00001 #include <Ice/Ice.h> 00002 #include "Ice/Scorbot.ice.H" 00003 #include "Image/Point2D.H" 00004 #include <signal.h> 00005 #include <ncurses.h> 00006 00007 Robots::ScorbotIcePrx scorbot; 00008 WINDOW *mainWin; 00009 00010 // ###################################################################### 00011 void cleanupAndExit() 00012 { 00013 std::cerr << 00014 std::endl << "*** EXITING - STOPPING SCORBOT ***" << std::endl; 00015 scorbot->stopAllMotors(); 00016 sleep(1); 00017 endwin(); 00018 exit(0); 00019 } 00020 00021 // ###################################################################### 00022 void terminate(int s) 00023 { 00024 cleanupAndExit(); 00025 } 00026 00027 // ###################################################################### 00028 void printMenu() 00029 { 00030 mvprintw(0,0,"Scorbot Remote Control"); 00031 clrtoeol(); 00032 mvprintw(2,0,"P Set Position of Target: "); 00033 clrtoeol(); 00034 mvprintw(3,0,"ESC quits"); 00035 clrtoeol(); 00036 00037 refresh(); 00038 } 00039 00040 Point2D<double> getTargetPos() 00041 { 00042 double x, y; 00043 00044 mvprintw(4, 0, ""); 00045 clrtoeol(); 00046 mvprintw(5, 0, ""); 00047 clrtoeol(); 00048 nodelay(mainWin, false); 00049 echo(); 00050 mvscanw(2, 27, "%f %f", &x, &y); 00051 clrtoeol(); 00052 mvprintw(4, 0, "Pointing To: %f, %f", -343, y); 00053 clrtoeol(); 00054 nodelay(mainWin, true); 00055 noecho(); 00056 00057 return Point2D<double>(x,y); 00058 } 00059 00060 // ###################################################################### 00061 // Point the camera at a 2D position on the board at a given angle (camera to board angle, in radians), with the camera 00062 // 'camDist' mm away from the target, and the linear slide set xOffset mm to the side of the target. 00063 Robots::ArmPos calcArmPos(Point2D<double> targetPos, double xOffset, double angle, double camDist) 00064 { 00065 double x = targetPos.i; 00066 double y = targetPos.j; 00067 00068 Robots::ArmPos pos; 00069 00070 // Slide offset 00071 pos.ex1 = x + xOffset; 00072 00073 // Base Angle 00074 pos.base = atan2(y,x); 00075 00076 //Distance to the target from the base axis 00077 double x_t = sqrt(x*x+y*y); 00078 00079 //Camera Positions 00080 double camX = x_t - cos(angle)*camDist; 00081 double camY = sin(angle)*camDist; 00082 00083 mvprintw(5, 0, "CamPos: %f, %f", camX, camY); 00084 refresh(); 00085 00086 return pos; 00087 } 00088 00089 // ###################################################################### 00090 void mainLoop() 00091 { 00092 mainWin = initscr(); 00093 mainWin = mainWin; 00094 noecho(); 00095 cbreak(); 00096 00097 nodelay(mainWin, true); 00098 //keypad(win, true); 00099 00100 printMenu(); 00101 while(1) 00102 { 00103 char c = getch(); 00104 00105 switch(c) 00106 { 00107 case 27: //Escape Key 00108 cleanupAndExit(); 00109 break; 00110 case 'p': 00111 case 'P': 00112 Point2D<double> targetPos = getTargetPos(); 00113 Robots::ArmPos armPos = calcArmPos(targetPos, 0, M_PI/4, 500); 00114 00115 break; 00116 00117 } 00118 00119 printMenu(); 00120 00121 // mvprintw(3, 0, "You Pressed: %d ", c); 00122 clrtoeol(); 00123 refresh(); 00124 wrefresh(mainWin); 00125 00126 } 00127 00128 } 00129 00130 // ###################################################################### 00131 int main(int argc, char* argv[]) 00132 { 00133 00134 signal(SIGHUP, terminate); signal(SIGINT, terminate); 00135 signal(SIGQUIT, terminate); signal(SIGTERM, terminate); 00136 signal(SIGALRM, terminate); 00137 00138 int status = 0; 00139 Ice::CommunicatorPtr ic; 00140 00141 try { 00142 ic = Ice::initialize(argc, argv); 00143 Ice::ObjectPrx base = ic->stringToProxy( 00144 "ScorbotServer:default -p 10000"); 00145 scorbot = Robots::ScorbotIcePrx::checkedCast(base); 00146 if(!scorbot) 00147 throw "Invalid Proxy!"; 00148 00149 mainLoop(); 00150 00151 } catch(const Ice::Exception& ex) { 00152 std::cerr << ex << std::endl; 00153 status = 1; 00154 } catch(const char* msg) { 00155 std::cerr << msg << std::endl; 00156 status = 1; 00157 } 00158 00159 if(ic) 00160 ic->destroy(); 00161 00162 cleanupAndExit(); 00163 return status; 00164 }