![](/media/cache/profiles/68a15b5278e4f7c4c056df9d5f1d3b1f.jpg.50x50_q85.jpg)
save loops
soundSpot.h@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 | #ifndef SOUNDSPOT_H |
mbedalvaro | 0:df6fdd9b99f0 | 2 | #define SOUNDSPOT_H |
mbedalvaro | 0:df6fdd9b99f0 | 3 | |
mbedalvaro | 0:df6fdd9b99f0 | 4 | //#define SEND_AS_BLOB // for test (sending positions in a single OSC blob chunk) |
mbedalvaro | 0:df6fdd9b99f0 | 5 | //#define SEND_AS_STRING |
mbedalvaro | 0:df6fdd9b99f0 | 6 | #define SEND_AS_POINTS |
mbedalvaro | 0:df6fdd9b99f0 | 7 | |
mbedalvaro | 0:df6fdd9b99f0 | 8 | // The global object OSC for sending/receiving messages: |
mbedalvaro | 0:df6fdd9b99f0 | 9 | #include "mbedOSC.h" |
mbedalvaro | 0:df6fdd9b99f0 | 10 | extern OSCMessage recMes; |
mbedalvaro | 0:df6fdd9b99f0 | 11 | extern OSCMessage sendMes; |
mbedalvaro | 0:df6fdd9b99f0 | 12 | extern OSCClass osc; |
mbedalvaro | 0:df6fdd9b99f0 | 13 | |
mbedalvaro | 0:df6fdd9b99f0 | 14 | extern DigitalOut myled2; // for tests... |
mbedalvaro | 0:df6fdd9b99f0 | 15 | |
mbedalvaro | 0:df6fdd9b99f0 | 16 | #include "classLaserSensingTrajectory.h" |
mbedalvaro | 0:df6fdd9b99f0 | 17 | #include "classRigidScafold.h" |
mbedalvaro | 0:df6fdd9b99f0 | 18 | |
mbedalvaro | 0:df6fdd9b99f0 | 19 | |
mbedalvaro | 0:df6fdd9b99f0 | 20 | #define min(a, b) (((a) < (b)) ? (a) : (b)) |
mbedalvaro | 0:df6fdd9b99f0 | 21 | #define max(a, b) (((a) > (b)) ? (a) : (b)) |
mbedalvaro | 0:df6fdd9b99f0 | 22 | |
mbedalvaro | 0:df6fdd9b99f0 | 23 | // This is a polymorphic class, and elasticLoop, livingSpot are derived classes that |
mbedalvaro | 0:df6fdd9b99f0 | 24 | // instantiate particular methods (update, etc). |
mbedalvaro | 0:df6fdd9b99f0 | 25 | class soundSpot { |
mbedalvaro | 0:df6fdd9b99f0 | 26 | protected: // (note: protected means that the child classes will have access to these variables/methods; private means that only this class members have access) |
mbedalvaro | 0:df6fdd9b99f0 | 27 | |
mbedalvaro | 0:df6fdd9b99f0 | 28 | // number of points of this blob: |
mbedalvaro | 0:df6fdd9b99f0 | 29 | int numPoints; // this in fact is equal to sensingTrajectory.size() for instance. |
mbedalvaro | 0:df6fdd9b99f0 | 30 | Timer measureSendPeriod; |
mbedalvaro | 0:df6fdd9b99f0 | 31 | |
mbedalvaro | 0:df6fdd9b99f0 | 32 | public: |
mbedalvaro | 0:df6fdd9b99f0 | 33 | |
mbedalvaro | 0:df6fdd9b99f0 | 34 | // CONSTRUCTOR AND DESTRUCTOR -------------------------------------------------------------------------------------------------------------- |
mbedalvaro | 0:df6fdd9b99f0 | 35 | soundSpot(); |
mbedalvaro | 0:df6fdd9b99f0 | 36 | // Destructor (virtual, because one can only delete an object of a derived type |
mbedalvaro | 0:df6fdd9b99f0 | 37 | // through a pointer to one of its base classes IF the base class has a virtual destructor) |
mbedalvaro | 0:df6fdd9b99f0 | 38 | virtual ~soundSpot();// { cout<<"Destroying Base";} |
mbedalvaro | 0:df6fdd9b99f0 | 39 | |
mbedalvaro | 0:df6fdd9b99f0 | 40 | // METHODS ------------------------------------------------------------------------------------------------------------- |
mbedalvaro | 0:df6fdd9b99f0 | 41 | // COMMON methods to all the kind of blobs: |
mbedalvaro | 0:df6fdd9b99f0 | 42 | // (1) properties of the visible loop: |
mbedalvaro | 0:df6fdd9b99f0 | 43 | void setColor(unsigned char c); |
mbedalvaro | 0:df6fdd9b99f0 | 44 | void setGreenColor(unsigned char c); |
mbedalvaro | 0:df6fdd9b99f0 | 45 | void setBlueColor(unsigned char c); |
mbedalvaro | 0:df6fdd9b99f0 | 46 | |
mbedalvaro | 0:df6fdd9b99f0 | 47 | // (2) Sending modes: |
mbedalvaro | 0:df6fdd9b99f0 | 48 | void stopAllSending(void); |
mbedalvaro | 0:df6fdd9b99f0 | 49 | void resetAllSendingModes(void); |
mbedalvaro | 0:df6fdd9b99f0 | 50 | void initCommonVariables(); |
mbedalvaro | 0:df6fdd9b99f0 | 51 | void sendData(void); // send data common to all kind of blobs |
mbedalvaro | 0:df6fdd9b99f0 | 52 | |
mbedalvaro | 0:df6fdd9b99f0 | 53 | // VIRTUAL METHODS (common to all blobs, but each type of blob will implement it differently): |
mbedalvaro | 0:df6fdd9b99f0 | 54 | // Blob creation: |
mbedalvaro | 0:df6fdd9b99f0 | 55 | virtual void setRegionMotion(float mmix, float mmiy, float mmax, float mmay)=0; |
mbedalvaro | 0:df6fdd9b99f0 | 56 | // virtual void createBlob()=0; // note: the number of masses in the blob will be given from the scafold. |
mbedalvaro | 0:df6fdd9b99f0 | 57 | // virtual void createBlob(int _id, ElasticLoopMode _elasticBlobMode, vector2D _initPos)=0; |
mbedalvaro | 0:df6fdd9b99f0 | 58 | // Update: |
mbedalvaro | 0:df6fdd9b99f0 | 59 | virtual void update(vector2Df referencePos=vector2Df(0,0))=0; |
mbedalvaro | 0:df6fdd9b99f0 | 60 | virtual void computeBoundingBox(void)=0; // this is virtual because the displayed blob may be the scafold itself (in case of laser spot), or the elastic loop. |
mbedalvaro | 0:df6fdd9b99f0 | 61 | // Draw (on lsdTrajectory): |
mbedalvaro | 0:df6fdd9b99f0 | 62 | virtual void draw(void)=0; // NOTE: this method actually "renders" the trajectory using the laser renderer (not yet done) |
mbedalvaro | 0:df6fdd9b99f0 | 63 | // Send data through OSC: |
mbedalvaro | 0:df6fdd9b99f0 | 64 | virtual void sendDataSpecific(void)=0; // send data specific to each blob |
mbedalvaro | 0:df6fdd9b99f0 | 65 | |
mbedalvaro | 0:df6fdd9b99f0 | 66 | // Some parameters (very different treatement for each type of blob): |
mbedalvaro | 0:df6fdd9b99f0 | 67 | virtual void setSpeed(float speed=7.0)=0; |
mbedalvaro | 0:df6fdd9b99f0 | 68 | virtual void setSize(float size=20.0)=0; |
mbedalvaro | 0:df6fdd9b99f0 | 69 | virtual void speedFactor(float speedfactor=100.0)=0; |
mbedalvaro | 0:df6fdd9b99f0 | 70 | virtual void sizeFactor(float sizeFactor=100.0)=0; |
mbedalvaro | 0:df6fdd9b99f0 | 71 | |
mbedalvaro | 0:df6fdd9b99f0 | 72 | void addAngleCorrection(float _moreAngleCorrection); |
mbedalvaro | 0:df6fdd9b99f0 | 73 | void setAngleCorrection(float _angleCorrection); |
mbedalvaro | 0:df6fdd9b99f0 | 74 | |
mbedalvaro | 0:df6fdd9b99f0 | 75 | virtual void explosion()=0; // not very nice programming style, it should be a MODE instead or something like that. |
mbedalvaro | 0:df6fdd9b99f0 | 76 | virtual vector2Df getCenter()=0; |
mbedalvaro | 0:df6fdd9b99f0 | 77 | virtual void resetPositionSpeed()=0; |
mbedalvaro | 0:df6fdd9b99f0 | 78 | virtual void setPositionSpeed(vector2Df _pos, vector2Df _spe)=0; |
mbedalvaro | 0:df6fdd9b99f0 | 79 | |
mbedalvaro | 0:df6fdd9b99f0 | 80 | virtual void showChildParameters()=0; |
mbedalvaro | 0:df6fdd9b99f0 | 81 | void printParameters(); // first show common parameters, then call the virtual method: |
mbedalvaro | 0:df6fdd9b99f0 | 82 | |
mbedalvaro | 0:df6fdd9b99f0 | 83 | // DATA -------------------------------------------------------------------------------------------------------------- |
mbedalvaro | 0:df6fdd9b99f0 | 84 | int identifier; //0, 1, 2... |
mbedalvaro | 0:df6fdd9b99f0 | 85 | char spotName[20]; //spot, elastic,... (this may be useful to set particular variables by forcing type cast (bad, but couldn't find a more elegant solution for the moment) |
mbedalvaro | 0:df6fdd9b99f0 | 86 | unsigned char blobColor, transientBlobColor; // transient blob color is the color of the blob when touching something for instance. |
mbedalvaro | 0:df6fdd9b99f0 | 87 | bool blueTouch; // this is equivalent to "debugDelayMirrors" #define, but per-blob (ie. blue active in dark zones). Good for elastic loops... |
mbedalvaro | 0:df6fdd9b99f0 | 88 | |
mbedalvaro | 0:df6fdd9b99f0 | 89 | // initial position and speed of the blob: |
mbedalvaro | 0:df6fdd9b99f0 | 90 | vector2Df startCenter; |
mbedalvaro | 0:df6fdd9b99f0 | 91 | vector2Df startSpeed; |
mbedalvaro | 0:df6fdd9b99f0 | 92 | |
mbedalvaro | 0:df6fdd9b99f0 | 93 | // Special fields: |
mbedalvaro | 0:df6fdd9b99f0 | 94 | vector2Df gravity; |
mbedalvaro | 0:df6fdd9b99f0 | 95 | |
mbedalvaro | 0:df6fdd9b99f0 | 96 | // SCAFOLD (rigid structure of points, with a center and methods to create it): |
mbedalvaro | 0:df6fdd9b99f0 | 97 | RigidScafold bluePrint; |
mbedalvaro | 0:df6fdd9b99f0 | 98 | |
mbedalvaro | 0:df6fdd9b99f0 | 99 | // LASER DISPLAY/SENSING-TRAJECTORY with DATA BUFFER for each blob: |
mbedalvaro | 0:df6fdd9b99f0 | 100 | LaserSensingTrajectory displaySensingBuffer; |
mbedalvaro | 0:df6fdd9b99f0 | 101 | |
mbedalvaro | 0:df6fdd9b99f0 | 102 | // the following are common to all kind of blobs, but the way these quantities are calculated (in the update() method) differs greatly: |
mbedalvaro | 0:df6fdd9b99f0 | 103 | vector2Df totalLightForce; |
mbedalvaro | 0:df6fdd9b99f0 | 104 | vector2Df recenteringVectorLoop; |
mbedalvaro | 0:df6fdd9b99f0 | 105 | float angleCorrectionForceLoop; |
mbedalvaro | 0:df6fdd9b99f0 | 106 | float angleRecenteringVector; // auxiliary variables for sending data (for the recenteringVectorLoop) |
mbedalvaro | 0:df6fdd9b99f0 | 107 | float normRecenteringVector; |
mbedalvaro | 0:df6fdd9b99f0 | 108 | |
mbedalvaro | 0:df6fdd9b99f0 | 109 | // Geometry/kinematics of the loop: |
mbedalvaro | 0:df6fdd9b99f0 | 110 | float cx, cy, w, h; |
mbedalvaro | 0:df6fdd9b99f0 | 111 | float area, approxArea; |
mbedalvaro | 0:df6fdd9b99f0 | 112 | float totalKineticEnergy; |
mbedalvaro | 0:df6fdd9b99f0 | 113 | |
mbedalvaro | 0:df6fdd9b99f0 | 114 | bool render; // when false, the blob is NOT RENDERED |
mbedalvaro | 0:df6fdd9b99f0 | 115 | bool standByMode; // when true, the blob is NOT UPDATED |
mbedalvaro | 0:df6fdd9b99f0 | 116 | |
mbedalvaro | 0:df6fdd9b99f0 | 117 | // something touched the blob: |
mbedalvaro | 0:df6fdd9b99f0 | 118 | bool searchActive; |
mbedalvaro | 0:df6fdd9b99f0 | 119 | bool firstTimeNoTouch; |
mbedalvaro | 0:df6fdd9b99f0 | 120 | //bool lightTouched; // belongs to the lsdTrajectory |
mbedalvaro | 0:df6fdd9b99f0 | 121 | int noTouchedCounter; |
mbedalvaro | 0:df6fdd9b99f0 | 122 | vector2Df randomForce; |
mbedalvaro | 0:df6fdd9b99f0 | 123 | |
mbedalvaro | 0:df6fdd9b99f0 | 124 | // the blob touched the wall limits (these variables are updated in the "update" method, and implemented differently for each blob) |
mbedalvaro | 0:df6fdd9b99f0 | 125 | bool blobWallCollision; |
mbedalvaro | 0:df6fdd9b99f0 | 126 | int wallCounter; |
mbedalvaro | 0:df6fdd9b99f0 | 127 | |
mbedalvaro | 0:df6fdd9b99f0 | 128 | // HARDWARE SENDING MODE (can be changed by serial or osc commands): |
mbedalvaro | 0:df6fdd9b99f0 | 129 | unsigned short periodSendingData; // max data rate for sending data (in ms) |
mbedalvaro | 0:df6fdd9b99f0 | 130 | bool sendSerial; |
mbedalvaro | 0:df6fdd9b99f0 | 131 | bool sendOSC; |
mbedalvaro | 0:df6fdd9b99f0 | 132 | bool sendingOnlyWhenTouch; // when this is "true", data will be send ONLY when something touches the blob (border or light touch). Note that the max speed of sending |
mbedalvaro | 0:df6fdd9b99f0 | 133 | // will be limited to periodSendingData. |
mbedalvaro | 0:df6fdd9b99f0 | 134 | // SENDING modes for things COMMON TO ALL THE KIND OF BLOBS: |
mbedalvaro | 0:df6fdd9b99f0 | 135 | // (d) Light sensing statistics: |
mbedalvaro | 0:df6fdd9b99f0 | 136 | bool sendingBlobMaxMin; // max and min intensities are calculated directly from the lsdTrajectory |
mbedalvaro | 0:df6fdd9b99f0 | 137 | bool sendingTouched; // when someone touches the blob (can be calculated directly from the lsdTrajectory) |
mbedalvaro | 0:df6fdd9b99f0 | 138 | bool sendingLightForce; // the total light force (note: this CANNOT be calculated on the lsdTrajectory, but on the update method, particular to each loop). |
mbedalvaro | 0:df6fdd9b99f0 | 139 | // (e) Recentering vector: (note: redundant with sendingLightForce, IF the correction angle is known). |
mbedalvaro | 0:df6fdd9b99f0 | 140 | bool sendingRecenteringVector; |
mbedalvaro | 0:df6fdd9b99f0 | 141 | bool sendingRecenteringAngle; |
mbedalvaro | 0:df6fdd9b99f0 | 142 | bool sendingRecenteringNorm; |
mbedalvaro | 0:df6fdd9b99f0 | 143 | |
mbedalvaro | 0:df6fdd9b99f0 | 144 | // SPECIFIC TO EACH BLOB (move this from here, use virtual methods to set their values): |
mbedalvaro | 0:df6fdd9b99f0 | 145 | // (a) anchor mass data: |
mbedalvaro | 0:df6fdd9b99f0 | 146 | bool sendingAnchorPosition; |
mbedalvaro | 0:df6fdd9b99f0 | 147 | bool sendingAnchorForce; // this is the total force on the anchor mass, not just the recentering force |
mbedalvaro | 0:df6fdd9b99f0 | 148 | bool sendingAnchorTouchWall; |
mbedalvaro | 0:df6fdd9b99f0 | 149 | // (b) data from blob points: |
mbedalvaro | 0:df6fdd9b99f0 | 150 | bool sendingLoopPositions; |
mbedalvaro | 0:df6fdd9b99f0 | 151 | bool sendingLoopForces;// this is not just the forces from light, but all the forces in each particle |
mbedalvaro | 0:df6fdd9b99f0 | 152 | bool sendingLoopForcesLight; |
mbedalvaro | 0:df6fdd9b99f0 | 153 | bool sendingLoopRegions; // from this we can detect "hits" |
mbedalvaro | 0:df6fdd9b99f0 | 154 | bool sendingLoopTouchWall; |
mbedalvaro | 0:df6fdd9b99f0 | 155 | // (c) Blob geometry/kinematics: |
mbedalvaro | 0:df6fdd9b99f0 | 156 | bool sendingBlobArea; |
mbedalvaro | 0:df6fdd9b99f0 | 157 | bool sendingKineticEnergy; |
mbedalvaro | 0:df6fdd9b99f0 | 158 | bool sendingBlobNormals; |
mbedalvaro | 0:df6fdd9b99f0 | 159 | bool sendingBlobAngles; // redundant with sendingBlobNormals, but simplified (only angle of normal) |
mbedalvaro | 0:df6fdd9b99f0 | 160 | }; |
mbedalvaro | 0:df6fdd9b99f0 | 161 | |
mbedalvaro | 0:df6fdd9b99f0 | 162 | #endif |