Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Versuch21 by
Classes/Spurhaltung.cpp@5:93d3efe46493, 2018-05-09 (annotated)
- Committer:
- baumgant
- Date:
- Wed May 09 13:33:59 2018 +0000
- Revision:
- 5:93d3efe46493
- Parent:
- 3:f44ef28cfb2d
PES2;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| baumgant | 0:b886f13e4ac6 | 1 | #include "Spurhaltung.h" |
| baumgant | 0:b886f13e4ac6 | 2 | #include "IRSensorG.h" |
| baumgant | 0:b886f13e4ac6 | 3 | |
| baumgant | 0:b886f13e4ac6 | 4 | using namespace std; |
| baumgant | 0:b886f13e4ac6 | 5 | |
| baumgant | 5:93d3efe46493 | 6 | const int Spurhaltung::DISTANCE = 100; |
| baumgant | 5:93d3efe46493 | 7 | const float Spurhaltung::SPEED = 100; |
| baumgant | 5:93d3efe46493 | 8 | const int Spurhaltung::DIFF = 1; |
| baumgant | 5:93d3efe46493 | 9 | const int Spurhaltung::DIFF2 = 120; |
| baumgant | 5:93d3efe46493 | 10 | const int Spurhaltung::DIFF3 = 180; |
| baumgant | 5:93d3efe46493 | 11 | const int Spurhaltung::OFFSET = 30; |
| baumgant | 5:93d3efe46493 | 12 | const int Spurhaltung::TOLERANCE1 = 8; |
| baumgant | 5:93d3efe46493 | 13 | const int Spurhaltung::TOLERANCE2 = -8; |
| baumgant | 5:93d3efe46493 | 14 | |
| baumgant | 0:b886f13e4ac6 | 15 | Spurhaltung::Spurhaltung(IRSensorG& Sensor2, IRSensorG& Sensor3): |
| baumgant | 0:b886f13e4ac6 | 16 | Sensor2(Sensor2), Sensor3(Sensor3) |
| baumgant | 0:b886f13e4ac6 | 17 | |
| baumgant | 0:b886f13e4ac6 | 18 | { |
| baumgant | 0:b886f13e4ac6 | 19 | this->sr = sr; |
| baumgant | 0:b886f13e4ac6 | 20 | this->sl = sl; |
| baumgant | 0:b886f13e4ac6 | 21 | } |
| baumgant | 0:b886f13e4ac6 | 22 | |
| baumgant | 0:b886f13e4ac6 | 23 | Spurhaltung::~Spurhaltung() {} |
| baumgant | 0:b886f13e4ac6 | 24 | |
| baumgant | 2:efa9a78591da | 25 | //------------------------------------------------------------------------------ |
| baumgant | 2:efa9a78591da | 26 | |
| baumgant | 0:b886f13e4ac6 | 27 | int Spurhaltung::speedl() |
| baumgant | 0:b886f13e4ac6 | 28 | { |
| baumgant | 5:93d3efe46493 | 29 | // Wand links und rechts |
| baumgant | 5:93d3efe46493 | 30 | if(abs(Sensor2.read()+OFFSET-Sensor3.read())<DIFF3) { |
| baumgant | 5:93d3efe46493 | 31 | int sl = Sensor3.read() * 1.825 - 30; |
| baumgant | 5:93d3efe46493 | 32 | printf(""); |
| baumgant | 5:93d3efe46493 | 33 | if(Sensor3.read() > (DISTANCE + 50)) { |
| baumgant | 5:93d3efe46493 | 34 | sl = SPEED; |
| baumgant | 5:93d3efe46493 | 35 | return sl; |
| baumgant | 5:93d3efe46493 | 36 | } else { |
| baumgant | 5:93d3efe46493 | 37 | return sl; |
| baumgant | 5:93d3efe46493 | 38 | } |
| baumgant | 0:b886f13e4ac6 | 39 | } |
| baumgant | 5:93d3efe46493 | 40 | // nur Wand links |
| baumgant | 5:93d3efe46493 | 41 | else if ((Sensor2.read() < DIFF2) and (Sensor3.read() > DIFF3)) { |
| baumgant | 5:93d3efe46493 | 42 | if ((Sensor2.read() - DISTANCE + OFFSET) > TOLERANCE1) { |
| baumgant | 5:93d3efe46493 | 43 | int sl = 80 - DIFF; |
| baumgant | 5:93d3efe46493 | 44 | //printf("Fridolin\r\n"); |
| baumgant | 5:93d3efe46493 | 45 | return sl; |
| baumgant | 5:93d3efe46493 | 46 | } else if ((Sensor2.read() - DISTANCE + OFFSET) < TOLERANCE2) { |
| baumgant | 5:93d3efe46493 | 47 | int sl = 80 + DIFF; |
| baumgant | 5:93d3efe46493 | 48 | return sl; |
| baumgant | 5:93d3efe46493 | 49 | } else { |
| baumgant | 5:93d3efe46493 | 50 | sl = SPEED; |
| baumgant | 5:93d3efe46493 | 51 | return sl; |
| baumgant | 5:93d3efe46493 | 52 | } |
| baumgant | 5:93d3efe46493 | 53 | } |
| baumgant | 5:93d3efe46493 | 54 | |
| baumgant | 5:93d3efe46493 | 55 | // nur Wand rechts |
| baumgant | 5:93d3efe46493 | 56 | else if ((Sensor3.read()<DIFF2) and (Sensor2.read()>DIFF3)) { |
| baumgant | 5:93d3efe46493 | 57 | if ((Sensor3.read() - DISTANCE) > TOLERANCE1) { |
| baumgant | 5:93d3efe46493 | 58 | sl = 80 + DIFF; |
| baumgant | 5:93d3efe46493 | 59 | //printf("Fridolin\r\n"); |
| baumgant | 5:93d3efe46493 | 60 | return sl; |
| baumgant | 5:93d3efe46493 | 61 | } else if ((Sensor3.read() - DISTANCE) < TOLERANCE2) { |
| baumgant | 5:93d3efe46493 | 62 | int sl = 80 - DIFF; |
| baumgant | 5:93d3efe46493 | 63 | return sl; |
| baumgant | 5:93d3efe46493 | 64 | } else { |
| baumgant | 5:93d3efe46493 | 65 | sl = SPEED; |
| baumgant | 5:93d3efe46493 | 66 | return sl; |
| baumgant | 5:93d3efe46493 | 67 | } |
| baumgant | 5:93d3efe46493 | 68 | } |
| baumgant | 5:93d3efe46493 | 69 | } |
| baumgant | 5:93d3efe46493 | 70 | |
| baumgant | 0:b886f13e4ac6 | 71 | |
| baumgant | 2:efa9a78591da | 72 | //------------------------------------------------------------------------------ |
| baumgant | 2:efa9a78591da | 73 | |
| mrbb2 | 1:6ef5bc60e69c | 74 | int Spurhaltung::speedr() |
| mrbb2 | 1:6ef5bc60e69c | 75 | { |
| baumgant | 5:93d3efe46493 | 76 | // Wand links und rechts |
| baumgant | 5:93d3efe46493 | 77 | if(abs(Sensor3.read()-Sensor2.read()+OFFSET)<DIFF3) { |
| baumgant | 5:93d3efe46493 | 78 | int sr = (Sensor2.read()) * 1.825; |
| baumgant | 5:93d3efe46493 | 79 | printf(""); |
| baumgant | 5:93d3efe46493 | 80 | if(Sensor2.read() > (DISTANCE - OFFSET + 50)) { |
| baumgant | 5:93d3efe46493 | 81 | sr = SPEED; |
| baumgant | 5:93d3efe46493 | 82 | return sr; |
| baumgant | 5:93d3efe46493 | 83 | } else { |
| baumgant | 5:93d3efe46493 | 84 | return sr; |
| baumgant | 5:93d3efe46493 | 85 | } |
| baumgant | 0:b886f13e4ac6 | 86 | } |
| baumgant | 5:93d3efe46493 | 87 | // nur Wand links |
| baumgant | 5:93d3efe46493 | 88 | else if ((Sensor2.read() < DIFF2) and (Sensor3.read() > DIFF3)) { |
| baumgant | 5:93d3efe46493 | 89 | if ((Sensor2.read() - DISTANCE + OFFSET) > TOLERANCE1) { |
| baumgant | 5:93d3efe46493 | 90 | int sr = 80 + DIFF; |
| baumgant | 5:93d3efe46493 | 91 | //printf("Karolina\r\n"); |
| baumgant | 5:93d3efe46493 | 92 | return sr; |
| baumgant | 5:93d3efe46493 | 93 | } else if ((Sensor2.read() - DISTANCE + OFFSET) < TOLERANCE2) { |
| baumgant | 5:93d3efe46493 | 94 | int sr = 80 - DIFF; |
| baumgant | 5:93d3efe46493 | 95 | return sr; |
| baumgant | 5:93d3efe46493 | 96 | } else { |
| baumgant | 5:93d3efe46493 | 97 | sr = SPEED; |
| baumgant | 5:93d3efe46493 | 98 | return sr; |
| baumgant | 5:93d3efe46493 | 99 | } |
| baumgant | 5:93d3efe46493 | 100 | } |
| baumgant | 5:93d3efe46493 | 101 | // nur Wand rechts |
| baumgant | 5:93d3efe46493 | 102 | else if((Sensor3.read() < DIFF2) and (Sensor2.read() > DIFF3)) { |
| baumgant | 5:93d3efe46493 | 103 | if ((Sensor3.read() - DISTANCE) > TOLERANCE1) { |
| baumgant | 5:93d3efe46493 | 104 | int sr = 80 - DIFF; |
| baumgant | 5:93d3efe46493 | 105 | //printf("Karolina\r\n"); |
| baumgant | 5:93d3efe46493 | 106 | return sr; |
| baumgant | 5:93d3efe46493 | 107 | } else if ((Sensor3.read() - DISTANCE) < TOLERANCE2) { |
| baumgant | 5:93d3efe46493 | 108 | int sr = 80 + DIFF; |
| baumgant | 5:93d3efe46493 | 109 | return sr; |
| baumgant | 5:93d3efe46493 | 110 | } else { |
| baumgant | 5:93d3efe46493 | 111 | sr = SPEED; |
| baumgant | 5:93d3efe46493 | 112 | return sr; |
| baumgant | 5:93d3efe46493 | 113 | } |
| baumgant | 5:93d3efe46493 | 114 | } |
| baumgant | 5:93d3efe46493 | 115 | } |
