ENERGY MONITOR 0.2
Loading...
Searching...
No Matches
electric.h
Go to the documentation of this file.
1#pragma once
2#include "signal.h"
3#include "signal_operation.h"
4#define _USE_MATH_DEFINES
5#include <cmath>
6#include <math.h>
7
8
14class _current: public signal{
15 public:
19 }
20};
21
22class _voltage: public signal{
23 public:
27 }
28};
29
30
36
42#include <iostream>
43#include <stdlib.h>
44class _power: public signal{
45 private:
46 double active = 0;
47 double reactive = 0;
48 double apparent = 0;
49 double frequency = 0;
50 double powerFactor = 0;
51 double phase = 0xFFFFFFFFFFFFFFFF; //just initialize with an impossible phase number to detect if we change it
52 bool dynamic_loading = false;
53 //We keep the pointers assosiated with the multiplication if any happens
54 _current* base_current = NULL;
55 _voltage* base_volt = NULL;
56
57 public:
58
59 _power(_voltage &volt, _current &current){
60 //GET POWER FROM CURRENT AND VOLTAGE MULTIPLICATION
61
62 signal_operation_global.multiply(volt,current,*this, INTERSECT);
63
64
65 if(!this->isTimeAnalysed())this->analyse();
66
67 frequency = this->analytics.base_frequency;
68 active = this->analytics.avg;
69
70 if(stoi(settings.get_setting("electrical","use_rms_for_apparent"))){
71 apparent = current.get_analytics()->rms * volt.get_analytics()->rms;
72 }else{
73 apparent = (this->analytics.avg_ptp)/2;
74 }
75
76
77
78 //CHECK FOR THE HOW THE LOAD BEHAVIOR IS
79 //LOADS WITH VITH VARIABLE BEHAVIOR IE.CHANGED FREQUENCIES/CHANGED PHASE SHIFTS LEAD TO NO PHASE ANGLE CALCULATION TILL NOW
80 //WHICH LEADS TO FAULTY REACTIVE COMPONENT
81 //HOWEVER LIKE THIS WILL ALWAYS GET REACTIVE COMPONENT AND IGNORE ITS SIGN
82 if(stoi(settings.get_setting("electrical","assume_static_load"))){
83 phase = -signal_operation_global.phase_diff(volt, current);
84 reactive = (apparent - active)*sign(phase);
85 }else{
86 reactive = (apparent - active);
87 }
88
89 powerFactor = (active/apparent);
90 base_current = &current;
91 base_volt = &volt;
92 }
93
94
99 double get_energy(double time_start,double time_end){
100 if(!this->isTimeAnalysed())this->analyse();
101 //if time_start is smaller than time_end swap both and multiply by -1(sign) in the end
102 int sign = 1;
103 if(time_start > time_end){
104 double temp = time_end;
105 time_end = time_start;
106 time_start = temp;
107 sign = -1;
108 }
109
110 if(time_start > this->get_analytics()->timeEnd)return 0;
111 if(time_end < this->get_analytics()->timeStart)return 0;
112 unsigned int idx = 0;
113 double energy_0 = 0;
114 double energy_1 = 0;
115 //Search for the time corresponding index in the data and iterate index
116 while(this->getValue(idx, _time) < time_start)idx++;
117 if(time_start < this->get_analytics()->timeStart)return 0;
118 //capture energy_0
119
120 energy_0 = getValue(idx, _area);
121 //Find time end and energy at it
122 while( (this->getValue(idx,_time) < time_end) && (idx < analytics.samples_num - 1) ) idx++;
123 //capture energy_1
124 energy_1 = getValue(idx, _area);
125
126 return (energy_1 - energy_0)*sign;
127 }
128
129 double get_active()const{
130 return active;
131 }
132
133 double get_apparent()const{
134 return apparent;
135 }
136
137 double get_reactive()const{
138 return reactive;
139 }
140
141 double get_frequency()const{
142 return active;
143 }
144
145 double get_PF()const{
146 return powerFactor;
147 }
148
149 int get_loadType()const{
150 switch (sign(roundTo(reactive, 0.001)))
151 {
152 case positive:
153 return INDUCTIVE;
154 break;
155
156 case negative:
157 return CAPACITIVE;
158
159 case zero:
160 return RESISTIVE;
161
162 default:
163 return -1;
164 break;
165 }
166 }
167
168 double get_phase()const{
169 return phase;
170 }
171
172 int pf_type()const{
173 switch (sign(roundTo(phase, 0.01)))
174 {
175 case positive:
176 return LAGGING;
177 break;
178 case negative:
179 return LEADING;
180 break;
181 case zero:
182 return SYNC;
183 break;
184
185 default:
186 return -1;
187 break;
188 }
189 }
190
191};
192
193
197inline double tarrif_calc(double energy_in_month)
198{
199 double cost=0;
200 if (energy_in_month <= 100)
201 {
202 if (energy_in_month > 50) cost = 0.68 * (energy_in_month - 50) + 50 * 0.58;
203 }
204 else if (energy_in_month <= 650)
205 {
206 if (energy_in_month > 350) cost = 1.4 * (energy_in_month - 350) + 1.25 * 150 + 0.83 * 200;
207 else if (energy_in_month > 200) cost = 1.25 * (energy_in_month - 200) + 0.83 * 200;
208 else cost = 0.83 * energy_in_month;
209 }
210 else if (energy_in_month <= 1000) cost = energy_in_month * 1.5;
211 else cost = energy_in_month * 1.65;
212 return cost;
213}
_current(signal sig)
Definition electric.h:16
_current()
Definition electric.h:18
double get_frequency() const
Definition electric.h:141
int get_loadType() const
Definition electric.h:149
int pf_type() const
Definition electric.h:172
double get_phase() const
Definition electric.h:168
double get_reactive() const
Definition electric.h:137
double get_apparent() const
Definition electric.h:133
double get_active() const
Definition electric.h:129
double get_energy(double time_start, double time_end)
get the energy over the coarse of a certain time period
Definition electric.h:99
_power(_voltage &volt, _current &current)
Definition electric.h:59
double get_PF() const
Definition electric.h:145
string get_setting(string class_name, string setting)
Definition setting.h:56
double phase_diff(signal &base_sig1, signal &base_sig2)
calculates phase difference based on maximas and minimas times
signal multiply(signal &base_sig1, signal &base_sig2, int mode=INTERSECT)
_voltage(signal sig)
Definition electric.h:24
_voltage()
Definition electric.h:26
signal class the parent class for every other signal or any form of time-sorted (time,...
Definition signal.h:39
const bool isTimeAnalysed()
check if the signal was analysed in the time domain
Definition signal.h:276
const _analytics * get_analytics() const
get analytics of the time_domain analysed signal
Definition signal.cpp:691
bool analyse()
generalized huge analysis in the time domain fetches basic data
Definition signal.cpp:674
double getValue(int row, int col)
easy abstracted values extraction
Definition signal.h:118
_settings settings
Definition core.cpp:3
#define zero
Definition core.h:26
t roundTo(t num, t n)
Definition core.h:145
#define negative
Definition core.h:27
#define sign(c)
Definition core.h:28
#define positive
Definition core.h:25
double tarrif_calc(double energy_in_month)
calculate energy cost based on how much energy was consumed in a month according to egyptian tarrif
Definition electric.h:197
power_type
Definition electric.h:31
@ RESISTIVE
Definition electric.h:34
@ INDUCTIVE
Definition electric.h:32
@ CAPACITIVE
Definition electric.h:33
pf_type
Definition electric.h:37
@ LAGGING
Definition electric.h:38
@ SYNC
Definition electric.h:40
@ LEADING
Definition electric.h:39
this file includes the base class "signal" for signals modeling and analysing thier time-domain / fre...
@ _area
Definition signal.h:33
@ _time
Definition signal.h:29
_signal_operation signal_operation_global
this file includes the basic signal_operation class responsible for doing any required manipulation o...
@ INTERSECT
a basic structure that holds basic analysis results
double rms
rms for the signal
Definition signal.h:81