Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Achim Morschhauser
GeomagLogger
Commits
097d880b
Commit
097d880b
authored
Nov 18, 2021
by
Achim Morschhauser
Browse files
Auto-Formatting
parent
a73d4a5a
Changes
20
Expand all
Hide whitespace changes
Inline
Side-by-side
src/Driver_Obs_Serial.cpp
View file @
097d880b
...
...
@@ -17,7 +17,7 @@
* *
****************************************************************************
***************************************************************************/
Driver_Obs_Serial
::
Driver_Obs_Serial
(
std
::
string
port
,
buffer_obs
*
buffer
)
:
Driver_Obs_Serial
::
Driver_Obs_Serial
(
std
::
string
port
,
buffer_obs
*
buffer
)
:
serial
(
port
)
{
this
->
buffer
=
buffer
;
...
...
@@ -28,8 +28,8 @@ Driver_Obs_Serial::Driver_Obs_Serial(std::string port, buffer_obs* buffer) :
}
Driver_Obs_Serial
::
Driver_Obs_Serial
(
std
::
string
port
,
buffer_obs
*
buffer
,
Obs_Calibration_Vector
*
cal
)
:
Driver_Obs_Serial
::
Driver_Obs_Serial
(
std
::
string
port
,
buffer_obs
*
buffer
,
Obs_Calibration_Vector
*
cal
)
:
serial
(
port
)
{
this
->
buffer
=
buffer
;
...
...
@@ -57,7 +57,7 @@ Driver_Obs_Serial::Driver_Obs_Serial(std::string port, buffer_obs* buffer,
***************************************************************************/
int
Driver_Obs_Serial
::
receive_raw
(
char
*
buf
,
int
buflen
,
double
timeout
)
{
return
(
serial
.
receive_raw
(
buf
,
buflen
,
timeout
));
return
(
serial
.
receive_raw
(
buf
,
buflen
,
timeout
));
}
...
...
@@ -68,7 +68,7 @@ int Driver_Obs_Serial::receive_raw(char *buf, int buflen, double timeout) {
***************************************************************************/
int
Driver_Obs_Serial
::
receive
(
char
*
buf
,
int
buflen
,
double
timeout
)
{
return
(
serial
.
receive
(
buf
,
buflen
,
timeout
));
return
(
serial
.
receive
(
buf
,
buflen
,
timeout
));
}
...
...
@@ -80,7 +80,7 @@ int Driver_Obs_Serial::receive(char *buf, int buflen, double timeout) {
int
Driver_Obs_Serial
::
receive
(
char
*
buf
,
int
buflen
,
double
timeout
,
struct
timespec
*
recv_time
)
{
return
(
serial
.
receive
(
buf
,
buflen
,
timeout
,
recv_time
));
return
(
serial
.
receive
(
buf
,
buflen
,
timeout
,
recv_time
));
}
...
...
@@ -89,11 +89,11 @@ int Driver_Obs_Serial::receive(char *buf, int buflen, double timeout,
* Send a command to the serial port. CMD is given as constant char. *
* *
********************************************************************************/
int
Driver_Obs_Serial
::
send
(
const
char
*
cmd
)
{
int
Driver_Obs_Serial
::
send
(
const
char
*
cmd
)
{
//fprintf(stderr,"Send: %s\n",cmd);
//fprintf(stderr,"Send: %s\n",cmd);
return
(
serial
.
send
(
cmd
));
return
(
serial
.
send
(
cmd
));
}
...
...
@@ -104,7 +104,7 @@ int Driver_Obs_Serial::send(const char* cmd) {
********************************************************************************/
int
Driver_Obs_Serial
::
send
(
char
*
cmd
)
{
return
(
serial
.
send
(
cmd
));
return
(
serial
.
send
(
cmd
));
}
...
...
@@ -115,7 +115,7 @@ int Driver_Obs_Serial::send(char *cmd) {
********************************************************************************/
int
Driver_Obs_Serial
::
waitanswer
(
double
timeout
)
{
return
(
serial
.
waitanswer
(
timeout
));
return
(
serial
.
waitanswer
(
timeout
));
}
...
...
@@ -126,7 +126,7 @@ int Driver_Obs_Serial::waitanswer(double timeout) {
********************************************************************************/
int
Driver_Obs_Serial
::
flush
()
{
return
(
serial
.
flush
());
return
(
serial
.
flush
());
}
...
...
@@ -135,9 +135,9 @@ int Driver_Obs_Serial::flush() {
* Set the termination characters. *
* *
********************************************************************************/
int
Driver_Obs_Serial
::
set_term
(
const
char
*
term
,
int
termlen
)
{
int
Driver_Obs_Serial
::
set_term
(
const
char
*
term
,
int
termlen
)
{
return
(
serial
.
set_term
(
term
,
termlen
));
return
(
serial
.
set_term
(
term
,
termlen
));
}
...
...
@@ -149,7 +149,6 @@ int Driver_Obs_Serial::set_term(const char* term, int termlen) {
********************************************************************************
*******************************************************************************/
/********************************************************************************
* *
* Initialize the GSM. *
...
...
@@ -159,7 +158,6 @@ int Driver_Obs_Serial::set_term(const char* term, int termlen) {
********************************************************************************/
int
Driver_Obs_Serial
::
set_baud
(
int
baud
)
{
////////////////////////////////////////////////////////////////////////
//
// Set the baud rate of serial port
...
...
@@ -180,12 +178,12 @@ int Driver_Obs_Serial::set_baud(int baud) {
*
*
******************************************************************************/
int
Driver_Obs_Serial
::
find_baud
(
const
char
*
init_chars
)
{
int
Driver_Obs_Serial
::
find_baud
(
const
char
*
init_chars
)
{
//! The init string
char
*
tmp
[]
=
{
(
char
*
)
init_chars
};
//! The init string
char
*
tmp
[]
=
{
(
char
*
)
init_chars
};
return
(
find_baud
(
tmp
,
1
));
return
(
find_baud
(
tmp
,
1
));
}
...
...
@@ -195,7 +193,7 @@ int Driver_Obs_Serial::find_baud(const char* init_chars) {
* Several init strings are passed.
*
******************************************************************************/
int
Driver_Obs_Serial
::
find_baud
(
char
*
init_chars
[],
int
init_chars_num
)
{
int
Driver_Obs_Serial
::
find_baud
(
char
*
init_chars
[],
int
init_chars_num
)
{
////////////////////////////////////////////////////////////////////////
//
...
...
@@ -203,12 +201,12 @@ int Driver_Obs_Serial::find_baud(char* init_chars[], int init_chars_num) {
//
////////////////////////////////////////////////////////////////////////
int
baud
=
0
;
char
buf
[
100
]
=
""
;
int
*
valid_baudrates
;
int
valid_baudrates_size
;
int
baud
=
0
;
char
buf
[
100
]
=
""
;
int
*
valid_baudrates
;
int
valid_baudrates_size
;
// Get the valid baudrates from the serial driver
// Get the valid baudrates from the serial driver
serial
.
get_baudrates
(
&
valid_baudrates
,
&
valid_baudrates_size
);
// Try different baudrates
...
...
@@ -217,17 +215,17 @@ int Driver_Obs_Serial::find_baud(char* init_chars[], int init_chars_num) {
//fprintf(stderr,"---- Test baud rate: %7d ----\n", valid_baudrates[i]);
serial
.
set_baud
(
valid_baudrates
[
i
]);
usleep
(
100000
);
usleep
(
100000
);
for
(
int
j
=
0
;
j
<
init_chars_num
-
1
;
j
++
){
for
(
int
j
=
0
;
j
<
init_chars_num
-
1
;
j
++
)
{
//fprintf(stderr,"BAUD: %s\n",init_chars[j]);
send
(
init_chars
[
j
]);
usleep
(
10000
);
flush
();
}
send
(
init_chars
[
init_chars_num
-
1
]);
if
(
waitanswer
(
1
)
==
0
)
{
send
(
init_chars
[
init_chars_num
-
1
]);
if
(
waitanswer
(
1
)
==
0
)
{
baud
=
valid_baudrates
[
i
];
break
;
}
...
...
@@ -245,11 +243,10 @@ int Driver_Obs_Serial::find_baud(char* init_chars[], int init_chars_num) {
// Check if baud rate could be determined
if
(
baud
==
0
)
{
fprintf
(
stderr
,
"==== Baud rate could not be determined. ====
\n
"
);
fprintf
(
stderr
,
"==== Baud rate could not be determined. ====
\n
"
);
return
(
-
1
);
}
else
{
fprintf
(
stderr
,
"==== Baud rate set to %7d ====
\n
"
,
baud
);
fprintf
(
stderr
,
"==== Baud rate set to %7d ====
\n
"
,
baud
);
return
(
0
);
}
...
...
@@ -261,63 +258,62 @@ int Driver_Obs_Serial::find_baud(char* init_chars[], int init_chars_num) {
* Test function is passed.
*
******************************************************************************/
int
Driver_Obs_Serial
::
find_baud
(
Driver_Obs_Serial
*
context
,
int
(
Driver_Obs_Serial
::*
check_baud
)(
int
))
{
////////////////////////////////////////////////////////////////////////
//
// Try to auto-determine the baud rate
//
////////////////////////////////////////////////////////////////////////
int
baud
=
0
;
char
buf
[
100
]
=
""
;
int
*
valid_baudrates
;
int
valid_baudrates_size
;
// Get the valid baudrates from the serial driver
serial
.
get_baudrates
(
&
valid_baudrates
,
&
valid_baudrates_size
);
// Try different baudrates
for
(
int
i
=
0
;
i
<
valid_baudrates_size
;
i
++
)
{
//fprintf(stderr,"---- Test baud rate: %7d ----\n", valid_baudrates[i]);
if
(
context
->
check_baud
(
valid_baudrates
[
i
])
==
1
)
{
baud
=
valid_baudrates
[
i
];
break
;
}
/* This has to be in loop as characters received at false baud rate
somehow can corrupt the value in valid_baudrates_size (Buffer overrun
in driver_serial::receive */
serial
.
get_baudrates
(
&
valid_baudrates
,
&
valid_baudrates_size
);
//fprintf(stderr," %d\n",valid_baudrates_size);
}
// Check if baud rate could be determined
if
(
baud
==
0
)
{
fprintf
(
stderr
,
"==== Baud rate could not be determined. ====
\n
"
);
return
(
-
1
);
}
else
{
fprintf
(
stderr
,
"==== Baud rate set to %7d ====
\n
"
,
baud
);
return
(
0
);
}
int
Driver_Obs_Serial
::
find_baud
(
Driver_Obs_Serial
*
context
,
int
(
Driver_Obs_Serial
::*
check_baud
)(
int
))
{
////////////////////////////////////////////////////////////////////////
//
// Try to auto-determine the baud rate
//
////////////////////////////////////////////////////////////////////////
int
baud
=
0
;
char
buf
[
100
]
=
""
;
int
*
valid_baudrates
;
int
valid_baudrates_size
;
// Get the valid baudrates from the serial driver
serial
.
get_baudrates
(
&
valid_baudrates
,
&
valid_baudrates_size
);
// Try different baudrates
for
(
int
i
=
0
;
i
<
valid_baudrates_size
;
i
++
)
{
//fprintf(stderr,"---- Test baud rate: %7d ----\n", valid_baudrates[i]);
if
(
context
->
check_baud
(
valid_baudrates
[
i
])
==
1
)
{
baud
=
valid_baudrates
[
i
];
break
;
}
/* This has to be in loop as characters received at false baud rate
somehow can corrupt the value in valid_baudrates_size (Buffer overrun
in driver_serial::receive */
serial
.
get_baudrates
(
&
valid_baudrates
,
&
valid_baudrates_size
);
//fprintf(stderr," %d\n",valid_baudrates_size);
}
// Check if baud rate could be determined
if
(
baud
==
0
)
{
fprintf
(
stderr
,
"==== Baud rate could not be determined. ====
\n
"
);
return
(
-
1
);
}
else
{
fprintf
(
stderr
,
"==== Baud rate set to %7d ====
\n
"
,
baud
);
return
(
0
);
}
}
int
Driver_Obs_Serial
::
check_baud
(
int
baud
){
fprintf
(
stderr
,
"WRONG
\n
"
);
return
(
0
);
int
Driver_Obs_Serial
::
check_baud
(
int
baud
)
{
fprintf
(
stderr
,
"WRONG
\n
"
);
return
(
0
);
}
int
Driver_Obs_Serial
::
set_ignnul
(
bool
ignnul
){
if
(
serial
.
set_ignnul
(
ignnul
)){
return
(
0
);
}
return
(
-
1
);
}
\ No newline at end of file
int
Driver_Obs_Serial
::
set_ignnul
(
bool
ignnul
)
{
if
(
serial
.
set_ignnul
(
ignnul
))
{
return
(
0
);
}
return
(
-
1
);
}
src/Filter_Obs.cpp
View file @
097d880b
This diff is collapsed.
Click to expand it.
src/Obs_Calibration_Vector.cpp
View file @
097d880b
...
...
@@ -10,29 +10,31 @@
#include
<time.h>
Obs_Calibration_Vector
::
Obs_Calibration_Vector
(
int
N
,
double
offset
,
double
scale
){
double
scale
)
{
this
->
N
=
N
;
this
->
N
=
N
;
this
->
offset
.
assign
(
N
,
offset
);
this
->
scale
.
assign
(
N
,
scale
);
this
->
offset
.
assign
(
N
,
offset
);
this
->
scale
.
assign
(
N
,
scale
);
if
(
offset
==
0
&&
scale
==
1
){
do_cal
=
0
;
if
(
offset
==
0
&&
scale
==
1
)
{
do_cal
=
0
;
}
else
{
do_cal
=
1
;
do_cal
=
1
;
}
}
Obs_Calibration_Vector
::
Obs_Calibration_Vector
(
std
::
vector
<
double
>
&
offset
,
std
::
vector
<
double
>
&
scale
){
Obs_Calibration_Vector
::
Obs_Calibration_Vector
(
std
::
vector
<
double
>
&
offset
,
std
::
vector
<
double
>
&
scale
)
{
N
=
offset
.
size
();
N
=
offset
.
size
();
if
(
offset
.
size
()
!=
scale
.
size
()){
if
(
offset
.
size
()
!=
scale
.
size
())
{
std
::
cerr
<<
"Size of offset and scale do not match!"
<<
std
::
endl
;
if
(
offset
.
size
()
>
scale
.
size
());
N
=
scale
.
size
();
if
(
offset
.
size
()
>
scale
.
size
())
;
N
=
scale
.
size
();
}
// Set size and replace
...
...
@@ -40,10 +42,14 @@ Obs_Calibration_Vector::Obs_Calibration_Vector(std::vector<double>& offset,
this
->
scale
=
scale
;
int
i
=
0
;
while
(
do_cal
==
0
&&
i
<
N
){
if
(
offset
[
i
]
!=
0
);
do_cal
=
1
;
if
(
scale
[
i
]
!=
1
);
do_cal
=
1
;
int
i
=
0
;
while
(
do_cal
==
0
&&
i
<
N
)
{
if
(
offset
[
i
]
!=
0
)
;
do_cal
=
1
;
if
(
scale
[
i
]
!=
1
)
;
do_cal
=
1
;
}
}
...
...
@@ -61,59 +67,58 @@ Obs_Calibration_Vector::Obs_Calibration_Vector(std::vector<double>& offset,
* Calibrate generic data *
* *
***************************************************************************/
int
Obs_Calibration_Vector
::
calibrate
(
data_obs
*
data
){
int
Obs_Calibration_Vector
::
calibrate
(
data_obs
*
data
)
{
if
(
do_cal
){
if
(
do_cal
)
{
// Check compatibility with data
if
(
data
->
get_Ndata
()
+
data
->
get_Nsupp
()
!=
N
){
std
::
cerr
<<
"Calibration constants do not match data!"
<<
std
::
endl
;
return
(
-
1
);
}
// Check compatibility with data
if
(
data
->
get_Ndata
()
+
data
->
get_Nsupp
()
!=
N
)
{
std
::
cerr
<<
"Calibration constants do not match data!"
<<
std
::
endl
;
return
(
-
1
);
}
double
MD
=
data
->
get_MD
();
// Use std:vector to get and set data at once
std
::
vector
<
double
>
datav
;
data
->
get_all
(
datav
);
double
MD
=
data
->
get_MD
();
// Use std:vector to get and set data at once
std
::
vector
<
double
>
datav
;
data
->
get_all
(
datav
);
for
(
int
i
=
0
;
i
<
N
;
i
++
){
if
(
datav
[
i
]
!=
MD
)
datav
[
i
]
=
datav
[
i
]
*
scale
[
i
]
+
offset
[
i
];
}
data
->
set_all
(
datav
);
for
(
int
i
=
0
;
i
<
N
;
i
++
)
{
if
(
datav
[
i
]
!=
MD
)
datav
[
i
]
=
datav
[
i
]
*
scale
[
i
]
+
offset
[
i
];
}
data
->
set_all
(
datav
);
}
}
return
(
0
);
return
(
0
);
}
/****************************************************************************
* *
* Calibration constants for the ADC *
* *
***************************************************************************/
int
Obs_Calibration_Vector
::
get_adc_calibrate
(
std
::
string
config
,
std
::
string
&
offset
,
std
::
string
&
scale
){
std
::
unordered_map
<
std
::
string
,
struct
adc_cal
>::
iterator
it
;
struct
adc_cal
cal
;
it
=
map_ADC
.
find
(
config
);
if
(
it
!=
map_ADC
.
end
())
{
cal
=
it
->
second
;
scale
=
cal
.
scale
;
offset
=
cal
.
offset
;
}
else
{
scale
=
""
;
offset
=
""
;
return
(
-
1
);
}
return
(
0
);
std
::
string
&
offset
,
std
::
string
&
scale
)
{
std
::
unordered_map
<
std
::
string
,
struct
adc_cal
>::
iterator
it
;
struct
adc_cal
cal
;
it
=
map_ADC
.
find
(
config
);
if
(
it
!=
map_ADC
.
end
())
{
cal
=
it
->
second
;
scale
=
cal
.
scale
;
offset
=
cal
.
offset
;
}
else
{
scale
=
""
;
offset
=
""
;
return
(
-
1
);
}
return
(
0
);
}
...
...
@@ -122,26 +127,25 @@ int Obs_Calibration_Vector::get_adc_calibrate(std::string config,
* Calibration constants for the ADC *
* *
***************************************************************************/
int
Obs_Calibration_Vector
::
add_adc_calibrate
(
std
::
string
config
,
std
::
string
offset
,
std
::
string
scale
,
std
::
string
comment
){
struct
adc_cal
cal
;
cal
.
offset
=
offset
;
cal
.
scale
=
scale
;
cal
.
comment
=
comment
;
// Check if already exists
if
(
map_ADC
.
count
(
config
)
>
0
)
{
std
::
cerr
<<
"Calibration for "
<<
config
<<
" overwritten."
<<
std
::
endl
;
}
map_ADC
[
config
]
=
cal
;
std
::
cerr
<<
"Calibration for "
<<
config
<<
" newly written."
<<
std
::
endl
;
return
(
0
);
int
Obs_Calibration_Vector
::
add_adc_calibrate
(
std
::
string
config
,
std
::
string
offset
,
std
::
string
scale
,
std
::
string
comment
)
{
struct
adc_cal
cal
;
cal
.
offset
=
offset
;
cal
.
scale
=
scale
;
cal
.
comment
=
comment
;
// Check if already exists
if
(
map_ADC
.
count
(
config
)
>
0
)
{
std
::
cerr
<<
"Calibration for "
<<
config
<<
" overwritten."
<<
std
::
endl
;
}
map_ADC
[
config
]
=
cal
;
std
::
cerr
<<
"Calibration for "
<<
config
<<
" newly written."
<<
std
::
endl
;
return
(
0
);
}
...
...
@@ -150,199 +154,198 @@ int Obs_Calibration_Vector::add_adc_calibrate(std::string config,
* Save the calibration constants to file
*
***************************************************************************/
int
Obs_Calibration_Vector
::
save_adc_calibrate
(
std
::
string
filename
)
{
/* PROBLEM: Save the comments in ADC file when new configurations are
* stored. Cannot simply append, as duplicates need to be treated.
* SOLUTION: Save whole file in string before writing. Then, rewrite
* file. Comment out duplicates instead of overwriting.
*/
std
::
ifstream
file_in
(
filename
);
std
::
stringstream
buf_in
;
std
::
ofstream
file_out
;
std
::
string
line
,
config
,
comment
;
time_t
t
=
time
(
0
);
// Current time
char
time_c
[
20
];
// Current time as c-string
size_t
pos_c
;
struct
adc_cal
cal
,
cal_buf
;
// Copy the file to stringstream
if
(
file_in
)
{
buf_in
<<
file_in
.
rdbuf
();
file_in
.
close
();
}
// Copy the hash map
// The copy will be used to track what has been written to file
std
::
unordered_map
<
std
::
string
,
struct
adc_cal
>
map_ADC_cpy
(
map_ADC
);
// Open file as new blank file
file_out
.
open
(
filename
,
std
::
ios
::
out
|
std
::
ios
::
trunc
);
// Read from buffered file
while
(
getline
(
buf_in
,
line
)
)
{
// Parse from line
// Comment line
if
(
parse_adc_calibration
(
line
,
config
,
cal_buf
,
comment
)
<
0
){
file_out
<<
line
<<
std
::
endl
;
}
else
{
// Configuration exists
if
(
map_ADC_cpy
.
count
(
config
)
>
0
)
{
cal
=
map_ADC_cpy
[
config
];
map_ADC_cpy
.
erase
(
config
);
// Existing config is the same
if
(
cal
==
cal_buf
){
file_out
<<
line
<<
std
::
endl
;
// Existing config is different
}
else
{
file_out
<<
"# "
<<
asctime
(
gmtime
(
&
t
))
<<
" UTC
\n
"
;
file_out
<<
"#"
<<
line
<<
std
::
endl
;
file_out
<<
config
<<
","
<<
cal
.
offset
<<
","
<<
cal
.
scale
<<
";"
<<
comment
<<
std
::
endl
;
}
// Configuration doesn't exist
}
else
{
file_out
<<
line
<<
std
::
endl
;
}
}
}
// Write all remaining configurations
for
(
auto
&
i
:
map_ADC_cpy
){
strftime
(
time_c
,
sizeof
(
time_c
),
"%F %T"
,
gmtime
(
&
t
));
file_out
<<
"
\n
# "
<<
i
.
second
.
comment
<<
" "
<<
time_c
<<
" UTC
\n
"
;
file_out
<<
i
.
first
<<
","
<<
i
.
second
.
scale
<<
","
<<
i
.
second
.
offset
<<
";"
<<
std
::
endl
;
}
file_out
.
close
();
return
(
0
);
}
int
Obs_Calibration_Vector
::
save_adc_calibrate
()
{
if
(
file_ADC
.
empty
())
return
(
-
1
);
return
(
save_adc_calibrate
(
file_ADC
));
}
int
Obs_Calibration_Vector
::
save_adc_calibrate
(
std
::
string
filename
)
{
/* PROBLEM: Save the comments in ADC file when new configurations are
* stored. Cannot simply append, as duplicates need to be treated.