software:firmware
MakAir Firmware
PressureValve Class Reference

Controls a pressure valve. More...

#include <pressure_valve.h>

Public Member Functions

 PressureValve ()
 Default constructor. More...
 
 PressureValve (HardwareTimer *p_hardwareTimer, uint16_t p_timerChannel, uint16_t p_valvePin, uint16_t p_openApertureAngle, uint16_t p_closeApertureAngle)
 Parameterized constructor. More...
 
void setup ()
 Initialize this valve. More...
 
void open ()
 Request opening of the Pressure Valve. More...
 
void open (uint16_t p_command)
 Request opening of the Pressure Valve with a given angle. More...
 
uint16_t openLinear (uint16_t p_command)
 Request opening of the Pressure Valve with a given angle with linearization. More...
 
int32_t getSectionBigHoseX100 ()
 
void openSection (int32_t p_sectionMultiplyBy100)
 Request opening of the Pressure Valve with a given section (in mm^2) More...
 
void close ()
 Request closing of the Pressure Valve. More...
 
void execute ()
 Command the valve to go to the requested aperture. More...
 
uint16_t minAperture () const
 Minimum valve aperture angle in degrees. More...
 
uint16_t maxAperture () const
 Maximum valve aperture angle in degrees. More...
 

Public Attributes

uint16_t command
 Value of the requested aperture. More...
 
uint16_t position
 Current aperture. More...
 
uint16_t positionLinear
 Current aperture linear. More...
 

Private Attributes

uint16_t minApertureAngle
 Minimum valve aperture angle in degrees. More...
 
uint16_t maxApertureAngle
 Maximum valve aperture angle in degrees. More...
 
uint16_t openApertureAngle
 Open aperture angle in degrees. More...
 
uint16_t closeApertureAngle
 Close aperture angle in degrees. More...
 
HardwareTimer * actuator
 Hardware time for this valve. More...
 
uint16_t timerChannel
 TIM channel for this valve. More...
 
uint16_t valvePin
 Data pin for this valve. More...
 

Detailed Description

Controls a pressure valve.

Definition at line 29 of file pressure_valve.h.

Constructor & Destructor Documentation

◆ PressureValve() [1/2]

PressureValve::PressureValve ( )

Default constructor.

Definition at line 28 of file pressure_valve.cpp.

28 {}

◆ PressureValve() [2/2]

PressureValve::PressureValve ( HardwareTimer *  p_hardwareTimer,
uint16_t  p_timerChannel,
uint16_t  p_valvePin,
uint16_t  p_openApertureAngle,
uint16_t  p_closeApertureAngle 
)

Parameterized constructor.

Parameters
p_hardwareTimerHardware time for this valve
p_timerChannelTIM channel for this valve
p_valvePinData pin for this valve
p_openApertureAngleOpen aperture angle in degrees
p_closeApertureAngleClose aperture angle in degrees

Definition at line 30 of file pressure_valve.cpp.

34  {
35  actuator = p_hardwareTimer;
36  timerChannel = p_timerChannel;
37  valvePin = p_valvePin;
38  openApertureAngle = p_openApertureAngle;
39  closeApertureAngle = p_closeApertureAngle;
40  minApertureAngle = min(p_closeApertureAngle, p_openApertureAngle);
41  maxApertureAngle = max(p_closeApertureAngle, p_openApertureAngle);
42  command = p_closeApertureAngle;
43  position = -1;
44  positionLinear = 0;
45 }
uint16_t valvePin
Data pin for this valve.
uint16_t maxApertureAngle
Maximum valve aperture angle in degrees.
uint16_t closeApertureAngle
Close aperture angle in degrees.
uint16_t openApertureAngle
Open aperture angle in degrees.
uint16_t command
Value of the requested aperture.
uint16_t minApertureAngle
Minimum valve aperture angle in degrees.
HardwareTimer * actuator
Hardware time for this valve.
uint16_t timerChannel
TIM channel for this valve.
uint16_t positionLinear
Current aperture linear.
uint16_t position
Current aperture.

Member Function Documentation

◆ close()

void PressureValve::close ( )

