Sooner Competitive Robotics / Mbed 2 deprecated IEEE_14_Freescale

Dependencies:   mbed

Fork of IEEE_14_Freescale by IEEE 2014 Mbed

Revision:
7:3b2cf7efe5d1
Parent:
1:c28fac16a109
Child:
9:aff48e331147
--- a/remoteEnc.cpp	Mon Nov 18 23:45:12 2013 +0000
+++ b/remoteEnc.cpp	Fri Nov 22 18:00:18 2013 +0000
@@ -43,6 +43,8 @@
     outState=0;
 }
 
+// this handles getting the encoder values and returns an array of 4 integers which are the offsets since last reset
+// I think there's a memory leak here, since the array that is returned is never freed
 const int* remoteEnc::getVals(){
     int charBuf[8];
     cs=0;
@@ -55,6 +57,15 @@
     for(i=0;i<4;i++){
         latestActual[i]=(charBuf[2*i]+(charBuf[2*i+1]<<8))*direction[i];
         //DBGPRINT("[%d, %d]",i,latestActual[i]);
+        
+        
+        // this allows the encoder counter to overflow safely, as long as we have at least one poll per quadrant
+        //
+        // since we only get 16bit values out of the remote device, overflow is a concern
+        // this code deals with it by dividing the 2^16 possible integers into 4 quadrants
+        // when we move from one quadrant to the next (one of the two most significant bits change)
+        // this picks up on the direction and updated 'latestQuadrant' which is a 32 bit integer
+        // which is used for the top 18 bits of the final result
         if((latestActual[i]&0xC000)!=(latestQuadrant[i]&0xC000)){
             DBGPRINT("*%x and %x to ",latestActual[i],latestQuadrant[i]);
             if((latestActual[i]&0xC000)==((latestQuadrant[i]+0x4000)&0xC000)){
@@ -85,6 +96,7 @@
     //DBGPRINT("[%d, %d]",slot,latestActual[slot]);
     if((latestActual[slot]&0xC000)!=(latestQuadrant[slot]&0xC000)){
         DBGPRINT("*%x and %x to ",latestActual[slot],latestQuadrant[slot]);
+        //same as the discription in getVals() (might want to make function)
         if((latestActual[slot]&0xC000)==((latestQuadrant[slot]+0x4000)&0xC000)){
             latestQuadrant[slot]+=0x4000;
         } else if ((latestActual[slot]&0xC000)==((latestQuadrant[slot]-0x4000)&0xC000)){
@@ -97,11 +109,14 @@
     return latestCount[slot];
 }
 
+// allows for the universal reversal of the inputs from an encoder, so directions can be switched in software
 int remoteEnc::setDirections(int dir1, int dir2, int dir3, int dir4){
     direction[0]= (dir1 == -1)? -1: 1;
     direction[1]= (dir2 == -1)? -1: 1;
     direction[2]= (dir3 == -1)? -1: 1;
     direction[3]= (dir4 == -1)? -1: 1;
+    
+    // need to reinitalize the internal memory of the encoders
     int i;
     for(i=0;i<4;i++){
         referenceCounts[i]=0;