Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion FprimeZephyrReference/Components/PowerMonitor/PowerMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ namespace Components {
// ----------------------------------------------------------------------

PowerMonitor ::PowerMonitor(const char* const compName)
: PowerMonitorComponentBase(compName), m_totalPower_mWh(0.0f), m_lastUpdateTime_s(0.0) {}
: PowerMonitorComponentBase(compName),
m_totalPower_mWh(0.0f),
m_totalGeneration_mWh(0.0f),
m_lastUpdateTime_s(0.0) {}

PowerMonitor ::~PowerMonitor() {}

Expand All @@ -35,6 +38,9 @@ void PowerMonitor ::run_handler(FwIndexType portNum, U32 context) {
// Update total power consumption with combined system and solar power
F64 totalPowerW = sysPowerW + solPowerW;
this->updatePower(totalPowerW);

// Update total solar power generation
this->updateGeneration(solPowerW);
}

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

void PowerMonitor ::RESET_TOTAL_GENERATION_cmdHandler(FwOpcodeType opCode, U32 cmdSeq) {
this->m_totalGeneration_mWh = 0.0f;
this->log_ACTIVITY_LO_TotalGenerationReset();
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
}

// ----------------------------------------------------------------------
// Helper method implementations
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -88,4 +100,32 @@ void PowerMonitor ::updatePower(F64 powerW) {
this->tlmWrite_TotalPowerConsumption(this->m_totalPower_mWh);
}

void PowerMonitor ::updateGeneration(F64 powerW) {
// Guard against invalid power values
if (powerW < 0.0 || powerW > 1000.0) { // Sanity check: power should be 0-1000W
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verify if the INA219Manager is able to return negative power numbers or if the number will always be positive. It is possible for the sensor to read a negative power number if power flows backwards through the shunt resistor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The INA219 sensor can return negative power values when current flows backwards through the shunt resistor. The current implementation at line 105 rejects negative values with the guard if (powerW < 0.0 || powerW > 1000.0).

For solar power generation tracking, this guard may need adjustment depending on how the sensor is wired:

  • If wired to measure charging current as positive, negative values would indicate discharge and should be ignored for generation tracking
  • If wired the opposite way, the logic would need to be inverted

The current implementation assumes positive power = generation and rejects negative values. This should be verified against the actual hardware wiring. If negative values are valid for generation (e.g., sensor wired backwards), the guard should be changed to if (powerW > -1000.0 && powerW < 0.0) and use abs(powerW) for accumulation, or adjust based on the specific wiring configuration.

return;
}

F64 now_s = this->getCurrentTimeSeconds();

// Initialize time on first call
if (this->m_lastUpdateTime_s == 0.0) {
// Emit initial telemetry value
this->tlmWrite_TotalPowerGenerated(this->m_totalGeneration_mWh);
return;
}

F64 dt_s = now_s - this->m_lastUpdateTime_s;

// Only accumulate if time has passed and delta is reasonable (< 10 seconds to avoid time jumps)
if (dt_s > 0.0 && dt_s < 10.0) {
// Convert to mWh: Power (W) * time (hours) * 1000
F32 energyAdded_mWh = static_cast<F32>(powerW * (dt_s / 3600.0) * 1000.0);
this->m_totalGeneration_mWh += energyAdded_mWh;
}

// Emit telemetry update
this->tlmWrite_TotalPowerGenerated(this->m_totalGeneration_mWh);
}

} // namespace Components
11 changes: 11 additions & 0 deletions FprimeZephyrReference/Components/PowerMonitor/PowerMonitor.fpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,25 @@ module Components {
@ Command to reset the accumulated power consumption
sync command RESET_TOTAL_POWER()

@ Command to reset the accumulated power generation
sync command RESET_TOTAL_GENERATION()

@ Telemetry channel for accumulated power consumption in mWh
telemetry TotalPowerConsumption: F32

@ Telemetry channel for accumulated solar power generation in mWh
telemetry TotalPowerGenerated: F32

@ Event logged when total power consumption is reset
event TotalPowerReset() \
severity activity low \
format "Total power consumption reset to 0 mWh"

@ Event logged when total power generation is reset
event TotalGenerationReset() \
severity activity low \
format "Total power generation reset to 0 mWh"

###############################################################################
# Standard AC Ports: Required for Channels, Events, Commands, and Parameters #
###############################################################################
Expand Down
11 changes: 11 additions & 0 deletions FprimeZephyrReference/Components/PowerMonitor/PowerMonitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class PowerMonitor final : public PowerMonitorComponentBase {
U32 cmdSeq //!< The command sequence number
) override;

//! Handler implementation for RESET_TOTAL_GENERATION
void RESET_TOTAL_GENERATION_cmdHandler(FwOpcodeType opCode, //!< The opcode
U32 cmdSeq //!< The command sequence number
) override;

// ----------------------------------------------------------------------
// Helper methods
// ----------------------------------------------------------------------
Expand All @@ -52,13 +57,19 @@ class PowerMonitor final : public PowerMonitorComponentBase {
//! Update power consumption with new power reading
void updatePower(F64 powerW);

//! Update solar power generation with new power reading
void updateGeneration(F64 powerW);

// ----------------------------------------------------------------------
// Member variables
// ----------------------------------------------------------------------

//! Accumulated power consumption in mWh
F32 m_totalPower_mWh;

//! Accumulated solar power generation in mWh
F32 m_totalGeneration_mWh;

//! Last update time in seconds
F64 m_lastUpdateTime_s;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ telemetry packets ReferenceDeploymentPackets {
ReferenceDeployment.ina219SolManager.Current
ReferenceDeployment.ina219SolManager.Power
ReferenceDeployment.powerMonitor.TotalPowerConsumption
ReferenceDeployment.powerMonitor.TotalPowerGenerated
}

} omit {
Expand Down