Buggy bois / Mbed 2 deprecated UDITTEST

Dependencies:   mbed

Revision:
7:cb07cdb35b6c
Parent:
6:477382219bcf
Child:
8:5ed6685f6edd
--- a/Robot.h	Tue Mar 26 16:19:47 2019 +0000
+++ b/Robot.h	Thu Mar 28 01:07:54 2019 +0000
@@ -1,11 +1,10 @@
 class Robot {
     private:
-    float static const distanceBetweenWheels = 0.19;
-    int static const numberOfSamples = 100;
-    int static const numberOfSensors = 6;
-    //attenuation factor
-    float static const AF = 0.3f;
-    Wheel* leftWheel;
+    float static const distanceBetweenWheels = 0.19; //currently not used may be used later
+    int static const numberOfSamples = 100; //number of samples the line voltage array will hold
+    int static const numberOfSensors = 6; //how many sensors you want to use
+    float AF; //attenuation factor just a number you want to minimize max speed by, obviously dont go higher than 1
+    Wheel* leftWheel; //a pointer to the left wheel
     Wheel* rightWheel;
     lineSensor* sensorArray[numberOfSensors];
     PID2 controller;
@@ -20,11 +19,15 @@
     
     float RoboticAngularVelocity;
     
+    char state;
+    
     public:
     
-    Robot(Wheel* LW, Wheel* RW, lineSensor* SA[]) : controller(2.0f, 0.0f, 0.0f, 0.0006f)
+    Robot(Wheel* LW, Wheel* RW, lineSensor* SA[]) : controller(2.0f, 0.0f, 0.0f, 0.0006f) //this controller actually does nothing atm
     {
         updater.detach();
+        AF  = 0.3f; //change this value to change max speedzzzzzzzzzzzzzzzzz
+        state = 'S';
         
         Reflec = 0;        
         lvIndex = 0;
@@ -78,13 +81,14 @@
     
     void robotUpdates(void) //sampling rate the ticker is attached I.E the wheel speed is updated everytime this function is called
         {
-            float ambientLight;
-            float reading;
-            ambientLight = sensorArray[sensorNumber]->calcLineVoltage();  
-            sensorArray[sensorNumber]->sample();
-            sensorArray[sensorNumber]->calcLineVoltage();
-            switch (sensorNumber)
-            {
+
+                float ambientLight;
+                float reading;
+                ambientLight = sensorArray[sensorNumber]->calcLineVoltage();  
+                sensorArray[sensorNumber]->sample();
+                sensorArray[sensorNumber]->calcLineVoltage();
+                switch (sensorNumber)
+                {
                 case 0:
                     lineVoltages[lvIndex%numberOfSamples] = ((sensorArray[sensorNumber]->returnLineVoltage() - ambientLight) * -400.0f);
                     break;
@@ -103,14 +107,32 @@
                 case 5:
                     lineVoltages[lvIndex%numberOfSamples] = lineVoltages[lvIndex%numberOfSamples]+((sensorArray[sensorNumber]->returnLineVoltage() - ambientLight) * 400.0f);
                     break;
-            }
-            sensorNumber ++;
-            if (sensorNumber >= numberOfSensors)
-            {
-            sensorNumber = 0;
-            adjustAngularVelocity(lineVoltages[lvIndex%numberOfSamples]);
-            lvIndex++;
-            }
+                }
+                sensorNumber ++;
+                if (reading <= Reflec*11) {endOfLineDetection++;}
+            
+                if (sensorNumber >= numberOfSensors)
+                {
+                    if (endOfLineDetection <= 6)
+                    {
+                        AF  = 0.3f;
+                        sensorNumber = 0;
+                        adjustAngularVelocity(lineVoltages[lvIndex%numberOfSamples]);
+                        lvIndex++;
+                    }
+                    else 
+                    {
+                        if (AF > 0) //this will Slowly grind the buggy to a HALT to overcome line breaks and overshooting in turns -> dont want it to instantly stop
+                        {
+                        AF  -= 0.1f;
+                        adjustAngularVelocity(lineVoltages[lvIndex%numberOfSamples-1]); //base your action on the previous measured value
+                        }
+                        else //well shit, you're buggy must have totally overshot the line, lets just hope while spinning it can find a line, direction not guaranteed
+                        {
+                        spin(); //spin like mad and hope you find a line
+                        }
+                    }
+                }
         }
     
     void stopMovement(void)
@@ -132,6 +154,17 @@
     controller.setSetPoint(0.0f);
     updater.attach(callback(this, &Robot::robotUpdates),0.0001f);   
     }
+    
+    void spin()
+    {
+        rightWheel->adjustAngularVelocity(rightWheel->returnMaxAngularVel()*0.3f);
+        leftWheel->adjustAngularVelocity((rightWheel->returnMaxAngularVel())*-0.3f);
+    }
+    
+    void setState(char S)
+    {
+        state = S;
+    }
 
 };