just a test

Dependencies:   mbed

Fork of scoreLight_Advanced by Alvaro Cassinelli

Committer:
mbedalvaro
Date:
Wed Nov 07 14:41:55 2012 +0000
Revision:
34:1244fa3f2559
Parent:
33:43e8bc451ef0
Child:
36:233b12d0b1f0
added hardware button & potentiometer to select the thresholding mode and parameters. Problems with the ADC conversion though...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedalvaro 32:52273c3291fe 1 #include "classLaserSensingTrajectory.h"
mbedalvaro 32:52273c3291fe 2
mbedalvaro 32:52273c3291fe 3 LaserSensingTrajectory::LaserSensingTrajectory():lightTouched(false),
mbedalvaro 33:43e8bc451ef0 4 modeThreshold(FIXED),
mbedalvaro 33:43e8bc451ef0 5 min_contrast_ratio(MIN_CONTRAST_RATIO), threshold_factor(THRESHOLD_FACTOR), min_acceptable_intensity(MIN_ACCEPTABLE_INTENSITY),
mbedalvaro 33:43e8bc451ef0 6 fixedThreshold(FIXED_THRESHOLD) {
mbedalvaro 32:52273c3291fe 7 }
mbedalvaro 32:52273c3291fe 8
mbedalvaro 32:52273c3291fe 9 LaserSensingTrajectory::~LaserSensingTrajectory() {
mbedalvaro 32:52273c3291fe 10 // lsdTrajectory.clear(); // there is no need to clear the vector, the destructor of this vector is called by default (and it's NOT a vector of pointers)
mbedalvaro 32:52273c3291fe 11 }
mbedalvaro 32:52273c3291fe 12
mbedalvaro 32:52273c3291fe 13
mbedalvaro 32:52273c3291fe 14 void LaserSensingTrajectory::setDelayMirrors(int delay) {
mbedalvaro 32:52273c3291fe 15 delayMirrorSamples=delay;
mbedalvaro 32:52273c3291fe 16 }
mbedalvaro 32:52273c3291fe 17
mbedalvaro 32:52273c3291fe 18 void LaserSensingTrajectory::addDelayMirrors(int add_delay) {
mbedalvaro 32:52273c3291fe 19 delayMirrorSamples+=add_delay;
mbedalvaro 32:52273c3291fe 20 }
mbedalvaro 32:52273c3291fe 21
mbedalvaro 32:52273c3291fe 22 void LaserSensingTrajectory::processSensedData() {
mbedalvaro 32:52273c3291fe 23 // Compute max and min intensity on the loop
mbedalvaro 32:52273c3291fe 24 maxI=0;
mbedalvaro 32:52273c3291fe 25 minI=255; // ratio has been normalized between 0 and 255
mbedalvaro 32:52273c3291fe 26 int auxSize=lsdTrajectory.size();
mbedalvaro 32:52273c3291fe 27
mbedalvaro 32:52273c3291fe 28 // Compute minimum and maximum intensities:
mbedalvaro 32:52273c3291fe 29 for (int i = 0; i < lsdTrajectory.size(); i++) {
mbedalvaro 32:52273c3291fe 30 unsigned char mesI=lsdTrajectory[i].intensity;
mbedalvaro 32:52273c3291fe 31 if (maxI<mesI) maxI=mesI;
mbedalvaro 32:52273c3291fe 32 if (minI>mesI) minI=mesI;
mbedalvaro 32:52273c3291fe 33 }
mbedalvaro 32:52273c3291fe 34
mbedalvaro 32:52273c3291fe 35 // Compute autoThreshold:
mbedalvaro 33:43e8bc451ef0 36 switch(modeThreshold) {
mbedalvaro 33:43e8bc451ef0 37 case AUTO:
mbedalvaro 33:43e8bc451ef0 38 if (minI==0) minI=1;
mbedalvaro 34:1244fa3f2559 39 if (maxI<min_acceptable_intensity) autoThreshold=255;// (we consider that the saccade is FULL on something black - this is noise)
mbedalvaro 33:43e8bc451ef0 40 else if (1.0*maxI/minI > min_contrast_ratio ) {
mbedalvaro 33:43e8bc451ef0 41 autoThreshold = (unsigned char) (1.0 * (maxI-minI) * threshold_factor + minI); // threshold_factor = 2/3 or 1/2 is a good value.
mbedalvaro 33:43e8bc451ef0 42 } else {// ... otherwise, we consider that the saccade is FULL on something white
mbedalvaro 33:43e8bc451ef0 43 autoThreshold=0;
mbedalvaro 33:43e8bc451ef0 44 }
mbedalvaro 34:1244fa3f2559 45 break;
mbedalvaro 33:43e8bc451ef0 46 case FIXED:
mbedalvaro 33:43e8bc451ef0 47 autoThreshold=fixedThreshold;
mbedalvaro 34:1244fa3f2559 48 break;
mbedalvaro 32:52273c3291fe 49 }
mbedalvaro 34:1244fa3f2559 50
mbedalvaro 32:52273c3291fe 51
mbedalvaro 32:52273c3291fe 52 // Segment the trajectory (only two levels for the time being, but we can have more - meaning different forces, real or even complex values to have different angle forces...):
mbedalvaro 32:52273c3291fe 53 // NOTE: if using 1 and -1 instead of 1 and 0, we can avoid having to add a "blob internal pressure"! -1 means a force towards the interior, and +1 outwards...
mbedalvaro 32:52273c3291fe 54 // This means that the loop will naturally become inside-out depending on the color of the main surface! (but will mantain its normal size). Much better and elegant solution than the
mbedalvaro 32:52273c3291fe 55 // idea of the blob "constant" internal pressure...
mbedalvaro 32:52273c3291fe 56 lightTouched=false;
mbedalvaro 32:52273c3291fe 57 // int counterLight=0;
mbedalvaro 32:52273c3291fe 58 for (int i = 0; i < lsdTrajectory.size(); i++) {
mbedalvaro 32:52273c3291fe 59 int delayedpoint=(i+auxSize+delayMirrorSamples)%auxSize; // this way we can have negative delayMirrorSamples if required (would be absurd though)
mbedalvaro 32:52273c3291fe 60 if (lsdTrajectory[delayedpoint].intensity>=autoThreshold) { // this means a WHITE zone:
mbedalvaro 32:52273c3291fe 61 lsdTrajectory[i].lightZone= -1;
mbedalvaro 32:52273c3291fe 62 // counterLight++;
mbedalvaro 32:52273c3291fe 63 } else { // something touched: DARK ZONE
mbedalvaro 32:52273c3291fe 64 lsdTrajectory[i].lightZone= 2;
mbedalvaro 32:52273c3291fe 65 lightTouched=true; // (for the whole loop)
mbedalvaro 32:52273c3291fe 66 }
mbedalvaro 32:52273c3291fe 67 }
mbedalvaro 32:52273c3291fe 68 //coko=counterLight;
mbedalvaro 32:52273c3291fe 69 // lightRatio=1.0*counterLight/lsdTrajectory.size();
mbedalvaro 32:52273c3291fe 70 }