PrimaryMotorCortexService.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "NeovisionII/PrimaryMotorCortexService.H"
00039 #include "Util/sformat.H"
00040 #include "Image/DrawOps.H"
00041
00042 using namespace std;
00043 using namespace ImageIceMod;
00044
00045 PrimaryMotorCortexI::PrimaryMotorCortexI(OptionManager& mgr,
00046 const std::string& descrName,
00047 const std::string& tagName) :
00048 ModelComponent(mgr, descrName, tagName)
00049 {
00050
00051
00052 IceStorm::TopicPrx topicPrx;
00053 itsTopicsSubscriptions.push_back(TopicInfo("PrefrontalCortexTopic", topicPrx));
00054 itsTopicsSubscriptions.push_back(TopicInfo("HomeInterfaceTopic", topicPrx));
00055
00056 itsScorbot = nub::soft_ref<Scorbot>(new Scorbot(mgr));
00057 addSubComponent(itsScorbot);
00058
00059 itsHomePosition.base = 10413;
00060 itsHomePosition.sholder = -9056;
00061 itsHomePosition.elbow = 11867;
00062 itsHomePosition.wristRoll = -6707;
00063 itsHomePosition.wristPitch = 4513;
00064 itsHomePosition.gripper = -1434;
00065 itsHomePosition.ex1 = 500;
00066 }
00067
00068
00069
00070 PrimaryMotorCortexI::~PrimaryMotorCortexI()
00071 {
00072 unsubscribeSimEvents();
00073 }
00074
00075 void PrimaryMotorCortexI::start2()
00076 {
00077 }
00078
00079 void PrimaryMotorCortexI::stop1()
00080 {
00081
00082 itsScorbot->stopControl();
00083 itsScorbot->motorsOff();
00084 LINFO("Done");
00085 }
00086
00087 void PrimaryMotorCortexI::init()
00088 {
00089
00090 itsScorbot->motorsOn();
00091 itsScorbot->startControl();
00092 Scorbot::ArmPos currArmPos = itsScorbot->getArmPos();
00093 itsScorbot->setArmPos(currArmPos);
00094
00095 goHome();
00096 LINFO("!At Home.");
00097
00098 }
00099
00100
00101 void PrimaryMotorCortexI::initSimEvents(Ice::CommunicatorPtr icPtr, Ice::ObjectPrx objectPrx)
00102 {
00103
00104 Ice::ObjectPrx obj = icPtr->stringToProxy("SimEvents/TopicManager:tcp -p 11111");
00105 IceStorm::TopicManagerPrx topicManager =
00106 IceStorm::TopicManagerPrx::checkedCast(obj);
00107
00108
00109 IceStorm::TopicPrx topic;
00110 try {
00111 topic = topicManager->retrieve("PrimaryMotorCortexTopic");
00112 } catch (const IceStorm::NoSuchTopic&) {
00113 topic = topicManager->create("PrimaryMotorCortexTopic");
00114 }
00115
00116 Ice::ObjectPrx pub = topic->getPublisher()->ice_oneway();
00117 itsEventsPub = SimEvents::EventsPrx::uncheckedCast(pub);
00118
00119
00120 itsObjectPrx = objectPrx;
00121
00122 for(uint i=0; i<itsTopicsSubscriptions.size(); i++)
00123 {
00124 try {
00125 IceStorm::QoS qos;
00126 itsTopicsSubscriptions[i].topicPrx =
00127 topicManager->retrieve(itsTopicsSubscriptions[i].name.c_str());
00128 itsTopicsSubscriptions[i].topicPrx->subscribeAndGetPublisher(qos, itsObjectPrx);
00129 } catch (const IceStorm::NoSuchTopic&) {
00130 LFATAL("Error! No %s topic found!", itsTopicsSubscriptions[i].name.c_str());
00131 } catch (const char* msg) {
00132 LINFO("Error %s", msg);
00133 } catch (const Ice::Exception& e) {
00134 cerr << e << endl;
00135 }
00136 }
00137 }
00138
00139 void PrimaryMotorCortexI::unsubscribeSimEvents()
00140 {
00141
00142 for(uint i=0; i<itsTopicsSubscriptions.size(); i++)
00143 {
00144 itsTopicsSubscriptions[i].topicPrx->unsubscribe(itsObjectPrx);
00145 }
00146 }
00147
00148
00149 void PrimaryMotorCortexI::moveObject(Point3D<float> P1)
00150 {
00151 std::vector<std::string> PositionNames;
00152
00153 std::vector<Scorbot::ArmPos> armPositions;
00154 Scorbot::ArmPos tmpArmPos;
00155
00156
00157 Point3D<float> aboveObjectPoint = P1;
00158 aboveObjectPoint.z = aboveObjectPoint.z + 100;
00159 tmpArmPos = itsScorbot->getIKArmPos(aboveObjectPoint);
00160 armPositions.push_back(tmpArmPos);
00161 PositionNames.push_back("Above Object");
00162
00163
00164 tmpArmPos.gripper = 0;
00165 armPositions.push_back(tmpArmPos);
00166 PositionNames.push_back("Open Gripper");
00167
00168
00169 tmpArmPos = itsScorbot->getIKArmPos(P1);
00170 armPositions.push_back(tmpArmPos);
00171 PositionNames.push_back("Object");
00172
00173
00174 Point3D<float> slidePoint = P1;
00175 slidePoint.y = slidePoint.y - 1000;
00176 tmpArmPos = itsScorbot->getIKArmPos(slidePoint);
00177 armPositions.push_back(tmpArmPos);
00178 PositionNames.push_back("Slide Object");
00179
00180
00181 slidePoint.z = slidePoint.z + 100;
00182 tmpArmPos = itsScorbot->getIKArmPos(slidePoint);
00183 armPositions.push_back(tmpArmPos);
00184 PositionNames.push_back("Above Slid Object");
00185
00186
00187 tmpArmPos.ex1 = 1000;
00188 armPositions.push_back(tmpArmPos);
00189 PositionNames.push_back("Sliding Back");
00190
00191
00192 for(uint i=0; i<armPositions.size(); i++)
00193 {
00194 LINFO("Going To %s\n", PositionNames.at(i).c_str());
00195 itsScorbot->setArmPos(armPositions[i]);
00196 sleep(1);
00197 int timer = 0;
00198 while(!itsScorbot->moveDone() && timer < 300)
00199 {
00200 usleep(10000);
00201 timer++;
00202 }
00203 if(PositionNames.at(i) == "Object")
00204 sleep(5);
00205
00206 }
00207
00208
00209 LINFO("Finished Moving Object - Going Home");
00210 goHome();
00211 }
00212
00213 void PrimaryMotorCortexI::goHome()
00214 {
00215
00216
00217 Scorbot::ArmPos itsHomePosition;
00218 itsHomePosition.base = 10413;
00219 itsHomePosition.sholder = -4500;
00220 itsHomePosition.elbow = 7567;
00221 itsHomePosition.wristRoll = 8371;
00222 itsHomePosition.wristPitch = 4811;
00223 itsHomePosition.gripper = -1434;
00224 itsHomePosition.ex1 = 500;
00225
00226 std::vector<Scorbot::ArmPos> armPositions;
00227 Scorbot::ArmPos tmpArmPos = itsScorbot->getArmPos();
00228
00229 LINFO("Add positions");
00230 {
00231
00232 tmpArmPos.gripper = 0;
00233 armPositions.push_back(tmpArmPos);
00234
00235
00236 tmpArmPos.sholder = itsHomePosition.sholder;
00237 tmpArmPos.elbow = itsHomePosition.elbow;
00238 tmpArmPos.wristRoll = itsHomePosition.wristRoll;
00239 tmpArmPos.wristPitch = itsHomePosition.wristPitch;
00240 armPositions.push_back(tmpArmPos);
00241
00242
00243 tmpArmPos.ex1 = itsHomePosition.ex1;
00244 armPositions.push_back(tmpArmPos);
00245
00246
00247 tmpArmPos.base = itsHomePosition.base;
00248 armPositions.push_back(tmpArmPos);
00249 }
00250
00251 LINFO("Move to positions");
00252
00253 for(uint i=0; i<armPositions.size(); i++)
00254 {
00255 LINFO("Going to Pos %i...", i);
00256 itsScorbot->setArmPos(armPositions[i]);
00257 sleep(1);
00258 int timer = 0;
00259 while(!itsScorbot->moveDone() && timer < 300)
00260 {
00261 usleep(10000);
00262 timer++;
00263 }
00264 LINFO("Pos %i reached in timer %i", i, timer);
00265 }
00266 }
00267
00268
00269 void PrimaryMotorCortexI::evolve(const SimEvents::EventMessagePtr& eMsg,
00270 const Ice::Current&)
00271 {
00272 LINFO("Got message");
00273 if (eMsg->ice_isA("::SimEvents::PrimaryMotorCortexBiasMessage"))
00274 {
00275 SimEvents::PrimaryMotorCortexBiasMessagePtr msg = SimEvents::PrimaryMotorCortexBiasMessagePtr::dynamicCast(eMsg);
00276
00277 if (msg->moveArm)
00278 {
00279 LINFO("Move Object");
00280 float x = msg->armPos.x;
00281 float y = msg->armPos.y;
00282 float z = msg->armPos.z;
00283
00284 moveObject(Point3D<float>(x,y,z));
00285
00286 }
00287 }
00288
00289 }
00290
00291
00292 class PrimaryMotorCortexService : public Ice::Service {
00293 protected:
00294 virtual bool start(int, char* argv[]);
00295 virtual bool stop() {
00296 if (itsMgr)
00297 delete itsMgr;
00298 return true;
00299 }
00300
00301 private:
00302 Ice::ObjectAdapterPtr itsAdapter;
00303 ModelManager *itsMgr;
00304 };
00305
00306 bool PrimaryMotorCortexService::start(int argc, char* argv[])
00307 {
00308
00309 itsMgr = new ModelManager("PrimaryMotorCortexService");
00310
00311 nub::ref<PrimaryMotorCortexI> pmc(new PrimaryMotorCortexI(*itsMgr));
00312 itsMgr->addSubComponent(pmc);
00313
00314 itsMgr->parseCommandLine((const int)argc, (const char**)argv, "", 0, 0);
00315
00316 char adapterStr[255];
00317 sprintf(adapterStr, "default -p %i", BrainObjects::PrimaryMotorCortexPort);
00318 itsAdapter = communicator()->createObjectAdapterWithEndpoints("PMCAdapter",
00319 adapterStr);
00320
00321 Ice::ObjectPtr object = pmc.get();
00322 Ice::ObjectPrx objectPrx = itsAdapter->add(object, communicator()->stringToIdentity("PMC"));
00323 pmc->initSimEvents(communicator(), objectPrx);
00324 itsAdapter->activate();
00325
00326 itsMgr->start();
00327 pmc->init();
00328
00329 return true;
00330 }
00331
00332
00333 int main(int argc, char** argv) {
00334
00335 PrimaryMotorCortexService svc;
00336 return svc.main(argc, argv);
00337 }