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