software:firmware
MakAir Firmware
Blower Class Reference

Controls a blower. More...

#include <blower.h>

Public Member Functions

 Blower ()
 Default constructor. More...
 
 Blower (HardwareTimer *p_hardwareTimer, uint16_t p_timerChannel, uint16_t p_blowerPin)
 Parameterized constructor. More...
 
void setup ()
 Initialize the hardware timer used to control the blower. More...
 
void runSpeedWithRampUp (uint16_t p_targetSpeed)
 Run the blower to a given speed applying a ramp-up to prevent high current drain. More...
 
void runSpeed (uint16_t p_runSpeed)
 Run the blower to a given speed. More...
 
void execute ()
 
void stop ()
 Stops the blower. More...
 
uint16_t getSpeed () const
 Get speed value. More...
 
uint16_t getTargetSpeed () const
 Get target speed value. More...
 
int32_t getBlowerPressure (int32_t p_flow)
 Given a flow in mL/min, return an estimated pressure just at the output of the blower. More...
 

Private Attributes

HardwareTimer * actuator
 Hardware timer used to control the blower. More...
 
uint16_t timerChannel
 Channel of the hardware timer used to control the blower. More...
 
uint16_t blowerPin
 Pin of the blower. More...
 
uint16_t m_speed
 Current speed. More...
 
uint16_t m_targetSpeed
 target speed More...
 
bool m_stopped
 Current state. More...
 
uint32_t m_lastCallDate
 

Detailed Description

Controls a blower.

Definition at line 27 of file blower.h.

Constructor & Destructor Documentation

◆ Blower() [1/2]

Blower::Blower ( )

Default constructor.

Definition at line 24 of file blower.cpp.

24 {}

◆ Blower() [2/2]

Blower::Blower ( HardwareTimer *  p_hardwareTimer,
uint16_t  p_timerChannel,
uint16_t  p_blowerPin 
)

Parameterized constructor.

Parameters
p_hardwareTimerHardware time for the blower
p_timerChannelTIM channel for this blower
p_blowerPinData pin for this blower

Definition at line 26 of file blower.cpp.

26  {
27  actuator = p_hardwareTimer;
28  timerChannel = p_timerChannel;
29  blowerPin = p_blowerPin;
30  m_stopped = true;
31  m_speed = 0;
32  m_targetSpeed = 0;
33  m_lastCallDate = millis();
34 }
HardwareTimer * actuator
Hardware timer used to control the blower.
Definition: blower.h:78
uint32_t m_lastCallDate
Definition: blower.h:96
bool m_stopped
Current state.
Definition: blower.h:93
uint16_t m_targetSpeed
target speed
Definition: blower.h:90
uint16_t timerChannel
Channel of the hardware timer used to control the blower.
Definition: blower.h:81
uint16_t blowerPin
Pin of the blower.
Definition: blower.h:84
uint16_t m_speed
Current speed.
Definition: blower.h:87

Member Function Documentation

◆ execute()

void Blower::execute ( )

Definition at line 55 of file blower.cpp.

55  {
56  // apply ramp-up
57  // Max acceleration is one unit per ms. This means full ramp-up in 1.8s
58  uint32_t currentDate = micros();
59  uint16_t runSpeed = 0;
60  if (m_targetSpeed > m_speed) {
61  runSpeed = min(m_targetSpeed, uint16_t(m_speed + (currentDate - m_lastCallDate) / 1000u));
62  } else {
64  }
65  m_lastCallDate = currentDate;
66  this->runSpeed(runSpeed);
67 }
void runSpeed(uint16_t p_runSpeed)
Run the blower to a given speed.
Definition: blower.cpp:69

◆ getBlowerPressure()

int32_t Blower::getBlowerPressure ( int32_t  p_flow)

Given a flow in mL/min, return an estimated pressure just at the output of the blower.

This pressure has been determined using the pressure-flow characteristic of the blower

Parameters
p_flowinspiratory flow in mL/min

Definition at line 85 of file blower.cpp.

85  {
86  int32_t returnValue;
87  // For now the blower has only been characterize at max speed
88  if (m_speed == MAX_BLOWER_SPEED) {
89  // This order 2 characteruzation has been made experimentally
90  returnValue =
91  703 - (281 * (p_flow / 100000)) - 832 * (p_flow / 200) * (p_flow / 500) / 100000;
92  } else {
93  // todo better characterization
94  returnValue = 703 * static_cast<int32_t>(m_speed) / static_cast<int32_t>(MAX_BLOWER_SPEED)
95  - (281 * (p_flow / 100000)) - 832 * (p_flow / 200) * (p_flow / 500) / 100000;
96  }
97 
98  return min(int32_t(703), max(returnValue, int32_t(0)));
99 }
#define MAX_BLOWER_SPEED
Definition: parameters.h:193

