Dependencies: Farbsensor IRSensorLib PID_Control Servo mbed PixyLib
Farbauswertung.cpp@15:26dbcd6ff48d, 2017-05-24 (annotated)
- Committer:
- ZHAW_Prometheus
- Date:
- Wed May 24 13:54:02 2017 +0000
- Revision:
- 15:26dbcd6ff48d
- Parent:
- 14:bee8101aad45
- Child:
- 16:ad45ef4fee04
Vers. 24.05.2017 15:50
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 | 12:472b26872a42 | 6 | //Konstruktor |
ZHAW_Prometheus | 15:26dbcd6ff48d | 7 | Farbauswertung::Farbauswertung(AnalogIn* SensorG, AnalogIn* SensorR, Servo& _servoAusw, Servo& _servoFoerder, int verzEin, int verzAus) : servoAusw(_servoAusw), servoFoerder(_servoFoerder) |
ZHAW_Prometheus | 0:422088ad7fc5 | 8 | { |
ZHAW_Prometheus | 0:422088ad7fc5 | 9 | this->SensorG = SensorG; |
ZHAW_Prometheus | 0:422088ad7fc5 | 10 | this->SensorR = SensorR; |
ZHAW_Prometheus | 13:7ce78048b733 | 11 | m_verzEin = verzEin; |
ZHAW_Prometheus | 13:7ce78048b733 | 12 | m_verzAus = verzAus; |
ZHAW_Prometheus | 13:7ce78048b733 | 13 | p1 = 0; |
ZHAW_Prometheus | 13:7ce78048b733 | 14 | p2 = merkerSize - verzEin; |
ZHAW_Prometheus | 0:422088ad7fc5 | 15 | farbsensor.init(SensorG, SensorR); |
schuema4 | 3:017c85c4b14b | 16 | zustand = gruen; |
ZHAW_Prometheus | 13:7ce78048b733 | 17 | memset(merker, 0, merkerSize); |
ZHAW_Prometheus | 0:422088ad7fc5 | 18 | } |
ZHAW_Prometheus | 0:422088ad7fc5 | 19 | |
ZHAW_Prometheus | 12:472b26872a42 | 20 | //Destruktor |
ZHAW_Prometheus | 12:472b26872a42 | 21 | Farbauswertung::~Farbauswertung() {} |
ZHAW_Prometheus | 12:472b26872a42 | 22 | |
ZHAW_Prometheus | 15:26dbcd6ff48d | 23 | void Farbauswertung::setSerialOutput(Serial *pc) |
ZHAW_Prometheus | 15:26dbcd6ff48d | 24 | { |
ZHAW_Prometheus | 0:422088ad7fc5 | 25 | this->pc = pc; |
ZHAW_Prometheus | 0:422088ad7fc5 | 26 | } |
ZHAW_Prometheus | 0:422088ad7fc5 | 27 | |
ZHAW_Prometheus | 12:472b26872a42 | 28 | //Methoden |
ZHAW_Prometheus | 15:26dbcd6ff48d | 29 | void Farbauswertung::printState() |
ZHAW_Prometheus | 15:26dbcd6ff48d | 30 | { |
ZHAW_Prometheus | 12:472b26872a42 | 31 | if (pc) { |
ZHAW_Prometheus | 13:7ce78048b733 | 32 | pc->printf("Gruen: %f\tRot: %f\tStatus: %d\n\r", farbsensor.readg(), farbsensor.readr(), zustand); |
ZHAW_Prometheus | 12:472b26872a42 | 33 | } |
ZHAW_Prometheus | 12:472b26872a42 | 34 | } |
ZHAW_Prometheus | 12:472b26872a42 | 35 | |
ZHAW_Prometheus | 15:26dbcd6ff48d | 36 | void Farbauswertung::auswertung() |
ZHAW_Prometheus | 15:26dbcd6ff48d | 37 | { |
ZHAW_Prometheus | 15:26dbcd6ff48d | 38 | servoFoerder = 0.2f; |
ZHAW_Prometheus | 15:26dbcd6ff48d | 39 | |
ZHAW_Prometheus | 15:26dbcd6ff48d | 40 | valInG = farbsensor.readg(); |
ZHAW_Prometheus | 15:26dbcd6ff48d | 41 | valInR = farbsensor.readr(); |
ZHAW_Prometheus | 15:26dbcd6ff48d | 42 | //Zustände am Farbsensor ins Array schreiben |
ZHAW_Prometheus | 15:26dbcd6ff48d | 43 | if ((valInG=68.0 && valInG<=70.0 && valInR>=70.0 && valInR<=71.6)|| (valInG>=77.2 && valInG<=77.39 && valInR>=75.0 && valInR<=77.0) || (valInG>=75.0 && valInG<=76.0 && valInR>=73.0 && valInR<=74.0)) { |
ZHAW_Prometheus | 15:26dbcd6ff48d | 44 | zustand = rot; |
ZHAW_Prometheus | 15:26dbcd6ff48d | 45 | } else { |
ZHAW_Prometheus | 15:26dbcd6ff48d | 46 | zustand = gruen; |
ZHAW_Prometheus | 15:26dbcd6ff48d | 47 | } |
ZHAW_Prometheus | 15:26dbcd6ff48d | 48 | merker[p1] = zustand; |
ZHAW_Prometheus | 15:26dbcd6ff48d | 49 | if (p1 < (merkerSize-1)) { |
ZHAW_Prometheus | 15:26dbcd6ff48d | 50 | p1++; |
ZHAW_Prometheus | 15:26dbcd6ff48d | 51 | } else { |
ZHAW_Prometheus | 15:26dbcd6ff48d | 52 | p1 = 0; |
ZHAW_Prometheus | 15:26dbcd6ff48d | 53 | } |
ZHAW_Prometheus | 15:26dbcd6ff48d | 54 | |
ZHAW_Prometheus | 15:26dbcd6ff48d | 55 | //Zustände an der Klappe abrufen und Klappe öffnen und schliessen |
ZHAW_Prometheus | 15:26dbcd6ff48d | 56 | sum = 0; |
ZHAW_Prometheus | 15:26dbcd6ff48d | 57 | for (uint16_t i = 0; i<m_verzAus; i++) { |
ZHAW_Prometheus | 15:26dbcd6ff48d | 58 | if ((p2+i) < merkerSize) { |
ZHAW_Prometheus | 15:26dbcd6ff48d | 59 | sum += merker[p2+i]; |
ZHAW_Prometheus | 13:7ce78048b733 | 60 | } else { |
ZHAW_Prometheus | 15:26dbcd6ff48d | 61 | sum += merker[p2+i-merkerSize]; |
ZHAW_Prometheus | 1:5c44e2462a8b | 62 | } |
ZHAW_Prometheus | 15:26dbcd6ff48d | 63 | } |
ZHAW_Prometheus | 15:26dbcd6ff48d | 64 | if (p2 < (merkerSize-1)) { |
ZHAW_Prometheus | 15:26dbcd6ff48d | 65 | p2++; |
ZHAW_Prometheus | 13:7ce78048b733 | 66 | } else { |
ZHAW_Prometheus | 15:26dbcd6ff48d | 67 | p2 = 0; |
schuema4 | 3:017c85c4b14b | 68 | } |
ZHAW_Prometheus | 15:26dbcd6ff48d | 69 | if (sum > 0) { |
ZHAW_Prometheus | 15:26dbcd6ff48d | 70 | servoAusw = 1.0f; |
ZHAW_Prometheus | 15:26dbcd6ff48d | 71 | } else { |
ZHAW_Prometheus | 15:26dbcd6ff48d | 72 | servoAusw = 0.01f; |
ZHAW_Prometheus | 15:26dbcd6ff48d | 73 | } |
ZHAW_Prometheus | 13:7ce78048b733 | 74 | } |