Browse Source

add structs to treppe to clear understanding

tags/v1.0.0
Simon Schmidt 3 years ago
parent
commit
d3ed19e639
2 changed files with 116 additions and 59 deletions
  1. 88
    48
      lib/treppe/treppe.cpp
  2. 28
    11
      lib/treppe/treppe.h

+ 88
- 48
lib/treppe/treppe.cpp View File

bool Treppe::dimm_stufe(uint8_t stufe) bool Treppe::dimm_stufe(uint8_t stufe)
{ {
if (fsm_outputs.dimmrichtung == DR_AUFDIMMEN) if (fsm_outputs.dimmrichtung == DR_AUFDIMMEN)
current_pwm += differenz_pwm_pro_tick;
dimmer_stufe.pwm += dimmer_stufe.delta_pwm;
else else
current_pwm -= differenz_pwm_pro_tick;
Serial.printf("%3.0f", current_pwm);
pwmController.setChannelPWM(stufe, static_cast<uint16_t>(current_pwm));
dimmer_stufe.pwm -= dimmer_stufe.delta_pwm;
Serial.printf("%3.0f", dimmer_stufe.pwm);
pwmController.setChannelPWM(stufe, static_cast<uint16_t>(dimmer_stufe.pwm));


current_tick++;
if (current_tick >= ticks_pro_stufe) {
dimmer_stufe.tick++;
if (dimmer_stufe.tick >= dimmer_stufe.ticks)
{
Serial.println(""); Serial.println("");
return false; return false;
} }
*/ */
void Treppe::anim_tick() void Treppe::anim_tick()
{ {
if (!dimm_stufe(stufe))
if (!dimm_stufe(dimmer_stufe.stufe))
{ {
// Serial.printf("anim_tick(): stufe: %d, start: %d, ziel: %d, current %f\n", // Serial.printf("anim_tick(): stufe: %d, start: %d, ziel: %d, current %f\n",
// stufe, start_pwm, ziel_pwm, current_pwm); // stufe, start_pwm, ziel_pwm, current_pwm);
if (fsm_outputs.laufrichtung == LR_HOCH) if (fsm_outputs.laufrichtung == LR_HOCH)
{ {
if (stufe >= stufen - 1)
if (dimmer_stufe.stufe >= stufen - 1)
{ {
anim_beendet = true; anim_beendet = true;
return; return;
} }
stufe++;
dimmer_stufe.stufe++;
} }
else else
{ {
if (stufe <= 0)
if (dimmer_stufe.stufe <= 0)
{ {
anim_beendet = true; anim_beendet = true;
return; return;
} }
stufe--;
dimmer_stufe.stufe--;
} }
current_tick = 0;
current_pwm = start_pwm;
dimmer_stufe.tick = 0;
dimmer_stufe.pwm = dimmer_stufe.start_pwm;
} }
} }


anim_beendet = false; anim_beendet = false;


if (fsm_outputs.laufrichtung == LR_HOCH) if (fsm_outputs.laufrichtung == LR_HOCH)
stufe = 0;
dimmer_stufe.stufe = 0;
else else
stufe = stufen - 1;
dimmer_stufe.stufe = stufen - 1;


if (fsm_outputs.dimmrichtung == DR_AUFDIMMEN) if (fsm_outputs.dimmrichtung == DR_AUFDIMMEN)
{ {
start_pwm = idle_pwm_internal;
ziel_pwm = active_pwm;
dimmer_stufe.start_pwm = idle_pwm_ist;
dimmer_stufe.ziel_pwm = active_pwm;
} }
else else
{ {
start_pwm = active_pwm;
ziel_pwm = idle_pwm_internal;
dimmer_stufe.start_pwm = active_pwm;
dimmer_stufe.ziel_pwm = idle_pwm_ist;
} }


current_tick = 0;
current_pwm = start_pwm;
dimmer_stufe.tick = 0;
dimmer_stufe.pwm = dimmer_stufe.start_pwm;
} }


void Treppe::print_state_on_change() void Treppe::print_state_on_change()


// follow up: averaging over many samples? // follow up: averaging over many samples?
float ldr = read_ldr(); float ldr = read_ldr();
if (ldr < ldr_schwelle)
active = 1;
if (ldr > ldr_schwelle + LDR_HYS)
active = 0;

