00001
00002
00003
00004
00005
00006
00007 #include "GT2004SensorDataProcessor.h"
00008 #include "Tools/Actorics/RobotDimensions.h"
00009 #include "Tools/Actorics/Kinematics.h"
00010 #include "Tools/Math/Common.h"
00011 #include "Platform/SystemCall.h"
00012
00013 #include "Tools/Debugging/Debugging.h"
00014
00015 GT2004SensorDataProcessor::GT2004SensorDataProcessor
00016 (const SensorDataProcessorInterfaces& interfaces)
00017 : SensorDataProcessor(interfaces),
00018 lastImageFrameNumber(0),
00019 gravity(0,0,0),
00020 accelerationWithGrav(0,0,0),
00021 leftRollStartTime(SystemCall::getCurrentSystemTime()),
00022 tempSysTime(0),
00023 rightRollStartTime(SystemCall::getCurrentSystemTime())
00024 {
00025 sensorDataRingBuffer.init();
00026 cameraMatrixRingBuffer.init();
00027 bodyPostureRingBuffer.init();
00028 for (int i = 0; i<3; i++) {
00029 previousAverage[i] = 0.0;
00030 actAverage[i] = 0.0;
00031 }
00032 }
00033
00034 void GT2004SensorDataProcessor::execute()
00035 {
00036 int i;
00037 CameraMatrix frameCameraMatrix;
00038 BodyPosture frameBodyPosture;
00039
00040 psdPercept.numOfPercepts = 0;
00041
00042 for (i = 0; i < sensorDataBuffer.numOfFrames; i++)
00043 {
00044 sensorDataRingBuffer.add(sensorDataBuffer.frame[i]);
00045
00046 sensorDataRingBuffer.updateAverage(SensorData::accelerationX,10,gravity.x);
00047 sensorDataRingBuffer.updateAverage(SensorData::accelerationY,10,gravity.y);
00048 sensorDataRingBuffer.updateAverage(SensorData::accelerationZ,10,gravity.z);
00049
00050 sensorDataRingBuffer.updateAverage(SensorData::accelerationX,2,accelerationWithGrav.x);
00051 sensorDataRingBuffer.updateAverage(SensorData::accelerationY,2,accelerationWithGrav.y);
00052 sensorDataRingBuffer.updateAverage(SensorData::accelerationZ,2,accelerationWithGrav.z);
00053
00054
00055 frameBodyPosture.frameNumber = sensorDataBuffer.frame[i].frameNumber;
00056 calculateBodyPostureFromLegSensors(sensorDataBuffer.frame[i], frameBodyPosture);
00057 calculateBodyPostureFromAccelerationSensors(sensorDataBuffer.frame[i], frameBodyPosture);
00058
00059 if (1)
00060 {
00061 frameBodyPosture.bodyTiltProvidedByMotionControl = motionInfo.bodyTilt;
00062 frameBodyPosture.neckHeightProvidedByMotionControl = motionInfo.neckHeight;
00063 frameBodyPosture.bodyRollProvidedByMotionControl = 0;
00064 }
00065 else
00066 {
00067
00068 }
00069
00070 buildCameraMatrix(sensorDataBuffer.frame[i], frameBodyPosture, frameCameraMatrix);
00071
00072 cameraMatrixRingBuffer.add(frameCameraMatrix);
00073 bodyPostureRingBuffer.add(frameBodyPosture);
00074
00075 buildPSDPercept(
00076 sensorDataBuffer.frame[i],
00077 cameraMatrixRingBuffer.getEntry(9),
00078 psdPercept[psdPercept.numOfPercepts++]);
00079 }
00080
00081 double bodyRollAngle = atan2((double)-gravity.y,(double)-gravity.z);
00082 if ((bodyRollAngle >= -gt2004SensorDataProcessorRolledOnWallAngle)||(bodyRollAngle <= -gt2004SensorDataProcessorFalldownRollAngle))
00083 leftRollStartTime = SystemCall::getCurrentSystemTime();
00084 if ((bodyRollAngle <= gt2004SensorDataProcessorRolledOnWallAngle)||(bodyRollAngle >= gt2004SensorDataProcessorFalldownRollAngle))
00085 rightRollStartTime = SystemCall::getCurrentSystemTime();
00086
00087 if (lastImageFrameNumber == imageFrameNumber)
00088 {
00089
00090 return;
00091 }
00092 lastImageFrameNumber = imageFrameNumber;
00093
00094
00095 for (i = 0; i < sensorDataRingBuffer.getNumberOfEntries(); i++)
00096 {
00097 if (sensorDataRingBuffer.getEntry(i).frameNumber <= imageFrameNumber) break;
00098 }
00099 cameraMatrix = cameraMatrixRingBuffer.getEntry(i);
00100 bodyPosture = bodyPostureRingBuffer.getEntry(i);
00101 bodyPosture.bodyTiltProvidedByMotionControl = motionInfo.bodyTilt;
00102 bodyPosture.bodyRollProvidedByMotionControl = 0;
00103 bodyPosture.neckHeightProvidedByMotionControl = motionInfo.neckHeight;
00104
00105 Vector3<double> acceleration =
00106 accelerationWithGrav - gravity;
00107 bodyPercept.acceleration = acceleration;
00108
00109 int switches = detectSwitches();
00110
00111 bodyPercept.setFrameNumber(sensorDataBuffer.frame[0].frameNumber);
00112
00113 bodyPercept.setMouthState(detectMouthState());
00114
00115 bodyPercept.setBodyPSDHighValue(sensorDataBuffer.lastFrame().data[SensorData::bodyPsd]>150000);
00116
00117 bodyPercept.setBodyPSDDistance(getDistanceToSIFOC());
00118
00119 bodyPercept.setSwitches(switches);
00120 if (!motionInfo.motionIsStable)
00121 {
00122 bodyPercept.setState(BodyPercept::standing);
00123 }
00124 else if (detectFallDown())
00125 {
00126 bodyPercept.setState(BodyPercept::crashed);
00127
00128 }
00129 else if (SystemCall::getTimeSince(leftRollStartTime)>1500)
00130 {
00131 bodyPercept.setState(BodyPercept::rollLeft);
00132
00133 }
00134 else if (SystemCall::getTimeSince(rightRollStartTime)>1500)
00135 {
00136 bodyPercept.setState(BodyPercept::rollRight);
00137
00138 }
00139 else if (detectPickup(gravity,acceleration))
00140 {
00141 bodyPercept.setState(BodyPercept::pickedUp);
00142 }
00143 else
00144 {
00145 bodyPercept.setState(BodyPercept::standing);
00146 }
00147
00148
00149 }
00150
00151
00152 void GT2004SensorDataProcessor::buildCameraMatrix(const SensorData& sensorData, const BodyPosture& bP, CameraMatrix& cameraMatrix)
00153 {
00154 double tilt = fromMicroRad(sensorData.data[SensorData::neckTilt]);
00155 double pan = fromMicroRad(sensorData.data[SensorData::headPan]);
00156 double tilt2 = fromMicroRad(sensorData.data[SensorData::headTilt]);
00157 Kinematics::calculateCameraMatrix(tilt, pan, tilt2, bP.bodyTiltCalculatedFromLegSensors, bP.neckHeightCalculatedFromLegSensors, cameraMatrix);
00158 cameraMatrix.setFrameNumber(sensorData.frameNumber);
00159
00160 cameraMatrix.isValid=motionInfo.motionIsStable;
00161 }
00162
00163 void GT2004SensorDataProcessor::buildPSDPercept(const SensorData& sensorData, const CameraMatrix& cameraMatrix, SinglePSDPercept& psdPercept)
00164 {
00165
00166
00167
00168
00169
00170 psdPercept.isValid = cameraMatrix.isValid;
00171
00172
00173 Vector3<double> psd;
00174 psd.y = 0; psd.z = 0;
00175
00176 if(sensorData.data[SensorData::psd]/1000 < 300)
00177 psd.x = sensorData.data[SensorData::psd]/1000 - 20;
00178 else
00179 psd.x = sensorData.data[SensorData::headPsdFar]/1000 - 30;
00180
00181 psdPercept.tooFarAway = (psd.x >= 1300 - 30);
00182 psdPercept.body = sensorData.data[SensorData::bodyPsd]/1000;
00183
00184
00185 psdPercept.neckTilt = fromMicroRad(sensorData.data[SensorData::neckTilt]);
00186
00187
00188 Vector3<double> objectRelativeToCamera = cameraMatrix.rotation * psd;
00189
00190
00191
00192
00193
00194 (Vector3<double>&)psdPercept = objectRelativeToCamera + cameraMatrix.translation;
00195
00196
00197
00198 psdPercept.setFrameNumber(sensorData.frameNumber);
00199 }
00200
00201 int GT2004SensorDataProcessor::detectSwitches()
00202 {
00203 int backMiddle, backFront, backBack,
00204 head, mouth, chin;
00205
00206 backMiddle= (int)( (int)(sensorDataBuffer.frame[0].data[SensorData::back]) > 10 ) << BodyPercept::backMiddle;
00207 backFront = (int)( (int)(sensorDataBuffer.frame[0].data[SensorData::backF]) > 10 ) << BodyPercept::backFront;
00208 backBack = (int)( (int)(sensorDataBuffer.frame[0].data[SensorData::backR]) > 10 ) << BodyPercept::backBack;
00209 head = (int)( (int)(sensorDataBuffer.frame[0].data[SensorData::head]) > 10 ) << BodyPercept::head;
00210 mouth = (int)(0 != (int)(sensorDataRingBuffer.getAverage(SensorData::mouth, 5)/-80000)) << BodyPercept::mouth;
00211 chin = (int)(0 != (int)(sensorDataRingBuffer.getAverage(SensorData::chin, 5)*10)) << BodyPercept::chin;
00212
00213 return
00214 backMiddle | backFront | backBack |
00215 head | mouth | chin;
00216 }
00217
00218 bool GT2004SensorDataProcessor::detectFallDown()
00219 {
00220 double bodyTiltAngle = atan2((double)gravity.x,(double)-gravity.z);
00221 double bodyRollAngle = atan2((double)-gravity.y,(double)-gravity.z);
00222
00223 return
00224 (bodyTiltAngle > gt2004SensorDataProcessorFalldownTiltAngle ||
00225 bodyTiltAngle < -gt2004SensorDataProcessorFalldownTiltAngle ||
00226 bodyRollAngle > gt2004SensorDataProcessorFalldownRollAngle ||
00227 bodyRollAngle < -gt2004SensorDataProcessorFalldownRollAngle);
00228 }
00229
00230 bool GT2004SensorDataProcessor::detectPickup(const Vector3<double>& gravity, const Vector3<double>& acceleration)
00231 {
00232 double accZ = acceleration * gravity;
00233
00234 return (accZ < -50000000000000.0);
00235 }
00236
00237 BodyPercept::MouthStates
00238 GT2004SensorDataProcessor::detectMouthState()
00239 {
00240 if(sensorDataBuffer.lastFrame().data[SensorData::mouth] < -370000)
00241 {
00242 return BodyPercept::mouthOpen;
00243 }
00244 else
00245 {
00246 return BodyPercept::mouthClosed;
00247 }
00248 }
00249
00250
00251
00252
00253 double GT2004SensorDataProcessor::SensorDataRingBuffer::getAverage (SensorData::sensors sensor, int ticks)
00254 {
00255 if (ticks>getNumberOfEntries()) ticks=getNumberOfEntries();
00256 if (ticks<1) return 0.0;
00257 double sum=0.0;
00258 for (int i=0;i<ticks;i++)
00259 sum+=getEntry(i).data[sensor];
00260 return sum/(double)ticks;
00261 }
00262
00263 void GT2004SensorDataProcessor::SensorDataRingBuffer::updateAverage(SensorData::sensors sensor, int ticks, double& average)
00264 {
00265 if (ticks<1) return;
00266 if (ticks<getNumberOfEntries())
00267 average-=((double)getEntry(ticks).data[sensor])/(double)ticks;
00268 average+=((double)getEntry(0).data[sensor])/(double)ticks;
00269 }
00270
00271 long GT2004SensorDataProcessor::SensorDataRingBuffer::interpolate (SensorData::sensors sensor, unsigned long imageFrame)
00272 {
00273 long frame = (long) imageFrame,
00274 currentValue = getEntry(0).data[sensor],
00275 currentFrame = (long) getEntry(0).frameNumber;
00276 if(currentFrame == frame)
00277 return currentValue;
00278 else
00279 {
00280 long oldValue = getEntry(1).data[sensor],
00281 oldFrame = (long) getEntry(1).frameNumber;
00282 if(currentFrame == oldFrame)
00283 return currentValue;
00284 else
00285 return currentValue + (oldValue - currentValue) * (frame - currentFrame) / (oldFrame - currentFrame);
00286 }
00287 }
00288
00289 void GT2004SensorDataProcessor::calculateBodyPostureFromLegSensors(const SensorData& sensorData, BodyPosture& bP)
00290 {
00291 RobotVertices robotVertices;
00292 Kinematics::calcNeckAndLegPositions(sensorDataBuffer.lastFrame(), robotVertices);
00293
00294 bP.bodyTiltCalculatedFromLegSensors = robotVertices.bodyTilt;
00295 bP.bodyRollCalculatedFromLegSensors = robotVertices.bodyRoll;
00296 bP.neckHeightCalculatedFromLegSensors = robotVertices.neckHeight;
00297 }
00298
00299 void GT2004SensorDataProcessor::calculateBodyPostureFromAccelerationSensors(const SensorData& sensorData, BodyPosture& bP)
00300 {
00301
00302 long accX = sensorDataBuffer.lastFrame().data[SensorData::accelerationX]/1000;
00303 long accY = sensorDataBuffer.lastFrame().data[SensorData::accelerationY]/1000;
00304 long accZ = sensorDataBuffer.lastFrame().data[SensorData::accelerationZ]/1000;
00305 bP.bodyTiltCalculatedFromAccelerationSensors = atan2((double)accX,(double)-accZ);
00306 bP.bodyRollCalculatedFromAccelerationSensors = atan2((double)-accY,(double)-accZ);
00307 }
00308
00309 double GT2004SensorDataProcessor::getCameraVelocity(const SensorData::sensors joint=SensorData::headPan)
00310 {
00311 const int numOfValues=4;
00312 int jointNum = 1;
00313 sensorDataRingBuffer.updateAverage(SensorData::neckTilt, numOfValues, actAverage[0]);
00314 sensorDataRingBuffer.updateAverage(SensorData::headPan, numOfValues, actAverage[1]);
00315 sensorDataRingBuffer.updateAverage(SensorData::headTilt, numOfValues, actAverage[2]);
00316 switch (joint)
00317 {
00318 case SensorData::headTilt: jointNum = 2;
00319 break;
00320 case SensorData::headPan: jointNum = 1;
00321 break;
00322 case SensorData::neckTilt: jointNum = 0;
00323 break;
00324 }
00325 double cameraSpeed = toDegrees(fromMicroRad((long) (actAverage[jointNum] - previousAverage[jointNum]))) / RobotDimensions::motionCycleTime;
00326 for (int index = 0; index < 3; index++)
00327 previousAverage[index] = actAverage[index];
00328
00329
00330
00331
00332
00333
00334
00335
00336 return cameraSpeed;
00337 }
00338
00339 double GT2004SensorDataProcessor::getDistanceToSIFOC()
00340 {
00341 double value = 50;
00342 double psdval = 0;
00343 for (int i = 0; i < psdPercept.numOfPercepts; ++i)
00344 psdval += psdPercept[i].body;
00345 psdval /= (double)(psdPercept.numOfPercepts);
00346 if (psdval >= 170)
00347 psdval = 170;
00348
00349
00350 if (psdval >= 160)
00351 value = 10-(psdval-160);
00352 else
00353
00354 if (psdval >= 145)
00355 value = ((15-(psdval-145))/1.5)+10;
00356 else
00357
00358 if (psdval >= 130)
00359 value = ((15-(psdval-130))/0.75)+20;
00360
00361 return value;
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
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677