This is sample implementation of On Off controller(bang bang controller) for microcontrollers Arduino, STM32 etc... Visit repository at: https://github.com/watchdoge256/On-OffController
/**************************************************************
* ^Signal value *
* | *
* | *
* | *
* | X *
* +----------------------------------------+ *
* hysteresis up ^ | X X X *
* v | X X X *
* target +----------------------------------------+ *
* ^ | X X X *
* hysteresis down v | X X X *
* +----------------------------------------+ *
* | X X *
* | X *
* | X *
* |X *
* --------------------------------------> *
* time *
***************************************************************/
The following code depicts the library basic usage, input and output functions should be provided by end user.
#include "onoffcore.h"
static int signal = 10;
static int change = -1;
void turnOnCB(void)
{
change = 1;
}
void turnOFFcb(void)
{
change = -1;
}
int main(void)
{
// controller initial settings
onoffcore_set_ON_callback(turnOnCB);
onoffcore_set_OFF_callback(turnOFFcb);
onoffcore_set_target(15);
onoffcore_set_hist_up(5);
onoffcore_set_hist_down(5);
while(1){
// in normal circumstances this function will be called by any interrupt, i.e. ADC reading done
onoffcore_set_signal(signal);
// this task need to be called periodically i.e. 1ms( could be implemented as OS task.
onoffcore_run();
signal+=change;
}
return 0;
}
this function sets target value which controller tries to achieve returns: 0- status success
this function sets down tolerance values for controller returns: 0- status success
this function sets upper tolerance range for controller returns: 0- status success
this function sets actual value of signal to controller logic returns: 0- status success
this function will be called when signal drops bellow hysteresis_down returns: 0- status success
this function will be called when signal exceeds hysteresis_up returns: 0- status success
this function returns programmed target value returns: 0- status success
this function returns down tolerance value for controller returns: 0- status success
this function returns tolerance value for controller returns: 0- status success
this function returns value controller sees as signal value at the moment returns: 0- status success
this function runs controller logic, this has to be called periodically(i.e. each 1ms)
Tags: #DIY, #Arduino, #STM32, #bangbang, #microcontrollers, #C, #library, #onoff