Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Sensor System
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Institut für Informatik
QE Research Group
FORTE
Sensor System
Commits
6977432d
Verified
Commit
6977432d
authored
2 years ago
by
Zoe Michaela Dietmar Pfister
Browse files
Options
Downloads
Patches
Plain Diff
Allow setting custom pins for ina219.cpp, add ina to main central mast
parent
1f7f284a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!39
Merge Develop into Main
,
!29
Allow setting custom pins for ina219.cpp, add ina to main central mast
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
client/client_central_mast/src/main.cpp
+113
-63
113 additions, 63 deletions
client/client_central_mast/src/main.cpp
client/libs/ina219/ina219.cpp
+6
-1
6 additions, 1 deletion
client/libs/ina219/ina219.cpp
client/libs/ina219/ina219.hpp
+7
-0
7 additions, 0 deletions
client/libs/ina219/ina219.hpp
with
126 additions
and
64 deletions
client/client_central_mast/src/main.cpp
+
113
−
63
View file @
6977432d
...
...
@@ -3,82 +3,132 @@
#include
"NoDataAvailableException.hpp"
#include
"rs485.hpp"
#include
<list>
#include
<ina219.hpp>
static
const
char
*
TAG
=
"MAIN"
;
#define uS_TO_S_FACTOR 1000000
/* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 5 *
60
/* Time ESP32 will go to sleep (in seconds) */
#define TIME_TO_SLEEP 5 *
1
/* Time ESP32 will go to sleep (in seconds) */
Forte_RS485
rs485
;
ForteINA219
ina219
;
float
getBatteryVoltage
()
{
//************ Measuring Battery Voltage ***********
// reference voltage of microcontroller 3.3v for esp32
const
float
reference_vcc
=
3.3
;
// value of R2 resistor [kOhm]
const
float
bat_res_gnd
=
20
;
// value of R1 resistor [kOhm]
const
float
bat_res_vcc
=
68
;
// max ADC value
const
int
adc
=
4095
;
float
sample
=
0
;
// Get 100 analog read to prevent unusefully read
for
(
int
i
=
0
;
i
<
100
;
i
++
)
{
sample
=
sample
+
analogRead
(
35
);
// read the voltage from the divider circuit
delay
(
2
);
}
sample
=
sample
/
100
;
float
battery_voltage
=
(
sample
/
4095
*
reference_vcc
*
(
bat_res_vcc
+
bat_res_gnd
)
/
bat_res_gnd
);
ESP_LOGI
(
TAG
,
"Battery Voltage: %4.2f V"
,
battery_voltage
);
ESP_LOGD
(
TAG
,
"ADC mean Value: %4.2f"
,
sample
);
return
battery_voltage
;
float
getBatteryVoltage
()
{
//************ Measuring Battery Voltage ***********
// reference voltage of microcontroller 3.3v for esp32
const
float
reference_vcc
=
3.3
;
// value of R2 resistor [kOhm]
const
float
bat_res_gnd
=
20
;
// value of R1 resistor [kOhm]
const
float
bat_res_vcc
=
68
;
// max ADC value
const
int
adc
=
4095
;
float
sample
=
0
;
// Get 100 analog read to prevent unusefully read
for
(
int
i
=
0
;
i
<
100
;
i
++
)
{
sample
=
sample
+
analogRead
(
35
);
// read the voltage from the divider circuit
delay
(
2
);
}
sample
=
sample
/
100
;
float
battery_voltage
=
(
sample
/
4095
*
reference_vcc
*
(
bat_res_vcc
+
bat_res_gnd
)
/
bat_res_gnd
);
ESP_LOGI
(
TAG
,
"Battery Voltage: %4.2f V"
,
battery_voltage
);
ESP_LOGD
(
TAG
,
"ADC mean Value: %4.2f"
,
sample
);
return
battery_voltage
;
}
// FIXME: put this in ESPNow.hpp and let stupid Markus Rampp know how you did it. After years of Python he moved past
// the idea of types and declarations
void
send_msgs
(
const
std
::
__cxx11
::
list
<
Message
>
msgs
)
{
for
(
const
Message
&
msg
:
msgs
)
{
if
(
msg
.
send
()
!=
ESP_OK
)
{
RtcMemory
::
store_data
(
msg
.
getMessageAsMinifiedJsonString
());
}
unsigned
long
ts
=
millis
();
// it takes ~110ms for receiving an acknowledgement by the host in perfect conditions
uint16_t
message_timeout
=
2000
;
while
(
!
was_msg_received
())
{
if
((
millis
()
-
ts
)
>
message_timeout
)
{
RtcMemory
::
store_data
(
msg
.
getMessageAsMinifiedJsonString
());
ESP_LOGE
(
TAG
,
"Timeout: Host not available
\n
"
);
break
;
}
}
ESP_LOGD
(
TAG
,
"Time until acknowledgement: %ld"
,
millis
()
-
ts
);
}
void
send_msgs
(
const
std
::
__cxx11
::
list
<
Message
>
msgs
)
{
for
(
const
Message
&
msg
:
msgs
)
{
if
(
msg
.
send
()
!=
ESP_OK
)
{
RtcMemory
::
store_data
(
msg
.
getMessageAsMinifiedJsonString
());
}
unsigned
long
ts
=
millis
();
// it takes ~110ms for receiving an acknowledgement by the host in perfect conditions
uint16_t
message_timeout
=
2000
;
while
(
!
was_msg_received
())
{
if
((
millis
()
-
ts
)
>
message_timeout
)
{
RtcMemory
::
store_data
(
msg
.
getMessageAsMinifiedJsonString
());
ESP_LOGE
(
TAG
,
"Timeout: Host not available
\n
"
);
break
;
}
}
ESP_LOGD
(
TAG
,
"Time until acknowledgement: %ld"
,
millis
()
-
ts
);
}
}
void
setup
()
{
// whole loop should be around ~3000 ms
Serial
.
begin
(
115200
);
rs485
.
setup
();
getBatteryVoltage
();
// ~2100ms
try
{
auto
messages
=
rs485
.
buildMessages
();
espnow_setup
();
send_msgs
(
messages
);
}
catch
(
const
NoDataAvailableException
&
e
)
{
std
::
cerr
<<
e
.
what
()
<<
'\n'
;
}
ESP_LOGD
(
TAG
,
"Going to sleep for %d seconds"
,
TIME_TO_SLEEP
);
esp_sleep_enable_timer_wakeup
(
TIME_TO_SLEEP
*
uS_TO_S_FACTOR
);
esp_deep_sleep_start
();
void
setup
()
{
// whole loop should be around ~3000 ms
Serial
.
begin
(
115200
);
rs485
.
setup
();
getBatteryVoltage
();
ina219
.
setup
();
ina219
.
setPinSetup
({
21
,
22
});
// ~2100ms
try
{
auto
messages
=
rs485
.
buildMessages
();
// split into arrays of 6 messages TODO: Make this cleaner with default constructor
std
::
array
<
Message
,
6
>
messages_1
=
{
Message
::
nullMessage
(),
Message
::
nullMessage
(),
Message
::
nullMessage
(),
Message
::
nullMessage
(),
Message
::
nullMessage
(),
Message
::
nullMessage
()};
std
::
array
<
Message
,
6
>
messages_2
=
{
Message
::
nullMessage
(),
Message
::
nullMessage
(),
Message
::
nullMessage
(),
Message
::
nullMessage
(),
Message
::
nullMessage
(),
Message
::
nullMessage
()};
auto
ina219_messages
=
ina219
.
buildMessages
();
std
::
array
<
Message
,
6
>
ina_array
=
{
Message
::
nullMessage
(),
Message
::
nullMessage
(),
Message
::
nullMessage
(),
Message
::
nullMessage
(),
Message
::
nullMessage
(),
Message
::
nullMessage
()};
// log ina messages
for
(
const
auto
&
msg
:
ina219_messages
)
{
ESP_LOGI
(
TAG
,
"INA219 Message: %s"
,
msg
.
getMessageAsMinifiedJsonString
().
c_str
());
}
int
i
=
0
;
for
(
const
auto
&
msg
:
messages
)
{
if
(
i
<
6
)
{
messages_1
[
i
]
=
msg
;
}
else
{
messages_2
[
i
-
6
]
=
msg
;
}
i
++
;
}
i
=
0
;
for
(
const
auto
&
msg
:
ina219_messages
)
{
ina_array
[
i
]
=
msg
;
i
++
;
}
espnow_setup
();
Message
::
sendMessages
(
messages_1
);
Message
::
sendMessages
(
messages_2
);
Message
::
sendMessages
(
ina_array
);
}
catch
(
const
NoDataAvailableException
&
e
)
{
std
::
cerr
<<
e
.
what
()
<<
'\n'
;
}
ESP_LOGD
(
TAG
,
"Going to sleep for %d seconds"
,
TIME_TO_SLEEP
);
esp_sleep_enable_timer_wakeup
(
TIME_TO_SLEEP
*
uS_TO_S_FACTOR
);
esp_deep_sleep_start
();
}
void
loop
()
{}
This diff is collapsed.
Click to expand it.
client/libs/ina219/ina219.cpp
+
6
−
1
View file @
6977432d
#include
"ina219.hpp"
void
ForteINA219
::
setup
()
{
Wire
.
begin
(
I2C_SDA
,
I2C_SCL
);
Wire
.
begin
(
pinSetup
.
sda
,
pinSetup
.
scl
);
if
(
!
ina219
.
init
())
{
// Sensor init went wrong
ESP_LOGW
(
sensorInformation
.
getHardwareNameString
().
c_str
(),
...
...
@@ -56,6 +56,11 @@ std::list<Message> ForteINA219::buildMessages() {
return
messages
;
}
void
ForteINA219
::
setPinSetup
(
pin_setup_ina219
pins
){
this
->
pinSetup
=
pins
;
}
SensorInformation
ForteINA219
::
getSensorInformation
()
const
{
return
sensorInformation
;
}
This diff is collapsed.
Click to expand it.
client/libs/ina219/ina219.hpp
+
7
−
0
View file @
6977432d
...
...
@@ -19,16 +19,23 @@ struct out_data_ina219 {
bool
ina219_overflow
=
false
;
};
struct
pin_setup_ina219
{
uint8_t
sda
;
uint8_t
scl
;
};
class
ForteINA219
:
public
ForteSensor
<
out_data_ina219
>
{
public:
void
setup
()
override
;
out_data_ina219
readData
()
override
;
std
::
list
<
Message
>
buildMessages
()
override
;
[[
nodiscard
]]
SensorInformation
getSensorInformation
()
const
override
;
void
setPinSetup
(
pin_setup_ina219
pinSetup
);
private:
INA219_WE
ina219
;
out_data_ina219
data
;
pin_setup_ina219
pinSetup
{
21
,
22
};
const
SensorInformation
sensorInformation
{
HardwareName
::
INA219
,
SensorProtocol
::
I2C
};
const
uint8_t
INA219_BASE_ADDRESS
=
0x40
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment