![](/media/cache/profiles/68a15b5278e4f7c4c056df9d5f1d3b1f.jpg.50x50_q85.jpg)
save loops
soundSpot.cpp@1:3be7b7d050f4, 2014-12-02 (annotated)
- Committer:
- mbedalvaro
- Date:
- Tue Dec 02 08:29:59 2014 +0000
- Revision:
- 1:3be7b7d050f4
- Parent:
- 0:df6fdd9b99f0
updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedalvaro | 0:df6fdd9b99f0 | 1 | #include "soundSpot.h" |
mbedalvaro | 0:df6fdd9b99f0 | 2 | |
mbedalvaro | 0:df6fdd9b99f0 | 3 | // SHOULD NOT BE HERE: (only because I am using serial princ pc object) |
mbedalvaro | 0:df6fdd9b99f0 | 4 | #include "hardwareIO.h" |
mbedalvaro | 0:df6fdd9b99f0 | 5 | |
mbedalvaro | 0:df6fdd9b99f0 | 6 | // Constructor: |
mbedalvaro | 0:df6fdd9b99f0 | 7 | soundSpot::soundSpot() { // by default, the child constructor call the parameterless default constructor (we could force another by doing: soundSpot::soundSpot : LivingSpot (params...) { ..} |
mbedalvaro | 0:df6fdd9b99f0 | 8 | |
mbedalvaro | 0:df6fdd9b99f0 | 9 | // DEFAULT sending mode will be all off: |
mbedalvaro | 0:df6fdd9b99f0 | 10 | stopAllSending(); |
mbedalvaro | 0:df6fdd9b99f0 | 11 | resetAllSendingModes(); |
mbedalvaro | 0:df6fdd9b99f0 | 12 | |
mbedalvaro | 0:df6fdd9b99f0 | 13 | initCommonVariables(); |
mbedalvaro | 0:df6fdd9b99f0 | 14 | |
mbedalvaro | 0:df6fdd9b99f0 | 15 | // initialize timer for sending OSC data: |
mbedalvaro | 0:df6fdd9b99f0 | 16 | periodSendingData=25;// by default, we send the data every 25 ms. Note: the "data" to send will of course depend on the kind of blob. That will be therefore re-set when |
mbedalvaro | 0:df6fdd9b99f0 | 17 | // instantiating the kind of blob. |
mbedalvaro | 0:df6fdd9b99f0 | 18 | measureSendPeriod.start(); |
mbedalvaro | 0:df6fdd9b99f0 | 19 | } |
mbedalvaro | 0:df6fdd9b99f0 | 20 | |
mbedalvaro | 0:df6fdd9b99f0 | 21 | // IMPORTANT: the destructor of the base class is virtual, but it won't be implemented with the same name in the child class; therefore, it |
mbedalvaro | 0:df6fdd9b99f0 | 22 | // must be implemented in the base class (and it will be called when using delete of the child, first the delete child, then delete base) |
mbedalvaro | 0:df6fdd9b99f0 | 23 | soundSpot::~soundSpot() { |
mbedalvaro | 0:df6fdd9b99f0 | 24 | } |
mbedalvaro | 0:df6fdd9b99f0 | 25 | |
mbedalvaro | 0:df6fdd9b99f0 | 26 | void soundSpot::printParameters() { |
mbedalvaro | 0:df6fdd9b99f0 | 27 | // first show common parameters, then call the virtual method: |
mbedalvaro | 0:df6fdd9b99f0 | 28 | pc.printf("Mirror delay :%d\n", displaySensingBuffer.delayMirrorSamples); |
mbedalvaro | 0:df6fdd9b99f0 | 29 | pc.printf("Angle correction force :%f\n", angleCorrectionForceLoop); |
mbedalvaro | 0:df6fdd9b99f0 | 30 | pc.printf("Thresholding mode :%d\n", displaySensingBuffer.modeThreshold); |
mbedalvaro | 0:df6fdd9b99f0 | 31 | pc.printf("Min Contrast Ratio :%f / Current Contrast: %f\n", displaySensingBuffer.min_contrast_ratio, 1.0*displaySensingBuffer.maxI/displaySensingBuffer.minI); |
mbedalvaro | 0:df6fdd9b99f0 | 32 | pc.printf("Threshold Factor: %f\n", displaySensingBuffer.threshold_factor); |
mbedalvaro | 0:df6fdd9b99f0 | 33 | pc.printf("Min Acceptable Intensity :%f\n", displaySensingBuffer.min_acceptable_intensity); |
mbedalvaro | 0:df6fdd9b99f0 | 34 | pc.printf("Current Max / Min Intensity: %d / %d\n", displaySensingBuffer.maxI, displaySensingBuffer.minI, displaySensingBuffer.maxI/displaySensingBuffer.minI); |
mbedalvaro | 0:df6fdd9b99f0 | 35 | this->showChildParameters(); |
mbedalvaro | 0:df6fdd9b99f0 | 36 | } |
mbedalvaro | 0:df6fdd9b99f0 | 37 | |
mbedalvaro | 0:df6fdd9b99f0 | 38 | void soundSpot::setColor(unsigned char c) { |
mbedalvaro | 0:df6fdd9b99f0 | 39 | blobColor=0x07&c; // we will use the first three bits to set the RGB colors. |
mbedalvaro | 0:df6fdd9b99f0 | 40 | } |
mbedalvaro | 0:df6fdd9b99f0 | 41 | |
mbedalvaro | 0:df6fdd9b99f0 | 42 | void soundSpot::setGreenColor(unsigned char c) { |
mbedalvaro | 0:df6fdd9b99f0 | 43 | // this set/reset the green bit only: |
mbedalvaro | 0:df6fdd9b99f0 | 44 | if (c>0) blobColor=blobColor|0x2; //meaning second bit to 1 |
mbedalvaro | 0:df6fdd9b99f0 | 45 | else blobColor=blobColor&0x5; // meaning second bit to 0 |
mbedalvaro | 0:df6fdd9b99f0 | 46 | } |
mbedalvaro | 0:df6fdd9b99f0 | 47 | |
mbedalvaro | 0:df6fdd9b99f0 | 48 | void soundSpot::setBlueColor(unsigned char c) { |
mbedalvaro | 0:df6fdd9b99f0 | 49 | // this set/reset the green bit only: |
mbedalvaro | 0:df6fdd9b99f0 | 50 | if (c>0) blobColor=blobColor|0x1; //meaning first bit to 1 |
mbedalvaro | 0:df6fdd9b99f0 | 51 | else blobColor=blobColor&0x6; // meaning first bit to 0 |
mbedalvaro | 0:df6fdd9b99f0 | 52 | } |
mbedalvaro | 0:df6fdd9b99f0 | 53 | |
mbedalvaro | 0:df6fdd9b99f0 | 54 | void soundSpot::addAngleCorrection(float _moreAngleCorrection) { |
mbedalvaro | 0:df6fdd9b99f0 | 55 | angleCorrectionForceLoop+=_moreAngleCorrection; |
mbedalvaro | 0:df6fdd9b99f0 | 56 | } |
mbedalvaro | 0:df6fdd9b99f0 | 57 | void soundSpot::setAngleCorrection(float _angleCorrection) { |
mbedalvaro | 0:df6fdd9b99f0 | 58 | angleCorrectionForceLoop=_angleCorrection; |
mbedalvaro | 0:df6fdd9b99f0 | 59 | } |
mbedalvaro | 0:df6fdd9b99f0 | 60 | |
mbedalvaro | 0:df6fdd9b99f0 | 61 | void soundSpot::initCommonVariables() { |
mbedalvaro | 0:df6fdd9b99f0 | 62 | firstTimeNoTouch=true; |
mbedalvaro | 0:df6fdd9b99f0 | 63 | // randomForce.set(1,0);// first time there won't be any force, or random force |
mbedalvaro | 0:df6fdd9b99f0 | 64 | // randomForce=randomForce.getRotatedDeg(1.0*(rand()%360)); |
mbedalvaro | 0:df6fdd9b99f0 | 65 | // randomForce.normalize(); |
mbedalvaro | 0:df6fdd9b99f0 | 66 | |
mbedalvaro | 0:df6fdd9b99f0 | 67 | noTouchedCounter=0; |
mbedalvaro | 0:df6fdd9b99f0 | 68 | wallCounter=0; |
mbedalvaro | 0:df6fdd9b99f0 | 69 | blobWallCollision=false; |
mbedalvaro | 0:df6fdd9b99f0 | 70 | //slidingDirection=true; // (will change when touching wall) |
mbedalvaro | 0:df6fdd9b99f0 | 71 | |
mbedalvaro | 0:df6fdd9b99f0 | 72 | gravity.set(0,0); |
mbedalvaro | 0:df6fdd9b99f0 | 73 | |
mbedalvaro | 0:df6fdd9b99f0 | 74 | render=true; |
mbedalvaro | 0:df6fdd9b99f0 | 75 | standByMode=true; // ALWAYS START IN STANDBY MODE |
mbedalvaro | 0:df6fdd9b99f0 | 76 | } |
mbedalvaro | 0:df6fdd9b99f0 | 77 | |
mbedalvaro | 0:df6fdd9b99f0 | 78 | void soundSpot::resetAllSendingModes() { |
mbedalvaro | 0:df6fdd9b99f0 | 79 | // RESET SENDING DATA: |
mbedalvaro | 0:df6fdd9b99f0 | 80 | sendingOnlyWhenTouch=false; |
mbedalvaro | 0:df6fdd9b99f0 | 81 | // (a) anchor mass data: |
mbedalvaro | 0:df6fdd9b99f0 | 82 | sendingAnchorPosition=false; |
mbedalvaro | 0:df6fdd9b99f0 | 83 | sendingAnchorForce=false; // this is the total force on the anchor mass, not just the recentering force |
mbedalvaro | 0:df6fdd9b99f0 | 84 | sendingAnchorTouchWall=false; |
mbedalvaro | 0:df6fdd9b99f0 | 85 | // (b) data from blob points: |
mbedalvaro | 0:df6fdd9b99f0 | 86 | sendingLoopPositions=false; |
mbedalvaro | 0:df6fdd9b99f0 | 87 | sendingLoopForces=false;// this is not just the forces from light, but all the forces in each particle |
mbedalvaro | 0:df6fdd9b99f0 | 88 | sendingLoopForcesLight=false;// forces only from light |
mbedalvaro | 0:df6fdd9b99f0 | 89 | sendingLoopRegions=false; // from this we can detect "hits" |
mbedalvaro | 0:df6fdd9b99f0 | 90 | sendingLoopTouchWall=false; |
mbedalvaro | 0:df6fdd9b99f0 | 91 | // (c) Blob geometry: |
mbedalvaro | 0:df6fdd9b99f0 | 92 | sendingBlobArea=false; |
mbedalvaro | 0:df6fdd9b99f0 | 93 | sendingBlobNormals=false; |
mbedalvaro | 0:df6fdd9b99f0 | 94 | sendingBlobAngles=false; // redundant with sendingBlobNormals, but simplified (only angle of normal) |
mbedalvaro | 0:df6fdd9b99f0 | 95 | sendingKineticEnergy=false; |
mbedalvaro | 0:df6fdd9b99f0 | 96 | // (d) Light sensing statistics: |
mbedalvaro | 0:df6fdd9b99f0 | 97 | sendingBlobMaxMin=false; |
mbedalvaro | 0:df6fdd9b99f0 | 98 | sendingLightForce=false; // the total light force |
mbedalvaro | 0:df6fdd9b99f0 | 99 | sendingTouched=false; |
mbedalvaro | 0:df6fdd9b99f0 | 100 | // (e) Recentering vector: (note: redundant with sendingLightForce, IF the correction angle is known). |
mbedalvaro | 0:df6fdd9b99f0 | 101 | sendingRecenteringVector=false; |
mbedalvaro | 0:df6fdd9b99f0 | 102 | sendingRecenteringAngle=false; |
mbedalvaro | 0:df6fdd9b99f0 | 103 | sendingRecenteringNorm=false; |
mbedalvaro | 0:df6fdd9b99f0 | 104 | } |
mbedalvaro | 0:df6fdd9b99f0 | 105 | |
mbedalvaro | 0:df6fdd9b99f0 | 106 | void soundSpot::stopAllSending() { |
mbedalvaro | 0:df6fdd9b99f0 | 107 | // STOP HARDWARE SENDING MODE (per spot): |
mbedalvaro | 0:df6fdd9b99f0 | 108 | sendSerial=false; |
mbedalvaro | 0:df6fdd9b99f0 | 109 | sendOSC=true; |
mbedalvaro | 0:df6fdd9b99f0 | 110 | } |
mbedalvaro | 0:df6fdd9b99f0 | 111 | |
mbedalvaro | 0:df6fdd9b99f0 | 112 | void soundSpot::sendData(void) { // send data common to all kind of blobs |
mbedalvaro | 0:df6fdd9b99f0 | 113 | // send common things, such as testing if it is the right time to send data: |
mbedalvaro | 0:df6fdd9b99f0 | 114 | |
mbedalvaro | 0:df6fdd9b99f0 | 115 | if (measureSendPeriod.read_ms()>periodSendingData) { |
mbedalvaro | 0:df6fdd9b99f0 | 116 | measureSendPeriod.stop(); |
mbedalvaro | 0:df6fdd9b99f0 | 117 | measureSendPeriod.reset(); |
mbedalvaro | 0:df6fdd9b99f0 | 118 | |
mbedalvaro | 0:df6fdd9b99f0 | 119 | // Send data specific to the derived class: |
mbedalvaro | 0:df6fdd9b99f0 | 120 | if ((sendingOnlyWhenTouch==false)||(blobWallCollision==true)||(displaySensingBuffer.lightTouched==true)) |
mbedalvaro | 0:df6fdd9b99f0 | 121 | sendDataSpecific(); // this will depend on the kind of blob |
mbedalvaro | 0:df6fdd9b99f0 | 122 | |
mbedalvaro | 0:df6fdd9b99f0 | 123 | measureSendPeriod.start(); |
mbedalvaro | 0:df6fdd9b99f0 | 124 | } |
mbedalvaro | 0:df6fdd9b99f0 | 125 | |
mbedalvaro | 0:df6fdd9b99f0 | 126 | } |
mbedalvaro | 0:df6fdd9b99f0 | 127 |