-
Notifications
You must be signed in to change notification settings - Fork 4
Samk humidity control #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
674806b
1e3a824
852b544
7c55e7f
4780510
59dfe1f
65d9806
0f24701
65218ba
661c567
e260093
b1a5ac6
4254bc6
d37fde7
53006bd
7da5c58
c28330b
1d31b19
b9f5c75
7541937
fd3b55f
609e31d
eabb573
b829813
8063c7a
b28c1b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| /* | ||
| * CHumidityController.cpp | ||
| * | ||
| * Created on: Aug. 11, 2022 | ||
| * Author: samk | ||
| */ | ||
|
|
||
| #include "CHumidityController.h" | ||
| #include <stdio.h> | ||
| #include "CBME280.h" | ||
| #include "CHumidifier.h" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this used? This might have been something legacy or something I introduced. Check if it does anything and if not remove.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I included that while I was previously using the CHumidifier class to do the humidifier abstraction. I forgot to remove it since it's no longer being used. |
||
| #include "ICommand.h" | ||
| #include "IHardwareMap.h" | ||
|
|
||
| #define MIN_TEMPERATURE 10 | ||
| #define MAX_TEMPERATURE 50 | ||
| #define MIN_POWER 0 | ||
| #define MAX_POWER 100 | ||
| #define DISABLE_OVERRIDE -1 | ||
| #define DISABLE_TARGET 0 | ||
| #define MIN_HUMIDITY 85 | ||
| #define MAX_HUMIDITY 95 | ||
|
|
||
| CHumidityController::CHumidityController(IHardwareMap *p_hardware, | ||
| etl::string<MAX_STRING_SIZE> name, | ||
| uint32_t run_period_ms, | ||
| SPI_HandleTypeDef *p_spi, | ||
| GPIO_TypeDef *p_slave_select_port, | ||
| uint16_t slave_select_pin) | ||
| : CController(name, run_period_ms), | ||
| mp_hw(p_hardware) | ||
| { | ||
| // TODO Auto-generated constructor stub | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just realised that this constructor is not going to work because when CHumidityController is created, the SPI port hardware will not be setup yet, so the constructor pointers will be all |
||
| reset(); | ||
|
|
||
| // TODO Fix this constructor | ||
| // CBME280 humidity_sensor(p_spi, p_slave_select_port, slave_select_pin); | ||
| // mp_humidity_sensor = &humidity_sensor; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Run all repeated humidity control activities. | ||
| * | ||
| */ | ||
| void CHumidityController::run() | ||
| { | ||
| float power = 0; | ||
| if (m_power_override == DISABLE_OVERRIDE) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would recommend that special numbers are not used. They can be confusing. I would recommend that you create a special member variable
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when processing commands, if you get a command that overrides the power, then you simply toggle |
||
| { | ||
| if (m_target_humidity != DISABLE_TARGET) | ||
| { | ||
| float actual_humidity = mp_humidity_sensor->getHumidity(); | ||
| power = m_control_loop.run(m_target_humidity, actual_humidity); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| power = m_power_override; | ||
| } | ||
| mp_hw->setHumidifierPower(power); | ||
| } | ||
|
|
||
| bool CHumidityController::newCommand(ICommand *p_command, | ||
| IComChannel *p_comchannel) | ||
| { | ||
| bool b_command_recognised = false; | ||
| ICommand::command_error_code_t result = ICommand::COMMAND_OK; | ||
| if (p_command->getName()->compare("?humidity") == 0) | ||
| { | ||
| sendStatus(p_comchannel); | ||
| b_command_recognised = true; | ||
| } | ||
|
|
||
| if (p_command->getName()->compare("humidity") == 0) | ||
| { | ||
| result = setHumidity(p_command); | ||
| b_command_recognised = true; | ||
| } | ||
|
|
||
| if (p_command->getName()->compare("heater") == 0) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably have a more obvious name for a command that overrides humidity power. Perhaps: humidifier_override(). |
||
| { | ||
| result = overrideHumidifier(p_command); | ||
| b_command_recognised = true; | ||
| } | ||
| if (b_command_recognised) | ||
| { | ||
| switch (result) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is now a method in CController that will do what this |
||
| { | ||
| case ICommand::COMMAND_OK: | ||
| // p_comchannel->send("OK.\n"); | ||
| break; | ||
| case ICommand::ERROR_ARG_COUNT: | ||
| p_comchannel->send("Wrong number of arguments.\n"); | ||
| break; | ||
| case ICommand::ERROR_OUT_OF_BOUNDS: | ||
| p_comchannel->send("Argument out of bounds.\n"); | ||
| break; | ||
| case ICommand::ERROR_TYPE_MISMATCH: | ||
| p_comchannel->send("Argument type mismatch.\n"); | ||
| break; | ||
| default: | ||
| p_comchannel->send("Non-specific error with the command."); | ||
| } | ||
| } | ||
| return b_command_recognised; | ||
| } | ||
|
|
||
| void CHumidityController::reset() | ||
| { | ||
| m_target_humidity = DISABLE_TARGET; | ||
| m_power_override = DISABLE_OVERRIDE; | ||
| m_control_loop.reset(); | ||
| } | ||
|
|
||
| void CHumidityController::sendStatus(IComChannel *p_comchannel) | ||
| { | ||
| etl::string<MAX_STRING_SIZE> message; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are currently discussing how to return responses to be understood by higher layers. This might change drastically in the future. |
||
| char value[10]; | ||
| // send target humidity | ||
| message.assign("Target: "); | ||
| sprintf(value, "%2.1f", m_target_humidity); | ||
| message.append(value); | ||
| p_comchannel->send(message); | ||
| // Send actual humidity | ||
| message.assign("Humidity: "); | ||
| sprintf(value, "%2.1f, ", mp_humidity_sensor->getHumidity()); | ||
| message.append(value); | ||
| p_comchannel->send(message); | ||
| // Send heater power | ||
| message.assign("Power: "); | ||
| float power; | ||
| if (m_power_override == DISABLE_OVERRIDE) | ||
| { | ||
| power = mp_hw->getHardPwmOutput(CHANNEL_NUMBER - 1); | ||
| } | ||
| else | ||
| { | ||
| power = m_power_override; | ||
| } | ||
| sprintf(value, "%4.1f, ", power); | ||
| message.append(value); | ||
| p_comchannel->send(message); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Set humidity value | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incomplete doxy header. Should have |
||
| */ | ||
| ICommand::command_error_code_t CHumidityController::setHumidity( | ||
| ICommand *p_command) | ||
| { | ||
| // Sanitise command arguments | ||
| if (p_command->getArgumentCount() != 1) | ||
| { | ||
| return ICommand::ERROR_ARG_COUNT; | ||
| } | ||
| m_target_humidity = (*p_command)[0]; | ||
|
|
||
| if (m_target_humidity > MAX_HUMIDITY) | ||
| { | ||
| m_target_humidity = MAX_HUMIDITY; | ||
| } | ||
| if ((m_target_humidity < MIN_HUMIDITY) && | ||
| (m_target_humidity != DISABLE_TARGET)) | ||
| { | ||
| m_target_humidity = MIN_HUMIDITY; | ||
| } | ||
|
|
||
| return ICommand::COMMAND_OK; | ||
| } | ||
|
|
||
| ICommand::command_error_code_t CHumidityController::overrideHumidifier( | ||
| ICommand *p_command) | ||
| { | ||
| // Sanitise command arguments | ||
| if (p_command->getArgumentCount() != 1) | ||
| { | ||
| return ICommand::ERROR_ARG_COUNT; | ||
| } | ||
| float power = (*p_command)[0]; | ||
|
|
||
| // Execute command | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary comment. And it's wrong, since you are sanitising before executing. |
||
| if (power > MAX_POWER) | ||
| { | ||
| power = MAX_POWER; | ||
| } | ||
| if ((power < MIN_POWER) && (power != DISABLE_OVERRIDE)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is complex logic and would be much easier if you used boolean switch to control override. |
||
| { | ||
| power = MIN_POWER; | ||
| } | ||
| m_power_override = power; | ||
| return ICommand::COMMAND_OK; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * CHumidityController.h | ||
| * | ||
| * Created on: Aug. 11, 2022 | ||
| * Author: samk | ||
| */ | ||
|
|
||
| #ifndef CHUMIDITYCONTROLLER_H_ | ||
| #define CHUMIDITYCONTROLLER_H_ | ||
|
|
||
| #include "CBME280.h" | ||
| #include "CController.h" | ||
| #include "CHumidifier.h" | ||
| #include "CPIDLoop.h" | ||
| #include "IHardwareMap.h" | ||
|
|
||
| #define CHANNEL_NUMBER 1 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this still here? |
||
|
|
||
| class CHumidityController : public CController | ||
| { | ||
| public: | ||
| CHumidityController(IHardwareMap *p_hardwaremap, | ||
| etl::string<MAX_STRING_SIZE> name, | ||
| uint32_t run_period_ms, | ||
| SPI_HandleTypeDef *p_spi, | ||
| GPIO_TypeDef *p_slave_select_port, | ||
| uint16_t slave_select_pin); | ||
| virtual void run(); | ||
| virtual bool newCommand(ICommand *p_command, IComChannel *p_comchannel); | ||
| virtual void reset(); | ||
|
|
||
| private: | ||
| void sendStatus(IComChannel *p_comchannel); | ||
| ICommand::command_error_code_t setHumidity(ICommand *p_command); | ||
| ICommand::command_error_code_t overrideHumidifier(ICommand *p_command); | ||
|
|
||
| IHardwareMap *mp_hw; | ||
|
|
||
| CBME280 *mp_humidity_sensor; | ||
|
|
||
| float m_target_humidity; | ||
| float m_power_override; | ||
| CPIDLoop m_control_loop; | ||
| }; | ||
|
|
||
| #endif /* CHUMIDITYCONTROLLER_H_ */ | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The header files that are included in
CHumidityController.hdo not need to be included here. They will be visible. Only include here header file for own class and whatever headers you need to operate, e.g.stdio.h.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted. I was a bit confused about including header files in the .cpp and .h. Thanks