if (ldr < ldr_schwelle) {
idle_pwm_soll = idle_pwm_max;
active = true;
}
if (ldr > ldr_schwelle + LDR_HYS) {
idle_pwm_soll = 0;
active = false;
}

if (idle_pwm_soll != idle_pwm_ist) {
}
activate_idle_pwm(active); activate_idle_pwm(active);
return active; return active;
} }


void Treppe::task() void Treppe::task()
{
// fsm_inputs.ldr_schwelle = 1UL;
// fsm_inputs.sensor_oben = 0UL;
// fsm_inputs.sensor_unten = 0UL;
// fsm_inputs.anim_beendet = anim_beendet;

{
uint32_t m=micros();
fsm_inputs.ldr_schwelle = check_ldr(); fsm_inputs.ldr_schwelle = check_ldr();
Serial.print("1:");
Serial.println(micros()-m);
m=micros();
fsm_inputs.sensor_oben = read_sensor(SENSOR_OBEN); fsm_inputs.sensor_oben = read_sensor(SENSOR_OBEN);
fsm_inputs.sensor_unten = read_sensor(SENSOR_UNTEN); fsm_inputs.sensor_unten = read_sensor(SENSOR_UNTEN);
fsm_inputs.anim_beendet = static_cast<bool>(anim_beendet);

fsm_inputs.anim_beendet = anim_beendet;
Serial.print("2:");
Serial.println(micros()-m);
m=micros();
FSMTreppe_Obj.setExternalInputs(&fsm_inputs); FSMTreppe_Obj.setExternalInputs(&fsm_inputs);
FSMTreppe_Obj.step(); FSMTreppe_Obj.step();
fsm_outputs = FSMTreppe_Obj.getExternalOutputs(); fsm_outputs = FSMTreppe_Obj.getExternalOutputs();
Serial.print("3:");
Serial.println(micros()-m);

m=micros();
print_state_on_change(); print_state_on_change();
Serial.print("4:");
Serial.println(micros()-m);



m=micros();
if( fsm_outputs.status == ST_AUFDIMMEN_HOCH || if( fsm_outputs.status == ST_AUFDIMMEN_HOCH ||
fsm_outputs.status == ST_ABDIMMEN_HOCH || fsm_outputs.status == ST_ABDIMMEN_HOCH ||
fsm_outputs.status == ST_AUFDIMMEN_RUNTER || fsm_outputs.status == ST_AUFDIMMEN_RUNTER ||
else else
anim_tick(); anim_tick();
} }
Serial.print("5:");
Serial.println(micros()-m);


m=micros();
// else if (fsm_outputs.status == ST_DIMMEN_LDR) {
// if(anim_beendet) {
// berechne_dimmer();
// anim_beendet = false;
// }
// anim_beendet = dimm_treppe();
// }
Serial.print("6:");
Serial.println(micros()-m);
} }


void Treppe::berechne_dimmer() void Treppe::berechne_dimmer()
{ {
ticks_pro_stufe = time_per_stair / INT_TIME; // [ms]
differenz_pwm_pro_tick = (float)(active_pwm - idle_pwm_internal)
/ (float)ticks_pro_stufe;
dimmer_stufe.ticks = time_per_stair / INT_TIME; // [ms]
dimmer_stufe.delta_pwm = (float)(active_pwm - idle_pwm_ist)
/ (float)dimmer_stufe.ticks;

dimmer_ldr.ticks = time_ldr / INT_TIME; // [ms]
dimmer_ldr.delta_pwm = (float)(idle_pwm_soll - idle_pwm_ist)
/ (float)dimmer_ldr.ticks;
} }


void Treppe::setup() void Treppe::setup()
pinMode(OE, OUTPUT); pinMode(OE, OUTPUT);
digitalWrite(OE, 0); digitalWrite(OE, 0);


Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick);
Serial.printf("dimmer_stufe.delta_pwm %f\n", dimmer_stufe.delta_pwm);
Serial.printf("Treppe: initial parameters: stufen=%d\n", stufen); Serial.printf("Treppe: initial parameters: stufen=%d\n", stufen);
} }


