Skip to content
Snippets Groups Projects
Commit 37663c34 authored by Armin Co's avatar Armin Co
Browse files

ss19

parents
Branches ss19 ss20
No related tags found
No related merge requests found
# Smart-Grid-Modell (SGM)
Das Smart-Grid-Modell ist Teil der digitalen Lernfabrik des Automatisierungslabors.
Dies ist der Stand der Software und der Dokumentation, nach dem Abschluss des Projektes im Sommersemester 2019.
Es ist der Ausgangspunkt für die Weiterentwicklungen im SS20.
File added
File added
File added
File added
File added
#include "Berechnung.h"
#include "I2C.h"
const int verbrauch[] = {98, 95, 93, 94, 95, 101, 115, 127, 132, 134, 136, 139, 138, 136, 134, 132, 130, 132, 132, 131, 125, 119, 114, 105, 98}; //Uhrzeitabhängigkeit
const int leistungwind[] = {0,3,25,82,174,321,532,815,1180,1612,1890,2000,2100}; //Windgeschwindigkeit abhängig Stromerzeugung
const int sonneuhrzeit[] = {0,0,0,0,0,0,3,12,30,52,73,88,97,100,98,91,81,66,46,25,10,2,0,0,0}; //Sonnenkraft nach Uhrzeit
uint32_t jahresverbrauch = 0;
uint32_t anzahlwind = 0;
uint32_t anzahlsonne = 0;
int verbauchkw = 0; //Aktuelle Verbrauch
int erzeugungwindkw = 0; //Aktuelle Stromerzeugung durch Wind in kWh
int erzeugungsonnekw = 0; //Aktuelle Stromerzeugung durch Solar in kWh
int windgeschwindigkeitms = 0;
int sonnenleistung = 0;
double anteil = 0; //Anteil reg. Energie am Gesamtverbrauch
int stunde = 0;
int minute = 0;
double anteilsolar = 0;
double anteilwind = 0;
//PWM
#define PIN_Motor 0x02
#define PIN_Lampe 0x03
#define PIN_Solar 0x04
#define PIN_Wind 0x05
#define PIN_Wind_Solar 0x06
#define PIN_Netz 0x07
#define PIN_Mix_gruen 0x08
#define PIN_Mix_rot 0x09
//Normale IOs
#define PIN_HaueserWeiss 0x10
#define PIN_Haus1_rot 47
#define PIN_Haus1_gruen 41
#define PIN_Haus2_rot 53
#define PIN_Haus2_gruen 45
#define PIN_Haus3_rot 51
#define PIN_Haus3_gruen 49
#define PIN_Haus4_rot 43
#define PIN_Haus4_gruen 37
#define PIN_Haus5_rot 39
#define PIN_Haus5_gruen 35
#define OFF 0x00
#define GRUEN 0x01
#define ROT 0x02
#define GELB 0x03
#define MAX_LUX 600
void berechneWindStrom(void)
{
erzeugungwindkw = 0;
if (windgeschwindigkeitms < 42)
erzeugungwindkw = 2100 * anzahlwind;
if (windgeschwindigkeitms < 13)
erzeugungwindkw = leistungwind[windgeschwindigkeitms]*anzahlwind;
}
void berechneSonneStrom(char modus)
{
if (modus == 0)
{
erzeugungsonnekw = (lux/MAX_LUX)*anzahlsonne*0.1;
}
else
erzeugungsonnekw = sonnenleistung*anzahlsonne*0.1/100;
}
void berechneAnteil (void)
{
anteil = ((double) erzeugungwindkw + (double) erzeugungsonnekw) / (double) verbauchkw * 100.0;
anteilsolar = (double) erzeugungsonnekw / (double) verbauchkw * 100.0;
anteilwind = (double) erzeugungwindkw / (double) verbauchkw * 100.0;
}
void berechneStromverbrauchProMinute()
{
int umrechnung = jahresverbrauch / 2887.0 / 365.0;
verbauchkw = umrechnung * verbrauch[stunde];
if (stunde == 23)
{
verbauchkw += (umrechnung *verbrauch[0] - umrechnung *verbrauch[stunde])/60.0*minute ;
}
else
{
verbauchkw += (umrechnung *verbrauch[stunde+1] - umrechnung *verbrauch[stunde])/60.0*minute ;
}
}
void LED_haus_anschluss(char hausNr, char farbe)
{
char pinG = 0;
char pinR = 0;
switch (hausNr)
{
case 1:
pinG = PIN_Haus1_gruen;
pinR = PIN_Haus1_rot;
break;
case 2:
pinG = PIN_Haus2_gruen;
pinR = PIN_Haus2_rot;
break;
case 3:
pinG = PIN_Haus3_gruen;
pinR = PIN_Haus3_rot;
break;
case 4:
pinG = PIN_Haus4_gruen;
pinR = PIN_Haus4_rot;
break;
case 5:
pinG = PIN_Haus5_gruen;
pinR = PIN_Haus5_rot;
break;
default:
break;
}
switch (farbe)
{
case OFF:
setOutputPin(pinG, 0);
setOutputPin(pinR, 0);
break;
case GRUEN:
setOutputPin(pinG, 1);
setOutputPin(pinR, 0);
break;
case GELB:
setOutputPin(pinG, 1);
setOutputPin(pinR, 1);
break;
case ROT:
setOutputPin(pinG, 0);
setOutputPin(pinR, 1);
break;
default:
break;
}
}
void update_LED_haus_anschluss(void)
{
if (anteil < 5) //5x rot
{
for (int i = 1; i <= 5; i++)
LED_haus_anschluss(i, ROT);
}
else if (anteil < 15) // 1x gelb, 4x rot
{
LED_haus_anschluss(1, GELB);
for (int i = 2; i <= 5; i++)
LED_haus_anschluss(i, ROT);
}
else if (anteil < 25) // 1x grün, 4x rot
{
LED_haus_anschluss(1, GRUEN);
for (int i = 2; i <= 5; i++)
LED_haus_anschluss(i, ROT);
}
else if (anteil < 35) // 1x grün, 1x gelb, 3x rot
{
LED_haus_anschluss(1, GRUEN);
LED_haus_anschluss(2, GELB);
for (int i = 3; i <= 5; i++)
LED_haus_anschluss(i, ROT);
}
else if (anteil < 45) // 2x grün, 3x rot
{
for (int i = 1; i <= 2; i++)
LED_haus_anschluss(i, GRUEN);
for (int i = 3; i <= 5; i++)
LED_haus_anschluss(i, ROT);
}
else if (anteil < 55) // 2x grün, 1x gelb, 2x rot
{
for (int i = 1; i <= 2; i++)
LED_haus_anschluss(i, GRUEN);
LED_haus_anschluss(3, GELB);
for (int i = 4; i <= 5; i++)
LED_haus_anschluss(i, ROT);
}
else if (anteil < 65) // 3x grün, 2x rot
{
for (int i = 1; i <= 3; i++)
LED_haus_anschluss(i, GRUEN);
for (int i = 4; i <= 5; i++)
LED_haus_anschluss(i, ROT);
}
else if (anteil < 75) // 3x grün, 1x gelb, 1x rot
{
for (int i = 1; i <= 3; i++)
LED_haus_anschluss(i, GRUEN);
LED_haus_anschluss(4, GELB);
LED_haus_anschluss(5, ROT);
}
else if (anteil < 85) // 4x grün, 1x rot
{
for (int i = 1; i <= 4; i++)
LED_haus_anschluss(i, GRUEN);
LED_haus_anschluss(5, ROT);
}
else if (anteil < 95) // 4x grün, 1x gelb
{
for (int i = 1; i <= 4; i++)
LED_haus_anschluss(i, GRUEN);
LED_haus_anschluss(5, GELB);
}
else // 5x grün
{
for (int i = 1; i <= 5; i++)
LED_haus_anschluss(i, GRUEN);
}
}
void update_LED_mixed(void)
{
int gruenSkaliert = anteil * 2.55;
if (gruenSkaliert > 255)
gruenSkaliert = 255;
int rotSkaliert = gruenSkaliert - 255;
if (anteil <= 1)
rotSkaliert = 255;
setOutputPin(PIN_Mix_gruen, gruenSkaliert);
setOutputPin(PIN_Mix_rot, rotSkaliert);
}
void update_Windrad_Motor(void)
{
if (windgeschwindigkeitms > 42 || windgeschwindigkeitms == 0)
setOutputPin(PIN_Motor, 0);
else if (windgeschwindigkeitms < 13 && windgeschwindigkeitms > 0)
{
int windSkaliert = 60;
setOutputPin(PIN_Motor, windSkaliert);
}
else
setOutputPin(PIN_Motor, 100);
}
void update_Lampe(void)
{
int sonneSkaliert = sonnenleistung * 2.55;
setOutputPin(PIN_Lampe, sonneSkaliert);
}
void update_LED_Netz(void)
{
int netzSkaliert = 0;
if (anteil <= 99)
netzSkaliert = (100 - anteil) * 2.55;
setOutputPin(PIN_Netz, netzSkaliert);
}
void update_LED_Solar(void)
{
int solarSkaliert = anteilsolar * 2.55;
if (solarSkaliert > 0)
solarSkaliert += 50;
if (solarSkaliert > 255)
solarSkaliert = 255;
setOutputPin(PIN_Solar, solarSkaliert);
}
void update_LED_Wind(void)
{
int windSkaliert = anteilwind * 2.55;
if (windSkaliert > 0)
windSkaliert += 50;
if (windSkaliert > 255)
windSkaliert = 255;
setOutputPin(PIN_Wind, windSkaliert);
}
void update_LED_WindSolar(void)
{
int windSolarSkaliert = anteil * 2.55;
if (windSolarSkaliert > 0)
windSolarSkaliert += 50;
if (windSolarSkaliert > 255)
windSolarSkaliert = 255;
setOutputPin(PIN_Wind_Solar, windSolarSkaliert);
}
void updateLEDSmartGrid(int aus)
{
if (aus == 0)
{
setOutputPin(PIN_Solar, 0);
setOutputPin(PIN_Mix_gruen, 0);
setOutputPin(PIN_Mix_rot, 0);
setOutputPin(PIN_Motor, 0);
setOutputPin(PIN_Netz, 0);
setOutputPin(PIN_Wind, 0);
setOutputPin(PIN_Wind_Solar, 0);
setOutputPin(PIN_Haus1_gruen, 0);
setOutputPin(PIN_Haus1_rot, 0);
setOutputPin(PIN_Haus2_gruen, 0);
setOutputPin(PIN_Haus2_rot, 0);
setOutputPin(PIN_Haus3_gruen, 0);
setOutputPin(PIN_Haus3_rot, 0);
setOutputPin(PIN_Haus4_gruen, 0);
setOutputPin(PIN_Haus4_rot, 0);
setOutputPin(PIN_Haus5_gruen, 0);
setOutputPin(PIN_Haus5_rot, 0);
}else
{
update_LED_haus_anschluss();
update_LED_mixed();
update_Windrad_Motor();
update_Lampe();
update_LED_Netz();
update_LED_Solar();
update_LED_Wind();
update_LED_WindSolar();
}
}
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
extern const int verbrauch[]; //Uhrzeitabhängigkeit
extern const int leistungwind[]; //Windgeschwindigkeit abhängig Stromerzeugung
extern const int sonneuhrzeit[]; //Sonnenkraft nach Uhrzeit
extern uint32_t jahresverbrauch;
extern uint32_t anzahlwind;
extern uint32_t anzahlsonne;
extern int verbauchkw; //Aktuelle Verbrauch
extern int erzeugungwindkw; //Aktuelle Stromerzeugung durch Wind in kWh
extern int erzeugungsonnekw; //Aktuelle Stromerzeugung durch Solar in kWh
extern int lux;
extern int windgeschwindigkeitms;
extern int sonnenleistung;
extern double anteil; //Anteil reg. Energie am Gesamtverbrauch
extern int stunde;
extern int minute;
extern void berechneWindStrom(void);
extern void berechneSonneStrom(char modus);
extern void berechneAnteil (void);
extern void berechneStromverbrauchProMinute(void);
extern void updateLEDSmartGrid(int aus);
//#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
//#include <fcntl.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include "I2C.h"
void setOutputPin(char pin, char value)
{
int file;
if ((file = wiringPiI2CSetup(AddressArduino)) < 0)
{
fprintf(stderr, "I2C: Failed to access %d\n", AddressArduino);
// return;
}
if (ioctl(file, I2C_SLAVE, AddressArduino) < 0)
{
fprintf(stderr, "I2C: Failed to acquire bus access/talk to slave 0x%x\n", AddressArduino);
//return;
}
unsigned char sendBytes[2];
sendBytes[0] = pin;
sendBytes[1] = value;
printf("%x, %x \n", sendBytes[0], sendBytes[1]);
if (write(file, sendBytes, 2) == 1) {
// As we are not talking to direct hardware but a microcontroller we
// need to wait a short while so that it can respond.
//
// 1ms seems to be enough but it depends on what workload it has
usleep(10000);
}
// Now wait else you could crash the arduino by sending requests too fast
usleep(10000);
close(file);
}
int getLuxValue()
{
//wiringPiSetupSys();
int handle = wiringPiI2CSetup(0x23);
wiringPiI2CWrite(handle, 0x10);
sleep(1);
int word = wiringPiI2CReadReg16(handle,0x00);
int lux = ((word & 0xff00)>>8) | ((word & 0x00ff)<<8);
if (lux == 65535)
{
printf("Sensor nicht gefunden");
//return -1;
}
printf("Beleuchtung beträgt %d lux\n", lux);
return lux +1;
}
#define AddressArduino 0x14
#define AddressLuxSensor 0x23
extern void setOutputPin(char pin, char value);
extern int getLuxValue();
int lux;
\ No newline at end of file
SOURCES = I2C.c Berechnung.c
INCLUDES = I2C.h Berechnung.h
LIBS = -lwiringPi -lm
smart: Smart02.c $(SOURCES) $(INCLUDES)
gcc -Wall $< $(SOURCES) $(LIBS) `pkg-config --cflags --libs gtk+-3.0` -export-dynamic -std=c99 -o $@
test: test.c I2C.c I2C.h
gcc -Wall $< I2C.c $(LIBS) -std=c99 -o $@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkAdjustment" id="adjustmentAnzahlSolar">
<property name="upper">100000</property>
<property name="step_increment">1</property>
<signal name="value-changed" handler="on_adjustmentAnzahlSolar_value_changed" swapped="no"/>
</object>
<object class="GtkAdjustment" id="adjustmentAnzahlWind">
<property name="upper">100</property>
<property name="step_increment">1</property>
<signal name="value-changed" handler="on_adjustmentAnzahlWind_value_changed" swapped="no"/>
</object>
<object class="GtkAdjustment" id="adjustmentEinwohner">
<property name="lower">1000</property>
<property name="upper">500000</property>
<property name="value">1000</property>
<property name="step_increment">1000</property>
<signal name="value-changed" handler="on_adjustmentEinwohner_value_changed" swapped="no"/>
</object>
<object class="GtkAdjustment" id="adjustmentIndustrie">
<property name="upper">50</property>
<property name="step_increment">1</property>
<signal name="value-changed" handler="on_adjustmentIndustrie_value_changed" swapped="no"/>
</object>
<object class="GtkWindow" id="windowEinstellungen">
<property name="can_focus">False</property>
<property name="window_position">center</property>
<property name="hide_titlebar_when_maximized">True</property>
<property name="deletable">False</property>
<signal name="destroy" handler="on_windowEinstellungen_destroy" swapped="no"/>
<child>
<object class="GtkFixed" id="fixed2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="buttonOK">
<property name="label" translatable="yes">OK</property>
<property name="width_request">80</property>
<property name="height_request">49</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_buttonOK_clicked" swapped="no"/>
</object>
<packing>
<property name="x">970</property>
<property name="y">423</property>
</packing>
</child>
<child>
<object class="GtkScale" id="scaleEinwohner">
<property name="width_request">570</property>
<property name="height_request">50</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_right">449</property>
<property name="adjustment">adjustmentEinwohner</property>
<property name="round_digits">0</property>
<property name="digits">0</property>
</object>
<packing>
<property name="x">160</property>
<property name="y">18</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelEinwohner">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Einwohner</property>
</object>
<packing>
<property name="x">35</property>
<property name="y">10</property>
</packing>
</child>
<child>
<object class="GtkScale" id="scaleIndustrie">
<property name="width_request">570</property>
<property name="height_request">50</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="adjustment">adjustmentIndustrie</property>
<property name="round_digits">0</property>
<property name="digits">0</property>
</object>
<packing>
<property name="x">160</property>
<property name="y">88</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelInfoVerbrauch">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
</object>
<packing>
<property name="x">730</property>
<property name="y">10</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelIndustire">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Anteil
Industrie</property>
</object>
<packing>
<property name="x">35</property>
<property name="y">80</property>
</packing>
</child>
<child>
<object class="GtkScale" id="scaleAnzahlWind">
<property name="width_request">570</property>
<property name="height_request">50</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="adjustment">adjustmentAnzahlWind</property>
<property name="round_digits">0</property>
<property name="digits">0</property>
</object>
<packing>
<property name="x">160</property>
<property name="y">158</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelAnzahlWind">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Anzahl
Windräder</property>
</object>
<packing>
<property name="x">35</property>
<property name="y">150</property>
</packing>
</child>
<child>
<object class="GtkScale" id="scaleAnzahlSolar">
<property name="width_request">570</property>
<property name="height_request">50</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="adjustment">adjustmentAnzahlSolar</property>
<property name="round_digits">0</property>
<property name="digits">0</property>
</object>
<packing>
<property name="x">158</property>
<property name="y">237</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelAnzahlSolar">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">m² Solar</property>
</object>
<packing>
<property name="x">35</property>
<property name="y">230</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelInfoWind">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
</object>
<packing>
<property name="x">730</property>
<property name="y">150</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelInfoSolar">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
</object>
<packing>
<property name="x">730</property>
<property name="y">230</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="radiobuttonSimulation">
<property name="label" translatable="yes">Simulations-
modus</property>
<property name="width_request">108</property>
<property name="height_request">59</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="xalign">0</property>
<property name="draw_indicator">False</property>
<property name="group">radiobuttonSpiel</property>
<signal name="toggled" handler="on_radiobuttonModus_toggled" swapped="no"/>
</object>
<packing>
<property name="x">747</property>
<property name="y">392</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="radiobuttonSpiel">
<property name="label" translatable="yes">Spielmodus</property>
<property name="width_request">110</property>
<property name="height_request">43</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="xalign">0</property>
<property name="active">True</property>
<property name="draw_indicator">False</property>
<property name="group">radiobuttonSimulation</property>
<signal name="toggled" handler="on_radiobuttonModus_toggled" swapped="no"/>
</object>
<packing>
<property name="x">747</property>
<property name="y">340</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label1">
<property name="width_request">100</property>
<property name="height_request">30</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">kWh pro Jahr</property>
</object>
<packing>
<property name="x">733</property>
<property name="y">3</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelWindAnteil">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">0</property>
</object>
<packing>
<property name="x">900</property>
<property name="y">150</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelSolarAnteil">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">0</property>
</object>
<packing>
<property name="x">900</property>
<property name="y">230</property>
</packing>
</child>
</object>
</child>
</object>
<object class="GtkAdjustment" id="adjustmentSonne">
<property name="upper">100</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
<signal name="value-changed" handler="on_adjustmentSonne_value_changed" swapped="no"/>
</object>
<object class="GtkAdjustment" id="adjustmentUhr">
<property name="upper">1439</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
<signal name="value-changed" handler="on_adjustmentUhr_value_changed" swapped="no"/>
</object>
<object class="GtkAdjustment" id="adjustmentWind">
<property name="upper">200</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
<signal name="value-changed" handler="on_adjustmentWind_value_changed" swapped="no"/>
</object>
<object class="GtkWindow" id="window_main">
<property name="can_focus">False</property>
<property name="window_position">center-always</property>
<property name="deletable">False</property>
<signal name="destroy" handler="on_window_main_destroy" swapped="no"/>
<child>
<object class="GtkFixed" id="fixed1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkScale" id="scaleSonne">
<property name="width_request">502</property>
<property name="height_request">47</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="adjustment">adjustmentSonne</property>
<property name="round_digits">1</property>
</object>
<packing>
<property name="x">132</property>
<property name="y">536</property>
</packing>
</child>
<child>
<object class="GtkScale" id="scaleWind">
<property name="width_request">508</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="adjustment">adjustmentWind</property>
<property name="round_digits">1</property>
</object>
<packing>
<property name="x">132</property>
<property name="y">453</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelWind">
<property name="width_request">75</property>
<property name="height_request">44</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Wind in
km/h</property>
</object>
<packing>
<property name="x">39</property>
<property name="y">481</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelSonne">
<property name="width_request">100</property>
<property name="height_request">43</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Sonne</property>
</object>
<packing>
<property name="x">28</property>
<property name="y">550</property>
</packing>
</child>
<child>
<object class="GtkScale" id="scaleUhr">
<property name="width_request">83</property>
<property name="height_request">319</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="orientation">vertical</property>
<property name="adjustment">adjustmentUhr</property>
<property name="round_digits">0</property>
<property name="digits">-1</property>
<property name="draw_value">False</property>
<property name="has_origin">False</property>
</object>
<packing>
<property name="x">810</property>
<property name="y">122</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelUhrzeit">
<property name="width_request">100</property>
<property name="height_request">36</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Uhrzeit</property>
</object>
<packing>
<property name="x">807</property>
<property name="y">55</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelVerbrauch">
<property name="width_request">71</property>
<property name="height_request">39</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">65072</property>
</object>
<packing>
<property name="x">166</property>
<property name="y">85</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelStromVerbrauch">
<property name="width_request">70</property>
<property name="height_request">42</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Aktueller
Stromverbrauch
in kWh</property>
</object>
<packing>
<property name="x">44</property>
<property name="y">80</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelUhr">
<property name="width_request">100</property>
<property name="height_request">41</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">00:00</property>
</object>
<packing>
<property name="x">807</property>
<property name="y">85</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelStromErzeugungWind">
<property name="width_request">103</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Stromerzeugung
(Wind) in kWh</property>
</object>
<packing>
<property name="x">38</property>
<property name="y">155</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelWindErzeugung">
<property name="width_request">100</property>
<property name="height_request">44</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">0</property>
</object>
<packing>
<property name="x">165</property>
<property name="y">172</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelStromErzeugungSonne">
<property name="width_request">108</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Stromerzeugung
(Sonne) in kWh</property>
</object>
<packing>
<property name="x">36</property>
<property name="y">237</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelSonneErzeugung">
<property name="width_request">100</property>
<property name="height_request">44</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">0</property>
</object>
<packing>
<property name="x">165</property>
<property name="y">259</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelAnteilText">
<property name="width_request">146</property>
<property name="height_request">53</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Anteil
reg. Energie</property>
</object>
<packing>
<property name="x">30</property>
<property name="y">321</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="labelAnteil">
<property name="width_request">86</property>
<property name="height_request">46</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">0</property>
</object>
<packing>
<property name="x">171</property>
<property name="y">328</property>
</packing>
</child>
<child>
<object class="GtkButton" id="buttonEinstellungen">
<property name="label" translatable="yes">Einstellungen</property>
<property name="width_request">98</property>
<property name="height_request">57</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_buttonEinstellungen_clicked" swapped="no"/>
</object>
<packing>
<property name="x">809</property>
<property name="y">533</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
#include <gtk/gtk.h>
#include <math.h>
#include <wiringPi.h>
#include <stdint.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include "I2C.h"
#include "Berechnung.h"
GtkBuilder *builder;
GtkWidget *windowMain;
GtkWidget *windowEinstellungen;
GtkWidget *g_lbl_verbrauch;
GtkWidget *g_lbl_uhrzeit;
GtkWidget *g_lbl_wind;
GtkWidget *g_adj_Sonne;
GtkWidget *g_adj_Uhr;
GtkWidget *g_adj_Wind;
GtkWidget *g_scl_Sonne;
GtkWidget *g_scl_Uhr;
GtkWidget *g_scl_Wind;
GtkWidget *g_lbl_anteil;
GtkWidget *g_lbl_sonne;
GtkWidget *g_lbl_InfoVerbrauch;
GtkWidget *g_lbl_InfoWind;
GtkWidget *g_lbl_InfoSolar;
GtkWidget *g_adj_Einwohner;
GtkWidget *g_adj_Industrie;
GtkWidget *g_adj_AnzahlWind;
GtkWidget *g_adj_AnzahlSolar;
GtkWidget *g_btn_OK;
GtkWidget *g_rad_Spiel;
GtkWidget *g_rad_Simulation;
GtkWidget *g_lbl_AnteilSolar;
GtkWidget *g_lbl_AnteilWind;
int exitProgram = 0;
int fensterOffen = 1;
uint32_t currentTime = 0;
uint32_t rcTime = 0;
uint32_t luxTime = 0;
char run = 1;
char modus = 0;
int jahressolar;
int jahreswind;
int zufallwind = 18;
int bewoelkung = 0;
char minUpdated = 0;
void SetModus(char m)
{
modus = m;
switch (modus)
{
case 0:
// Error
break;
case 1:
// Spielmodus
gtk_widget_set_sensitive(GTK_WIDGET(g_scl_Sonne), TRUE);
gtk_widget_set_sensitive(GTK_WIDGET(g_scl_Uhr), TRUE);
gtk_widget_set_sensitive(GTK_WIDGET(g_scl_Wind), TRUE);
break;
case 2:
// Simulationsmodus
gtk_widget_set_sensitive(GTK_WIDGET(g_scl_Sonne), FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(g_scl_Uhr), FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(g_scl_Wind), FALSE);
break;
default:
// Error
break;
}
}
uint64_t update_Info_Verbrauch()
{
char info[50] = {0};
uint64_t einwohner = (int) gtk_adjustment_get_value (GTK_ADJUSTMENT(g_adj_Einwohner));
int industrie = (int) gtk_adjustment_get_value (GTK_ADJUSTMENT(g_adj_Industrie));
uint64_t verbrauch = (einwohner * 3500) * ( 1 + 1/50.0 * industrie);
sprintf(info, "%" PRIu64, verbrauch);
gtk_label_set_text(GTK_LABEL(g_lbl_InfoVerbrauch), info);
return verbrauch; // Jahresverbrauch in kWh
}
void energiemix_berechnen() //Anteil im Einstellungsfenster -> Jahressverbrauch
{
uint64_t verbrauch = update_Info_Verbrauch();
double solaranteil = ((double) jahressolar / (double) verbrauch) * 100.0;
double windanteil = ((double) jahreswind / (double) verbrauch) * 100.0;
char str_solar[10] = {0};
sprintf(str_solar, "%f", solaranteil);
gtk_label_set_text(GTK_LABEL(g_lbl_AnteilSolar), str_solar);
char str_wind[10] = {0};
sprintf(str_wind, "%f", windanteil);
gtk_label_set_text(GTK_LABEL(g_lbl_AnteilWind), str_wind);
}
void main_loop()
{
int uhrTime = millis();
int uhrzeit = 300;
bewoelkung = rand() % 6 + 1;
while(run)
{
currentTime = millis();
//printf("RC TASK %d", currentTime);
gtk_main_iteration_do(FALSE);
if (modus == 2) //Simulationsmodus
{
if (currentTime >= uhrTime) //jede 1000ms -> + 1 Minute
{
minUpdated = 1;
uhrTime = currentTime + 1000;
gtk_adjustment_set_value (GTK_ADJUSTMENT(g_adj_Uhr), uhrzeit);
gtk_main_iteration_do(FALSE);
uhrzeit++;
if (uhrzeit > 1439) //Zurücksetzen der Uhr
{
uhrzeit = 0;
bewoelkung = rand() % 6 + 1;
}
//printf("c: %d rc: %d", currentTime, uhrTime);
}
}
// printf("c: %d rc: %d", currentTime, rcTime);
if (currentTime >= rcTime) //rc loop
{
//LED-Ansteuerung alle 2 Sekunden
rcTime = currentTime +2000;
updateLEDSmartGrid(1);
printf("\nRC TASK %d", currentTime);
//rc-TASK
}
if (currentTime >= luxTime) //Lux-Wert auslesen alle 2,5 s
{
lux = getLuxValue();
printf("\nLux:%d", lux);
luxTime = currentTime + 2500;
}
else
{
//printf("Else %d", currentTime);
static uint8_t task =0;
switch (task++ % 5) {
case 0: //Anzeige Windstrom aktualisieren
berechneWindStrom();
char str_wind[10] = {0};
sprintf(str_wind, "%d", erzeugungwindkw);
gtk_label_set_text(GTK_LABEL(g_lbl_wind), str_wind);
gtk_main_iteration_do(FALSE);
break;
case 1: //Anzeige Sonnestrom aktualisieren
berechneSonneStrom(modus);
char str_sonne[10] = {0};
sprintf(str_sonne, "%d", erzeugungsonnekw);
gtk_label_set_text(GTK_LABEL(g_lbl_sonne), str_sonne);
gtk_main_iteration_do(FALSE);
break;
case 2: //Anzeige reg. Energie Anteil aktualisieren
berechneAnteil();
char str_anteil[10] = {0};
sprintf(str_anteil, "%f", anteil);
gtk_label_set_text(GTK_LABEL(g_lbl_anteil), str_anteil);
gtk_main_iteration_do(FALSE);
break;
case 3:
if (modus == 2 && minUpdated)
{
minUpdated = 0;
//printf("\n Bewölkung: %d", bewoelkung);
if (minute%10 == 0)
{
double zufall = (rand() % 101)/100.0 + (0.1*bewoelkung);
int sonne = sonneuhrzeit[stunde];
if (sonne != 0)
sonne = sonne * zufall;
gtk_adjustment_set_value (GTK_ADJUSTMENT(g_adj_Sonne), sonne);
gtk_main_iteration_do(FALSE);
}
int wind_rand_spanne = 10;
int zufallw = rand() % wind_rand_spanne;
if (zufallw < wind_rand_spanne/2+1)
zufallw = -zufallw;
else
zufallw -= wind_rand_spanne/2;
zufallwind += zufallw;
if (zufallwind < 0)
zufallwind = 0;
gtk_adjustment_set_value (GTK_ADJUSTMENT(g_adj_Wind), zufallwind);
gtk_main_iteration_do(FALSE);
}
berechneStromverbrauchProMinute();
char str_verbrauch[6] = {0};
char str_uhrzeit[6] = {0};
sprintf(str_verbrauch, "%d", verbauchkw);
sprintf(str_uhrzeit, "%02d:%02d", stunde, minute);
gtk_label_set_text(GTK_LABEL(g_lbl_verbrauch), str_verbrauch);
gtk_main_iteration_do(FALSE);
gtk_label_set_text(GTK_LABEL(g_lbl_uhrzeit), str_uhrzeit);
gtk_main_iteration_do(FALSE);
break;
}
}
}
}
int oeffneEinstellungsfenster()
{
//gtk_window_maximize (windowEinstellungen);
//gtk_window_fullscreen(GTK_WINDOW(windowEinstellungen));
gtk_widget_hide(windowMain);
gtk_widget_show(windowEinstellungen);
fensterOffen = 1;
update_Info_Verbrauch();
while (fensterOffen)
{
gtk_main_iteration_do(FALSE);
}
if (exitProgram)
return 1;
anzahlsonne = (int) gtk_adjustment_get_value (GTK_ADJUSTMENT (g_adj_AnzahlSolar));
verbauchkw = 0 / 2887.0 / 365.0 * 98.0; //Verbrauch um 0 Uhr
gtk_widget_hide(windowEinstellungen);
//gtk_window_fullscreen(GTK_WINDOW(windowMain));
//gtk_window_maximize (windowMain);
gtk_widget_show(windowMain);
return 0;
}
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "Smart.glade", NULL);
windowMain = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
windowEinstellungen = GTK_WIDGET(gtk_builder_get_object(builder, "windowEinstellungen"));
gtk_builder_connect_signals(builder, NULL);
g_lbl_verbrauch = GTK_WIDGET(gtk_builder_get_object(builder, "labelVerbrauch"));
g_lbl_uhrzeit = GTK_WIDGET(gtk_builder_get_object(builder, "labelUhr"));
g_adj_Sonne = GTK_WIDGET(gtk_builder_get_object(builder, "adjustmentSonne"));
g_adj_Uhr = GTK_WIDGET(gtk_builder_get_object(builder, "adjustmentUhr"));
g_adj_Wind = GTK_WIDGET(gtk_builder_get_object(builder, "adjustmentWind"));
g_scl_Sonne = GTK_WIDGET(gtk_builder_get_object(builder, "scaleSonne"));
g_scl_Uhr = GTK_WIDGET(gtk_builder_get_object(builder, "scaleUhr"));
g_scl_Wind = GTK_WIDGET(gtk_builder_get_object(builder, "scaleWind"));
g_lbl_wind = GTK_WIDGET(gtk_builder_get_object(builder, "labelWindErzeugung"));
g_lbl_sonne = GTK_WIDGET(gtk_builder_get_object(builder, "labelSonneErzeugung"));
g_lbl_anteil = GTK_WIDGET(gtk_builder_get_object(builder, "labelAnteil"));
g_lbl_InfoVerbrauch = GTK_WIDGET(gtk_builder_get_object(builder, "labelInfoVerbrauch"));
g_lbl_InfoWind = GTK_WIDGET(gtk_builder_get_object(builder, "labelInfoWind"));
g_lbl_InfoSolar = GTK_WIDGET(gtk_builder_get_object(builder, "labelInfoSolar"));
g_adj_Einwohner = GTK_WIDGET(gtk_builder_get_object(builder, "adjustmentEinwohner"));
g_adj_Industrie = GTK_WIDGET(gtk_builder_get_object(builder, "adjustmentIndustrie"));
g_adj_AnzahlWind = GTK_WIDGET(gtk_builder_get_object(builder, "adjustmentAnzahlWind"));
g_adj_AnzahlSolar = GTK_WIDGET(gtk_builder_get_object(builder, "adjustmentAnzahlSolar"));
g_btn_OK = GTK_WIDGET(gtk_builder_get_object(builder, "buttonOK"));
g_rad_Spiel = GTK_WIDGET(gtk_builder_get_object(builder, "radiobuttonSpiel"));
g_rad_Simulation = GTK_WIDGET(gtk_builder_get_object(builder, "radiobuttonSimulation"));
g_lbl_AnteilSolar = GTK_WIDGET(gtk_builder_get_object(builder, "labelSolarAnteil"));
g_lbl_AnteilWind = GTK_WIDGET(gtk_builder_get_object(builder, "labelWindAnteil"));
g_object_unref(builder);
//wiringPiSetupSys();
// handleSensor = wiringPiI2CSetup(0x23);
gtk_label_set_text(GTK_LABEL(g_lbl_InfoWind), "0");
gtk_label_set_text(GTK_LABEL(g_lbl_InfoSolar), "0");
SetModus(2);
if (oeffneEinstellungsfenster())
return 0;
main_loop();
// gtk_main();
printf("\nMain quit");
return 0;
}
void on_adjustmentAnzahlWind_value_changed() //Wind im Einstellungsfenster
{
char str_wind[15] = {0};
anzahlwind = (int) gtk_adjustment_get_value (GTK_ADJUSTMENT(g_adj_AnzahlWind));
jahreswind = 2000*2100*anzahlwind;
sprintf(str_wind, "%d", jahreswind);
gtk_label_set_text(GTK_LABEL(g_lbl_InfoWind), str_wind);
energiemix_berechnen();
}
void on_adjustmentAnzahlSolar_value_changed() //Solar im Einstellungsfenster
{
char str_solar[10] = {0};
anzahlsonne = (int) gtk_adjustment_get_value (GTK_ADJUSTMENT(g_adj_AnzahlSolar));
jahressolar = 100*anzahlsonne;
sprintf(str_solar, "%d", jahressolar);
gtk_label_set_text(GTK_LABEL(g_lbl_InfoSolar), str_solar);
energiemix_berechnen();
}
void on_adjustmentEinwohner_value_changed() //Einwohner im Einstellungsfenster
{
update_Info_Verbrauch();
energiemix_berechnen();
}
void on_adjustmentIndustrie_value_changed() //Industrie im Einstellungsfenster
{
update_Info_Verbrauch();
energiemix_berechnen();
}
void on_buttonOK_clicked()
{
printf("\non_buttonOK_clicked()");
jahresverbrauch = update_Info_Verbrauch();
fensterOffen = 0;
}
void on_buttonEinstellungen_clicked()
{
oeffneEinstellungsfenster();
}
void on_windowEinstellungen_destroy()
{
printf("\non_windowEinstellungen_destroy()");
fensterOffen = 0;
exitProgram = 1;
run = 0;
}
void on_adjustmentWind_value_changed() // Windgeschwindigkeit updaten
{
double windgeschwindigkeitkmh = (double) gtk_adjustment_get_value (GTK_ADJUSTMENT(g_adj_Wind));
windgeschwindigkeitms = (int) round(windgeschwindigkeitkmh/3.6);
}
void on_adjustmentSonne_value_changed()
{
sonnenleistung = (int) gtk_adjustment_get_value (GTK_ADJUSTMENT(g_adj_Sonne));
}
// called when Uhrzeit changed
void on_adjustmentUhr_value_changed()
{
int uhr = (int) gtk_adjustment_get_value (GTK_ADJUSTMENT(g_adj_Uhr));
stunde = uhr / 60;
minute = uhr % 60;
}
void on_radiobuttonModus_toggled(GtkToggleButton *togglebutton, gpointer user_data)
{
if (!gtk_toggle_button_get_active(togglebutton))
return;
gboolean spiel = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(g_rad_Spiel));
gboolean simulation = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(g_rad_Simulation));
if (spiel && !simulation)
SetModus(1);
else if (!spiel && simulation)
SetModus(2);
else
SetModus(0);
}
// called when window is closed
void on_window_main_destroy()
{
printf("\non_window_main_destroy()");
// gtk_main_quit();
updateLEDSmartGrid(0);
run = 0;
}
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include "I2C.h"
int main (int argc, char *argv[])
{
//printf("%d args", argc);
if (argc != 3)
{
printf("Invalid number of arguments");
return 1;
}
int pin = atoi(argv[1]);
int value = atoi(argv[2]);
setOutputPin(pin, value);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment