![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
PM_2
main.cpp@0:a0ac3b80cd08, 2021-05-13 (annotated)
- Committer:
- balsijoe
- Date:
- Thu May 13 11:16:31 2021 +0000
- Revision:
- 0:a0ac3b80cd08
PM_2 Futterstation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
balsijoe | 0:a0ac3b80cd08 | 1 | /* |
balsijoe | 0:a0ac3b80cd08 | 2 | Projekt Fütterungsstation |
balsijoe | 0:a0ac3b80cd08 | 3 | Name: main.cpp |
balsijoe | 0:a0ac3b80cd08 | 4 | Datum: 26.04.21 |
balsijoe | 0:a0ac3b80cd08 | 5 | Autor: Joel Balsiger, Simon Künzli |
balsijoe | 0:a0ac3b80cd08 | 6 | */ |
balsijoe | 0:a0ac3b80cd08 | 7 | |
balsijoe | 0:a0ac3b80cd08 | 8 | //Include von Files |
balsijoe | 0:a0ac3b80cd08 | 9 | #include "mbed.h" |
balsijoe | 0:a0ac3b80cd08 | 10 | #include "platform/mbed_thread.h" |
balsijoe | 0:a0ac3b80cd08 | 11 | #include <ttmathbig.h> |
balsijoe | 0:a0ac3b80cd08 | 12 | |
balsijoe | 0:a0ac3b80cd08 | 13 | //Vorlage Zeit & Buttons |
balsijoe | 0:a0ac3b80cd08 | 14 | using namespace std::chrono; |
balsijoe | 0:a0ac3b80cd08 | 15 | |
balsijoe | 0:a0ac3b80cd08 | 16 | InterruptIn user_button(USER_BUTTON); |
balsijoe | 0:a0ac3b80cd08 | 17 | DigitalOut led(LED1); |
balsijoe | 0:a0ac3b80cd08 | 18 | |
balsijoe | 0:a0ac3b80cd08 | 19 | bool executeMainTask = false; |
balsijoe | 0:a0ac3b80cd08 | 20 | Timer user_button_timer, loop_timer; |
balsijoe | 0:a0ac3b80cd08 | 21 | int Ts_ms = 50; |
balsijoe | 0:a0ac3b80cd08 | 22 | |
balsijoe | 0:a0ac3b80cd08 | 23 | void button_fall(); |
balsijoe | 0:a0ac3b80cd08 | 24 | void button_rise(); |
balsijoe | 0:a0ac3b80cd08 | 25 | |
balsijoe | 0:a0ac3b80cd08 | 26 | //Peripherie |
balsijoe | 0:a0ac3b80cd08 | 27 | AnalogIn analog_in_PA_0(PA_0); //FS Futter Behälter Ultraschall |
balsijoe | 0:a0ac3b80cd08 | 28 | AnalogIn analog_in_PA_1(PA_1); //FS Wasser Behälter Ultraschall |
balsijoe | 0:a0ac3b80cd08 | 29 | AnalogIn analog_in_PA_2(PA_2); //FS Futter Napf Waage |
balsijoe | 0:a0ac3b80cd08 | 30 | AnalogIn analog_in_PA_3(PA_3); //FS Wasser Napf Wasserstand |
balsijoe | 0:a0ac3b80cd08 | 31 | AnalogOut analog_out_PA_4(PA_4); //Futterausgabe DC-Motor |
balsijoe | 0:a0ac3b80cd08 | 32 | AnalogOut analog_out_PA_5(PA_5); //Wasserausgabe Ventil |
balsijoe | 0:a0ac3b80cd08 | 33 | DigitalOut Low_Level_PA_6(PA_6); //Warn-LED Füllstand |
balsijoe | 0:a0ac3b80cd08 | 34 | |
balsijoe | 0:a0ac3b80cd08 | 35 | //Funktionen |
balsijoe | 0:a0ac3b80cd08 | 36 | int Food_Bowl_Check(); //Futterstand auslesen |
balsijoe | 0:a0ac3b80cd08 | 37 | |
balsijoe | 0:a0ac3b80cd08 | 38 | void Give_Out_Food(int max_Food); //Futter ausgeben bis Limit erreicht |
balsijoe | 0:a0ac3b80cd08 | 39 | |
balsijoe | 0:a0ac3b80cd08 | 40 | int Water_Bowl_Check(); //Wasserstand auslesen |
balsijoe | 0:a0ac3b80cd08 | 41 | |
balsijoe | 0:a0ac3b80cd08 | 42 | void Give_Out_Water(int max_Water); //Wasser ausgeben bis Limit erreicht |
balsijoe | 0:a0ac3b80cd08 | 43 | |
balsijoe | 0:a0ac3b80cd08 | 44 | int Get_FS_Food(); //Füllstand Futtrbehälter auslesen |
balsijoe | 0:a0ac3b80cd08 | 45 | |
balsijoe | 0:a0ac3b80cd08 | 46 | int Get_FS_Water(); //Füllstand Futtrbehälter auslesen |
balsijoe | 0:a0ac3b80cd08 | 47 | |
balsijoe | 0:a0ac3b80cd08 | 48 | //Main Programm |
balsijoe | 0:a0ac3b80cd08 | 49 | int main() |
balsijoe | 0:a0ac3b80cd08 | 50 | { |
balsijoe | 0:a0ac3b80cd08 | 51 | //Vorlage Zeit & Buttons |
balsijoe | 0:a0ac3b80cd08 | 52 | user_button.fall(&button_fall); |
balsijoe | 0:a0ac3b80cd08 | 53 | user_button.rise(&button_rise); |
balsijoe | 0:a0ac3b80cd08 | 54 | loop_timer.start(); |
balsijoe | 0:a0ac3b80cd08 | 55 | |
balsijoe | 0:a0ac3b80cd08 | 56 | //Initialisierung |
balsijoe | 0:a0ac3b80cd08 | 57 | int FoodOut = 0, WaterOut = 0; |
balsijoe | 0:a0ac3b80cd08 | 58 | int Refill_Pressed = 1; //Taste für Nachfüllen |
balsijoe | 0:a0ac3b80cd08 | 59 | int max_Food = 80; //Limit in % Futtermenge |
balsijoe | 0:a0ac3b80cd08 | 60 | int max_Water = 80; //Limit in % Wassermenge |
balsijoe | 0:a0ac3b80cd08 | 61 | int FS_Food, FS_Water; //Füllstand Behälter in % |
balsijoe | 0:a0ac3b80cd08 | 62 | int Warning_Low_Level = 0; //Warnung tiefer Füllstand |
balsijoe | 0:a0ac3b80cd08 | 63 | double Time_Counter = 0; //Anzahl x * 50ms |
balsijoe | 0:a0ac3b80cd08 | 64 | double Time_Interval = 8; //Futterauffüllung nach x Stunden |
balsijoe | 0:a0ac3b80cd08 | 65 | |
balsijoe | 0:a0ac3b80cd08 | 66 | //Endlosschlaufe |
balsijoe | 0:a0ac3b80cd08 | 67 | while(1) |
balsijoe | 0:a0ac3b80cd08 | 68 | { |
balsijoe | 0:a0ac3b80cd08 | 69 | loop_timer.reset(); |
balsijoe | 0:a0ac3b80cd08 | 70 | |
balsijoe | 0:a0ac3b80cd08 | 71 | //Start |
balsijoe | 0:a0ac3b80cd08 | 72 | //Telegram Internetansteuerung |
balsijoe | 0:a0ac3b80cd08 | 73 | |
balsijoe | 0:a0ac3b80cd08 | 74 | //Tastenabfrage |
balsijoe | 0:a0ac3b80cd08 | 75 | //Check Taster |
balsijoe | 0:a0ac3b80cd08 | 76 | if(Refill_Pressed == 0) { |
balsijoe | 0:a0ac3b80cd08 | 77 | FoodOut = 1; |
balsijoe | 0:a0ac3b80cd08 | 78 | WaterOut = 1; |
balsijoe | 0:a0ac3b80cd08 | 79 | } |
balsijoe | 0:a0ac3b80cd08 | 80 | |
balsijoe | 0:a0ac3b80cd08 | 81 | //Futterausgabe |
balsijoe | 0:a0ac3b80cd08 | 82 | if(FoodOut == 1){ |
balsijoe | 0:a0ac3b80cd08 | 83 | Give_Out_Food(max_Food); |
balsijoe | 0:a0ac3b80cd08 | 84 | FoodOut = 0; |
balsijoe | 0:a0ac3b80cd08 | 85 | } |
balsijoe | 0:a0ac3b80cd08 | 86 | |
balsijoe | 0:a0ac3b80cd08 | 87 | //Wasserausgabe |
balsijoe | 0:a0ac3b80cd08 | 88 | if(WaterOut == 1){ |
balsijoe | 0:a0ac3b80cd08 | 89 | Give_Out_Water(max_Water); |
balsijoe | 0:a0ac3b80cd08 | 90 | WaterOut = 0; |
balsijoe | 0:a0ac3b80cd08 | 91 | } |
balsijoe | 0:a0ac3b80cd08 | 92 | |
balsijoe | 0:a0ac3b80cd08 | 93 | //Füllstand aktualisieren |
balsijoe | 0:a0ac3b80cd08 | 94 | FS_Food = Get_FS_Food(); |
balsijoe | 0:a0ac3b80cd08 | 95 | FS_Water = Get_FS_Water(); |
balsijoe | 0:a0ac3b80cd08 | 96 | |
balsijoe | 0:a0ac3b80cd08 | 97 | //Füllstand Warnung |
balsijoe | 0:a0ac3b80cd08 | 98 | if(FS_Food <= 10 || FS_Water <= 10){ |
balsijoe | 0:a0ac3b80cd08 | 99 | Low_Level_PA_6.write(1); //Warn-LED ein |
balsijoe | 0:a0ac3b80cd08 | 100 | Warning_Low_Level = 1; |
balsijoe | 0:a0ac3b80cd08 | 101 | } else{ |
balsijoe | 0:a0ac3b80cd08 | 102 | Low_Level_PA_6.write(0); //Warn-LED aus |
balsijoe | 0:a0ac3b80cd08 | 103 | Warning_Low_Level = 0; |
balsijoe | 0:a0ac3b80cd08 | 104 | } |
balsijoe | 0:a0ac3b80cd08 | 105 | //Zeitinterval Futterauffüllung |
balsijoe | 0:a0ac3b80cd08 | 106 | if(Time_Counter >= Time_Interval *60 *60 *20){ |
balsijoe | 0:a0ac3b80cd08 | 107 | FoodOut = 1; |
balsijoe | 0:a0ac3b80cd08 | 108 | WaterOut = 1; |
balsijoe | 0:a0ac3b80cd08 | 109 | Time_Counter = 0; |
balsijoe | 0:a0ac3b80cd08 | 110 | } |
balsijoe | 0:a0ac3b80cd08 | 111 | Time_Counter++; |
balsijoe | 0:a0ac3b80cd08 | 112 | |
balsijoe | 0:a0ac3b80cd08 | 113 | //Stop |
balsijoe | 0:a0ac3b80cd08 | 114 | int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count(); |
balsijoe | 0:a0ac3b80cd08 | 115 | int dT_loop_ms = Ts_ms - T_loop_ms; |
balsijoe | 0:a0ac3b80cd08 | 116 | thread_sleep_for(dT_loop_ms); |
balsijoe | 0:a0ac3b80cd08 | 117 | } |
balsijoe | 0:a0ac3b80cd08 | 118 | } |
balsijoe | 0:a0ac3b80cd08 | 119 | |
balsijoe | 0:a0ac3b80cd08 | 120 | //Funktionen |
balsijoe | 0:a0ac3b80cd08 | 121 | int Food_Bowl_Check(){ //Futterstand auslesen |
balsijoe | 0:a0ac3b80cd08 | 122 | int Bowl_Level = 30, V_In, R_FSR, R = 100000, V_plus = 5; |
balsijoe | 0:a0ac3b80cd08 | 123 | //Bowl_Level = Drucksensor Ausgabe |
balsijoe | 0:a0ac3b80cd08 | 124 | //V_In = analog_in_PA_2.read() * 5; //0-1 |
balsijoe | 0:a0ac3b80cd08 | 125 | //R_FSR = (V_plus / V_In) * R - R //0-R |
balsijoe | 0:a0ac3b80cd08 | 126 | Bowl_Level = round(analog_in_PA_2.read() * 100); //0-100 |
balsijoe | 0:a0ac3b80cd08 | 127 | return Bowl_Level; |
balsijoe | 0:a0ac3b80cd08 | 128 | } |
balsijoe | 0:a0ac3b80cd08 | 129 | |
balsijoe | 0:a0ac3b80cd08 | 130 | void Give_Out_Food(int max_Food){ //Futter ausgeben bis Limit erreicht |
balsijoe | 0:a0ac3b80cd08 | 131 | while (Food_Bowl_Check() <= max_Food){ |
balsijoe | 0:a0ac3b80cd08 | 132 | //DC Motor drehen |
balsijoe | 0:a0ac3b80cd08 | 133 | analog_out_PA_4.write(1); //0-1 -> 0-5V |
balsijoe | 0:a0ac3b80cd08 | 134 | } |
balsijoe | 0:a0ac3b80cd08 | 135 | //DC Motor stoppen |
balsijoe | 0:a0ac3b80cd08 | 136 | analog_out_PA_4.write(0); |
balsijoe | 0:a0ac3b80cd08 | 137 | } |
balsijoe | 0:a0ac3b80cd08 | 138 | |
balsijoe | 0:a0ac3b80cd08 | 139 | int Water_Bowl_Check(){ //Wasserstand auslesen |
balsijoe | 0:a0ac3b80cd08 | 140 | int Bowl_Level = 100; |
balsijoe | 0:a0ac3b80cd08 | 141 | //Bowl_Level = Füllstand Ausgabe |
balsijoe | 0:a0ac3b80cd08 | 142 | Bowl_Level = round(analog_in_PA_3.read() * 100); //0-1 * 100 |
balsijoe | 0:a0ac3b80cd08 | 143 | return Bowl_Level; |
balsijoe | 0:a0ac3b80cd08 | 144 | } |
balsijoe | 0:a0ac3b80cd08 | 145 | |
balsijoe | 0:a0ac3b80cd08 | 146 | void Give_Out_Water(int max_Water){ //Wasser ausgeben bis Limit erreicht |
balsijoe | 0:a0ac3b80cd08 | 147 | while (Water_Bowl_Check() <= max_Water){ |
balsijoe | 0:a0ac3b80cd08 | 148 | //Ventil Öffnen |
balsijoe | 0:a0ac3b80cd08 | 149 | analog_out_PA_5.write(1); //0-1 -> 0-5V |
balsijoe | 0:a0ac3b80cd08 | 150 | } |
balsijoe | 0:a0ac3b80cd08 | 151 | //Ventil Schliessen |
balsijoe | 0:a0ac3b80cd08 | 152 | analog_out_PA_5.write(0); |
balsijoe | 0:a0ac3b80cd08 | 153 | } |
balsijoe | 0:a0ac3b80cd08 | 154 | |
balsijoe | 0:a0ac3b80cd08 | 155 | int Get_FS_Food(){ //Füllstand Futtrbehälter auslesen |
balsijoe | 0:a0ac3b80cd08 | 156 | int Level, max_distance = 120; //distance in mm |
balsijoe | 0:a0ac3b80cd08 | 157 | //Level = 100% - Ultraschallsensor in % |
balsijoe | 0:a0ac3b80cd08 | 158 | Level = 100 - round(analog_in_PA_0.read() *3.3f / max_distance * 100); //100% -(x * 3.3 / max * 100) |
balsijoe | 0:a0ac3b80cd08 | 159 | return Level; |
balsijoe | 0:a0ac3b80cd08 | 160 | } |
balsijoe | 0:a0ac3b80cd08 | 161 | |
balsijoe | 0:a0ac3b80cd08 | 162 | int Get_FS_Water(){ //Füllstand Wasserbehälter auslesen |
balsijoe | 0:a0ac3b80cd08 | 163 | int Level, max_distance = 120; //distance in mm |
balsijoe | 0:a0ac3b80cd08 | 164 | //Level = 100% - Ultraschallsensor in % |
balsijoe | 0:a0ac3b80cd08 | 165 | Level = 100 - round(analog_in_PA_1.read() *3.3f / max_distance * 100); //100% -(x * 3.3 / max * 100) |
balsijoe | 0:a0ac3b80cd08 | 166 | return Level; |
balsijoe | 0:a0ac3b80cd08 | 167 | } |
balsijoe | 0:a0ac3b80cd08 | 168 | |
balsijoe | 0:a0ac3b80cd08 | 169 | void button_fall() |
balsijoe | 0:a0ac3b80cd08 | 170 | { |
balsijoe | 0:a0ac3b80cd08 | 171 | user_button_timer.reset(); |
balsijoe | 0:a0ac3b80cd08 | 172 | user_button_timer.start(); |
balsijoe | 0:a0ac3b80cd08 | 173 | } |
balsijoe | 0:a0ac3b80cd08 | 174 | |
balsijoe | 0:a0ac3b80cd08 | 175 | void button_rise() |
balsijoe | 0:a0ac3b80cd08 | 176 | { |
balsijoe | 0:a0ac3b80cd08 | 177 | int t_button = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count(); |
balsijoe | 0:a0ac3b80cd08 | 178 | user_button_timer.stop(); |
balsijoe | 0:a0ac3b80cd08 | 179 | if(t_button > 200) executeMainTask = !executeMainTask; |
balsijoe | 0:a0ac3b80cd08 | 180 | } |