MovingHead7.cc

Go to the documentation of this file.
00001 //
00002 // Copyright 2003 Sony Corporation 
00003 //
00004 // Permission to use, copy, modify, and redistribute this software for
00005 // non-commercial use is hereby granted.
00006 //
00007 // This software is provided "as is" without warranty of any kind,
00008 // either expressed or implied, including but not limited to the
00009 // implied warranties of fitness for a particular purpose.
00010 //
00011 
00012 #include <math.h>
00013 #include <OPENR/OPENRAPI.h>
00014 #include <OPENR/OUnits.h>
00015 #include <OPENR/OSyslog.h>
00016 #include <OPENR/core_macro.h>
00017 #include "MovingHead7.h"
00018 
00019 MovingHead7::MovingHead7() : movingHeadState(MHS_IDLE)
00020 {
00021     for (int i = 0; i < NUM_JOINTS; i++) jointID[i] = oprimitiveID_UNDEF;
00022     for (int i = 0; i < NUM_COMMAND_VECTOR; i++) region[i] = 0;
00023 }
00024 
00025 OStatus
00026 MovingHead7::DoInit(const OSystemEvent& event)
00027 {
00028     OSYSDEBUG(("MovingHead7::DoInit()\n"));
00029 
00030     NEW_ALL_SUBJECT_AND_OBSERVER;
00031     REGISTER_ALL_ENTRY;
00032     SET_ALL_READY_AND_NOTIFY_ENTRY;
00033 
00034     OpenPrimitives();
00035     NewCommandVectorData();
00036 
00037     // 
00038     // OPENR::SetMotorPower(opowerON) is executed in blinkingLED.
00039     // So, it isn't necessary here.
00040     //
00041     
00042     return oSUCCESS;
00043 }
00044 
00045 OStatus
00046 MovingHead7::DoStart(const OSystemEvent& event)
00047 {
00048     OSYSDEBUG(("MovingHead7::DoStart()\n"));
00049 
00050     if (subject[sbjMove]->IsReady() == true) {
00051         AdjustDiffJointValue();
00052         movingHeadState = MHS_ADJUSTING_DIFF_JOINT_VALUE;
00053     } else {
00054         movingHeadState = MHS_START;
00055     }
00056 
00057     ENABLE_ALL_SUBJECT;
00058     ASSERT_READY_TO_ALL_OBSERVER;
00059 
00060     return oSUCCESS;
00061 }
00062 
00063 OStatus
00064 MovingHead7::DoStop(const OSystemEvent& event)
00065 {
00066     OSYSDEBUG(("MovingHead7::DoStop()\n"));
00067 
00068     movingHeadState = MHS_IDLE;
00069 
00070     DISABLE_ALL_SUBJECT;
00071     DEASSERT_READY_TO_ALL_OBSERVER;
00072 
00073     return oSUCCESS;
00074 }
00075 
00076 OStatus
00077 MovingHead7::DoDestroy(const OSystemEvent& event)
00078 {
00079     DELETE_ALL_SUBJECT_AND_OBSERVER;
00080     return oSUCCESS;
00081 }
00082 
00083 void
00084 MovingHead7::Ready(const OReadyEvent& event)
00085 {
00086     OSYSDEBUG(("MovingHead7::Ready()\n"));
00087 
00088     if (movingHeadState == MHS_IDLE) {
00089 
00090         OSYSDEBUG(("MHS_IDLE\n"));
00091         ; // do nothing
00092 
00093     } else if (movingHeadState == MHS_START) {
00094 
00095         OSYSDEBUG(("MHS_START\n"));
00096         AdjustDiffJointValue();
00097         movingHeadState = MHS_ADJUSTING_DIFF_JOINT_VALUE;
00098 
00099     } else if (movingHeadState == MHS_ADJUSTING_DIFF_JOINT_VALUE) {
00100 
00101         OSYSDEBUG(("MHS_ADJUSTING_DIFF_JOINT_VALUE\n"));
00102         SetJointGain();
00103         MovingResult r = MoveToZeroPos();
00104         movingHeadState = MHS_MOVING_TO_ZERO_POS;
00105 
00106     } else if (movingHeadState == MHS_MOVING_TO_ZERO_POS) {
00107 
00108         OSYSDEBUG(("MHS_MOVING_TO_ZERO_POS\n"));
00109         MovingResult r = MoveToZeroPos();
00110         if (r == MOVING_FINISH) {
00111             movingHeadState = MHS_SWING_HEAD;
00112         }
00113 
00114     } else { // movingHeadState == MHS_SWING_HEAD
00115 
00116         OSYSDEBUG(("MHS_SWING_HEAD\n"));
00117         MovingResult r = SwingHead();
00118         if (r == MOVING_FINISH) {
00119             movingHeadState = MHS_IDLE;
00120         }
00121     }
00122 }
00123 
00124 void
00125 MovingHead7::OpenPrimitives()
00126 {
00127     for (int i = 0; i < NUM_JOINTS; i++) {
00128         OStatus result = OPENR::OpenPrimitive(JOINT_LOCATOR[i], &jointID[i]);
00129         if (result != oSUCCESS) {
00130             OSYSLOG1((osyslogERROR, "%s : %s %d",
00131                       "MovingHead7::OpenPrimitives()",
00132                       "OPENR::OpenPrimitive() FAILED", result));
00133         }
00134     }
00135 }
00136 
00137 void
00138 MovingHead7::NewCommandVectorData()
00139 {
00140     OStatus result;
00141     MemoryRegionID      cmdVecDataID;
00142     OCommandVectorData* cmdVecData;
00143     OCommandInfo*       info;
00144 
00145     for (int i = 0; i < NUM_COMMAND_VECTOR; i++) {
00146 
00147         result = OPENR::NewCommandVectorData(NUM_JOINTS, 
00148                                              &cmdVecDataID, &cmdVecData);
00149         if (result != oSUCCESS) {
00150             OSYSLOG1((osyslogERROR, "%s : %s %d",
00151                       "MovingHead7::NewCommandVectorData()",
00152                       "OPENR::NewCommandVectorData() FAILED", result));
00153         }
00154 
00155         region[i] = new RCRegion(cmdVecData->vectorInfo.memRegionID,
00156                                  cmdVecData->vectorInfo.offset,
00157                                  (void*)cmdVecData,
00158                                  cmdVecData->vectorInfo.totalSize);
00159 
00160         cmdVecData->SetNumData(NUM_JOINTS);
00161 
00162         for (int j = 0; j < NUM_JOINTS; j++) {
00163             info = cmdVecData->GetInfo(j);
00164             info->Set(odataJOINT_COMMAND2, jointID[j], ocommandMAX_FRAMES);
00165         }
00166     }
00167 }
00168 
00169 void
00170 MovingHead7::SetJointGain()
00171 {
00172     OPENR::EnableJointGain(jointID[TILT1_INDEX]);
00173     OPENR::SetJointGain(jointID[TILT1_INDEX],
00174                         TILT1_PGAIN,
00175                         TILT1_IGAIN,
00176                         TILT1_DGAIN,
00177                         PSHIFT, ISHIFT, DSHIFT);
00178     
00179     OPENR::EnableJointGain(jointID[PAN_INDEX]);
00180     OPENR::SetJointGain(jointID[PAN_INDEX],
00181                         PAN_PGAIN,
00182                         PAN_IGAIN,
00183                         PAN_DGAIN,
00184                         PSHIFT, ISHIFT, DSHIFT);
00185 
00186     OPENR::EnableJointGain(jointID[TILT2_INDEX]);
00187     OPENR::SetJointGain(jointID[TILT2_INDEX],
00188                         TILT2_PGAIN,
00189                         TILT2_IGAIN,
00190                         TILT2_DGAIN,
00191                         PSHIFT, ISHIFT, DSHIFT);
00192 }
00193 
00194 MovingResult
00195 MovingHead7::AdjustDiffJointValue()
00196 {
00197     OJointValue current[NUM_JOINTS];
00198     for (int i = 0; i < NUM_JOINTS; i++) {
00199         OPENR::GetJointValue(jointID[i], &current[i]);
00200         SetJointValue(region[0], i,
00201                       degrees(current[i].value/1000000.0),
00202                       degrees(current[i].value/1000000.0));
00203     }
00204 
00205     subject[sbjMove]->SetData(region[0]);
00206     subject[sbjMove]->NotifyObservers();
00207 
00208     return MOVING_FINISH;
00209 }
00210 
00211 MovingResult
00212 MovingHead7::MoveToZeroPos()
00213 {
00214     static int counter = -1;
00215     static double s_tilt1, d_tilt1;
00216     static double s_pan,  d_pan;
00217     static double s_tilt2, d_tilt2;
00218 
00219     if (counter == -1) {
00220 
00221         OJointValue current;
00222 
00223         OPENR::GetJointValue(jointID[TILT1_INDEX], &current);
00224         s_tilt1 = degrees(current.value/1000000.0);
00225         d_tilt1 = (TILT1_ZERO_POS - s_tilt1) / (double)ZERO_POS_MAX_COUNTER;
00226 
00227         OPENR::GetJointValue(jointID[PAN_INDEX], &current);
00228         s_pan = degrees(current.value/1000000.0);
00229         d_pan = (PAN_ZERO_POS - s_pan) / (double)ZERO_POS_MAX_COUNTER;
00230 
00231         OPENR::GetJointValue(jointID[TILT2_INDEX], &current);
00232         s_tilt2 = degrees(current.value/1000000.0);
00233         d_tilt2 = (TILT2_ZERO_POS - s_tilt2) / (double)ZERO_POS_MAX_COUNTER;
00234 
00235         counter = 0;
00236 
00237         RCRegion* rgn = FindFreeRegion();
00238         OSYSDEBUG(("FindFreeRegion() %x \n", rgn));
00239         SetJointValue(rgn, TILT1_INDEX, s_tilt1, s_tilt1 + d_tilt1);
00240         SetJointValue(rgn, PAN_INDEX,   s_pan,   s_pan   + d_pan);
00241         SetJointValue(rgn, TILT2_INDEX, s_tilt2, s_tilt2 + d_tilt2);
00242         subject[sbjMove]->SetData(rgn);
00243 
00244         s_tilt1 += d_tilt1;
00245         s_pan   += d_pan;
00246         s_tilt2 += d_tilt2;
00247 
00248         counter++;
00249     } 
00250 
00251     RCRegion* rgn = FindFreeRegion();
00252     OSYSDEBUG(("FindFreeRegion()%x \n", rgn));
00253     SetJointValue(rgn, TILT1_INDEX, s_tilt1, s_tilt1 + d_tilt1);
00254     SetJointValue(rgn, PAN_INDEX,   s_pan,   s_pan   + d_pan);
00255     SetJointValue(rgn, TILT2_INDEX, s_tilt2, s_tilt2 + d_tilt2);
00256     subject[sbjMove]->SetData(rgn);
00257     subject[sbjMove]->NotifyObservers();
00258 
00259     s_tilt1 += d_tilt1;
00260     s_pan   += d_pan;
00261     s_tilt2 += d_tilt2;
00262 
00263     counter++;
00264     return (counter == ZERO_POS_MAX_COUNTER) ? MOVING_FINISH : MOVING_CONT;
00265 }
00266 
00267 MovingResult
00268 MovingHead7::SwingHead()
00269 {
00270     static int phase = 0; // [0:127]
00271 
00272     OSYSDEBUG(("MovingHead7::SwingHead() phase %d\n", phase));
00273 
00274     RCRegion* rgn = FindFreeRegion();
00275     if (rgn == 0) {
00276         OSYSLOG1((osyslogERROR, "%s : %s",
00277                   "MovingHead7::SwingHead()", "FindFreeRegion() FAILED"));
00278         return MOVING_FINISH;
00279     }
00280 
00281     SetJointValue(rgn, TILT1_INDEX, 0.0, 0.0);
00282     SetJointValue(rgn, PAN_INDEX,   phase);
00283     SetJointValue(rgn, TILT2_INDEX, 0.0, 0.0);
00284     subject[sbjMove]->SetData(rgn);
00285     subject[sbjMove]->NotifyObservers();
00286 
00287     phase = (phase + 1) % MAX_PHASE;
00288     return MOVING_CONT;
00289 }
00290 
00291 RCRegion*
00292 MovingHead7::FindFreeRegion()
00293 {
00294     for (int i = 0; i < NUM_COMMAND_VECTOR; i++) {
00295         if (region[i]->NumberOfReference() == 1) return region[i];
00296     }
00297 
00298     return 0;
00299 }
00300 
00301 void
00302 MovingHead7::SetJointValue(RCRegion* rgn, int idx, double start, double end)
00303 {
00304     OCommandVectorData* cmdVecData = (OCommandVectorData*)rgn->Base();
00305 
00306     OCommandInfo* info = cmdVecData->GetInfo(idx);
00307     info->Set(odataJOINT_COMMAND2, jointID[idx], ocommandMAX_FRAMES);
00308 
00309     OCommandData* data = cmdVecData->GetData(idx);
00310     OJointCommandValue2* jval = (OJointCommandValue2*)data->value;
00311 
00312     double delta = end - start;
00313     for (int i = 0; i < ocommandMAX_FRAMES; i++) {
00314         double dval = start + (delta * i) / (double)ocommandMAX_FRAMES;
00315         jval[i].value = oradians(dval);
00316     }
00317 }
00318 
00319 void
00320 MovingHead7::SetJointValue(RCRegion* rgn, int idx, int phase)
00321 {
00322     OCommandVectorData* cmdVecData = (OCommandVectorData*)rgn->Base();
00323 
00324     OCommandInfo* info = cmdVecData->GetInfo(idx);
00325     info->Set(odataJOINT_COMMAND2, jointID[idx], ocommandMAX_FRAMES);
00326 
00327     OCommandData* data = cmdVecData->GetData(idx);
00328     OJointCommandValue2* jval = (OJointCommandValue2*)data->value;
00329 
00330     double d1 = 2.0 * M_PI / (double)MAX_PHASE;
00331     double d2 = d1 / (double)ocommandMAX_FRAMES;
00332     for (int i = 0; i < ocommandMAX_FRAMES; i++) {
00333         double dval = SWING_AMPLITUDE * sin(d1 * phase + d2 * i);
00334         jval[i].value = oradians(dval);
00335     }
00336 }

Generated on Sun Dec 2 23:04:29 2007 for openSDK by  doxygen 1.3.9.1