software:firmware
MakAir Firmware
blower.cpp
Go to the documentation of this file.
1 
8 #pragma once
9 
10 // INCLUDES ===================================================================
11 
12 // Associated header
13 #include "../includes/blower.h"
14 
15 // Internal libraries
16 #include "../includes/parameters.h"
17 
18 // INITIALISATION =============================================================
19 
21 
22 // FUNCTIONS ==================================================================
23 
25 
26 Blower::Blower(HardwareTimer* p_hardwareTimer, uint16_t p_timerChannel, uint16_t p_blowerPin) {
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 }
35 
36 void Blower::setup() {
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 }
43 
44 void Blower::runSpeedWithRampUp(uint16_t p_targetSpeed) {
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 }
54 
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 }
68 
69 void Blower::runSpeed(uint16_t p_runSpeed) {
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 }
84 
85 int32_t Blower::getBlowerPressure(int32_t p_flow) {
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 }
100 
101 uint16_t Blower::getSpeed() const { return m_speed; }
102 
103 uint16_t Blower::getTargetSpeed() const { return m_targetSpeed; }
104 
105 void Blower::stop() {
106  actuator->setCaptureCompare(timerChannel, BlowerSpeed2MicroSeconds(0), MICROSEC_COMPARE_FORMAT);
107  m_stopped = true;
108  m_speed = 0;
109  m_targetSpeed = 0;
110 }
Blower blower
Definition: blower.cpp:20
#define BlowerSpeed2MicroSeconds(value)
Convert a speed to a value in microseconds for the blower controller.
Definition: blower.h:22
Controls a blower.
Definition: blower.h:27
HardwareTimer * actuator
Hardware timer used to control the blower.
Definition: blower.h:78
uint16_t getSpeed() const
Get speed value.
Definition: blower.cpp:101
uint32_t m_lastCallDate
Definition: blower.h:96
bool m_stopped
Current state.
Definition: blower.h:93
uint16_t getTargetSpeed() const
Get target speed value.
Definition: blower.cpp:103
void setup()
Initialize the hardware timer used to control the blower.
Definition: blower.cpp:36
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
int32_t getBlowerPressure(int32_t p_flow)
Given a flow in mL/min, return an estimated pressure just at the output of the blower.
Definition: blower.cpp:85
void stop()
Stops the blower.
Definition: blower.cpp:105
void runSpeedWithRampUp(uint16_t p_targetSpeed)
Run the blower to a given speed applying a ramp-up to prevent high current drain.
Definition: blower.cpp:44
uint16_t blowerPin
Pin of the blower.
Definition: blower.h:84
void runSpeed(uint16_t p_runSpeed)
Run the blower to a given speed.
Definition: blower.cpp:69
void execute()
Definition: blower.cpp:55
uint16_t m_speed
Current speed.
Definition: blower.h:87
Blower()
Default constructor.
Definition: blower.cpp:24
#define DBG_DO(statement)
Expand arbitrary code only when in debug mode.
Definition: debug.h:24
#define MAX_BLOWER_SPEED
Definition: parameters.h:193
#define MIN_BLOWER_SPEED
Definition: parameters.h:192