Request closing of the Pressure Valve.

Definition at line 54 of file pressure_valve.cpp.

◆ execute()

void PressureValve::execute ( )
inline

Command the valve to go to the requested aperture.

Note
Nothing will happen if this function is not called after requesting a new aperture

Definition at line 90 of file pressure_valve.h.

90  {
91  // On évite d'aller plus loin que les limites de la valve
92  if (command < minApertureAngle) {
94  } else if (command > maxApertureAngle) {
96  } else {
97  }
98 
99  if (command != position) {
101  MICROSEC_COMPARE_FORMAT);
102  position = command;
103  }
104  }
uint16_t valveAngle2MicroSeconds(uint16_t value)
Convert an angle in degrees to a value in microseconds for the valve controller.

◆ getSectionBigHoseX100()

int32_t PressureValve::getSectionBigHoseX100 ( )

Definition at line 139 of file pressure_valve.cpp.

139  {
140  int32_t section;
141  if (command > 105u) {
142  section = 0;
143  } else if (command >= 50u) {
144  section = 5760u - (558u * command / 10u);
145  } else {
146  section = 4390u - (5u * command) - (47u * command * command / 100u);
147  }
148 
149  return max(int32_t(0), section);
150 }

◆ maxAperture()

uint16_t PressureValve::maxAperture ( ) const
inline

Maximum valve aperture angle in degrees.

Definition at line 110 of file pressure_valve.h.

110 { return maxApertureAngle; }

◆ minAperture()

uint16_t PressureValve::minAperture ( ) const
inline

Minimum valve aperture angle in degrees.

Definition at line 107 of file pressure_valve.h.

107 { return minApertureAngle; }

◆ open() [1/2]

void PressureValve::open ( )

Request opening of the Pressure Valve.

Definition at line 52 of file pressure_valve.cpp.

◆ open() [2/2]

void PressureValve::open ( uint16_t  p_command)

Request opening of the Pressure Valve with a given angle.

Parameters
p_commandThe angle in degree

Definition at line 56 of file pressure_valve.cpp.

56 { command = p_command; }

◆ openLinear()

uint16_t PressureValve::openLinear ( uint16_t  p_command)

Request opening of the Pressure Valve with a given angle with linearization.

Parameters
p_commandThe angle in degree
Returns
The linearized angle calculated by this function

Definition at line 81 of file pressure_valve.cpp.

81  {
82  uint16_t cappedCommand =
83  // cppcheck-suppress misra-c2012-12.3 ; cppcheck error
84  min(max(uint16_t(minApertureAngle), p_command), uint16_t(maxApertureAngle));
85 
86  positionLinear = cappedCommand;
87  // The cappedCommand is in [ 0 ; 125 ], but the valve is only effective in [30; 100]
88  // So lets make it between 30 and 100
89  // A x10 multiplier is used here for better precision with integer calculations
90  // The value of intermediateValue will consequently be in [300 ; 1000]
91  uint32_t intermediateValue =
92  (((static_cast<uint32_t>(cappedCommand) * 75u) / 125u) + 30u) * 10u;
93 
94  /* An order 3 polynom is used to correct the non linearity of the valve.
95  To find this polynom, the following experimental protocol was used:
96  - Run the blower max speed during the full experience
97  - Plug inspiratory output on expiratory input
98  - For each openning value of the valve, wait 5s for stabilisation and measure Flow
99 
100  This gives a csv with:
101  openningValue[0;125], flow (mL/min)
102  0, 48137
103  10, 47780
104  ...
105  125, 127
106 
107  Then we calculated for each openning value the "linear target flow" with a basic linear
108  equation:
109  openningValue[0;125], flow (mL/min), targetFlow (mL/min)
110  0, 48137,48137
111  10, 47780, 44296
112  ...
113  125, 127, 0
114 
115  Then we manually found for each openning value, the openning value that should have been
116  used to reach the targetFlow: openningValue[0;125], flow (mL/min), targetFlow (mL/min),
117  correctedOpenningValue[0;125] 0, 48137,48137, 30 10, 47780, 44296, 55.2
118  ...
119  125, 127, 0, 100
120 
121  Then when made an order 3 regression between correctedOpenningValue and openningValue. This
122  is the relation below:
123  */
124  command = (uint16_t)(
125  (((76u * intermediateValue) / 10u)
126  - (((985u * intermediateValue) * intermediateValue) / 100000u)
127  + (((((44u * intermediateValue) * intermediateValue) / 1000u) * intermediateValue)
128  / 10000u)
129  - 1140u)
130  / 10u);
131 
132  // cppcheck-suppress misra-c2012-12.3 ; cppcheck error
133  command = min(max(uint16_t(minApertureAngle), command), uint16_t(maxApertureAngle));
134 
135  return command;
136 }

