test-ScorbotIK.C
00001 #include <Ice/Ice.h>
00002 #include "Ice/Scorbot.ice.H"
00003 #include "Image/Point2D.H"
00004 #include "Image/DrawOps.H"
00005 #include "Image/MathOps.H"
00006 #include "Image/ColorOps.H"
00007 #include <signal.h>
00008 #include "Component/ModelManager.H"
00009 #include "Raster/Raster.H"
00010 #include "Util/MathFunctions.H"
00011 #include "Util/Types.H"
00012 #include "Util/log.H"
00013 #include "Media/FrameSeries.H"
00014 #include "Transport/FrameInfo.H"
00015 #include "Raster/GenericFrame.H"
00016 #include "GUI/XWinManaged.H"
00017 #include "GUI/ImageDisplayStream.H"
00018 #include "GUI/PrefsWindow.H"
00019 #include "GUI/DebugWin.H"
00020
00021
00022 Robots::ScorbotIcePrx scorbot;
00023
00024
00025 void cleanupAndExit()
00026 {
00027 std::cerr <<
00028 std::endl << "*** EXITING - STOPPING SCORBOT ***" << std::endl;
00029 scorbot->stopAllMotors();
00030 sleep(1);
00031 exit(0);
00032 }
00033
00034
00035 void terminate(int s)
00036 {
00037 cleanupAndExit();
00038 }
00039
00040 int getKey(nub::ref<OutputFrameSeries> &ofs)
00041 {
00042 const nub::soft_ref<ImageDisplayStream> ids =
00043 ofs->findFrameDestType<ImageDisplayStream>();
00044
00045 const rutz::shared_ptr<XWinManaged> uiwin =
00046 ids.is_valid()
00047 ? ids->getWindow("Output")
00048 : rutz::shared_ptr<XWinManaged>();
00049 return uiwin->getLastKeyPress();
00050 }
00051
00052
00053
00054 Point2D<double> getTargetPos()
00055 {
00056 double x, y;
00057
00058 std::cout << "----------------------------------------" << std::endl;
00059 std::cout << "Enter a target point in mm: (x y): ";
00060 std::cin >> x >> y;
00061 return Point2D<double>(x,y);
00062 }
00063
00064
00065
00066
00067 Robots::ArmPos calcArmPos(Point2D<double> targetPos, double xOffset, double angle, double camDist)
00068 {
00069
00070
00071 double armSlideXoffset = 0.0;
00072 double armSlideYoffset = 0.0;
00073 double armZoffset = 0.0;
00074
00075
00076
00077 double camToWristX = 0.0;
00078 double camToWristY = 0.0;
00079
00080
00081
00082 double targetX = targetPos.i;
00083 double targetY = targetPos.j;
00084
00085
00086 double d_t = sqrt(pow(xOffset,2) + pow(targetY+armSlideYoffset,2));
00087
00088
00089 double x_hat_cam = cos(angle)*camDist;
00090 double y_hat_cam = sin(angle)*camDist;
00091
00092
00093
00094
00095
00096 double x_cam = d_t - x_hat_cam;
00097 double y_cam = y_hat_cam - armZoffset;
00098
00099
00100 double cam_x_off = camToWristX * cos(-angle) - camToWristY*sin(-angle);
00101 double cam_y_off = camToWristX * sin(-angle) + camToWristY*cos(-angle);
00102
00103
00104 double wrist_x = x_cam + cam_x_off;
00105 double wrist_y = y_cam + cam_y_off;
00106
00107
00108 double sliderPos = targetX + xOffset - armSlideXoffset;
00109
00110
00111 double baseAngle = atan2(xOffset, targetY+armSlideYoffset);
00112
00113
00114 Robots::ArmPos ik = scorbot->getIK(0, wrist_x, wrist_y);
00115
00116
00117 ik.ex1 = scorbot->mm2enc(sliderPos);
00118 ik.base = scorbot->ang2enc(baseAngle);
00119
00120 std::cout << "---IK---" << std::endl;
00121 std::cout << "base: " << ik.base << std::endl;
00122 std::cout << "shoulder: " << ik.shoulder << std::endl;
00123 std::cout << "elbow: " << ik.elbow << std::endl;
00124 std::cout << "wrist1: " << ik.wrist1 << std::endl;
00125 std::cout << "wrist2: " << ik.wrist2 << std::endl;
00126 std::cout << "ex1: " << ik.ex1 << std::endl;
00127 std::cout << "--------" << std::endl;
00128
00129 return ik;
00130 }
00131
00132
00133
00134 std::vector<Robots::ArmPos> getArmPositions()
00135 {
00136
00137 std::vector<Robots::ArmPos> positions;
00138 Robots::ArmPos armpos;
00139 armpos.base = 0;
00140 armpos.shoulder = 0;
00141 armpos.elbow = 0;
00142 armpos.wrist1 = 0;
00143 armpos.wrist2 = 0;
00144 armpos.ex1 = 0;
00145 armpos.ex2 = 0;
00146 armpos.duration = 500;
00147
00148 armpos.base=-823; armpos.shoulder=1594; armpos.elbow=-26751; armpos.wrist1=224; armpos.wrist2=-125; armpos.gripper=0; armpos.ex1=49;
00149 positions.push_back(armpos);
00150 armpos.base=-707; armpos.shoulder=1292; armpos.elbow=-27673; armpos.wrist1=1143; armpos.wrist2=-918; armpos.gripper=0; armpos.ex1=49;
00151 positions.push_back(armpos);
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 return positions;
00177
00178 }
00179
00180 void setWrist(Robots::ArmPos &armPos, float ang)
00181 {
00182 armPos.wrist1 = (ang*(2568.0*2.0)/M_PI);
00183 armPos.wrist2 = -1*ang*(2568.0*2.0)/M_PI;
00184 }
00185
00186
00187 int main(int argc, char* argv[])
00188 {
00189 signal(SIGHUP, terminate); signal(SIGINT, terminate);
00190 signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00191 signal(SIGALRM, terminate);
00192
00193
00194 ModelManager manager("Test Model for Scorbot robot arm controller");
00195
00196 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00197 manager.addSubComponent(ofs);
00198
00199 nub::ref<InputFrameSeries> ifs(new InputFrameSeries(manager));
00200 manager.addSubComponent(ifs);
00201
00202
00203
00204 if (manager.parseCommandLine(argc, argv, "FileName", 0, 1) == false) return(1);
00205
00206 std::string fileName = manager.getExtraArg(0);
00207
00208
00209 manager.start();
00210
00211 ifs->startStream();
00212 while(1)
00213 {
00214 GenericFrame input = ifs->readFrame();
00215 if (!input.initialized())
00216 break;
00217
00218
00219 Image<PixRGB<byte> > inImage = input.asRgb();
00220 Point2D<int> center(inImage.getWidth()/2, inImage.getHeight()/2);
00221 Dims winSize(512,512);
00222
00223 Image<PixRGB<byte> > objImg = crop(inImage, Rectangle::centerDims(center, winSize));
00224 drawCircle(objImg, Point2D<int>(256,256), 3, PixRGB<byte>(255,0,0), 3);
00225
00226 drawCircle(inImage, Point2D<int>(inImage.getWidth()/2,inImage.getHeight()/2), 3, PixRGB<byte>(255,0,0), 3);
00227
00228 ofs->writeRGB(inImage, "Output", FrameInfo("output", SRC_POS));
00229
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397 return 0;
00398 }