SatNOGS-COMMS  4.1.0
A COMMS subsystem for CubeSats
Loading...
Searching...
No Matches
emc1702.hpp
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
22#pragma once
23
24#include <etl/string.h>
25#include <etl/vector.h>
29
30namespace satnogs::comms::lib
31{
64{
65public:
69 static constexpr float fsv = 23.9883;
70
74 static constexpr float r_sense = 0.01;
75
79 struct status
80 {
81 bool busy = 0;
82 bool crit = 0;
83 bool diode_fault = 0;
84 bool low = 0;
85 bool high = 0;
86 bool peak = 0;
87 };
88
92 enum class sensor_mode : uint8_t
93 {
94 FULLY_ACTIVE = 0b00100000,
95 CURRENT_ONLY = 0b01100000,
96 TEMPERATURE_ONLY = 0b00100100,
97 STANDBY = 0b01100100
98 };
99
108 enum class conversion_rate : uint8_t
109 {
110 PER_1_SEC_8_MEAS = 0b00000111,
111 PER_1_SEC_4_MEAS = 0b00000110,
112 PER_1_SEC_2_MEAS = 0b00000101,
113 PER_1_SEC_1_MEAS = 0b00000100,
114 PER_2_SEC_1_MEAS = 0b00000011,
115 PER_4_SEC_1_MEAS = 0b00000010,
116 PER_8_SEC_1_MEAS = 0b00000001,
117 PER_16_SEC_1_MEAS = 0b00000000
118 };
119
129 enum class consecutive_alert_diode_fault : uint8_t
130 {
132 0b00000000,
134 0b00000010,
136 0b00000110,
138 0b00001110
139 };
140
145 enum class consecutive_alert_voltage : uint8_t
146 {
148 0b00000000,
150 0b10000100,
152 0b10001000,
154 0b10001100
155 };
156
161 enum class consecutive_alert_current : uint8_t
162 {
164 0b00000000,
166 0b01000000,
168 0b10000000,
170 0b11000000
171 };
172
191 enum class beta_config : uint8_t
192 {
193 MIN_BETA_0_050 = 0b00000000,
194 MIN_BETA_0_066 = 0b00000001,
195 MIN_BETA_0_087 = 0b00000010,
196 MIN_BETA_0_114 = 0b00000011,
197 MIN_BETA_0_150 = 0b00000100,
198 MIN_BETA_0_197 = 0b00000101,
199 MIN_BETA_0_260 = 0b00000110,
200 MIN_BETA_0_342 = 0b00000111,
201 MIN_BETA_0_449 = 0b00001000,
202 MIN_BETA_0_591 = 0b00001001,
203 MIN_BETA_0_778 = 0b00001010,
204 MIN_BETA_1_024 = 0b00001011,
205 MIN_BETA_1_348 = 0b00001100,
206 MIN_BETA_1_773 = 0b00001101,
207 MIN_BETA_2_333 = 0b00001110,
208 DISABLED = 0b00001111,
210 AUTO = 0b00010000
211 };
212
228 enum class ideality_factor : uint8_t
229 {
240 };
241
250 enum class averaging_control : uint8_t
251 {
252 DISABLED = 0b00000000,
253 AVERAGING2X = 0b00000001,
254 AVERAGING4X = 0b00000010,
255 AVERAGING8X = 0b00000011
256 };
257
269 enum class current_sampling_time : uint8_t
270 {
271 S_T_82_MS = 0b00000000,
272 S_T_164_MS = 0b00001000,
273 S_T_328_MS = 0b00001100
274 };
275
280 enum class max_expected_voltage : uint8_t
281 {
286 0b00000011
287 };
288
293 enum class peak_detection_threshold : uint8_t
294 {
295 THRESHOLD_10_mV = 0b00000000,
296 THRESHOLD_15_mV = 0b00010000,
297 THRESHOLD_20_mV = 0b00100000,
298 THRESHOLD_25_mV = 0b00110000,
299 THRESHOLD_30_mV = 0b01000000,
300 THRESHOLD_35_mV = 0b01010000,
301 THRESHOLD_40_mV = 0b01100000,
302 THRESHOLD_45_mV = 0b01110000,
303 THRESHOLD_50_mV = 0b10000000,
304 THRESHOLD_55_mV = 0b10010000,
305 THRESHOLD_60_mV = 0b10100000,
306 THRESHOLD_65_mV = 0b10110000,
307 THRESHOLD_70_mV = 0b11000000,
308 THRESHOLD_75_mV = 0b11010000,
309 THRESHOLD_80_mV = 0b11100000,
310 THRESHOLD_85_mV = 0b11110000
311 };
312
317 enum class peak_detection_duration : uint8_t
318 {
319 DURATION_1_00_ms = 0b00000000,
320 DURATION_5_12_ms = 0b00000001,
321 DURATION_25_60_ms = 0b00000010,
322 DURATION_51_20_ms = 0b00000011,
323 DURATION_76_80_ms = 0b00000100,
324 DURATION_102_40_ms = 0b00000101,
325 DURATION_128_00_ms = 0b00000110,
326 DURATION_256_00_ms = 0b00000111,
327 DURATION_384_00_ms = 0b00001000,
328 DURATION_512_00_ms = 0b00001001,
329 DURATION_768_00_ms = 0b00001010,
330 DURATION_1024_00_ms = 0b00001011,
331 DURATION_1536_00_ms = 0b00001100,
332 DURATION_2048_00_ms = 0b00001101,
333 DURATION_3072_00_ms = 0b00001110,
335 };
336
337 emc1702(const char *name, bsp::i2c &i2c, uint16_t addr, bsp::gpio &alert);
338
339 emc1702(const char *name, bsp::i2c &i2c, uint16_t addr);
340
341 void
342 get_status(status &s);
343
344 void
345 set_config(sensor_mode m) const;
346
347 float
348 get_sense_current() const;
349
350 float
351 get_source_voltage() const;
352
353 float
354 get_power() const;
355
356 bool
357 alert() const;
358
359 float
360 get_temperature_average() const; // in °C
361
362private:
363 etl::string<32> m_name;
364 bsp::i2c &m_i2c;
365 const uint32_t m_addr;
366 bool m_ready;
367 bsp::gpio &m_alert;
368 sensor_mode mode;
369 conversion_rate rate;
370 averaging_control avrg_temp;
373 beta_config beta;
374 ideality_factor i_factor;
375 consecutive_alert_voltage v_source_spike;
376 averaging_control avrg_v_source;
377 consecutive_alert_current v_sense_spike;
378 averaging_control avrg_i_sense;
379 current_sampling_time i_sampling;
380 max_expected_voltage v_sense_max;
384 status s;
385 bsp::dummy_gpio m_dummy;
386
387 void
388 set_peak_detection_config(peak_detection_threshold t,
390
391 void
392 get_measurements(sensor_mode m);
393
394 float
395 get_temperature_internal() const;
396
397 float
398 get_temperature_external() const;
399
400 void
401 set_conversion_rate(conversion_rate r) const;
402
403 void
404 set_int_temp_high_lim(float int_temp_high_lim = 85.0f) const; // in °C
405
406 void
407 set_int_temp_low_lim(float int_temp_low_lim = -128.0f) const; // in °C
408
409 void
410 set_ext_temp_high_lim(float ext_temp_high_lim = 85.0f) const; // in °C
411
412 void
413 set_ext_temp_low_lim(float ext_temp_low_lim = -128.0f) const; // in °C
414
415 void
416 get_one_shot_meas() const;
417
418 void
419 set_t_crit(float ext_t_crit = 100.0f, float int_t_crit = 100.0f,
420 float t_crit_hysterisis = 10.0f) const; // in °C
421
422 bool
423 get_diode_fault() const;
424
425 void
426 set_consecutive_alert(consecutive_alert_diode_fault c,
428
429 void
430 set_beta_configuration(beta_config b) const;
431
432 void
433 set_ideality_factor(ideality_factor i) const;
434
435 etl::vector<bool, 4>
436 get_high_limit_status() const;
437
438 etl::vector<bool, 4>
439 get_low_limit_status() const;
440
441 bool
442 get_crit_limit_status() const;
443
444 void
445 set_averaging_control(averaging_control a) const;
446
447 bool
448 get_sensor_info() const;
449
450 void
451 set_voltage_sampling_config(consecutive_alert_voltage v,
452 averaging_control s) const;
453
454 void
455 set_current_sense_sampling_config(consecutive_alert_current v,
458 max_expected_voltage m) const;
459
460 void
461 set_v_sense_lim(float v_sense_lim_low = 2032.0f,
462 float v_sense_lim_high = -2033.0f) const;
463
464 void
465 set_v_source_lim(float v_source_lim_low = 0.0f,
466 float v_source_lim_high = 23.9063f) const; // in V
467
468 void
469 set_v_crit_lim(float v_sense_lim_crit = 2032.0f, // in mV
470 float v_source_lim_crit = 23.9063f, // in V
471 float v_sense_hysterisis = 160.0f, // in mV
472 float v_source_hysterisis = 0.9375f) const; // in V
473
474 float
475 get_fsc() const;
476};
477
487{
488public:
489 emc1702_thermal_shutdown_needed(string_type file_name, numeric_type line)
490 : exception(exception::severity::MINOR, file_name, line,
491 error_msg{"Thermal Shutdon is required (from emc1702)",
492 "thermalshutdownemc1702", ETHERMSHUT})
493 {
494 }
495};
496
507{
508public:
509 emc1702_incorrect_sensor_info(string_type file_name, numeric_type line)
510 : exception(exception::severity::MINOR, file_name, line,
511 error_msg{"Error in getting sensor info. Probably there "
512 "is an error in communication",
513 "incorrectsensinfo", EBADINFSENS})
514 {
515 }
516};
517
527{
528public:
529 emc1702_not_ready(string_type file_name, numeric_type line)
530 : exception(
531 exception::severity::MINOR, file_name, line,
532 error_msg{"The ADCs of emc1702 currently converting. Readings not "
533 "ready",
534 "emc1702_busy", EBUSYSENS})
535 {
536 }
537};
538
546{
547public:
548 emc1702_inval(string_type file_name, numeric_type line)
549 : exception(
550 exception::severity::MINOR, file_name, line,
551 error_msg{"Invalid EMC1702 sensor", "emc1702_busy", EINVALSENS})
552 {
553 }
554};
555
556} // namespace satnogs::comms::lib
A dummy GPIO implementation with no effect. Can be used for CI testing or default construction of gpi...
Definition gpio.hpp:119
GPIO device abstraction.
Definition gpio.hpp:38
I2C device abstraction.
Definition i2c.hpp:40
emc1702_incorrect_sensor_info(string_type file_name, numeric_type line)
Definition emc1702.hpp:509
emc1702_inval(string_type file_name, numeric_type line)
Definition emc1702.hpp:548
emc1702_not_ready(string_type file_name, numeric_type line)
Definition emc1702.hpp:529
emc1702_thermal_shutdown_needed(string_type file_name, numeric_type line)
Definition emc1702.hpp:489
ideality_factor
Settings to set the Ideality Factor in the External Diode Ideality Factor Registers.
Definition emc1702.hpp:229
float get_sense_current() const
Retrieves current measurement from the EMC1702 sensor.
Definition emc1702.cpp:905
float get_temperature_average() const
Retrieves temperature from the EMC1702 sensor by averaging the internal and external diode measuremen...
Definition emc1702.cpp:278
averaging_control
Settings to set the digital averaging on mesurements in the Averaging Control Register,...
Definition emc1702.hpp:251
sensor_mode
Selects the mode of operation of the EMC1702 sensor.
Definition emc1702.hpp:93
conversion_rate
Settings to set the frequency of measurements in the Conversion Rate Register.
Definition emc1702.hpp:109
void get_status(status &s)
Definition emc1702.cpp:328
consecutive_alert_voltage
Settings to set the threshold for consecutive voltage out-of-limit measurements in the Voltage Sampli...
Definition emc1702.hpp:146
emc1702(const char *name, bsp::i2c &i2c, uint16_t addr, bsp::gpio &alert)
Constructs an EMC1702 sensor object with an ALERT GPIO pin.
Definition emc1702.cpp:135
current_sampling_time
Settings to set the Current Sensing Sampling Time in the Current Sense Sampling Configuration Registe...
Definition emc1702.hpp:270
static constexpr float fsv
Full Scale Voltage in volts.
Definition emc1702.hpp:69
static constexpr float r_sense
External sense resistor value in Ohms.
Definition emc1702.hpp:74
max_expected_voltage
Settings to set the Current Sense maximum expected voltage (full scale range) in the Current Sense Sa...
Definition emc1702.hpp:281
consecutive_alert_current
Settings to set the threshold for consecutive current out-of-limit measurements in the Current Sense ...
Definition emc1702.hpp:162
float get_source_voltage() const
Retrieves source voltage measurement from the EMC1702 sensor.
Definition emc1702.cpp:928
peak_detection_duration
Settings to set the peak detector minimum time threshold in the Peak Detection Configuration Register...
Definition emc1702.hpp:318
peak_detection_threshold
Settings to set the peak detector threshold level in the Peak Detection Configuration Register.
Definition emc1702.hpp:294
beta_config
Settings to set the Beta Compensation factor in the Beta Configuration Registers.
Definition emc1702.hpp:192
void set_config(sensor_mode m) const
Definition emc1702.cpp:353
consecutive_alert_diode_fault
Settings to set the threshold for consecutive diode fault alerts in the Consecutive Alert Register.
Definition emc1702.hpp:130
A class representing error messages in the SatNOGS-COMMS system.
Definition exception.hpp:83
severity
Severity levels of exceptions.
Definition exception.hpp:71
@ MINOR
Failure having minimal impact.
Definition exception.hpp:75
exception(severity sev, const char *file, int lineno, const error_msg &err_msg)
Constructor for the exception class.
#define EBADINFSENS
Parsing bad sensor info.
#define EINVALSENS
Invalid sensor.
#define ETHERMSHUT
Thermal shutdown is required.
#define EBUSYSENS
Readings from sensor not available yet.