just a test

Dependencies:   mbed

Fork of scoreLight_Advanced by Alvaro Cassinelli

Revision:
36:233b12d0b1f0
Parent:
34:1244fa3f2559
Child:
37:fa6b1f15819f
--- a/classLaserSensingTrajectory.cpp	Tue Nov 13 04:40:08 2012 +0000
+++ b/classLaserSensingTrajectory.cpp	Wed Mar 13 11:38:48 2013 +0000
@@ -1,9 +1,15 @@
 #include "classLaserSensingTrajectory.h"
+using namespace std;
 
 LaserSensingTrajectory::LaserSensingTrajectory():lightTouched(false), 
-modeThreshold(FIXED), 
-min_contrast_ratio(MIN_CONTRAST_RATIO), threshold_factor(THRESHOLD_FACTOR), min_acceptable_intensity(MIN_ACCEPTABLE_INTENSITY), 
-fixedThreshold(FIXED_THRESHOLD) {
+min_contrast_ratio(MIN_CONTRAST_RATIO), threshold_factor(THRESHOLD_FACTOR), 
+min_acceptable_intensity(MIN_ACCEPTABLE_INTENSITY), fixedThreshold(FIXED_THRESHOLD) 
+{
+    lsdTrajectory.clear(); // no need in principle! the constructor of the vector will give an empty vector!
+    // attention: properly set the state of the threshold switch in the IO.init (which by the way, in THIS hardware
+    // implementation indicates the state for ALL the objects..)
+   modeThreshold=AUTO;
+  // IO.setSwitchOneState(true);
 }
 
 LaserSensingTrajectory::~LaserSensingTrajectory() {
@@ -19,14 +25,15 @@
     delayMirrorSamples+=add_delay;
 }
 
-void LaserSensingTrajectory::processSensedData() {
+bool LaserSensingTrajectory::processSensedData() {
     // Compute max and min intensity on the loop
     maxI=0;
     minI=255; // ratio has been normalized between 0 and 255
-    int auxSize=lsdTrajectory.size();
+    unsigned short auxSize=lsdTrajectory.size(); // could be an unsigned char in principle... no more than 255 points per object, but well, memory in future versions
+    // of the microprocessor can be larger.
     
     // Compute minimum and maximum intensities:
-    for (int i = 0; i < lsdTrajectory.size(); i++) {
+    for (unsigned short i = 0; i < auxSize; i++) {
         unsigned char mesI=lsdTrajectory[i].intensity; 
         if (maxI<mesI)  maxI=mesI;
         if (minI>mesI)  minI=mesI;
@@ -53,18 +60,27 @@
     // 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...
     // 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
     // idea of the blob "constant" internal pressure...
-    lightTouched=false;
-  //  int counterLight=0;
-    for (int i = 0; i <  lsdTrajectory.size(); i++) {
-        int delayedpoint=(i+auxSize+delayMirrorSamples)%auxSize; // this way we can have negative delayMirrorSamples if required (would be absurd though)
+    bool isLightZone=false, isDarkZone=false;
+    for (unsigned short i = 0; i <  auxSize; i++) {
+        unsigned short delayedpoint=(i+auxSize+delayMirrorSamples)%auxSize; // this way we can have negative delayMirrorSamples if required (would be absurd though)
         if (lsdTrajectory[delayedpoint].intensity>=autoThreshold) { // this means a WHITE zone:
-            lsdTrajectory[i].lightZone=  -1;
-        //    counterLight++;
-        } else { // something touched: DARK ZONE
-            lsdTrajectory[i].lightZone= 2;
-            lightTouched=true; // (for the whole loop)
+            lsdTrajectory[i].lightZone= 1;
+            isLightZone=true;
+        } else { // this means DARK ZONE
+            lsdTrajectory[i].lightZone= 0;
+            isDarkZone=true;
         }
     }
-    //coko=counterLight;
-//    lightRatio=1.0*counterLight/lsdTrajectory.size();
+    
+    // In the case of AUTO mode, we assume that something is touching the object when the trajectory has at least one light zone and one dark zone;
+    // in the case of FIXED mode, we assume something is touching the object when there is at least one light zone (this is a convention, because it depends
+    // on the background - but we can argue that "no background" corresponds to no reflection (then dark), and, say, a ping pong ball or finger is front 
+    // will be "light":
+    if (modeThreshold==FIXED)  
+        lightTouched=isLightZone;
+    else 
+        lightTouched=isLightZone&&isDarkZone; // assuming only two modes for modeThreshold
+    
+    // Return lightTouched for commodity: 
+    return(lightTouched);
 }