Roboshark / Mbed 2 deprecated Roboshark_V62

Dependencies:   mbed

Fork of Roboshark_V6 by Roboshark

Committer:
ahlervin
Date:
Thu May 03 19:36:16 2018 +0000
Revision:
6:7bbcdd07bc2d
Child:
7:862d80e0ea2d
Aufgemotzter Regler noch nicht ganz fertig

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ahlervin 6:7bbcdd07bc2d 1 /*Roboshark V5
ahlervin 6:7bbcdd07bc2d 2 Regler.cpp
ahlervin 6:7bbcdd07bc2d 3 Erstellt: V.Ahlers
ahlervin 6:7bbcdd07bc2d 4 geändert: V.Ahlers
ahlervin 6:7bbcdd07bc2d 5 V.5.18
ahlervin 6:7bbcdd07bc2d 6 Regler zum geradeaus fahren
ahlervin 6:7bbcdd07bc2d 7 */
ahlervin 6:7bbcdd07bc2d 8
ahlervin 6:7bbcdd07bc2d 9 #include <cmath>
ahlervin 6:7bbcdd07bc2d 10 #include "Regler.h"
ahlervin 6:7bbcdd07bc2d 11
ahlervin 6:7bbcdd07bc2d 12
ahlervin 6:7bbcdd07bc2d 13 using namespace std;
ahlervin 6:7bbcdd07bc2d 14
ahlervin 6:7bbcdd07bc2d 15 const float Regler :: PERIOD = 0.2f;
ahlervin 6:7bbcdd07bc2d 16 const int Regler :: FIXSPEED = 50;
ahlervin 6:7bbcdd07bc2d 17 float faktor = 0.02;
ahlervin 6:7bbcdd07bc2d 18
ahlervin 6:7bbcdd07bc2d 19
ahlervin 6:7bbcdd07bc2d 20
ahlervin 6:7bbcdd07bc2d 21 Regler::Regler(AnalogIn& IrRight, AnalogIn& IrLeft):
ahlervin 6:7bbcdd07bc2d 22 IrRight (IrRight), IrLeft (IrLeft) {
ahlervin 6:7bbcdd07bc2d 23
ahlervin 6:7bbcdd07bc2d 24 SpeedR = 0;
ahlervin 6:7bbcdd07bc2d 25 SpeedL = 0;
ahlervin 6:7bbcdd07bc2d 26 ticker.attach(callback(this, &Regler::setSpeed), PERIOD);
ahlervin 6:7bbcdd07bc2d 27 }
ahlervin 6:7bbcdd07bc2d 28
ahlervin 6:7bbcdd07bc2d 29 Regler::~Regler(){
ahlervin 6:7bbcdd07bc2d 30 ticker.detach();
ahlervin 6:7bbcdd07bc2d 31 }
ahlervin 6:7bbcdd07bc2d 32
ahlervin 6:7bbcdd07bc2d 33 void Regler::setSpeed (){
ahlervin 6:7bbcdd07bc2d 34 measR2 = IrRight.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
ahlervin 6:7bbcdd07bc2d 35 measR2 = measR2* 1000; // Change the value to be in the 0 to 1000 range
ahlervin 6:7bbcdd07bc2d 36 measL2 = IrLeft.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
ahlervin 6:7bbcdd07bc2d 37 measL2 = measL2 * 1000; // Change the value to be in the 0 to 1000 range
ahlervin 6:7bbcdd07bc2d 38
ahlervin 6:7bbcdd07bc2d 39 if((measR2 < 100)) {
ahlervin 6:7bbcdd07bc2d 40 if(measL2 > 280){
ahlervin 6:7bbcdd07bc2d 41 div3 = measL2 - 280;
ahlervin 6:7bbcdd07bc2d 42 kor3 = faktor*div3;
ahlervin 6:7bbcdd07bc2d 43 div2 = 0;
ahlervin 6:7bbcdd07bc2d 44 div1 = 0;
ahlervin 6:7bbcdd07bc2d 45 div4 = 0;
ahlervin 6:7bbcdd07bc2d 46 SpeedR = FIXSPEED;
ahlervin 6:7bbcdd07bc2d 47 SpeedL = FIXSPEED + kor3;
ahlervin 6:7bbcdd07bc2d 48 } else if (measR2 < 280){
ahlervin 6:7bbcdd07bc2d 49 div4 = 280 - measR2;
ahlervin 6:7bbcdd07bc2d 50 kor4 = faktor*div4;
ahlervin 6:7bbcdd07bc2d 51 div2 = 0;
ahlervin 6:7bbcdd07bc2d 52 div1 = 0;
ahlervin 6:7bbcdd07bc2d 53 div3 = 0;
ahlervin 6:7bbcdd07bc2d 54 SpeedR = FIXSPEED + kor4;
ahlervin 6:7bbcdd07bc2d 55 SpeedL = FIXSPEED;
ahlervin 6:7bbcdd07bc2d 56 }
ahlervin 6:7bbcdd07bc2d 57
ahlervin 6:7bbcdd07bc2d 58 if ((measR2 > measL2) && (meas2R > 100)) { //IR Sensor werte werden verglichen und die Korrektur wird berechnet
ahlervin 6:7bbcdd07bc2d 59 div1 = measR2 - measL2;
ahlervin 6:7bbcdd07bc2d 60 kor1 = faktor*div1;
ahlervin 6:7bbcdd07bc2d 61 div2 = 0;
ahlervin 6:7bbcdd07bc2d 62 SpeedR = FIXSPEED;
ahlervin 6:7bbcdd07bc2d 63 SpeedL = FIXSPEED + kor1;
ahlervin 6:7bbcdd07bc2d 64 } else if ((measR2 < measL2)&& meas2R > 100) {
ahlervin 6:7bbcdd07bc2d 65 div2 = measL2 - measR2;
ahlervin 6:7bbcdd07bc2d 66 kor2 = 0.02f*div2;
ahlervin 6:7bbcdd07bc2d 67 div1 = 0;
ahlervin 6:7bbcdd07bc2d 68 SpeedR = FIXSPEED + kor2;
ahlervin 6:7bbcdd07bc2d 69 SpeedL = FIXSPEED;
ahlervin 6:7bbcdd07bc2d 70 } else {
ahlervin 6:7bbcdd07bc2d 71 SpeedR = FIXSPEED;
ahlervin 6:7bbcdd07bc2d 72 SpeedL = FIXSPEED;
ahlervin 6:7bbcdd07bc2d 73 }
ahlervin 6:7bbcdd07bc2d 74 //printf("Div1 = %f\n",div1);
ahlervin 6:7bbcdd07bc2d 75 //printf("Div2 = %f\n",div2);
ahlervin 6:7bbcdd07bc2d 76 //printf("SpeedR1 = %f\n",SpeedR);
ahlervin 6:7bbcdd07bc2d 77 //printf("SpeedL1 = %f\n",SpeedL);
ahlervin 6:7bbcdd07bc2d 78 }
ahlervin 6:7bbcdd07bc2d 79 float Regler :: get_SpeedR (){
ahlervin 6:7bbcdd07bc2d 80 SpeedR = SpeedR;
ahlervin 6:7bbcdd07bc2d 81 //printf("SpeedR2 = %f\n",SpeedR);
ahlervin 6:7bbcdd07bc2d 82 return SpeedR;
ahlervin 6:7bbcdd07bc2d 83 }
ahlervin 6:7bbcdd07bc2d 84 float Regler :: get_SpeedL (){
ahlervin 6:7bbcdd07bc2d 85 SpeedL = SpeedL;
ahlervin 6:7bbcdd07bc2d 86 //printf("SpeedL2 = %f\n",SpeedL);
ahlervin 6:7bbcdd07bc2d 87 return SpeedL;
ahlervin 6:7bbcdd07bc2d 88 }
ahlervin 6:7bbcdd07bc2d 89
ahlervin 6:7bbcdd07bc2d 90
ahlervin 6:7bbcdd07bc2d 91
ahlervin 6:7bbcdd07bc2d 92
ahlervin 6:7bbcdd07bc2d 93
ahlervin 6:7bbcdd07bc2d 94