|
|
|
|
|
|
|
|
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; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |