Raspberry Pi Weighting Control System
This project serves as a simple weighting control system, that was realized as a Bachelor Thesis
Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
measurement Class Reference

Container for measurement data and (convenience) variables, that are used to show measurement in GUI. More...

#include <user_workspace.h>

Public Member Functions

void log_measurement_to_debug ()
 
void init_value_strings ()
 
void init_start_end_strings ()
 
void init_convinience_vars (const char *measurer_uname, const char *measuree_uname)
 
int compare (measurement *m)
 

Static Public Member Functions

static void init_simple_measuring (measurement *m, std::vector< double > &values)
 
static void init_measuring (measurement *m, user_workspace *uspace, std::vector< double > &values, std::tm *start_tm, std::tm *end_tm, unsigned long m_length)
 
static void calc_measuring_results (measurement *m)
 
static void prepare_continuous_to_picked (std::vector< measurement > &ms, measurement *picked)
 
static std::pair< double, double > calc_every_x_values_stats (measurement *m)
 Calculates the median of medians and avarages of avarages of every X values (X is a defined constant in .cpp). Is maybe avarage of medians preferred (can be changed)? More...
 
static std::pair< double, double > calc_all_values_stats (measurement *m)
 Calculates the median and avarage of all values. More...
 

Public Attributes

unsigned long id
 
unsigned long measurement_number
 
unsigned long measurement_part = 1
 
unsigned long measuree_id
 
unsigned long measurer_id
 
unsigned long measuring_length
 
std::tm measuring_start
 
std::tm measuring_end
 
std::string description
 
uint8_t value_length
 
unsigned long value_count
 
std::vector< double > values
 
double x_val_med
 
double x_val_avg
 
double all_val_med
 
double all_val_avg
 
std::tm created_at
 
bool continuous = false
 
int cont_measurements
 
HX711::Mass::Unit current_unit = HX711::Mass::Unit::G
 
std::string x_val_med_str
 
std::string x_val_avg_str
 
std::string all_val_med_str
 
std::string all_val_avg_str
 
std::string measuree_uname
 
std::string measurer_uname
 
std::string length_str
 
std::string start_str
 
std::string end_str
 

Detailed Description

Container for measurement data and (convenience) variables, that are used to show measurement in GUI.

Definition at line 34 of file user_workspace.h.

Member Function Documentation

◆ calc_all_values_stats()

std::pair< double, double > measurement::calc_all_values_stats ( measurement m)
static

Calculates the median and avarage of all values.

Returns
std::pair<double, double> first is median, second is avarage

Definition at line 246 of file user_workspace.cpp.

246  {
247  std::vector<double> vect = m->values; // copy to not modify original vector order
248  return std::pair<double, double>(HX711::Utility::median(&vect), HX711::Utility::average(&vect));
249 }
std::vector< double > values

◆ calc_every_x_values_stats()

std::pair< double, double > measurement::calc_every_x_values_stats ( measurement m)
static

Calculates the median of medians and avarages of avarages of every X values (X is a defined constant in .cpp). Is maybe avarage of medians preferred (can be changed)?

Returns
std::pair<double, double> - first is avarage of medians, second is avarage of avarages

Definition at line 220 of file user_workspace.cpp.

220  {
221  size_t it = 0, inc = 0;
222  std::vector<double> vect = m->values; // copy to not modify original vector order
223  std::vector<double> meds;
224  std::vector<double> avgs;
225 
226  while (it < vect.size()) {
227  if ((it + X_SAMPLES) < vect.size())
228  inc = X_SAMPLES;
229  else
230  inc = vect.size() - it;
231 
232  auto it_start = vect.begin() + it;
233  auto it_end = vect.begin() + it + inc;
234 
235  std::vector<double> tmp(it_start, it_end);
236 
237  meds.push_back(HX711::Utility::median(&tmp));
238  avgs.push_back(HX711::Utility::average(&tmp));
239 
240  it += inc;
241  }
242 
243  return std::pair<double, double>(HX711::Utility::median(&meds), HX711::Utility::average(&avgs));
244 }
#define X_SAMPLES

◆ calc_measuring_results()

void measurement::calc_measuring_results ( measurement m)
static

Calculates Avarage and Median of measuring

Definition at line 179 of file user_workspace.cpp.

