FSM mostly working, need to implement bufferd param changes
This commit is contained in:
parent
af40298378
commit
1e8d24dc40
@ -1,183 +1,79 @@
|
||||
#include "treppe.h"
|
||||
|
||||
uint8_t Treppe::softstart_led(uint8_t led, uint16_t startval, uint16_t stopval)
|
||||
|
||||
/*
|
||||
dimm_stufe
|
||||
- dimmt stufe (0 - 15, PCA9685 outputs) mit linearen ticks
|
||||
von idle bis active brightness
|
||||
- return false solange gedimmt wird
|
||||
- return true bei nächster stufe
|
||||
*/
|
||||
bool Treppe::dimm_stufe(uint8_t stufe)
|
||||
{
|
||||
/*
|
||||
softstart task
|
||||
|
||||
- get's called at regular intervals (1ms at the moment)
|
||||
- dimms single led (0 - 15, PCA9685 outputs) with linear intervals vom startval to stopval
|
||||
- calculates pwm steps depending on startval, stopval and timeinterval
|
||||
- -> results in constanst speed
|
||||
- returns 1 if led dimming is running
|
||||
- returns 0 if led dimming is finished
|
||||
|
||||
*/
|
||||
|
||||
static uint8_t lastled = 255;
|
||||
static float current_pwm = 0;
|
||||
static float stepsize = 1.0;
|
||||
if (led != lastled)
|
||||
{
|
||||
pwmController.setChannelPWM(led, (uint16_t)startval);
|
||||
lastled = led;
|
||||
current_pwm = startval;
|
||||
stepsize = INT_TIME * abs(stopval - startval) / (float)time_per_stair; // only valid at 1ms function call interval
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (startval > stopval)
|
||||
{
|
||||
current_pwm -= stepsize;
|
||||
}
|
||||
if (fsm_outputs.dimmrichtung == DR_AUFDIMMEN)
|
||||
current_pwm += differenz_pwm_pro_tick;
|
||||
else
|
||||
{
|
||||
current_pwm += stepsize;
|
||||
}
|
||||
// Serial.println((uint16_t)current_pwm);
|
||||
pwmController.setChannelPWM(led, (uint16_t)current_pwm);
|
||||
if (current_pwm > stopval - stepsize && current_pwm < stopval + stepsize)
|
||||
{
|
||||
if (stopval == 0)
|
||||
pwmController.setChannelPWM(led, 0);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
current_pwm -= differenz_pwm_pro_tick;
|
||||
pwmController.setChannelPWM(stufe, static_cast<uint16_t> (current_pwm));
|
||||
|
||||
current_tick++;
|
||||
if (current_tick >= ticks_pro_stufe)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Treppe::ledsequence()
|
||||
/*
|
||||
animation tick
|
||||
- nach dem dimmen einer stufe wird die stufe weitergezählt
|
||||
- abbruch am ende => anim_beendet = true;
|
||||
*/
|
||||
void Treppe::anim_tick()
|
||||
{
|
||||
static int8_t led = 0;
|
||||
static uint16_t brightness = 0;
|
||||
static uint16_t lastbrightness = 0;
|
||||
static uint16_t status = 0;
|
||||
uint16_t status_build = 0;
|
||||
status_build |= direction << 8;
|
||||
status_build |= state;
|
||||
if (status_build != status)
|
||||
{ // check if any parameter changed
|
||||
finish = 0; // set state unfinished -> start action
|
||||
if (direction)
|
||||
led = 0; // reset led counter depending of direction
|
||||
else
|
||||
led = stairs - 1;
|
||||
if (state)
|
||||
{
|
||||
brightness = active_brightness; // set brightness value depending of on/off
|
||||
lastbrightness = idle_brightness;
|
||||
}
|
||||
else
|
||||
{
|
||||
brightness = idle_brightness;
|
||||
lastbrightness = active_brightness;
|
||||
}
|
||||
status = status_build; // set parameter memory
|
||||
|
||||
Serial.printf("----Status Changed! onoff: %d, dir: %d\n", state, direction);
|
||||
}
|
||||
if (!finish)
|
||||
{ // finish == 0 -> action pending
|
||||
if (!softstart_led(led, lastbrightness, brightness))
|
||||
{
|
||||
Serial.printf("one LED finished: led: %d, last: %d, curr %d\n",
|
||||
led, lastbrightness, brightness);
|
||||
|
||||
if (direction)
|
||||
{
|
||||
led++;
|
||||
if (led >= stairs)
|
||||
finish = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
led--;
|
||||
if (led < 0)
|
||||
finish = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Treppe::rampe()
|
||||
{
|
||||
if (state)
|
||||
if (!dimm_stufe(stufe))
|
||||
{
|
||||
finish = 0;
|
||||
state = 0; // set parameter memory
|
||||
}
|
||||
Serial.printf("anim_tick(): stufe: %d, start: %d, ziel: %d, current %f\n",
|
||||
stufe, start_pwm, ziel_pwm, current_pwm);
|
||||
|
||||
if (!finish)
|
||||
{
|
||||
if (direction)
|
||||
{ // aufwärts
|
||||
if (tick >= ticks_treppe - 1)
|
||||
{ // ziel erreicht
|
||||
Serial.println("[Treppe] oberster tick !");
|
||||
finish = 1;
|
||||
if (fsm_outputs.laufrichtung == LR_HOCH)
|
||||
{
|
||||
if (stufe >= stufen-1) {
|
||||
anim_beendet = true;
|
||||
return;
|
||||
}
|
||||
tick++; // eins hoch
|
||||
stufe++;
|
||||
}
|
||||
else
|
||||
{ // abwärts
|
||||
if (tick <= 0)
|
||||
{ // ziel erreicht
|
||||
Serial.println("[Treppe] unterster tick !");
|
||||
finish = 1;
|
||||
{
|
||||
if (stufe <= 0) {
|
||||
anim_beendet = true;
|
||||
return;
|
||||
}
|
||||
tick--; // eins runter
|
||||
stufe--;
|
||||
}
|
||||
|
||||
stufe = tick / ticks_pro_stufe;
|
||||
|
||||
float new_pwm = 0.0;
|
||||
if (an_aus)
|
||||
{
|
||||
new_pwm = differenz_pwm_pro_tick * (tick - ticks_pro_stufe * stufe);
|
||||
new_pwm += idle_brightness;
|
||||
if (direction)
|
||||
new_pwm += differenz_pwm_pro_tick;
|
||||
}
|
||||
else
|
||||
{
|
||||
new_pwm = active_brightness - differenz_pwm_pro_tick * (tick - ticks_pro_stufe * stufe);
|
||||
new_pwm += idle_brightness;
|
||||
if (direction)
|
||||
new_pwm -= differenz_pwm_pro_tick;
|
||||
}
|
||||
|
||||
pwmController.setChannelPWM(stufe, (uint16_t)new_pwm);
|
||||
Serial.printf("tick %04u, led %02d:%02u, pwm %4.1f\n",
|
||||
tick,
|
||||
stufe,
|
||||
(tick - ticks_pro_stufe * stufe),
|
||||
new_pwm);
|
||||
current_tick = 0;
|
||||
current_pwm = start_pwm;
|
||||
}
|
||||
}
|
||||
|
||||
void Treppe::setup()
|
||||
{
|
||||
// startbedingunen für animation
|
||||
void Treppe::start_animation() {
|
||||
anim_beendet = false;
|
||||
|
||||
pwmController.resetDevices();
|
||||
// Deactive PCA9685 Phase Balancer due to LED Flickering
|
||||
// https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
|
||||
// see also lib/PCA9685-Arduin/PCA9685.h:204
|
||||
pwmController.init(PCA9685_PhaseBalancer_None);
|
||||
//pwmController.init(PCA9685_PhaseBalancer_Linear);
|
||||
pwmController.setPWMFrequency(100);
|
||||
pwmController.setAllChannelsPWM(idle_brightness);
|
||||
if(fsm_outputs.laufrichtung == LR_HOCH)
|
||||
stufe = 0;
|
||||
else
|
||||
stufe = stufen-1;
|
||||
|
||||
if(fsm_outputs.dimmrichtung == DR_AUFDIMMEN) {
|
||||
start_pwm = idle_brightness;
|
||||
ziel_pwm = active_brightness;
|
||||
} else {
|
||||
start_pwm = active_brightness;
|
||||
ziel_pwm = idle_brightness;
|
||||
}
|
||||
|
||||
pinMode(A0, INPUT);
|
||||
pinMode(SENSOR_OBEN, INPUT);
|
||||
pinMode(SENSOR_UNTEN, INPUT);
|
||||
pinMode(OE, OUTPUT);
|
||||
digitalWrite(OE, 0);
|
||||
|
||||
Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick);
|
||||
Serial.println("Hello from Treppe");
|
||||
Serial.print("Treppe: initial parameters: stairs=");
|
||||
Serial.println(stairs);
|
||||
current_tick = 0;
|
||||
current_pwm = start_pwm;
|
||||
}
|
||||
|
||||
void Treppe::print_state_on_change()
|
||||
@ -214,68 +110,69 @@ void Treppe::task()
|
||||
|
||||
fsm_inputs.sensor_oben = read_sensor(SENSOR_OBEN);
|
||||
fsm_inputs.sensor_unten = read_sensor(SENSOR_UNTEN);
|
||||
fsm_inputs.anim_beendet = static_cast<bool>(finish);
|
||||
if(finish) // flanke nach finished reicht ?
|
||||
finish = false;
|
||||
fsm_inputs.anim_beendet = static_cast<bool>(anim_beendet);
|
||||
|
||||
FSMTreppe_Obj.setExternalInputs(&fsm_inputs);
|
||||
FSMTreppe_Obj.step();
|
||||
fsm_outputs = FSMTreppe_Obj.getExternalOutputs();
|
||||
print_state_on_change();
|
||||
|
||||
direction = fsm_outputs.laufrichtung;
|
||||
state = fsm_outputs.dimmrichtung;
|
||||
|
||||
if(fsm_outputs.status > ST_INAKTIV_LDR) {
|
||||
ledsequence();
|
||||
if(fsm_outputs.status > ST_RUHEZUSTAND) {
|
||||
if( anim_beendet == true &&
|
||||
( fsm_outputs.status == ST_AUFDIMMEN_HOCH || fsm_outputs.status == ST_ABDIMMEN_HOCH ||
|
||||
fsm_outputs.status == ST_AUFDIMMEN_RUNTER || fsm_outputs.status == ST_ABDIMMEN_RUNTER ))
|
||||
{
|
||||
start_animation();
|
||||
}
|
||||
if( !anim_beendet )
|
||||
anim_tick();
|
||||
}
|
||||
// setTick(ticks_treppe);
|
||||
// setAnAus(1);
|
||||
// setDirection(0);
|
||||
// setState(0);
|
||||
}
|
||||
|
||||
uint16_t Treppe::setIdle(uint16_t _idle_brightness)
|
||||
void Treppe::berechne_dimmer() {
|
||||
ticks_pro_stufe = time_per_stair / 20; // [ms]
|
||||
differenz_pwm_pro_tick = (float) (active_brightness - idle_brightness)
|
||||
/ (float) ticks_pro_stufe;
|
||||
}
|
||||
|
||||
void Treppe::setup()
|
||||
{
|
||||
pwmController.resetDevices();
|
||||
// Deactive PCA9685 Phase Balancer due to LED Flickering
|
||||
// https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
|
||||
// see also lib/PCA9685-Arduin/PCA9685.h:204
|
||||
pwmController.init(PCA9685_PhaseBalancer_None);
|
||||
//pwmController.init(PCA9685_PhaseBalancer_Linear);
|
||||
pwmController.setPWMFrequency(100);
|
||||
pwmController.setAllChannelsPWM(idle_brightness);
|
||||
|
||||
pinMode(A0, INPUT);
|
||||
pinMode(SENSOR_OBEN, INPUT);
|
||||
pinMode(SENSOR_UNTEN, INPUT);
|
||||
pinMode(OE, OUTPUT);
|
||||
digitalWrite(OE, 0);
|
||||
|
||||
Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick);
|
||||
Serial.printf("Treppe: initial parameters: stufen=%d\n", stufen);
|
||||
}
|
||||
|
||||
// ###################################################################################################################
|
||||
// GEBUFFERT => Erst im Ruhezustand übernehmen !!!!
|
||||
void Treppe::set_idle_pwm(uint16_t _idle_brightness)
|
||||
{
|
||||
idle_brightness = _idle_brightness;
|
||||
Serial.println("Treppe: idle brightness changed!");
|
||||
return idle_brightness;
|
||||
berechne_dimmer();
|
||||
Serial.printf("Treppe: idle_brightness=%d\n", idle_brightness);
|
||||
}
|
||||
uint16_t Treppe::setActive(uint16_t _active_brightness)
|
||||
void Treppe::set_active_pwm(uint16_t _active_brightness)
|
||||
{
|
||||
active_brightness = _active_brightness;
|
||||
Serial.println("Treppe: active brightness changed!");
|
||||
return active_brightness;
|
||||
berechne_dimmer();
|
||||
Serial.printf("Treppe: active_brightness=%d\n", active_brightness);
|
||||
}
|
||||
uint16_t Treppe::setTime(uint16_t _time_per_stair)
|
||||
void Treppe::set_time_per_stair(uint16_t _time_per_stair)
|
||||
{
|
||||
time_per_stair = _time_per_stair;
|
||||
Serial.println("Treppe: time changed!");
|
||||
return time_per_stair;
|
||||
}
|
||||
|
||||
void Treppe::setDirection(uint8_t _direction)
|
||||
{
|
||||
switch_direction = _direction;
|
||||
Serial.printf("Treppe: switch_direction=%d!\n", switch_direction);
|
||||
if (finish)
|
||||
Serial.println("apply direction request immediately");
|
||||
else
|
||||
Serial.println("currently active, dir change afterwards");
|
||||
// to do: implement state command variable to determine dimm-state
|
||||
}
|
||||
|
||||
void Treppe::setState(uint8_t _state)
|
||||
{
|
||||
if (state == _state)
|
||||
return;
|
||||
else
|
||||
{
|
||||
switch_state = _state;
|
||||
Serial.printf("Treppe: switch_state=%d!\n", switch_state);
|
||||
if (finish)
|
||||
Serial.println("apply state request immediately");
|
||||
else
|
||||
Serial.println("currently active, state changes after activity");
|
||||
}
|
||||
berechne_dimmer();
|
||||
Serial.printf("Treppe: time_per_stair=%d\n", time_per_stair);
|
||||
}
|
@ -11,26 +11,25 @@
|
||||
|
||||
class Treppe {
|
||||
private:
|
||||
uint8_t stairs;
|
||||
uint16_t time_per_stair = 300; // dimmtime per stair [ms]
|
||||
const uint8_t stufen;
|
||||
uint16_t time_per_stair = 300; // dimmtime per stair [ms]
|
||||
uint16_t idle_brightness = 0;
|
||||
uint16_t active_brightness = 500;
|
||||
|
||||
uint8_t direction = 0;
|
||||
uint8_t switch_direction = 0;
|
||||
uint8_t state = 0;
|
||||
uint8_t switch_state = 0;
|
||||
bool finish = 1;
|
||||
|
||||
// alternative
|
||||
uint32_t tick = 0;
|
||||
uint32_t stufe = 0;
|
||||
uint8_t an_aus = 0;
|
||||
|
||||
uint32_t ticks_treppe = 0;
|
||||
uint32_t ticks_pro_stufe = 0;
|
||||
uint16_t start_pwm = 0;
|
||||
uint16_t ziel_pwm = 0;
|
||||
|
||||
bool anim_beendet = true;
|
||||
uint8_t stufe = 0;
|
||||
uint16_t current_tick = 0;
|
||||
float current_pwm = 0.0;
|
||||
uint16_t ticks_pro_stufe = 0;
|
||||
float differenz_pwm_pro_tick = 0.0;
|
||||
// alternative
|
||||
|
||||
bool dimm_stufe(uint8_t stufe);
|
||||
void anim_tick();
|
||||
void start_animation();
|
||||
void berechne_dimmer();
|
||||
|
||||
// initialize with i2c-Address 0, use Wire Library
|
||||
PCA9685 pwmController;
|
||||
@ -48,25 +47,23 @@ private:
|
||||
ST_WARTEN_RUNTER =6,
|
||||
ST_ABDIMMEN_RUNTER =7
|
||||
};
|
||||
enum fsm_laufrichtung_t {
|
||||
LR_RUNTER=0,
|
||||
LR_HOCH=1
|
||||
};
|
||||
enum fsm_dimmrichtung_t {
|
||||
DR_ABDIMMEN=0,
|
||||
DR_AUFDIMMEN=1
|
||||
};
|
||||
|
||||
uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval);
|
||||
void ledsequence();
|
||||
void rampe();
|
||||
bool read_sensor(int sensor) {
|
||||
int pegel = digitalRead(sensor);
|
||||
return static_cast<bool>(pegel);
|
||||
}
|
||||
public:
|
||||
Treppe(uint8_t _stairs) : stairs(_stairs){
|
||||
Treppe(uint8_t _stufen) : stufen(_stufen){
|
||||
FSMTreppe_Obj.initialize();
|
||||
|
||||
|
||||
|
||||
ticks_pro_stufe = time_per_stair / 20; // [ms]
|
||||
ticks_treppe = ticks_pro_stufe * stairs;
|
||||
|
||||
differenz_pwm_pro_tick = (float) (active_brightness - idle_brightness)
|
||||
/ (float) ticks_pro_stufe;
|
||||
berechne_dimmer();
|
||||
}
|
||||
~Treppe() {
|
||||
FSMTreppe_Obj.terminate();
|
||||
@ -76,26 +73,7 @@ public:
|
||||
void task(); // call periodically
|
||||
|
||||
// Parameter section
|
||||
uint16_t setIdle(uint16_t _idle_brightness);
|
||||
uint16_t setActive(uint16_t _active_brightness);
|
||||
uint16_t setTime(uint16_t _time_per_stair);
|
||||
|
||||
void setTick(uint32_t _tick) {
|
||||
tick = _tick;
|
||||
Serial.printf("Treppe: Tick: %u!\n", tick);
|
||||
}
|
||||
uint32_t getTicks() {
|
||||
return ticks_treppe;
|
||||
}
|
||||
|
||||
// Runtime Parameter section
|
||||
void setDirection(uint8_t _direction);
|
||||
void setState(uint8_t _state);
|
||||
void setAnAus(uint8_t _an_aus) {
|
||||
an_aus = _an_aus;
|
||||
}
|
||||
|
||||
uint8_t getState() { return state;};
|
||||
uint8_t getFinished() { return finish;};
|
||||
uint8_t getDirection() {return direction;};
|
||||
void set_idle_pwm(uint16_t _idle_brightness);
|
||||
void set_active_pwm(uint16_t _active_brightness);
|
||||
void set_time_per_stair(uint16_t _time_per_stair);
|
||||
};
|
14
src/main.cpp
14
src/main.cpp
@ -94,23 +94,9 @@ void setup() {
|
||||
os_timer_arm(&timer1, 20, true);
|
||||
}
|
||||
|
||||
#include <random>
|
||||
|
||||
uint32_t c = 0;
|
||||
|
||||
void loop() {
|
||||
// if(stairs.getState() == 0) {
|
||||
// delay(1000);
|
||||
|
||||
// // uint32_t t = rand() % stairs.getTicks();
|
||||
// // uint32_t d = rand() % 2;
|
||||
|
||||
// // stairs.setTick(t);
|
||||
// // stairs.setDirection(d);
|
||||
|
||||
// stairs.setDirection(!stairs.getDirection());
|
||||
// stairs.setState(1);
|
||||
// }
|
||||
if(inter){
|
||||
Serial.printf("interrupt\n");
|
||||
inter = 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user