00001 #ifndef __PREMOTOR_COMPLEX_C__ 00002 #define __PREMOTOR_COMPLEX_C__ 00003 00004 #include "Util/log.H" 00005 #include "PreMotorComplex.H" 00006 #include "Raster/Raster.H" 00007 00008 00009 // ###################################################################### 00010 PreMotorComplex::PreMotorComplex(nub::soft_ref<SubController> motor, /*rutz::shared_ptr<CaptainAgent pfc,*/ std::string name) 00011 //, rutz::shared_ptr<AgentManagerA> managerPtr) 00012 // : 00013 // itsCurrentMove(), 00014 // itsNextMove(), 00015 // itsPrimitiveMotor(motor) 00016 : SubmarineAgent(name) 00017 { 00018 // itsName = name; 00019 // //managerA = NULL;//managerPtr; 00020 // //cortex = NULL; //managerA->getCaptainAgent(); 00021 // itsStop = itsInterrupt = isMoveDone = false; 00022 // //initial depth value at surface 00023 // itsPrimitiveMotor->killMotors(); 00024 // itsPrimitiveMotor->setMotorsOn(false); 00025 00026 } 00027 00028 // ###################################################################### 00029 PreMotorComplex::PreMotorComplex(std::string name) 00030 : SubmarineAgent(name) 00031 //, rutz::shared_ptr<AgentManagerA> managerPtr) 00032 // : 00033 // itsCurrentMove(), 00034 // itsNextMove() 00035 { 00036 itsName = name; 00037 //managerA = NULL;//managerPtr; 00038 //cortex = NULL; //managerA->getCaptainAgent(); 00039 // itsStop = itsInterrupt = false; 00040 } 00041 00042 // ###################################################################### 00043 PreMotorComplex::~PreMotorComplex() { 00044 } 00045 00046 // ###################################################################### 00047 // void PreMotorComplex::start() { 00048 00049 // itsPrimitiveMotor->setMotorsOn(false); 00050 // while(!itsPrimitiveMotor->getKillSwitch()) { 00051 // sleep(1); 00052 // LINFO("Waiting for Kill Switch"); 00053 // } 00054 00055 00056 // while(itsPrimitiveMotor->getKillSwitch() && !itsStop) { 00057 00058 // while(itsCurrentMove.is_valid() && !itsInterrupt) { 00059 // LINFO("RUNNING MOVE: %d & %d", 00060 // itsCurrentMove.is_valid(), !itsInterrupt); 00061 // run(); 00062 // } 00063 00064 // // itsPrimitiveMotor->setLevel(); 00065 00066 // if(itsInterrupt) { 00067 // LINFO("INTERRUPT MOVE SET"); 00068 00069 // if(itsNextMove.is_valid()) { 00070 // LINFO("NEW MOVE SET TO RUN"); 00071 // itsCurrentMove = itsNextMove; 00072 // itsNextMove.reset(); 00073 // itsInterrupt = false; 00074 // } 00075 // else 00076 // LINFO("NEW MOVE IS NOT VALID. NO MOVE"); 00077 // } 00078 // else 00079 // LINFO("NO MOVE"); 00080 00081 // isMoveDone = false; 00082 // } 00083 00084 00085 // LINFO("STOP EVERYTHING"); 00086 00087 // itsCurrentMove.reset(); 00088 // itsNextMove.reset(); 00089 00090 // itsPrimitiveMotor->killMotors(); 00091 // itsPrimitiveMotor->setMotorsOn(false); 00092 // } 00093 00094 00095 // // ###################################################################### 00096 // void PreMotorComplex::run(rutz::shared_ptr<ComplexMovement> c) { 00097 // msgHalt(); 00098 // itsNextMove = c; 00099 // //itsInterrupt = false; 00100 // itsCurrentMove.reset(); 00101 // //msgHalt(); 00102 // //run(); 00103 // } 00104 00105 00106 // // ###################################################################### 00107 // void PreMotorComplex::run() { 00108 00109 // // uint i = 0; 00110 00111 // // while(!itsInterrupt && itsCurrentMove.is_valid()) { 00112 00113 00114 // // ////////// once function calls /////// 00115 // // ////////// world/sensor moves ///////// 00116 // // i = 0; 00117 // // while(!itsInterrupt && i < itsCurrentMove->W_onceFuncs.size()) { 00118 // // (*this.*(itsCurrentMove->W_onceFuncs[i]))(itsCurrentMove->W_onceParams[i]); 00119 // // i++; 00120 // // } 00121 00122 // // ////////// vision moves /////////// 00123 // // i = 0; 00124 // // while(!itsInterrupt && i < itsCurrentMove->Vis_onceFuncs.size()) { 00125 // // (*this.*(itsCurrentMove->Vis_onceFuncs[i]))(itsCurrentMove->Vis_onceParams[i]); 00126 // // i++; 00127 // // } 00128 00129 00130 // // /////////// repeat function calls //////////// 00131 // // /////////// world/sensor moves //////////// 00132 // // i = 0; 00133 // // while(!itsInterrupt && i < itsCurrentMove->W_reFuncs.size()) { 00134 // // (*this.*(itsCurrentMove->W_reFuncs[i]))(itsCurrentMove->W_reParams[i]); 00135 // // i++; 00136 // // } 00137 00138 // // //////////// vision moves ////////// 00139 // // i = 0; 00140 // // while(!itsInterrupt && i < itsCurrentMove->Vis_reFuncs.size()) { 00141 // // (*this.*(itsCurrentMove->Vis_reFuncs[i]))(itsCurrentMove->Vis_reParams[i]); 00142 // // i++; 00143 // // } 00144 00145 // // if(itsCurrentMove->W_onceFuncs.size() > 0 || itsCurrentMove->Vis_onceFuncs.size() > 0) { 00146 // // isMoveDone = true; 00147 // // //////// clear the once function lists ////// 00148 // // itsCurrentMove->W_onceFuncs.clear(); 00149 // // itsCurrentMove->W_onceParams.clear(); 00150 // // itsCurrentMove->Vis_onceFuncs.clear(); 00151 // // itsCurrentMove->Vis_onceParams.clear(); 00152 // // } 00153 00154 // // if(itsCurrentMove->Vis_reFuncs.size() == 0 && 00155 // // itsCurrentMove->W_reFuncs.size() == 0 && 00156 // // itsCurrentMove->Vis_onceFuncs.size() == 0 && 00157 // // itsCurrentMove->W_onceFuncs.size() == 0) 00158 // // break; 00159 00160 // // } 00161 00162 // // itsInterrupt = true; 00163 00164 // } 00165 00166 00167 00168 00169 // // ##### world/sensor moves ##### 00170 00171 // // ###################################################################### 00172 // void PreMotorComplex::turn(const SensorInput goal) { 00173 00174 // if(goal.angle.getVal() != INVALID) { 00175 // LINFO("turn to Aboslute Angle: %f", goal.angle.getVal()); 00176 // LINFO("current Heading: %d\n", itsPrimitiveMotor->getHeading()); 00177 // itsPrimitiveMotor->setHeading((int)goal.angle.getVal()); 00178 // while(itsPrimitiveMotor->getHeadingErr() > 5) 00179 // usleep(100000); 00180 // } 00181 00182 // } 00183 00184 00185 // // ###################################################################### 00186 // void PreMotorComplex::forward(const SensorInput goal) { 00187 00188 // if(INVALID != goal.data) { 00189 // LINFO("move forward/reverse to dist(m): %f", goal.data); 00190 // // int speed; 00191 // int direction = goal.data > 0 ? 1 : -1; 00192 // itsPrimitiveMotor->setSpeed(direction * 100); 00193 // int time = abs((int)goal.data) * VELOCITY_CONST; 00194 // sleep(time); 00195 // itsPrimitiveMotor->setSpeed(-1* direction * 100); 00196 // sleep(1); 00197 // itsPrimitiveMotor->setSpeed(0); 00198 // //printf("get current heading: %d\n", itsPrimitiveMotor->getHeading()); 00199 // } 00200 00201 00202 // } 00203 00204 00205 // // ###################################################################### 00206 // void PreMotorComplex::dive(const SensorInput goal) { 00207 00208 // if(INVALID != goal.data) { 00209 // int deptherr = (int)goal.data;// * DEPTH_ERROR_CONST; 00210 00211 00212 // LINFO("dive/surface to depth: %i", surfaceDepth); 00213 // int newDepth = surfaceDepth + deptherr; 00214 // LINFO("dive/surface to depth(Liors): %i", newDepth); 00215 // itsPrimitiveMotor->setDepth(newDepth); 00216 // // LINFO("Err %i", itsPrimitiveMotor->getDepthErr()); 00217 // sleep(1); 00218 // // while(itsPrimitiveMotor->getDepthErr() > 3) 00219 // // { 00220 // // usleep(100000); 00221 // // LINFO("curent depth: %d", itsPrimitiveMotor->getDepth()); 00222 // // } 00223 // } 00224 00225 00226 // } 00227 00228 00229 // void PreMotorComplex::wait(const SensorInput goal) { 00230 00231 // if(INVALID != goal.data) { 00232 // LINFO("waiting for %f secs", goal.data); 00233 // sleep((int)goal.data); 00234 // } 00235 // } 00236 00237 // void PreMotorComplex::startWait(const SensorInput goal) { 00238 00239 // if(INVALID != goal.data) { 00240 // LINFO("waiting for %f secs", goal.data); 00241 // sleep((int)goal.data); 00242 // } 00243 // itsPrimitiveMotor->setMotorsOn(true); 00244 // surfaceDepth = itsPrimitiveMotor->getDepth(); 00245 // } 00246 00247 00248 // // ##### vision moves ##### 00249 00250 // // ###################################################################### 00251 // void PreMotorComplex::vis_turn(const VisionInput& goal) { 00252 00253 // if(goal.position.is_valid() && goal.position->x != INVALID) { 00254 // int xerr = goal.position->x - 180; // HARDCODED CENTER!!!!!! 00255 // int desiredHeading = xerr * XAXIS_ERROR_CONST; 00256 00257 // itsPrimitiveMotor->setHeading(itsPrimitiveMotor->getHeading() 00258 // + desiredHeading); 00259 00260 // printf("turn to pixel coordinate: %d, %d, %d\n", 00261 // goal.position->x, goal.position->y, goal.position->z); 00262 00263 // while(itsPrimitiveMotor->getHeadingErr() > 5) 00264 // usleep(100000); 00265 // } 00266 // else if(goal.angle.is_valid()) { 00267 // int angerr = (90 - (int)goal.angle->getVal()); 00268 // int desiredHeading = angerr * ANGLE_ERROR_CONST; 00269 // itsPrimitiveMotor->setHeading(itsPrimitiveMotor->getHeading() 00270 // + desiredHeading); 00271 // printf("turn to camera angle: %f\n", goal.angle->getVal()); 00272 00273 // while(itsPrimitiveMotor->getHeadingErr() > 5) 00274 // usleep(100000); 00275 // } 00276 00277 00278 // } 00279 00280 // // ###################################################################### 00281 // void PreMotorComplex::vis_forward(const VisionInput& goal) { 00282 // if(goal.position.is_valid() && goal.position->y != INVALID) { 00283 // int yerr = 120 - goal.position->y; /// HARDCODED CENTER!!!!! 00284 // int desiredSpeed = yerr * YAXIS_ERROR_CONST; 00285 00286 // itsPrimitiveMotor->setSpeed(desiredSpeed); 00287 // int time = 100000 * YAXIS_ERROR_CONST; 00288 // usleep(time); 00289 // itsPrimitiveMotor->setSpeed(100); 00290 // //itsPrimitiveMotor->setSpeed(-1 * desiredSpeed); 00291 // usleep(50000); 00292 // itsPrimitiveMotor->setSpeed(0); 00293 00294 00295 // printf("move forward to pixel coordinate: %d, %d, %d\n", 00296 // goal.position->x, goal.position->y, goal.position->z); 00297 // } 00298 // } 00299 00300 00301 // // ###################################################################### 00302 // void PreMotorComplex::vis_dive(const VisionInput& goal) { 00303 // if(goal.position.is_valid() && goal.position->z != INVALID) { 00304 // int zerr = goal.position->z - 120; ///// HARDCODED CENTER!!!! 00305 // int desiredDepth = zerr * ZAXIS_ERROR_CONST; 00306 00307 // itsPrimitiveMotor->setDepth(itsPrimitiveMotor->getDepth() 00308 // + desiredDepth); 00309 00310 00311 // printf("dive to pixel coordinate: %d, %d, %d\n", 00312 // goal.position->x, goal.position->y, goal.position->z); 00313 // // while(itsPrimitiveMotor->getDepthErr() > 5) 00314 // // usleep(100000); 00315 // } 00316 // } 00317 00318 // // ###################################################################### 00319 // void PreMotorComplex::vis_center(const VisionInput& goal) { 00320 // if(goal.position.is_valid()) { 00321 // LINFO("center on image coordinate: %d, %d, %d\n", 00322 // goal.position->x, goal.position->y, goal.position->z); 00323 00324 // vis_turn(goal); 00325 // if(goal.position->z != INVALID) 00326 // vis_dive(goal); 00327 // else 00328 // vis_forward(goal); 00329 // } 00330 // } 00331 00332 00333 // // ###################################################################### 00334 // void PreMotorComplex::vis_lost(const VisionInput& goal) { 00335 // if(goal.position.is_valid()) { 00336 00337 // LINFO("retract: find lost object, was last seen: %d, %d, %d\n", 00338 // goal.position->x, goal.position->y, goal.position->z); 00339 00340 // //vis_turn(-1*goal); 00341 // //if(goal.position->z != INVALID) 00342 // // vis_dive(-1*goal); 00343 // //else 00344 // // vis_forward(-1*goal); 00345 // } 00346 // } 00347 00348 #endif