00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <OPENR/OPENRAPI.h>
00013 #include <OPENR/OSyslog.h>
00014 #include <OPENR/core_macro.h>
00015 #include "BallTrackingHead.h"
00016
00017 BallTrackingHead::BallTrackingHead() : ballTrackingHeadState(BTHS_IDLE),
00018 fbkID(oprimitiveID_UNDEF),
00019 initSensorIndex(false),
00020 sensorRegions(),
00021 lastRefPan(0),
00022 lastRefTilt(0)
00023 {
00024 for (int i = 0; i < NUM_JOINTS; i++) jointID[i] = oprimitiveID_UNDEF;
00025 for (int i = 0; i < NUM_COMMAND_VECTOR; i++) region[i] = 0;
00026 for (int i = 0; i < NUM_JOINTS; i++) sensoridx[i] = -1;
00027 }
00028
00029 OStatus
00030 BallTrackingHead::DoInit(const OSystemEvent& event)
00031 {
00032 OSYSDEBUG(("BallTrackingHead::DoInit()\n"));
00033
00034 NEW_ALL_SUBJECT_AND_OBSERVER;
00035 REGISTER_ALL_ENTRY;
00036 SET_ALL_READY_AND_NOTIFY_ENTRY;
00037
00038 OpenPrimitives();
00039 NewCommandVectorData();
00040 SetCdtVectorDataOfPinkBall();
00041
00042 return oSUCCESS;
00043 }
00044
00045 OStatus
00046 BallTrackingHead::DoStart(const OSystemEvent& event)
00047 {
00048 OSYSDEBUG(("BallTrackingHead::DoStart()\n"));
00049
00050 BallTrackingHeadCommand headcmd(BTHCMD_MOVE_TO_ZERO_POS);
00051 subject[sbjMovingHead]->SetData(&headcmd, sizeof(headcmd));
00052 subject[sbjMovingHead]->NotifyObservers();
00053
00054 BallTrackingHeadCommand legscmd(BTHCMD_MOVE_TO_SLEEPING);
00055 subject[sbjMovingLegs]->SetData(&legscmd, sizeof(legscmd));
00056 subject[sbjMovingLegs]->NotifyObservers();
00057
00058 ballTrackingHeadState = BTHS_START;
00059
00060 ENABLE_ALL_SUBJECT;
00061 ASSERT_READY_TO_ALL_OBSERVER;
00062
00063 return oSUCCESS;
00064 }
00065
00066 OStatus
00067 BallTrackingHead::DoStop(const OSystemEvent& event)
00068 {
00069 OSYSDEBUG(("BallTrackingHead::DoStop()\n"));
00070
00071 ballTrackingHeadState = BTHS_IDLE;
00072
00073 DISABLE_ALL_SUBJECT;
00074 DEASSERT_READY_TO_ALL_OBSERVER;
00075
00076 return oSUCCESS;
00077 }
00078
00079 OStatus
00080 BallTrackingHead::DoDestroy(const OSystemEvent& event)
00081 {
00082 DELETE_ALL_SUBJECT_AND_OBSERVER;
00083 return oSUCCESS;
00084 }
00085
00086 void
00087 BallTrackingHead::NotifyMovingHeadResult(const ONotifyEvent& event)
00088 {
00089 OSYSDEBUG(("BallTrackingHead::NotifyMovingHeadResult()\n"));
00090
00091 if (ballTrackingHeadState == BTHS_IDLE) return;
00092
00093 BallTrackingHeadResult* result = (BallTrackingHeadResult*)event.Data(0);
00094 if (result->status == BTH_SUCCESS) {
00095
00096 if (ballTrackingHeadState == BTHS_START) {
00097 ballTrackingHeadState = BTHS_HEAD_ZERO_POS;
00098 } else if (ballTrackingHeadState == BTHS_LEGS_SLEEPING) {
00099 SearchBall();
00100 ballTrackingHeadState = BTHS_SEARCHING_BALL;
00101 }
00102
00103 } else {
00104
00105 OSYSLOG1((osyslogERROR, "%s : %s %d",
00106 "BallTrackingHead::NotifyMovingHeadResult()",
00107 "FAILED. result->status", result->status));
00108
00109 }
00110
00111 observer[event.ObsIndex()]->AssertReady();
00112 }
00113
00114 void
00115 BallTrackingHead::NotifyMovingLegsResult(const ONotifyEvent& event)
00116 {
00117 OSYSDEBUG(("BallTrackingHead::NotifyMovingLegsResult()\n"));
00118
00119 if (ballTrackingHeadState == BTHS_IDLE) return;
00120
00121 BallTrackingHeadResult* result = (BallTrackingHeadResult*)event.Data(0);
00122 if (result->status == BTH_SUCCESS) {
00123
00124 if (ballTrackingHeadState == BTHS_START) {
00125 ballTrackingHeadState = BTHS_LEGS_SLEEPING;
00126 } else if (ballTrackingHeadState == BTHS_HEAD_ZERO_POS) {
00127 SearchBall();
00128 ballTrackingHeadState = BTHS_SEARCHING_BALL;
00129 }
00130
00131 } else {
00132
00133 OSYSLOG1((osyslogERROR, "%s : %s %d",
00134 "BallTrackingHead::NotifyMovingLegsResult()",
00135 "FAILED. result->status", result->status));
00136
00137 }
00138
00139 observer[event.ObsIndex()]->AssertReady();
00140 }
00141
00142 void
00143 BallTrackingHead::NotifyLostFoundSoundResult(const ONotifyEvent& event)
00144 {
00145 OSYSDEBUG(("BallTrackingHead::NotifyLostFoundSoundResult()\n"));
00146
00147 if (ballTrackingHeadState == BTHS_IDLE) return;
00148
00149 BallTrackingHeadResult* result = (BallTrackingHeadResult*)event.Data(0);
00150 if (result->status != BTH_SUCCESS) {
00151 OSYSLOG1((osyslogERROR, "%s : %s %d",
00152 "BallTrackingHead::NotifyLostFoundSoundResult()",
00153 "FAILED. result->status", result->status));
00154 }
00155
00156 observer[event.ObsIndex()]->AssertReady();
00157 }
00158
00159 void
00160 BallTrackingHead::OpenPrimitives()
00161 {
00162 OStatus result;
00163
00164 for (int i = 0; i < NUM_JOINTS; i++) {
00165 result = OPENR::OpenPrimitive(JOINT_LOCATOR[i], &jointID[i]);
00166 if (result != oSUCCESS) {
00167 OSYSLOG1((osyslogERROR, "%s : %s %d",
00168 "BallTrackingHead::OpenPrimitives()",
00169 "OPENR::OpenPrimitive() FAILED", result));
00170 }
00171 }
00172
00173 result = OPENR::OpenPrimitive(FBK_LOCATOR, &fbkID);
00174 if (result != oSUCCESS) {
00175 OSYSLOG1((osyslogERROR, "%s : %s %d",
00176 "BallTrackingHead::OpenPrimitives()",
00177 "OPENR::OpenPrimitive() FAILED", result));
00178 }
00179 }
00180
00181 void
00182 BallTrackingHead::NewCommandVectorData()
00183 {
00184 OStatus result;
00185 MemoryRegionID cmdVecDataID;
00186 OCommandVectorData* cmdVecData;
00187 OCommandInfo* info;
00188
00189 for (int i = 0; i < NUM_COMMAND_VECTOR; i++) {
00190
00191 result = OPENR::NewCommandVectorData(NUM_JOINTS,
00192 &cmdVecDataID, &cmdVecData);
00193 if (result != oSUCCESS) {
00194 OSYSLOG1((osyslogERROR, "%s : %s %d",
00195 "BallTrackingHead::NewCommandVectorData()",
00196 "OPENR::NewCommandVectorData() FAILED", result));
00197 }
00198
00199 region[i] = new RCRegion(cmdVecData->vectorInfo.memRegionID,
00200 cmdVecData->vectorInfo.offset,
00201 (void*)cmdVecData,
00202 cmdVecData->vectorInfo.totalSize);
00203
00204 cmdVecData->SetNumData(NUM_JOINTS);
00205
00206 for (int j = 0; j < NUM_JOINTS; j++) {
00207 info = cmdVecData->GetInfo(j);
00208 info->Set(odataJOINT_COMMAND2, jointID[j], NUM_FRAMES);
00209 }
00210 }
00211 }
00212
00213 void
00214 BallTrackingHead::PlaySound(BallTrackingHeadCommandType type)
00215 {
00216 OSYSDEBUG(("BallTrackingHead::PlaySound()\n"));
00217 BallTrackingHeadCommand cmd(type);
00218 subject[sbjLostFoundSound]->SetData(&cmd, sizeof(cmd));
00219 subject[sbjLostFoundSound]->NotifyObservers();
00220 }