◆ getSpeed()

uint16_t Blower::getSpeed ( ) const

Get speed value.

Definition at line 101 of file blower.cpp.

101 { return m_speed; }

◆ getTargetSpeed()

uint16_t Blower::getTargetSpeed ( ) const

Get target speed value.

Definition at line 103 of file blower.cpp.

103 { return m_targetSpeed; }

◆ runSpeed()

void Blower::runSpeed ( uint16_t  p_runSpeed)

Run the blower to a given speed.

Parameters
p_runSpeedSpeed between MIN_BLOWER_SPEED and MAX_BLOWER_SPEED

Definition at line 69 of file blower.cpp.

69  {
70  // cppcheck-suppress unsignedPositive ; MIN_BLOWER_SPEED might not be equal to 0
71  if ((p_runSpeed >= MIN_BLOWER_SPEED) && (p_runSpeed <= MAX_BLOWER_SPEED)) {
72  // do not forcefully set the capture compare again and again if speed do not change
73  if (m_stopped || (m_speed != p_runSpeed)) {
74  actuator->setCaptureCompare(timerChannel, BlowerSpeed2MicroSeconds(p_runSpeed),
75  MICROSEC_COMPARE_FORMAT);
76  m_speed = p_runSpeed;
77  m_stopped = false;
78  }
79  } else {
80  DBG_DO(Serial.print("Blower value is wrong: "));
81  DBG_DO(Serial.println(p_runSpeed));
82  }
83 }
#define BlowerSpeed2MicroSeconds(value)
Convert a speed to a value in microseconds for the blower controller.
Definition: blower.h:22
#define DBG_DO(statement)
Expand arbitrary code only when in debug mode.
Definition: debug.h:24
#define MIN_BLOWER_SPEED
Definition: parameters.h:192

◆ runSpeedWithRampUp()

void Blower::runSpeedWithRampUp ( uint16_t  p_targetSpeed)

Run the blower to a given speed applying a ramp-up to prevent high current drain.

Parameters
p_targetSpeedSpeed between MIN_BLOWER_SPEED and MAX_BLOWER_SPEED

Definition at line 44 of file blower.cpp.

44  {
45  // cppcheck-suppress unsignedPositive ; MIN_BLOWER_SPEED might not be equal to 0
46  if ((p_targetSpeed >= MIN_BLOWER_SPEED) && (p_targetSpeed <= MAX_BLOWER_SPEED)) {
47  if (p_targetSpeed != m_targetSpeed) { // first time with new target
48  m_lastCallDate = micros();
49  m_targetSpeed = p_targetSpeed;
50  m_speed = max(m_speed, uint16_t(MIN_BLOWER_SPEED));
51  }
52  }
53 }

◆ setup()

void Blower::setup ( )

Initialize the hardware timer used to control the blower.

Definition at line 36 of file blower.cpp.

36  {
37  actuator->setMode(timerChannel, TIMER_OUTPUT_COMPARE_PWM1, blowerPin);
38 
39  // Set PPM width to 1ms
40  actuator->setCaptureCompare(timerChannel, BlowerSpeed2MicroSeconds(0), MICROSEC_COMPARE_FORMAT);
41  actuator->resume();
42 }

◆ stop()

void Blower::stop ( )

Stops the blower.

Definition at line 105 of file blower.cpp.

105  {
106  actuator->setCaptureCompare(timerChannel, BlowerSpeed2MicroSeconds(0), MICROSEC_COMPARE_FORMAT);
107  m_stopped = true;
108  m_speed = 0;
109  m_targetSpeed = 0;
110 }

Member Data Documentation

◆ actuator

HardwareTimer* Blower::actuator
private

Hardware timer used to control the blower.

Definition at line 78 of file blower.h.

◆ blowerPin

uint16_t Blower::blowerPin
private

Pin of the blower.

Definition at line 84 of file blower.h.

◆ m_lastCallDate

uint32_t Blower::m_lastCallDate
private

Definition at line 96 of file blower.h.

◆ m_speed

uint16_t Blower::m_speed
private

Current speed.

Definition at line 87 of file blower.h.

◆ m_stopped

bool Blower::m_stopped
private

Current state.

Definition at line 93 of file blower.h.

◆ m_targetSpeed

uint16_t Blower::m_targetSpeed
private

target speed

Definition at line 90 of file blower.h.

◆ timerChannel

uint16_t Blower::timerChannel
private

Channel of the hardware timer used to control the blower.

Definition at line 81 of file blower.h.


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