44#include < Wire.h>
55
66#define VOLTAGE_DIVIDER (5.0 / (5.0 + 20.0 ))
7+ #define PYRO_VOLTAGE_DIVIDER (18.0 /(100.0 +18.0 ))
8+ #define PYRO_BATT_VOLTAGE_DIVIDER (100.0 /(100.0 +560.0 ))
9+ #define BATT_VOLTAGE_DIVIDER (100.0 /(100.0 +100.0 ))
10+ #define VOLTAGE_SCALE 3.3
11+ #define VOLTAGE_REG_WIDTH (1 <<16 )
712
813int read_board_pwr_monitor_register (int reg, int bytes) {
914 Wire1.beginTransmission (0x44 );
@@ -39,23 +44,72 @@ ErrorCode VoltageSensor::init() {
3944 * @return The scaled voltage given by the voltage sensor
4045*/
4146Voltage VoltageSensor::read () {
42- Voltage v_battery;
43- int voltage = read_board_pwr_monitor_register (0x5 , 2 );
44- int16_t current = read_board_pwr_monitor_register (0x7 , 2 );
45-
46- float voltage_normalized = voltage * 3.125 / 1000.0 ;
47- float absolute_current = current * 1.2 / 1000.0 ;
48-
49- // Serial.print("Voltage: ");
50- // Serial.println(voltage_normalized);
51- // Serial.print("Current: ");
52- // Serial.println(current);
53-
54- v_battery.voltage = voltage_normalized;
55- v_battery.current = absolute_current;
56- // Serial.print("Raw voltage reading: ");
57- // Serial.print(v_battery.voltage);
58- // Serial.println("");
59- // * 3.3f / 4095.f / VOLTAGE_DIVIDER;
60- return v_battery;
61- }
47+ Voltage voltage;
48+
49+ // CHECK PINS
50+ ADCAddress pinA;
51+ ADCAddress pinB;
52+ ADCAddress pinC;
53+ ADCAddress pinD;
54+ ADCAddress pinBat;
55+ ADCAddress pinPyro;
56+ pinA.pin_id = 0 ;
57+ pinB.pin_id = 1 ;
58+ pinC.pin_id = 4 ;
59+ pinD.pin_id = 5 ;
60+ pinBat.pin_id = 7 ;
61+ pinPyro.pin_id = 2 ;
62+
63+ // Getting ADC values
64+ AdcReadResult sensorA = adcAnalogRead (pinA);
65+ AdcReadResult sensorB = adcAnalogRead (pinB);
66+ AdcReadResult sensorC = adcAnalogRead (pinC);
67+ AdcReadResult sensorD = adcAnalogRead (pinD);
68+ AdcReadResult sensorBat = adcAnalogRead (pinBat);
69+ AdcReadResult sensorPyro = adcAnalogRead (pinPyro);
70+
71+ // Converts ADC value to voltage for each pin
72+ if (sensorBat.error == AdcError::NoError){
73+ float Bat_voltage = ((static_cast <float >(sensorBat.value ) / VOLTAGE_REG_WIDTH) * VOLTAGE_SCALE) / BATT_VOLTAGE_DIVIDER;
74+ voltage.v_Bat = Bat_voltage;
75+ Serial.print (" VBAT: " );
76+ Serial.println (Bat_voltage);
77+ }
78+
79+ if (sensorPyro.error == AdcError::NoError){
80+ float Pyro_voltage = ((static_cast <float >(sensorPyro.value ) / VOLTAGE_REG_WIDTH) * VOLTAGE_SCALE)/(PYRO_BATT_VOLTAGE_DIVIDER); // Accounting for voltage divider on MIDAS mini
81+ voltage.v_Pyro = Pyro_voltage;
82+ Serial.print (" PYRO: " );
83+ Serial.println (Pyro_voltage);
84+ }
85+
86+ if (sensorA.error == AdcError::NoError){
87+ float A_voltage = ((static_cast <float >(sensorA.value ) / VOLTAGE_REG_WIDTH) * VOLTAGE_SCALE) / PYRO_VOLTAGE_DIVIDER;
88+ voltage.continuity [0 ] = A_voltage;
89+ Serial.print (" A: " );
90+ Serial.println (A_voltage);
91+ }
92+
93+ if (sensorB.error == AdcError::NoError){
94+ float B_voltage = ((static_cast <float >(sensorB.value ) / VOLTAGE_REG_WIDTH) * VOLTAGE_SCALE) / PYRO_VOLTAGE_DIVIDER;
95+ voltage.continuity [1 ] = B_voltage;
96+ Serial.print (" B: " );
97+ Serial.println (B_voltage);
98+ }
99+
100+ if (sensorC.error == AdcError::NoError){
101+ float C_voltage = ((static_cast <float >(sensorC.value ) / VOLTAGE_REG_WIDTH) * VOLTAGE_SCALE) / PYRO_VOLTAGE_DIVIDER;
102+ voltage.continuity [2 ] = C_voltage;
103+ Serial.print (" C: " );
104+ Serial.println (C_voltage);
105+ }
106+
107+ if (sensorD.error == AdcError::NoError){
108+ float D_voltage = ((static_cast <float >(sensorD.value ) / VOLTAGE_REG_WIDTH) * VOLTAGE_SCALE) / PYRO_VOLTAGE_DIVIDER;
109+ voltage.continuity [3 ] = D_voltage;
110+ Serial.print (" D: " );
111+ Serial.println (D_voltage);
112+ }
113+
114+ return voltage;
115+ }
0 commit comments