void Treppe::set_idle_prozent(int prozent) {
void Treppe::set_idle_prozent(const int prozent)
{
uint16_t new_pwm = active_pwm * prozent / 100; uint16_t new_pwm = active_pwm * prozent / 100;
set_idle_pwm(new_pwm);
set_idle_pwm_max(new_pwm);
} }


void Treppe::set_idle_pwm(uint16_t new_idle_pwm)
void Treppe::set_idle_pwm_max(const uint16_t new_pwm)
{ {
if(new_idle_pwm > active_pwm) {
idle_pwm = active_pwm;
if(new_pwm > active_pwm) {
idle_pwm_max = active_pwm;
} else { } else {
idle_pwm = new_idle_pwm;
idle_pwm_max = new_pwm;
} }


Serial.printf("Treppe: idle_pwm=%d\n", idle_pwm);
Serial.printf("Treppe: idle_pwm_max=%d\n", idle_pwm_max);
berechne_dimmer(); berechne_dimmer();
activate_idle_pwm(true); activate_idle_pwm(true);
} }


if (fsm_outputs.status == ST_RUHEZUSTAND || fsm_outputs.status == ST_INAKTIV_LDR) if (fsm_outputs.status == ST_RUHEZUSTAND || fsm_outputs.status == ST_INAKTIV_LDR)
{ {
idle_pwm_internal = idle_pwm * active;
if (idle_pwm_internal != last_pwm)
idle_pwm_ist = idle_pwm_max * active;
if (idle_pwm_ist != last_pwm)
{ {
// Dimming Function for all LEDS ? // Dimming Function for all LEDS ?
berechne_dimmer(); berechne_dimmer();
pwmController.setAllChannelsPWM(idle_pwm_internal);
last_pwm = idle_pwm_internal;
pwmController.setAllChannelsPWM(idle_pwm_ist);
last_pwm = idle_pwm_ist;
} }
} }
} }

+ 28
- 11
lib/treppe/treppe.h View File

class Treppe { class Treppe {
private: private:
const uint8_t stufen; const uint8_t stufen;
const uint16_t time_ldr = 300;
uint16_t time_per_stair = 300; // dimmtime per stair [ms] uint16_t time_per_stair = 300; // dimmtime per stair [ms]
uint16_t idle_pwm = 100;
uint16_t idle_pwm_internal = 0;
uint16_t idle_pwm_max = 100;
uint16_t idle_pwm_ist = 0;
uint16_t idle_pwm_soll = 0;
uint16_t active_pwm = 700; uint16_t active_pwm = 700;


uint16_t ldr_schwelle = 2; // activation value for FSM [lx] uint16_t ldr_schwelle = 2; // activation value for FSM [lx]


uint16_t start_pwm = 0;
uint16_t ziel_pwm = 0;
bool anim_beendet = true; 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;

struct dimmer_stufe_t {
uint8_t stufe = 0;
uint16_t ticks = 0;
uint16_t tick = 0;

float delta_pwm = 0.0;
float pwm = 0.0;
uint16_t start_pwm = 0;
uint16_t ziel_pwm = 0;
};
dimmer_stufe_t dimmer_stufe;

struct dimmer_ldr_t {
uint16_t ticks = 0;
uint16_t tick = 0;

float delta_pwm = 0.0;
float pwm = 0.0;
uint16_t start_pwm = 0;
uint16_t ziel_pwm = 0;
};
dimmer_ldr_t dimmer_ldr;
// initialize with i2c-Address 0, use Wire Library // initialize with i2c-Address 0, use Wire Library
PCA9685 pwmController; PCA9685 pwmController;


// Parameter section // Parameter section
void set_idle_prozent(int prozent); void set_idle_prozent(int prozent);
void set_idle_pwm(uint16_t _idle_pwm);
void set_idle_pwm_max(const uint16_t new_pwm);
void activate_idle_pwm(bool active); void activate_idle_pwm(bool active);
void set_active_pwm(uint16_t _active_pwm); void set_active_pwm(uint16_t _active_pwm);
void set_time_per_stair(uint16_t _time_per_stair); void set_time_per_stair(uint16_t _time_per_stair);

Loading…
Cancel
Save