◆ openSection()

void PressureValve::openSection ( int32_t  p_sectionMultiplyBy100)

Request opening of the Pressure Valve with a given section (in mm^2)

Parameters
p_sectionMultiplyBy100The section to open tha valve (in mm^2 multiplied by 100)

Definition at line 58 of file pressure_valve.cpp.

58  {
59  // Min-max to prevent overflow
60  int32_t cappedSectionMultiplyBy100 =
61  // cppcheck-suppress misra-c2012-12.3 ; cppcheck error
62  min(max(int32_t(0), p_sectionMultiplyBy100), int32_t(3318));
63 
64  int32_t tempCommand;
65  if (cappedSectionMultiplyBy100 < 1960) {
66  tempCommand = 98 - (318 * cappedSectionMultiplyBy100 / 10000);
67  } else {
68  tempCommand =
69  2626 - (36 * cappedSectionMultiplyBy100) / 10
70  + 168 * ((cappedSectionMultiplyBy100 * cappedSectionMultiplyBy100) / 100) / 1000
71  - 264
72  * (((cappedSectionMultiplyBy100 * cappedSectionMultiplyBy100) / 100)
73  * (cappedSectionMultiplyBy100) / 100)
74  / 100000;
75  }
76  // cppcheck-suppress misra-c2012-12.3
77  command = min(max(int32_t(minApertureAngle), tempCommand), int32_t(maxApertureAngle));
78 }

◆ setup()

void PressureValve::setup ( )

Initialize this valve.

This must be called once to be able to use this Pressure Valve

Definition at line 47 of file pressure_valve.cpp.

47  {
48  actuator->setMode(timerChannel, TIMER_OUTPUT_COMPARE_PWM1, valvePin);
49  actuator->setCaptureCompare(timerChannel, 0, MICROSEC_COMPARE_FORMAT);
50 }

Member Data Documentation

◆ actuator

HardwareTimer* PressureValve::actuator
private

Hardware time for this valve.

Definition at line 135 of file pressure_valve.h.

◆ closeApertureAngle

uint16_t PressureValve::closeApertureAngle
private

Close aperture angle in degrees.

Definition at line 132 of file pressure_valve.h.

◆ command

uint16_t PressureValve::command

Value of the requested aperture.

Definition at line 113 of file pressure_valve.h.

◆ maxApertureAngle

uint16_t PressureValve::maxApertureAngle
private

Maximum valve aperture angle in degrees.

Definition at line 126 of file pressure_valve.h.

◆ minApertureAngle

uint16_t PressureValve::minApertureAngle
private

Minimum valve aperture angle in degrees.

Definition at line 123 of file pressure_valve.h.

◆ openApertureAngle

uint16_t PressureValve::openApertureAngle
private

Open aperture angle in degrees.

Definition at line 129 of file pressure_valve.h.

◆ position

uint16_t PressureValve::position

Current aperture.

Definition at line 116 of file pressure_valve.h.

◆ positionLinear

uint16_t PressureValve::positionLinear

Current aperture linear.

Definition at line 119 of file pressure_valve.h.

◆ timerChannel

uint16_t PressureValve::timerChannel
private

TIM channel for this valve.

Definition at line 138 of file pressure_valve.h.

◆ valvePin

uint16_t PressureValve::valvePin
private

Data pin for this valve.

Definition at line 141 of file pressure_valve.h.


The documentation for this class was generated from the following files: