zhaw_st16b_pes2_10 / Mbed 2 deprecated PES_Official

Dependencies:   mbed Servo ServoArm

Fork of PES_Yanick by zhaw_st16b_pes2_10

Files at this revision

API Documentation at this revision

Comitter:
beacon
Date:
Sun Mar 19 12:20:26 2017 +0000
Parent:
0:d267b248eff4
Child:
2:01e9de508316
Commit message:
[404 message not found]

Changed in this revision

Headers/Robot.h Show annotated file Show diff for this revision Revisions of this file
Sources/DistanceSensors.cpp Show annotated file Show diff for this revision Revisions of this file
Sources/Farbsensor.cpp Show annotated file Show diff for this revision Revisions of this file
Sources/Robot.cpp Show annotated file Show diff for this revision Revisions of this file
Sources/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Headers/Robot.h	Sat Mar 11 10:14:00 2017 +0000
+++ b/Headers/Robot.h	Sun Mar 19 12:20:26 2017 +0000
@@ -6,48 +6,89 @@
 
 #include "mbed.h"
 
+//DistanceSensors:
+#define NEAR 0.1f           //Used for distance Sensors. If they're to near to a wall -> turn
+#define LEFT 5              //Arrayindex of the left Sensor & left LED
+#define FWD 0               //Arrayindex of the front Sensor & front LED
+#define RIGHT 1             //Arrayindex of the right Sensor & right LED.
+
+//ColorSensors:
+#define RED_UPLIMIT 500     //Default limit in mV
+#define GREEN_DOWNLIMIT 501 //Default limit in mV
+#define GREEN_UPLIMIT 1200  //Default limit in mV
+#define GREEN 1
+#define NOBLOCK 0
+#define RED -1
+
 
 /* Declarations for the Motors in the Robot in order to drive -------- */
+
+class DistanceSensors
+{
+    public:
+        
+        DistanceSensors(AnalogIn* sensorVoltage, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number);
+        DistanceSensors();
+        
+        void init(AnalogIn* sensorVoltage, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number);
+        virtual ~DistanceSensors();    
+        float read();
+        
+        operator float();           //Is here to constantly return the value of read.
+        
+    private:
+        
+        AnalogIn*       sensorVoltage;
+        DigitalOut*     bit0;
+        DigitalOut*     bit1;
+        DigitalOut*     bit2;
+        
+        int             number;
+
+};
+
+class Farbsensor
+{
+    public:
+     
+        Farbsensor();
+        Farbsensor(AnalogIn* FarbVoltage);
+    
+        void init(AnalogIn* FarbVoltage);
+        int read();
+        operator int();
+    
+    private:
+    
+        AnalogIn*   FarbVoltage;
+};
+
 class Robot
 {
     
     public:
         
         //Robot related:
-        Robot(PwmOut* left, PwmOut* right, DigitalOut* enable);
+        Robot(PwmOut* left, PwmOut* right, DigitalOut* enableSignal, DigitalOut* leds, AnalogIn* FarbVoltage );
         void drive();
         void turnLeft();
         void turnRight();
         void turnAround(int left);
+        void stop();
+        
+        void search(int* counter);
         
         //DistanceSensors related:
-        class DistanceSensors
-        {
-            public:
-                
-                DistanceSensors(AnalogIn* sensorVoltage, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number);
-                DistanceSensors();
-                
-                void init(AnalogIn* sensorVoltage, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number);
-                virtual ~DistanceSensors();    
-                float read();
-                
-                operator float();           //Is here to constantly return the value of read.
-                
-            private:
-                
-                AnalogIn*       sensorVoltage;
-                DigitalOut*     bit0;
-                DigitalOut*     bit1;
-                DigitalOut*     bit2;
-                
-                int             number;
-
-        };
+        DistanceSensors sensors[6];
+        
+        void initializeDistanceSensors();
+        //void init();
         
+        //LEDS related:
+        DigitalOut*      leds;
         
-        DistanceSensors sensors[6];
-        void init();
+        //Farbsensors related:
+        Farbsensor FarbVoltage;
         
     private:
     
@@ -59,8 +100,6 @@
         DigitalIn*      errorSignal;
         DigitalIn*      overtemperatur;
         
-        //Distance sensors related:
-            //DistanceSensors   sensors[6];
 
 };
 
--- a/Sources/DistanceSensors.cpp	Sat Mar 11 10:14:00 2017 +0000
+++ b/Sources/DistanceSensors.cpp	Sun Mar 19 12:20:26 2017 +0000
@@ -3,32 +3,33 @@
 
 
 
-Robot::DistanceSensors::DistanceSensors() 
+DistanceSensors::DistanceSensors() 
 {
     //Nothing
 }
     
-Robot::DistanceSensors::DistanceSensors(AnalogIn* sensorVoltage, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number)
+DistanceSensors::DistanceSensors(AnalogIn* sensorVoltage, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number)
 {   
     init(sensorVoltage, bit0, bit1, bit2, number);
 }
 
 //initialise
-void Robot::DistanceSensors::init(AnalogIn* sensorVoltage, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number)
+void DistanceSensors::init(AnalogIn* sensorVoltage, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number)
 {
     this->sensorVoltage = sensorVoltage;
     this->bit0 = bit0;
     this->bit1 = bit1;
-    this->bit2 = bit2;    
+    this->bit2 = bit2;
+    this->number = number;
 }
 
 
-Robot::DistanceSensors::~DistanceSensors()
+DistanceSensors::~DistanceSensors()
 { 
 }
 
 
-float Robot::DistanceSensors::read()//Return the distance of an object
+float DistanceSensors::read()//Return the distance of an object
 {  
     *bit0 = number & 1; // Set the first bit of the Sensors MUX
     *bit1 = number & 2; // Set the second bit of the Sensors MUX
@@ -39,7 +40,7 @@
     return Distance;
 }
 
-Robot::DistanceSensors::operator float()
+DistanceSensors::operator float()
 { 
     return read();
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sources/Farbsensor.cpp	Sun Mar 19 12:20:26 2017 +0000
@@ -0,0 +1,42 @@
+#include "Robot.h"
+
+
+Farbsensor::Farbsensor()
+{   
+}
+
+Farbsensor::Farbsensor(AnalogIn* FarbVoltage)
+{
+    init(FarbVoltage);  
+}
+
+void Farbsensor::init(AnalogIn* FarbVoltage)
+{
+    this->FarbVoltage= FarbVoltage;
+}
+
+int Farbsensor::read()
+{
+    int farbe;  
+    float Ufarbsensor=FarbVoltage->read();
+    Ufarbsensor=Ufarbsensor*3300; //Set the Voltage between 0mV und 3300mV
+
+    if ((Ufarbsensor > GREEN_DOWNLIMIT) && (Ufarbsensor < GREEN_UPLIMIT))
+        {
+         farbe=1;
+        }
+    else if(Ufarbsensor < RED_UPLIMIT)
+        {
+         farbe=-1;
+        }
+    else 
+        {
+         farbe=0;
+        }
+    return farbe;
+}
+
+Farbsensor::operator int()
+{
+    return read();
+}
\ No newline at end of file
--- a/Sources/Robot.cpp	Sat Mar 11 10:14:00 2017 +0000
+++ b/Sources/Robot.cpp	Sun Mar 19 12:20:26 2017 +0000
@@ -2,14 +2,19 @@
 
 /* Work in progress -------------------------------------------- */
 
-Robot::Robot(PwmOut* left, PwmOut* right, DigitalOut* enableSignal)
+Robot::Robot(PwmOut* left, PwmOut* right, DigitalOut* enableSignal, DigitalOut* leds, AnalogIn* FarbVoltage )
 {
     this->powerSignal = enableSignal;
-    this->left = left;
-    this->right = right;
+    this->left =        left;
+    this->right =       right;
 
     this->left->period(0.00005f);
     this->right->period(0.00005f);
+    
+    this->leds =        leds;
+    
+    this->FarbVoltage = FarbVoltage;
+
 }
 
 /* work in progress ------------------------------------------- */
@@ -17,41 +22,95 @@
 {
     //pwm determine what direction it goes.
     *powerSignal = 1;
-    *left=  0.8f;
-    *right= 0.7f;
-    *left=  0.7f;
-    *right= 0.8f;
+    *left=  0.6f;
+    *right= 0.4f;
+}
+
+void Robot::turnLeft()
+{
+    
+    *powerSignal = 1;
+    *left=  0.4f;
+    *right= 0.4f;
+
 }
 
-void Robot::turnLeft(){
+void Robot::turnRight()
+{
+    *powerSignal = 1;
+    *left=  0.6f;
+    *right= 0.6f;
+}
+
+void Robot::turnAround(int left)
+{
     *powerSignal = 1;
-    *left=  0.35f;
-    *right= 0.65f;
-    
+
+    if (left) {
+        turnLeft();
+    }
+
+    else {
+        turnRight();
+    }
+}
+
+void Robot::stop()
+{
+    *powerSignal = 1;
+    *left=  0.5f;
+    *right= 0.5f;
 }
 
-void Robot::turnRight(){
-    *powerSignal = 1;
-    *left=  0.65f;
-    *right= 0.35f;
-}
-
-void Robot::turnAround(int left){
-    *powerSignal = 1;
+void Robot::search(int* counter){
+    int rando;
     
-    if (left){
-        turnLeft();
+    if (*counter >= 5) {
+        rando = rand() % 2;
+        while (this->sensors[FWD] < NEAR){
+            this->leds[FWD] = 1;
+            this->turnAround(rando);
+        }
+     this->leds[FWD] = 0;   
+    }
+    
+    else if (this->sensors[RIGHT] < NEAR){
+        *counter += 1;
+        while (this->sensors[RIGHT] < NEAR){
+            this->turnLeft();
+            this->leds[RIGHT] = 1;
+        }
+        this->leds[RIGHT] = 0;
     }
     
-    else{
-        turnRight();
+    else if (this->sensors[LEFT] < NEAR){
+        *counter += 1;
+        while (this->sensors[LEFT] < NEAR){
+            this->turnRight();
+            this->leds[LEFT] = 1;
+        }
+        this->leds[LEFT] = 0;
+    }
+    
+    else if (this->sensors[FWD] < NEAR) {
+        rando = rand() % 2;
+        while (this->sensors[FWD] < NEAR){
+            this->leds[FWD] = 1;
+            this->turnAround(rando);
+        }
+     this->leds[FWD] = 0;   
+    }
+    
+    else {
+        *counter = 0;
+        this->drive();
     }
 }
 
 //void Robot::init(){
 //  Robot.DistanceSensors.init(&sensorVoltage, &bit0, &bit1, &bit2, ii);
 //}
-    
+
 //Remember to set
 //DigitalOut powerMotor(PB_2) = 1;
 //DigitalIn errorMotor(PB_14);
--- a/Sources/main.cpp	Sat Mar 11 10:14:00 2017 +0000
+++ b/Sources/main.cpp	Sun Mar 19 12:20:26 2017 +0000
@@ -3,22 +3,6 @@
 
 #include <cstdlib>
 
-#define NEAR 0.1f   //Used for distance Sensors. If they're to near to a wall -> turn
-#define LEFT        //Arrayindex of the left Sensor
-#define FWD         //Arrayindex of the forward Sensor
-#define RIGHT       //Arrayindex of the right Sensor
-
-
-//Robot related:
-PwmOut          left(PA_8);
-PwmOut          right(PA_3);
-
-DigitalOut      powerSignal(PB_2);
-DigitalIn       errorSignal(PB_14);
-DigitalIn       overtemperatur(PB_15);
-
-Robot bot( &left, &right, &powerSignal );
-
 //DistanceSensors related:
 Serial pc(SERIAL_TX, SERIAL_RX);
 
@@ -27,79 +11,40 @@
 DigitalOut bit0(PH_1);
 DigitalOut bit1(PC_2);
 DigitalOut bit2(PC_3);
-//Robot::DistanceSensors sensors[6];
+
+//Leds related:
+DigitalOut leds[6] = { PC_8, PC_6, PB_12, PA_7, PC_0, PC_9 };
 
-//LED related:
-DigitalOut leds[] = { PC_8, PC_6, PB_12, PA_7, PC_0, PC_9 };
+//motor related:
+PwmOut          left(PA_8);
+PwmOut          right(PA_9);
 
-void initialiseDistanceSensors(){
+DigitalOut      powerSignal(PB_2);
+DigitalIn       errorSignal(PB_14);
+DigitalIn       overtemperatur(PB_15);
+
+//Farbsensor:
+AnalogIn FarbVoltage(A0);
+
+Robot sam( &left, &right, &powerSignal, leds, &FarbVoltage ); //Implement the Farbsensor into the Robot init function!!
+
+void initializeDistanceSensors(){
     for( int ii = 0; ii<6; ++ii) {
-        bot.sensors[ii].init(&sensorVoltage, &bit0, &bit1, &bit2, ii);
+        sam.sensors[ii].init(&sensorVoltage, &bit0, &bit1, &bit2, ii);
 
-        enable = 1;
+    enable = 1;
     }
 }
 
-void fahralgorithmus(){
+/* */
+int main(){
     
-    int rando = 0; //Will be used for rdandom actions.
-
-    while( 1 ) {    //Remember to change the indices. Also make into a seperate function
-
-        if ( bot.sensors[0] < NEAR ) { //Left Sensor
-            while ( bot.sensors[0] < NEAR ) {
-                bot.turnRight();
-            }
-        }
-
-        else if ( bot.sensors[1] < NEAR ) { //Front Sensor
-            rando = rand() % 2;
-            while ( bot.sensors[1] < NEAR ) {
-                bot.turnAround(rando);
-            }
-        }
-
-        else if ( bot.sensors[2] < NEAR ) { //Right Sensor
-            while ( bot.sensors[2] < NEAR ) {
-                bot.turnLeft();
-            }
-        }
-
-        else {
-            bot.drive();
-        }
-
-    }
-}
-
-/* * /
-
-
-int main(){
+    initializeDistanceSensors();
+    int counter = 0;        //counts how many times the robot has turned, before driving
+        
     while( 1 ){
-        bot.drive();
+        sam.search(&counter);
+        wait( 0.1f );
     }
     return 0;
-}
-/* */
-
-
-/* */
-int main()
-{
-    initialiseDistanceSensors();
-    
-    while( 1 ){
-        for(int i=0; i<6; i++){
-            if(bot.sensors[i] > 0.05f){
-                leds[i] = 1;
-            }
-            else{
-                leds[i] = 0;
-            }
-        }
-    }
-    
-    return 0;
-}
-/*  */
\ No newline at end of file
+}
\ No newline at end of file