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.
Dependencies: Farbsensor IRSensorLib PID_Control Servo mbed PixyLib
Farbauswertung.cpp@9:b83994ef4b08, 2017-05-19 (annotated)
- Committer:
- ZHAW_Prometheus
- Date:
- Fri May 19 12:30:42 2017 +0000
- Revision:
- 9:b83994ef4b08
- Parent:
- 8:077d0bb213a2
- Child:
- 12:472b26872a42
Vers. 19.05.2017 14:30
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ZHAW_Prometheus | 0:422088ad7fc5 | 1 | #include "mbed.h" |
ZHAW_Prometheus | 0:422088ad7fc5 | 2 | #include "cstdlib" |
ZHAW_Prometheus | 0:422088ad7fc5 | 3 | //#include <cmath> |
ZHAW_Prometheus | 0:422088ad7fc5 | 4 | #include "Farbauswertung.h" |
ZHAW_Prometheus | 0:422088ad7fc5 | 5 | |
ZHAW_Prometheus | 0:422088ad7fc5 | 6 | |
ZHAW_Prometheus | 0:422088ad7fc5 | 7 | Farbauswertung::Farbauswertung(AnalogIn* SensorG, AnalogIn* SensorR, Servo* servoAusw) |
ZHAW_Prometheus | 0:422088ad7fc5 | 8 | { |
ZHAW_Prometheus | 0:422088ad7fc5 | 9 | init(SensorG, SensorR, servoAusw); |
ZHAW_Prometheus | 0:422088ad7fc5 | 10 | } |
ZHAW_Prometheus | 0:422088ad7fc5 | 11 | |
ZHAW_Prometheus | 0:422088ad7fc5 | 12 | |
ZHAW_Prometheus | 0:422088ad7fc5 | 13 | /** |
ZHAW_Prometheus | 0:422088ad7fc5 | 14 | * Deletes the IRSensor object. |
ZHAW_Prometheus | 0:422088ad7fc5 | 15 | */ |
ZHAW_Prometheus | 0:422088ad7fc5 | 16 | Farbauswertung::~Farbauswertung() {} |
ZHAW_Prometheus | 0:422088ad7fc5 | 17 | |
ZHAW_Prometheus | 0:422088ad7fc5 | 18 | |
ZHAW_Prometheus | 0:422088ad7fc5 | 19 | void Farbauswertung::init(AnalogIn* SensorG, AnalogIn* SensorR, Servo* servoAusw) |
ZHAW_Prometheus | 0:422088ad7fc5 | 20 | { |
ZHAW_Prometheus | 0:422088ad7fc5 | 21 | this->SensorG = SensorG; |
ZHAW_Prometheus | 0:422088ad7fc5 | 22 | this->SensorR = SensorR; |
ZHAW_Prometheus | 0:422088ad7fc5 | 23 | this->servoAusw = servoAusw; |
ZHAW_Prometheus | 0:422088ad7fc5 | 24 | farbsensor.init(SensorG, SensorR); |
schuema4 | 3:017c85c4b14b | 25 | zustand = gruen; |
schuema4 | 3:017c85c4b14b | 26 | merker_rot = 0; |
schuema4 | 3:017c85c4b14b | 27 | merker_rot1 = 0; |
schuema4 | 3:017c85c4b14b | 28 | merker_gruen =0; |
ZHAW_Prometheus | 1:5c44e2462a8b | 29 | ausschaltZeit = 0; |
ZHAW_Prometheus | 8:077d0bb213a2 | 30 | einschlatZeit = 50; |
ZHAW_Prometheus | 0:422088ad7fc5 | 31 | } |
ZHAW_Prometheus | 0:422088ad7fc5 | 32 | |
ZHAW_Prometheus | 0:422088ad7fc5 | 33 | void Farbauswertung::setSerialOutput(Serial *pc) |
ZHAW_Prometheus | 0:422088ad7fc5 | 34 | { |
ZHAW_Prometheus | 0:422088ad7fc5 | 35 | this->pc = pc; |
ZHAW_Prometheus | 0:422088ad7fc5 | 36 | } |
ZHAW_Prometheus | 0:422088ad7fc5 | 37 | |
schuema4 | 3:017c85c4b14b | 38 | int Farbauswertung::getState() |
schuema4 | 3:017c85c4b14b | 39 | { |
ZHAW_Prometheus | 0:422088ad7fc5 | 40 | return zustand; |
ZHAW_Prometheus | 0:422088ad7fc5 | 41 | } |
ZHAW_Prometheus | 0:422088ad7fc5 | 42 | |
schuema4 | 3:017c85c4b14b | 43 | void Farbauswertung::auswertung() |
schuema4 | 3:017c85c4b14b | 44 | { |
ZHAW_Prometheus | 8:077d0bb213a2 | 45 | merker_gruen=0; |
ZHAW_Prometheus | 8:077d0bb213a2 | 46 | if ((farbsensor.readg()>=68 && farbsensor.readg()<=70 && farbsensor.readr()>=70 && farbsensor.readr()<=71.6)|| (farbsensor.readg()>=77.2 && farbsensor.readg()<=77.39 && farbsensor.readr()>=75 && farbsensor.readr()<=77) || (farbsensor.readg()>=75 && farbsensor.readg()<=76 && farbsensor.readr()>=73 && farbsensor.readr()<=74)) { |
schuema4 | 3:017c85c4b14b | 47 | |
ZHAW_Prometheus | 8:077d0bb213a2 | 48 | merker_gruen=1; |
ZHAW_Prometheus | 8:077d0bb213a2 | 49 | |
ZHAW_Prometheus | 8:077d0bb213a2 | 50 | |
ZHAW_Prometheus | 8:077d0bb213a2 | 51 | if (zustand ==1 && ausschaltZeit ==0) { //Wenn wechsel Grün auf Rot Ausschaltzeit um X erhöhen |
ZHAW_Prometheus | 8:077d0bb213a2 | 52 | ausschaltZeit+=180; |
ZHAW_Prometheus | 8:077d0bb213a2 | 53 | |
ZHAW_Prometheus | 8:077d0bb213a2 | 54 | zustand =0; |
ZHAW_Prometheus | 1:5c44e2462a8b | 55 | } |
ZHAW_Prometheus | 8:077d0bb213a2 | 56 | if(zustand ==1 && ausschaltZeit !=0) { //Wenn die einschaltzeit nicht gleich Null ist muss die zeit plus die Einschaltverzögerung verlängert werden |
ZHAW_Prometheus | 8:077d0bb213a2 | 57 | ausschaltZeit+=(180); |
ZHAW_Prometheus | 8:077d0bb213a2 | 58 | |
ZHAW_Prometheus | 8:077d0bb213a2 | 59 | zustand =0; |
ZHAW_Prometheus | 8:077d0bb213a2 | 60 | |
ZHAW_Prometheus | 8:077d0bb213a2 | 61 | } |
ZHAW_Prometheus | 8:077d0bb213a2 | 62 | zustand =0; |
ZHAW_Prometheus | 8:077d0bb213a2 | 63 | wait(0.5); |
ZHAW_Prometheus | 8:077d0bb213a2 | 64 | |
ZHAW_Prometheus | 8:077d0bb213a2 | 65 | } |
ZHAW_Prometheus | 8:077d0bb213a2 | 66 | if(merker_gruen==0) { |
ZHAW_Prometheus | 8:077d0bb213a2 | 67 | |
ZHAW_Prometheus | 8:077d0bb213a2 | 68 | zustand=1; //Grünes oder gar kein Lego |
schuema4 | 3:017c85c4b14b | 69 | } |
ZHAW_Prometheus | 1:5c44e2462a8b | 70 | |
ZHAW_Prometheus | 8:077d0bb213a2 | 71 | if(zustand == 0 || (merker_rot >=1 && merker_rot < einschlatZeit)) { //Einschaltverzögerung |
schuema4 | 3:017c85c4b14b | 72 | merker_rot ++; |
schuema4 | 3:017c85c4b14b | 73 | } |
schuema4 | 3:017c85c4b14b | 74 | if (merker_rot == einschlatZeit) { //wenn einschaltzeit abgelaufen => MerkerRot1 setzen + MerkerRot zurücksetzen |
schuema4 | 3:017c85c4b14b | 75 | merker_rot1=1; |
schuema4 | 3:017c85c4b14b | 76 | merker_rot=0; |
schuema4 | 3:017c85c4b14b | 77 | } |
schuema4 | 3:017c85c4b14b | 78 | |
schuema4 | 3:017c85c4b14b | 79 | if(merker_rot1>=1 && merker_rot1<ausschaltZeit) { // Ausschaltverzögerung + Auswurf schliessen |
schuema4 | 3:017c85c4b14b | 80 | merker_rot1 ++; |
schuema4 | 6:d611637e7cad | 81 | |
schuema4 | 3:017c85c4b14b | 82 | servoAusw->write(1.0f); |
schuema4 | 3:017c85c4b14b | 83 | } |
schuema4 | 3:017c85c4b14b | 84 | if (merker_rot1 == ausschaltZeit) { //wenn Ausschaltzeit abgelaufen => MerkerRot1 zurücksetzen + Auswurf öffnen |
schuema4 | 3:017c85c4b14b | 85 | merker_rot1=0; |
schuema4 | 3:017c85c4b14b | 86 | ausschaltZeit=0; |
schuema4 | 3:017c85c4b14b | 87 | servoAusw->write(0.01f); |
schuema4 | 3:017c85c4b14b | 88 | } |
schuema4 | 3:017c85c4b14b | 89 | |
schuema4 | 6:d611637e7cad | 90 | static int timer = 0; |
schuema4 | 6:d611637e7cad | 91 | ++timer; |
schuema4 | 6:d611637e7cad | 92 | |
schuema4 | 6:d611637e7cad | 93 | if( timer % 100 == 0) { |
schuema4 | 6:d611637e7cad | 94 | //Ausgaben an Konsole****************************************************** |
schuema4 | 6:d611637e7cad | 95 | if (pc) { |
schuema4 | 6:d611637e7cad | 96 | pc->printf("\n\r"); |
ZHAW_Prometheus | 8:077d0bb213a2 | 97 | // pc->printf("Gruen:%f\n\r", farbsensor.readg()); |
ZHAW_Prometheus | 8:077d0bb213a2 | 98 | // pc->printf("Rot:%f\n\r", farbsensor.readr()); |
schuema4 | 6:d611637e7cad | 99 | pc->printf("Status:%d\n\r",zustand); |
ZHAW_Prometheus | 8:077d0bb213a2 | 100 | pc->printf("Status Merker gruen:%d\n\r",merker_gruen); |
schuema4 | 6:d611637e7cad | 101 | pc->printf("Status Merker rt:%d\n\r",merker_rot); |
schuema4 | 6:d611637e7cad | 102 | pc->printf("Status Merker rot1:%d\n\r",merker_rot1); |
schuema4 | 6:d611637e7cad | 103 | pc->printf("Ausschaltzeit:%d\n\r",ausschaltZeit); |
schuema4 | 6:d611637e7cad | 104 | } |
ZHAW_Prometheus | 1:5c44e2462a8b | 105 | } |
schuema4 | 3:017c85c4b14b | 106 | |
ZHAW_Prometheus | 0:422088ad7fc5 | 107 | } |