SatNOGS-COMMS  4.1.0
A COMMS subsystem for CubeSats
Loading...
Searching...
No Matches
power.cpp
Go to the documentation of this file.
1/*
2 * SatNOGS-COMMS control library
3 *
4 * Copyright (C) 2023, Libre Space Foundation <http://libre.space>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: GNU General Public License v3.0 or later
20 */
21
24
25namespace satnogs::comms::lib
26{
27
34void
35power::enable(subsys sys, bool en)
36{
37 switch (sys) {
38 case subsys::CAN1:
39 m_can1_en.set(en);
40 m_can1_low_pwr.set(!en);
41 break;
42 case subsys::CAN2:
43 m_can2_en.set(en);
44 m_can2_low_pwr.set(!en);
45 break;
46 case subsys::RF_5V:
47 if (en) {
48 m_imon_5v_rf.start();
49 } else {
50 m_imon_5v_rf.stop();
51 }
52 m_rf_5v_en.set(en);
53 break;
54 case subsys::FPGA_5V:
55 if (en) {
56 m_imon_fpga.start();
57 } else {
58 m_imon_fpga.stop();
59 }
60 m_fpga_5v_en.set(en);
61 break;
63 m_can1_low_pwr.set(en);
64 break;
66 m_can2_low_pwr.set(en);
67 break;
68 case subsys::UHF:
69 m_uhf_en.set(en);
70 break;
71 case subsys::SBAND:
72 m_sband_en.set(en);
73 break;
74 default:
75 throw inval_arg_exception(__FILE__, __LINE__);
76 }
77}
78
85bool
87{
88 switch (sys) {
89 case subsys::CAN1:
90 return m_can1_en.get();
91 case subsys::CAN2:
92 return m_can2_en.get();
93 case subsys::RF_5V:
94 return m_rf_5v_en.get();
95 case subsys::FPGA_5V:
96 return m_fpga_5v_en.get();
98 return m_can1_low_pwr.get();
100 return m_can2_low_pwr.get();
101 case subsys::UHF:
102 return m_uhf_en.get();
103 case subsys::SBAND:
104 return m_sband_en.get();
105 default:
106 throw inval_arg_exception(__FILE__, __LINE__);
107 }
108}
109
116bool
118{
119 switch (tp) {
120 /* In v0.3 if the 3V3 has fault, the MCU will reset */
122 return m_pgood_5v.get();
124 return m_pgood_fpga.get();
126 return m_uhf_pgood.get();
128 return m_sband_pgood.get();
129 default:
130 throw inval_arg_exception(__FILE__, __LINE__);
131 }
132}
133
145float
147{
148 try {
149 switch (sys) {
150 case channel::VIN:
151 return m_monitor.get_source_voltage();
152 case channel::V_BAT:
153 return m_v_mon.vbat();
154 default:
155 throw inval_arg_exception(__FILE__, __LINE__);
156 }
157 } catch (...) {
158 return std::nanf("nan");
159 }
160}
161
173float
175{
176 try {
177 switch (sys) {
178 case channel::DIG_3V3:
179 return (m_imon_3v3.voltage() * 1000000 /
180 (m_efuse_adc_current_gain * m_r_lim.dig_3v3));
181 case channel::RF_5V:
182 return (m_imon_5v_rf.voltage() * 1000000 /
183 (m_efuse_adc_current_gain * m_r_lim.rf_5v));
184 case channel::FPGA:
185 return (m_imon_fpga.voltage() * 1000000 /
186 (m_efuse_adc_current_gain * m_r_lim.fpga_5v));
187 case channel::VIN:
188 return m_monitor.get_sense_current();
189 default:
190 throw inval_arg_exception(__FILE__, __LINE__);
191 return 0;
192 }
193 } catch (...) {
194 return std::nanf("nan");
195 }
196}
197
208float
210{
211 try {
212 switch (src) {
213 case sensor::EFUSES:
214 return (power::current(channel::DIG_3V3) * 3.3f +
217 case sensor::EMC1702:
218 return m_monitor.get_power();
219 case sensor::AVERAGE:
220 return (((power::current(channel::DIG_3V3) * 3.3f +
223 m_monitor.get_power()) /
224 2);
225 default:
226 throw inval_arg_exception(__FILE__, __LINE__);
227 }
228 } catch (...) {
229 return std::nanf("nan");
230 }
231}
232
234 : m_rf_5v_en(io.rf_5v_en),
235 m_fpga_5v_en(io.fpga_5v_en),
236 m_can1_en(io.can1_en),
237 m_can1_low_pwr(io.can1_low_pwr),
238 m_can2_en(io.can2_en),
239 m_can2_low_pwr(io.can2_low_pwr),
240 m_pgood_5v(io.rf_5v_pgood),
241 m_pgood_fpga(io.fpga_5v_pgood),
242 m_monitor("current-sensor", io.mon_i2c, 0b101101),
243 m_uhf_en(io.uhf_en),
244 m_uhf_pgood(io.uhf_pgood),
245 m_sband_en(io.sband_en),
246 m_sband_pgood(io.sband_pgood),
247 m_imon_5v_rf(io.imon_5v_rf),
248 m_imon_3v3(io.imon_3v3_d),
249 m_imon_fpga(io.imon_fpga),
250 m_v_mon(io.v_mon),
251 m_r_lim(io.rlim),
252 m_efuse_adc_current_gain(io.efuse_adc_current_gain)
253
254{
255 /* Disable all at startup */
256 enable(subsys::CAN1, false);
257 enable(subsys::CAN2, false);
258 enable(subsys::RF_5V, false);
259 enable(subsys::FPGA_5V, false);
260 enable(subsys::UHF, false);
261 enable(subsys::SBAND, false);
262
263 /* Start the ADC of the 3V3 which remains always on */
264 m_imon_3v3.start();
265}
266
267} // namespace satnogs::comms::lib
Generic exception indicating an invalid argument.
Represents the I/O configuration for the power management system.
Definition power.hpp:143
float voltage(channel sys) const
Retrieves the voltage of a specified channel.
Definition power.cpp:146
float current(channel sys) const
Retrieves the current of a specified channel.
Definition power.cpp:174
power(const io_conf &io)
Definition power.cpp:233
bool pgood(pgood_tp tp) const
Gets the power good indication from various power supplies.
Definition power.cpp:117
sensor
Identifies power measurement sources.
Definition power.hpp:66
@ AVERAGE
Average power from all available sources.
Definition power.hpp:69
@ EFUSES
Power measured by eFuses.
Definition power.hpp:67
@ EMC1702
Power measured by the EMC1702 sensor.
Definition power.hpp:68
pgood_tp
Power good indicator.
Definition power.hpp:90
float get_power(sensor src) const
Calculates the power consumption for a specified sensor source.
Definition power.cpp:209
subsys
Identifies subsystems managed by the power class.
Definition power.hpp:50
@ CAN2_LPWR
Low-power mode for CAN Bus 2.
Definition power.hpp:56
@ CAN1_LPWR
Low-power mode for CAN Bus 1.
Definition power.hpp:55
channel
Identifies voltage and current measurement channels.
Definition power.hpp:77
@ RF_5V
RF 5V voltage/current channel.
Definition power.hpp:79
@ V_BAT
Battery voltage channel.
Definition power.hpp:82
@ FPGA
FPGA voltage/current channel.
Definition power.hpp:78
@ DIG_3V3
Digital 3.3V voltage/current channel.
Definition power.hpp:80
@ VIN
Input voltage channel.
Definition power.hpp:81
void enable(subsys sys, bool en=true)
Enable/disable the power of subsystems.
Definition power.cpp:35
bool enabled(subsys sys) const
Checks the if the subsystem is enabled.
Definition power.cpp:86
const struct gpio_dt_spec sband_pgood
Definition startup.cpp:90
const struct gpio_dt_spec can1_en
Definition startup.cpp:63
const struct gpio_dt_spec can2_low_pwr
Definition startup.cpp:69
const struct gpio_dt_spec can2_en
Definition startup.cpp:67
const struct gpio_dt_spec uhf_pgood
Definition startup.cpp:86
const struct gpio_dt_spec can1_low_pwr
Definition startup.cpp:65