Skip to content

Commit 3dfbb9d

Browse files
CopilotMikefly123
andcommitted
Add TotalPowerGenerated telemetry for solar power tracking
- Added TotalPowerGenerated telemetry channel (F32, mWh) - Added RESET_TOTAL_GENERATION command to reset solar generation - Added TotalGenerationReset event - Implemented updateGeneration() helper to track solar power separately - Added m_totalGeneration_mWh member variable - Added TotalPowerGenerated to PowerMonitor packet (id 9) - Solar power is now tracked independently from consumption - All formatters pass Co-authored-by: Mikefly123 <[email protected]>
1 parent 759d02c commit 3dfbb9d

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

FprimeZephyrReference/Components/PowerMonitor/PowerMonitor.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ namespace Components {
1313
// ----------------------------------------------------------------------
1414

1515
PowerMonitor ::PowerMonitor(const char* const compName)
16-
: PowerMonitorComponentBase(compName), m_totalPower_mWh(0.0f), m_lastUpdateTime_s(0.0) {}
16+
: PowerMonitorComponentBase(compName),
17+
m_totalPower_mWh(0.0f),
18+
m_totalGeneration_mWh(0.0f),
19+
m_lastUpdateTime_s(0.0) {}
1720

1821
PowerMonitor ::~PowerMonitor() {}
1922

@@ -35,6 +38,9 @@ void PowerMonitor ::run_handler(FwIndexType portNum, U32 context) {
3538
// Update total power consumption with combined system and solar power
3639
F64 totalPowerW = sysPowerW + solPowerW;
3740
this->updatePower(totalPowerW);
41+
42+
// Update total solar power generation
43+
this->updateGeneration(solPowerW);
3844
}
3945

4046
// ----------------------------------------------------------------------
@@ -48,6 +54,12 @@ void PowerMonitor ::RESET_TOTAL_POWER_cmdHandler(FwOpcodeType opCode, U32 cmdSeq
4854
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
4955
}
5056

57+
void PowerMonitor ::RESET_TOTAL_GENERATION_cmdHandler(FwOpcodeType opCode, U32 cmdSeq) {
58+
this->m_totalGeneration_mWh = 0.0f;
59+
this->log_ACTIVITY_LO_TotalGenerationReset();
60+
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
61+
}
62+
5163
// ----------------------------------------------------------------------
5264
// Helper method implementations
5365
// ----------------------------------------------------------------------
@@ -88,4 +100,32 @@ void PowerMonitor ::updatePower(F64 powerW) {
88100
this->tlmWrite_TotalPowerConsumption(this->m_totalPower_mWh);
89101
}
90102

103+
void PowerMonitor ::updateGeneration(F64 powerW) {
104+
// Guard against invalid power values
105+
if (powerW < 0.0 || powerW > 1000.0) { // Sanity check: power should be 0-1000W
106+
return;
107+
}
108+
109+
F64 now_s = this->getCurrentTimeSeconds();
110+
111+
// Initialize time on first call
112+
if (this->m_lastUpdateTime_s == 0.0) {
113+
// Emit initial telemetry value
114+
this->tlmWrite_TotalPowerGenerated(this->m_totalGeneration_mWh);
115+
return;
116+
}
117+
118+
F64 dt_s = now_s - this->m_lastUpdateTime_s;
119+
120+
// Only accumulate if time has passed and delta is reasonable (< 10 seconds to avoid time jumps)
121+
if (dt_s > 0.0 && dt_s < 10.0) {
122+
// Convert to mWh: Power (W) * time (hours) * 1000
123+
F32 energyAdded_mWh = static_cast<F32>(powerW * (dt_s / 3600.0) * 1000.0);
124+
this->m_totalGeneration_mWh += energyAdded_mWh;
125+
}
126+
127+
// Emit telemetry update
128+
this->tlmWrite_TotalPowerGenerated(this->m_totalGeneration_mWh);
129+
}
130+
91131
} // namespace Components

FprimeZephyrReference/Components/PowerMonitor/PowerMonitor.fpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,25 @@ module Components {
2424
@ Command to reset the accumulated power consumption
2525
sync command RESET_TOTAL_POWER()
2626

27+
@ Command to reset the accumulated power generation
28+
sync command RESET_TOTAL_GENERATION()
29+
2730
@ Telemetry channel for accumulated power consumption in mWh
2831
telemetry TotalPowerConsumption: F32
2932

33+
@ Telemetry channel for accumulated solar power generation in mWh
34+
telemetry TotalPowerGenerated: F32
35+
3036
@ Event logged when total power consumption is reset
3137
event TotalPowerReset() \
3238
severity activity low \
3339
format "Total power consumption reset to 0 mWh"
3440

41+
@ Event logged when total power generation is reset
42+
event TotalGenerationReset() \
43+
severity activity low \
44+
format "Total power generation reset to 0 mWh"
45+
3546
###############################################################################
3647
# Standard AC Ports: Required for Channels, Events, Commands, and Parameters #
3748
###############################################################################

FprimeZephyrReference/Components/PowerMonitor/PowerMonitor.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class PowerMonitor final : public PowerMonitorComponentBase {
4242
U32 cmdSeq //!< The command sequence number
4343
) override;
4444

45+
//! Handler implementation for RESET_TOTAL_GENERATION
46+
void RESET_TOTAL_GENERATION_cmdHandler(FwOpcodeType opCode, //!< The opcode
47+
U32 cmdSeq //!< The command sequence number
48+
) override;
49+
4550
// ----------------------------------------------------------------------
4651
// Helper methods
4752
// ----------------------------------------------------------------------
@@ -52,13 +57,19 @@ class PowerMonitor final : public PowerMonitorComponentBase {
5257
//! Update power consumption with new power reading
5358
void updatePower(F64 powerW);
5459

60+
//! Update solar power generation with new power reading
61+
void updateGeneration(F64 powerW);
62+
5563
// ----------------------------------------------------------------------
5664
// Member variables
5765
// ----------------------------------------------------------------------
5866

5967
//! Accumulated power consumption in mWh
6068
F32 m_totalPower_mWh;
6169

70+
//! Accumulated solar power generation in mWh
71+
F32 m_totalGeneration_mWh;
72+
6273
//! Last update time in seconds
6374
F64 m_lastUpdateTime_s;
6475
};

FprimeZephyrReference/ReferenceDeployment/Top/ReferenceDeploymentPackets.fppi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ telemetry packets ReferenceDeploymentPackets {
6969
ReferenceDeployment.ina219SolManager.Current
7070
ReferenceDeployment.ina219SolManager.Power
7171
ReferenceDeployment.powerMonitor.TotalPowerConsumption
72+
ReferenceDeployment.powerMonitor.TotalPowerGenerated
7273
}
7374

7475
} omit {

0 commit comments

Comments
 (0)