519_La

Fork of Joystick_skeleton by Carter Sharer

Revision:
2:893fd930d3fb
Parent:
1:c42a77267f7b
Child:
3:6f854e0cae36
--- a/Joystick.cpp	Thu Oct 20 22:16:14 2016 +0000
+++ b/Joystick.cpp	Thu Oct 20 23:12:18 2016 +0000
@@ -10,57 +10,66 @@
     //Wait 1 second for voltage to settle
     wait(1); 
     
-    //Set Raw Center Values, this is where the joystick sits naturaly 
+    //(1)Set Raw Center Values, this is where the joystick sits naturaly 
+    //set raw horizontal center to current value (average with 5 readings)
+    //set raw vertial center to current value (average with 5 readings) 
     raw_hc = (horiz.read() + horiz.read() + horiz.read() + horiz.read() + horiz.read()) / 5.0;
     raw_vc = (vert.read() + vert.read() + vert.read() + vert.read() + vert.read()) / 5.0;
     
-    //Initalize the Rax Max to some value less then then real max value.  
-    //We dont know what the max value will be until we read it. 
-    //But we can assume it will be greater then 0.8 (center + 0.3)
+    //(2)Initalize the Rax Max to some value less then the real max value.  
+    //We dont know what the max value will be until we read it, thats ok. 
+    //But we can assume it will be greater then 0.8 (center + 0.3) so lets 
+    //set it to that for now.  We will update this later if we see a larger value
     //Now do the same for the the Raw Min
     float delta = 0.3;
-    rawMinH = raw_hc - delta; rawMaxH = raw_hc + delta;
-    rawMinV = raw_hc - delta; rawMaxV = raw_hc + delta;
+    rawMinH = raw_hc - delta; 
+    rawMaxH = raw_hc + delta;
+    rawMinV = raw_hc - delta; 
+    rawMaxV = raw_hc + delta;
 }
 
 //Returns the scaled vertial value of joystick
 float Joystick::horizontal(void) {
-    //Get average val (5 samples)
+    //(3)Get average val (5 samples)
     float avg = (horiz.read() + horiz.read() + horiz.read() + horiz.read() + horiz.read()) / 5.0;
     
-    //Watch for Max and Min Values if we see a new max/min update Raw Max/Min
+    //(4)Watch for Max and Min Values, if we see a new max/min update Raw Max/Min
     if(avg > rawMaxH)
         rawMaxH = avg;
     if(avg < rawMinH)
         rawMinH = avg;
     
-    //Here we will calculate the total range (Travel) of the joystick 
+    //(5)Here we will calculate the total range (Travel) of the joystick 
     //using the rawMax/rawMin values we have seen thus far
+    //Since the joystick is not symetrical we have to calculate two ranges
     //Calculate the range from [center, max] and [center, min]
     float range_pos = rawMaxH - raw_hc;
     float range_neg = raw_hc - rawMinH;
     
-    //Here we will calculate how much our current reading is in one 
-    //of the ranges to get a percentage
-    //Then we can scale this by multiplying it by our scale (_max/_min)
+    //(6)Here we will calculate how much our current reading is in one 
+    //of the ranges, this will give us a percentage of our _max value we 
+    //set in setScale. Then we can apply the scale by multiplying it by our 
+    //scale (_max/_min). 
     float val;
-    if(avg >= raw_hc) 
-        val = (((avg - raw_hc) / range_pos) * _max) + DEAD_ZONE/2; // percent to max * max
-    else 
-        val = (((raw_hc - avg) / range_neg) * _min) - DEAD_ZONE/2; //percent to min * min
+    if(avg >= raw_hc) //Positive Range
+        val = (((avg - raw_hc) / range_pos) * _max) + DEAD_ZONE; // percent to max * max
+    else  //Negative Range
+        val = (((raw_hc - avg) / range_neg) * _min) - DEAD_ZONE; //percent to min * min
          
-    //Here we will apply a dead zone
-    if((val <= DEAD_ZONE/2) && (val >= -DEAD_ZONE/2)) {
+    //(7)Here we will apply a dead zone.  If the |value| is <= our deadzone then 
+    //set it to 0.  Otherwise we need to shift the value closer to 0 by dead zone
+    if((val <= DEAD_ZONE) && (val >= -DEAD_ZONE)) {
         return 0;
     }
-    else if(val > DEAD_ZONE/2) {//posotive vals
-        return val - DEAD_ZONE/2;
+    else if(val > DEAD_ZONE) {//posotive vals
+        return val - DEAD_ZONE;
     }
     else { //negative vals
-        return val + DEAD_ZONE/2; 
+        return val + DEAD_ZONE; 
     }
 }
 
+//(8) Impliment vertial the same as you did for horizontal
 //Returns the scaled horizontal value of joystick
 float Joystick::vertical(void) {
     //Get average value
@@ -78,20 +87,20 @@
     
     float val;
     if(avg >= raw_vc) //find scaled pot value
-        val = (((avg - raw_vc) / range_pos) * _max) + DEAD_ZONE/2; 
+        val = (((avg - raw_vc) / range_pos) * _max) + DEAD_ZONE; 
     else 
-        val = (((raw_vc - avg) / range_neg) * _min) - DEAD_ZONE/2;
+        val = (((raw_vc - avg) / range_neg) * _min) - DEAD_ZONE;
     
     //If val is in dead zone range return 0
-    if((val <= DEAD_ZONE/2) && (val >= -DEAD_ZONE/2)) {
+    if((val <= DEAD_ZONE) && (val >= -DEAD_ZONE)) {
         return 0;
     }
     //Else return val minus dead zone / 2 
-    else if(val > DEAD_ZONE/2) {//posotive vals
-        return val - DEAD_ZONE/2;
+    else if(val > DEAD_ZONE) {//posotive vals
+        return val - DEAD_ZONE;
     }
     else { //negative vals
-        return val + DEAD_ZONE/2; 
+        return val + DEAD_ZONE; 
     }
 }