179  {
180  // called often by init_simple_measuring(...); disabling debug
181  // spdlog::debug("user_workspace.cpp - Initing calculated measuring parameters");
182  std::pair<double, double> x_vals = measurement::calc_every_x_values_stats(m);
183  m->x_val_med = x_vals.first;
184  m->x_val_avg = x_vals.second;
185 
186  std::pair<double, double> all_vals = measurement::calc_all_values_stats(m);
187  m->all_val_med = all_vals.first;
188  m->all_val_avg = all_vals.second;
189 }
static std::pair< double, double > calc_every_x_values_stats(measurement *m)
Calculates the median of medians and avarages of avarages of every X values (X is a defined constant ...
double all_val_med
double all_val_avg
static std::pair< double, double > calc_all_values_stats(measurement *m)
Calculates the median and avarage of all values.
double x_val_avg
double x_val_med

◆ compare()

int measurement::compare ( measurement m)

Compares @this to @m and prints out non-matching values

Definition at line 100 of file user_workspace.cpp.

100  {
101  spdlog::debug("Comparing:");
102  this->log_measurement_to_debug();
103  spdlog::debug("to:");
105  spdlog::info("Comparing start");
106 
107  if (this->measurement_number != m->measurement_number) {
108  spdlog::debug("measurement_number diff: {0} - {1}", this->measurement_number, m->measurement_number);
109  }
110  if (this->measuree_id != m->measuree_id) {
111  spdlog::debug("measuree_id diff: {0} - {1}", this->measuree_id, m->measuree_id);
112  }
113  if (this->measurer_id != m->measurer_id) {
114  spdlog::debug("measurer_id diff: {0} - {1}", this->measurer_id, m->measurer_id);
115  }
116  if (this->measuring_length != m->measuring_length) {
117  spdlog::debug("measuring_length diff: {0} - {1}", this->measuring_length, m->measuring_length);
118  }
119  if (this->x_val_med != m->x_val_med) {
120  spdlog::debug("x_val_med diff: {0} - {1}", this->x_val_med, m->x_val_med);
121  }
122  if (this->x_val_avg != m->x_val_avg) {
123  spdlog::debug("x_val_avg diff: {0} - {1}", this->x_val_avg, m->x_val_avg);
124  }
125  if (this->all_val_med != m->all_val_med) {
126  spdlog::debug("all_val_med diff: {0} - {1}", this->all_val_med, m->all_val_med);
127  }
128  if (this->all_val_avg != m->all_val_avg) {
129  spdlog::debug("all_val_avg diff: {0} - {1}", this->all_val_avg, m->all_val_avg);
130  }
131  if (this->values.size() != m->values.size()) {
132  spdlog::debug("values.size() diff: {0} - {1}", this->values.size(), m->values.size());
133  }
134 
135  size_t len = this->values.size() > m->values.size() ? m->values.size() : this->values.size();
136  for (size_t i = 0; i < len; i++) {
137  if (this->values[i] != m->values[i]) {
138  spdlog::error("values (binary) start to differ at index {0}", i);
139  break;
140  }
141  }
142 
143  spdlog::info("Comparing end");
144  return 0;
145 }
unsigned long measuring_length
void log_measurement_to_debug()
unsigned long measuree_id
unsigned long measurer_id
unsigned long measurement_number

◆ init_convinience_vars()

void measurement::init_convinience_vars ( const char *  measurer_uname,
const char *  measuree_uname 
)

Calls @init_value_strings and @init_start_end_strings and inits usernames with parameters @measurer_uname and @measuree_uname

Definition at line 93 of file user_workspace.cpp.

93  {
94  this->init_value_strings();
95  this->init_start_end_strings();
98 }
void init_start_end_strings()
std::string measurer_uname
void init_value_strings()
std::string measuree_uname

◆ init_measuring()

void measurement::init_measuring ( measurement m,
user_workspace uspace,
std::vector< double > &  values,
std::tm *  start_tm,
std::tm *  end_tm,
unsigned long  m_length 
)
static

Properly inits measuring values, that are inserted into database

Definition at line 158 of file user_workspace.cpp.

160 {
161  spdlog::debug("user_workspace.cpp - Initing basic measuring parameters");
162 
163  m->values = values;
164  m->measuree_id = uspace->has_subuser() ? uspace->subuser->id : uspace->get_user_id();
165  m->measurer_id = uspace->get_user_id();
166  m->measurement_number = uspace->has_subuser() ? uspace->subuser->measure_count + 1 :
167  uspace->get_user()->measure_count + 1;
168 
169  // dereferencing like this should copy
170  m->measuring_start = *start_tm;
171  m->measuring_end = *end_tm;
172  m->measuring_length = m_length;
173  m->value_count = values.size();
174  m->value_length = sizeof(double);
175 
177 }
uint8_t value_length
unsigned long value_count
std::tm measuring_end
std::tm measuring_start
static void calc_measuring_results(measurement *m)
unsigned long measure_count
unsigned long get_user_id()
std::unique_ptr< user_cont > subuser
user_cont * get_user()

◆ init_simple_measuring()

void measurement::init_simple_measuring ( measurement m,
std::vector< double > &  values 
)
static

Simple measuring is a measuring with values only. This is used for the values to be observed during measuring

Definition at line 147 of file user_workspace.cpp.

147  {
148  // this is called on every update - not logging even to debug
149  // spdlog::debug("user_workspace.cpp - Initing simple measurements");
150  m->values = values;
151  m->value_count = values.size();
152  m->value_length = sizeof(double);
153 
155  m->init_value_strings();
156 }

◆ init_start_end_strings()

void measurement::init_start_end_strings ( )

Inits strings for start and end date/times

Definition at line 68 of file user_workspace.cpp.

68  {
69  char buf[64] = {0};
70  strftime(buf, 64, "%Y-%m-%d %H:%M:%S", &(this->measuring_start));
71  // FIXME - is first or second version better?
72  // this->start_str = buf;
73  this->start_str = std::string(buf);
74 
75  memset(buf, 0, 64);
76  strftime(buf, 64, "%Y-%m-%d %H:%M:%S", &(this->measuring_end));
77  // this->end_str = buf;
78  this->end_str = std::string(buf);
79 
80  memset(buf, 0, 64);
81  if (this->continuous) {
82  // continuous measuring is minute at least, up to days, so length is in s
83  sprintf(buf, "%ld s", std::chrono::seconds(this->measuring_length).count());
84  } else {
85  // regular measuring is expected to take around a minute max
86  sprintf(buf, "%ld ms", std::chrono::duration_cast<std::chrono::milliseconds>
87  (std::chrono::microseconds(this->measuring_length)).count());
88  }
89  // this->length_str = buf;
90  this->length_str = std::string(buf);
91 }
std::string length_str
std::string end_str
std::string start_str

◆ init_value_strings()

void measurement::init_value_strings ( )

Intializes numbers to string, that can be displayed in GUI

Definition at line 48 of file user_workspace.cpp.

48  {
49  // Mass must be created as G. The hx sensor library atually returns measured double in Grams!
50  HX711::Mass x_med(x_val_med, HX711::Mass::Unit::G);
51  HX711::Mass x_avg(x_val_avg, HX711::Mass::Unit::G);
52  HX711::Mass all_med(all_val_med, HX711::Mass::Unit::G);
53  HX711::Mass all_avg(all_val_avg, HX711::Mass::Unit::G);
54 
55  /*
56  spdlog::info("Measured value no par: {0}", x_med.toString().c_str());
57  spdlog::info("Measured value MG: {0}", x_med.toString(HX711::Mass::Unit::MG).c_str());
58  spdlog::info("Measured value G: {0}", x_med.toString(HX711::Mass::Unit::G).c_str());
59  spdlog::info("Measured value KG: {0}", x_med.toString(HX711::Mass::Unit::KG).c_str());
60  */
61 
62  this->x_val_med_str = x_med.toString(current_unit);
63  this->x_val_avg_str = x_avg.toString(current_unit);
64  this->all_val_med_str = all_med.toString(current_unit);
65  this->all_val_avg_str = all_avg.toString(current_unit);
66 }
std::string all_val_avg_str
HX711::Mass::Unit current_unit
std::string all_val_med_str
std::string x_val_med_str
std::string x_val_avg_str

◆ log_measurement_to_debug()

void measurement::log_measurement_to_debug ( )

Prints measuring on debug level

Definition at line 22 of file user_workspace.cpp.

22  {
23  spdlog::debug("measurement - ID: {0}", this->id);
24  // spdlog::debug("measurement - status: {0}", this->status);
25  spdlog::debug("measurement - measurement_number: {0}", this->measurement_number);
26  spdlog::debug("measurement - measuree_id: {0}", this->measuree_id);
27  spdlog::debug("measurement - measurer_id: {0}", this->measurer_id);
28  spdlog::debug("measurement - measuring_length: {0}", this->measuring_length);
29  char buf[64] = {0};
30  strftime(buf, 64, "%Y-%m-%d %H:%M:%S", &(this->measuring_start));
31  spdlog::debug("measurement - measuring start: {0}", buf);
32  memset(buf, 0, 64);
33  strftime(buf, 64, "%Y-%m-%d %H:%M:%S", &(this->measuring_end));
34  spdlog::debug("measurement - measuring end: {0}", buf);
35  spdlog::debug("measurement - description: {0}", this->description.c_str());
36  spdlog::debug("measurement - value_length: {0}", this->value_length);
37  spdlog::debug("measurement - value_count: {0}", this->value_count);
38  spdlog::debug("measurement - raw_values size: {0}", this->values.size());
39  spdlog::debug("measurement - x_val_med: {0}", this->x_val_med);
40  spdlog::debug("measurement - x_val_avg: {0}", this->x_val_avg);
41  spdlog::debug("measurement - all_val_med: {0}", this->all_val_med);
42  spdlog::debug("measurement - all_val_avg: {0}", this->all_val_avg);
43  memset(buf, 0, 64);
44  strftime(buf, 64, "%Y-%m-%d %H:%M:%S", &(this->created_at));
45  spdlog::debug("measurement - created at: {0}", buf);
46 }
std::tm created_at
std::string description

◆ prepare_continuous_to_picked()

void measurement::prepare_continuous_to_picked ( std::vector< measurement > &  ms,
measurement picked 
)
static

Calculates Avarages of Avarages and Medians of several measurings, that are then put into @picked as a single measuring

Definition at line 191 of file user_workspace.cpp.

191  {
192  if (ms.size() <= 0) return; // this shouldn't occure, but just to be safe
193 
194  *picked = ms[0]; // copy first element to picked
195 
196  picked->measuring_end = ms[ms.size() - 1].measuring_end;
197  picked->cont_measurements = ms.size();
198  picked->continuous = true;
199 
200  unsigned long total_length = 0;
201  double xm_sum = 0.0, xa_sum = 0.0, am_sum = 0.0, aa_sum = 0.0;
202 
203  for (size_t i = 0; i < ms.size(); i++) {
204  // measuring length is in us, continuous length is assumed to be minutes, hours or even days -> stored in seconds
205  total_length += ms[i].measuring_length / 1000000;
206  xm_sum += ms[i].x_val_med;
207  xa_sum += ms[i].x_val_avg;
208  am_sum += ms[i].all_val_med;
209  aa_sum += ms[i].all_val_avg;
210  }
211 
212  picked->measuring_length = total_length;
213  // use avarages of all stats as result to show
214  picked->x_val_med = xm_sum / ms.size();
215  picked->x_val_avg = xa_sum / ms.size();
216  picked->all_val_med = am_sum / ms.size();
217  picked->all_val_avg = aa_sum / ms.size();
218 }
int cont_measurements

Member Data Documentation

◆ all_val_avg

double measurement::all_val_avg

Definition at line 55 of file user_workspace.h.

◆ all_val_avg_str

std::string measurement::all_val_avg_str

Definition at line 68 of file user_workspace.h.

◆ all_val_med

double measurement::all_val_med

Definition at line 54 of file user_workspace.h.

◆ all_val_med_str

std::string measurement::all_val_med_str

Definition at line 67 of file user_workspace.h.

◆ cont_measurements

int measurement::cont_measurements

Definition at line 60 of file user_workspace.h.

◆ continuous

bool measurement::continuous = false

Definition at line 59 of file user_workspace.h.

◆ created_at

std::tm measurement::created_at

Definition at line 56 of file user_workspace.h.

◆ current_unit

HX711::Mass::Unit measurement::current_unit = HX711::Mass::Unit::G

Definition at line 64 of file user_workspace.h.

◆ description

std::string measurement::description

Definition at line 46 of file user_workspace.h.

◆ end_str

std::string measurement::end_str

Definition at line 73 of file user_workspace.h.

◆ id

unsigned long measurement::id

Definition at line 37 of file user_workspace.h.

◆ length_str

std::string measurement::length_str

Definition at line 71 of file user_workspace.h.

◆ measuree_id

unsigned long measurement::measuree_id

Definition at line 41 of file user_workspace.h.

◆ measuree_uname

std::string measurement::measuree_uname

Definition at line 69 of file user_workspace.h.

◆ measurement_number

unsigned long measurement::measurement_number

Definition at line 39 of file user_workspace.h.

◆ measurement_part

unsigned long measurement::measurement_part = 1

Definition at line 40 of file user_workspace.h.

◆ measurer_id

unsigned long measurement::measurer_id

Definition at line 42 of file user_workspace.h.

◆ measurer_uname

std::string measurement::measurer_uname

Definition at line 70 of file user_workspace.h.

◆ measuring_end

std::tm measurement::measuring_end

Definition at line 45 of file user_workspace.h.

◆ measuring_length

unsigned long measurement::measuring_length

Definition at line 43 of file user_workspace.h.

◆ measuring_start

std::tm measurement::measuring_start

Definition at line 44 of file user_workspace.h.

◆ start_str

std::string measurement::start_str

Definition at line 72 of file user_workspace.h.

◆ value_count

unsigned long measurement::value_count

Definition at line 49 of file user_workspace.h.

◆ value_length

uint8_t measurement::value_length

Definition at line 48 of file user_workspace.h.

◆ values

std::vector<double> measurement::values

Definition at line 50 of file user_workspace.h.

◆ x_val_avg

double measurement::x_val_avg

Definition at line 53 of file user_workspace.h.

◆ x_val_avg_str

std::string measurement::x_val_avg_str

Definition at line 66 of file user_workspace.h.

◆ x_val_med

double measurement::x_val_med

Definition at line 52 of file user_workspace.h.

◆ x_val_med_str

std::string measurement::x_val_med_str

Definition at line 65 of file user_workspace.h.


The documentation for this class was generated from the following files: