changed structure of treppe.h and renamed treppe.cpp accordingly, added header guards
This commit is contained in:
parent
fb3a438eb0
commit
13af5fd197
@ -1,6 +1,7 @@
|
||||
// Wrapper for ESP8266WebServer with Filesystem as HTTP source
|
||||
#ifndef __HTTPSERVER_H
|
||||
#define __HTTPSERVER_H
|
||||
|
||||
#pragma once
|
||||
// Wrapper for ESP8266WebServer with Filesystem as HTTP source
|
||||
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <stdarg.h>
|
||||
@ -42,3 +43,5 @@ public:
|
||||
void logf(const char *format, ...);
|
||||
void logt(const char *format, ...);
|
||||
};
|
||||
|
||||
#endif // __HTTPSERVER_H
|
@ -9,14 +9,14 @@
|
||||
bool Treppe::dimm_stufe(uint8_t stufe)
|
||||
{
|
||||
if (fsm_outputs.dimmrichtung == DR_AUFDIMMEN)
|
||||
current_pwm += differenz_pwm_pro_tick;
|
||||
dimmer_stufe.pwm += dimmer_stufe.delta_pwm;
|
||||
else
|
||||
current_pwm -= differenz_pwm_pro_tick;
|
||||
Serial.printf("dimm_stufe %d %f\n", stufe, current_pwm);
|
||||
pwmController.setChannelPWM(stufe, static_cast<uint16_t>(current_pwm));
|
||||
dimmer_stufe.pwm -= dimmer_stufe.delta_pwm;
|
||||
Serial.printf("dimm_stufe %d %f\n", stufe, 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)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
@ -41,31 +41,31 @@ bool Treppe::dimm_treppe()
|
||||
*/
|
||||
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",
|
||||
stufe, start_pwm, ziel_pwm, current_pwm);
|
||||
dimmer_stufe.stufe, dimmer_stufe.start_pwm, dimmer_stufe.ziel_pwm, dimmer_stufe.pwm);
|
||||
|
||||
if (fsm_outputs.laufrichtung == LR_HOCH)
|
||||
{
|
||||
if (stufe >= stufen - 1)
|
||||
if (dimmer_stufe.stufe >= stufen - 1)
|
||||
{
|
||||
anim_beendet = true;
|
||||
return;
|
||||
}
|
||||
stufe++;
|
||||
dimmer_stufe.stufe++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (stufe <= 0)
|
||||
if (dimmer_stufe.stufe <= 0)
|
||||
{
|
||||
anim_beendet = true;
|
||||
return;
|
||||
}
|
||||
stufe--;
|
||||
dimmer_stufe.stufe--;
|
||||
}
|
||||
current_tick = 0;
|
||||
current_pwm = start_pwm;
|
||||
dimmer_stufe.tick = 0;
|
||||
dimmer_stufe.pwm = dimmer_stufe.start_pwm;
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,23 +75,23 @@ void Treppe::start_animation()
|
||||
anim_beendet = false;
|
||||
|
||||
if (fsm_outputs.laufrichtung == LR_HOCH)
|
||||
stufe = 0;
|
||||
dimmer_stufe.stufe = 0;
|
||||
else
|
||||
stufe = stufen - 1;
|
||||
dimmer_stufe.stufe = stufen - 1;
|
||||
|
||||
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
|
||||
{
|
||||
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()
|
||||
@ -102,6 +102,7 @@ void Treppe::print_state_on_change()
|
||||
fsm_inputs.anim_beendet != last_in.anim_beendet ||
|
||||
fsm_inputs.sensor_oben != last_in.sensor_oben ||
|
||||
fsm_inputs.sensor_unten != last_in.sensor_unten ||
|
||||
fsm_inputs.ldr_schwelle != last_in.ldr_schwelle ||
|
||||
fsm_outputs.dimmrichtung != last_out.dimmrichtung ||
|
||||
fsm_outputs.laufrichtung != last_out.laufrichtung ||
|
||||
fsm_outputs.status != last_out.status)
|
||||
@ -109,13 +110,13 @@ void Treppe::print_state_on_change()
|
||||
last_in.anim_beendet = fsm_inputs.anim_beendet;
|
||||
last_in.sensor_oben = fsm_inputs.sensor_oben;
|
||||
last_in.sensor_unten = fsm_inputs.sensor_unten;
|
||||
last_in.ldr_schwelle = fsm_inputs.ldr_schwelle;
|
||||
last_out.dimmrichtung = fsm_outputs.dimmrichtung;
|
||||
last_out.laufrichtung = fsm_outputs.laufrichtung;
|
||||
last_out.status = fsm_outputs.status;
|
||||
|
||||
Serial.printf("FSM IN: s_u: %d, s_o: %d, beendet: %d =>",
|
||||
fsm_inputs.sensor_oben, fsm_inputs.sensor_unten, fsm_inputs.anim_beendet);
|
||||
Serial.print(" step => ");
|
||||
Serial.printf("FSM IN: s_u: %d, s_o: %d ldr_sw: %d, beendet: %d =>",
|
||||
fsm_inputs.sensor_oben, fsm_inputs.sensor_unten, fsm_inputs.ldr_schwelle, fsm_inputs.anim_beendet);
|
||||
Serial.printf("OUT: LR: %d DR: %d ST: %d\n",
|
||||
fsm_outputs.laufrichtung, fsm_outputs.dimmrichtung, fsm_outputs.status);
|
||||
}
|
||||
@ -202,49 +203,94 @@ bool Treppe::check_ldr()
|
||||
|
||||
// follow up: averaging over many samples?
|
||||
float ldr = read_ldr();
|
||||
if (ldr < ldr_schwelle)
|
||||
active = 1;
|
||||
if (ldr > ldr_schwelle + LDR_HYS)
|
||||
active = 0;
|
||||
activate_idle_pwm(active);
|
||||
bool active = false;
|
||||
|
||||
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) {
|
||||
}
|
||||
return active;
|
||||
}
|
||||
|
||||
void Treppe::task()
|
||||
{
|
||||
uint32_t m=micros();
|
||||
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_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);
|
||||
|
||||
FSMTreppe_Obj.setExternalInputs(&fsm_inputs);
|
||||
m=micros();
|
||||
// FSMTreppe_Obj.setExternalInputs(&fsm_inputs);
|
||||
Serial.print("3:");
|
||||
Serial.println(micros()-m);
|
||||
|
||||
m=micros();
|
||||
FSMTreppe_Obj.step();
|
||||
fsm_outputs = FSMTreppe_Obj.getExternalOutputs();
|
||||
print_state_on_change();
|
||||
Serial.print("4:");
|
||||
Serial.println(micros()-m);
|
||||
|
||||
if (fsm_outputs.status > ST_RUHEZUSTAND)
|
||||
m=micros();
|
||||
fsm_outputs = FSMTreppe_Obj.getExternalOutputs();
|
||||
Serial.print("5:");
|
||||
Serial.println(micros()-m);
|
||||
|
||||
dimm_treppe();
|
||||
|
||||
m=micros();
|
||||
print_state_on_change();
|
||||
Serial.print("6:");
|
||||
Serial.println(micros()-m);
|
||||
|
||||
m=micros();
|
||||
|
||||
dimm_treppe();
|
||||
|
||||
if (fsm_outputs.status == ST_AUFDIMMEN_HOCH ||
|
||||
fsm_outputs.status == ST_ABDIMMEN_HOCH ||
|
||||
fsm_outputs.status == ST_AUFDIMMEN_RUNTER ||
|
||||
fsm_outputs.status == ST_ABDIMMEN_RUNTER)
|
||||
{
|
||||
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)
|
||||
)
|
||||
{
|
||||
if(anim_beendet)
|
||||
start_animation();
|
||||
}
|
||||
if (!anim_beendet)
|
||||
if(!anim_beendet)
|
||||
anim_tick();
|
||||
}
|
||||
|
||||
// else if (fsm_outputs.status == ST_DIMMEN_LDR) {
|
||||
// if(anim_beendet) {
|
||||
// berechne_dimmer();
|
||||
// anim_beendet = false;
|
||||
// }
|
||||
// anim_beendet = dimm_treppe();
|
||||
// }
|
||||
|
||||
Serial.print("7:");
|
||||
Serial.println(micros()-m);
|
||||
}
|
||||
|
||||
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()
|
||||
@ -264,24 +310,25 @@ void Treppe::setup()
|
||||
pinMode(OE, OUTPUT);
|
||||
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);
|
||||
}
|
||||
|
||||
void Treppe::set_idle_prozent(int prozent) {
|
||||
void Treppe::set_idle_prozent(const int prozent)
|
||||
{
|
||||
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 {
|
||||
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();
|
||||
activate_idle_pwm(true);
|
||||
}
|
||||
@ -292,13 +339,13 @@ void Treppe::activate_idle_pwm(bool active)
|
||||
|
||||
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 ?
|
||||
berechne_dimmer();
|
||||
pwmController.setAllChannelsPWM(idle_pwm_internal);
|
||||
last_pwm = idle_pwm_internal;
|
||||
pwmController.setAllChannelsPWM(idle_pwm_ist);
|
||||
last_pwm = idle_pwm_ist;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#ifndef __TREPPE_H
|
||||
#define __TREPPE_H
|
||||
|
||||
#include "FSMTreppe2/FSMTreppe2.h"
|
||||
#include "FSMTreppe3/FSMTreppe3.h"
|
||||
#include "PCA9685.h"
|
||||
|
||||
// #define LDRDEBUG // comment in to override LDR measurement
|
||||
@ -16,37 +17,56 @@
|
||||
class Treppe {
|
||||
private:
|
||||
const uint8_t stufen;
|
||||
const uint16_t time_ldr = 300;
|
||||
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 ldr_schwelle = 2; // activation value for FSM [lx]
|
||||
|
||||
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;
|
||||
|
||||
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
|
||||
PCA9685 pwmController;
|
||||
PCA9685 pwmController;
|
||||
FSMTreppeModelClass FSMTreppe_Obj;
|
||||
FSMTreppeModelClass::ExtU_FSMTreppe_T fsm_inputs;
|
||||
FSMTreppeModelClass::ExtY_FSMTreppe_T fsm_outputs;
|
||||
enum fsm_status_t {
|
||||
ST_INAKTIV_LDR =0,
|
||||
ST_RUHEZUSTAND =1,
|
||||
ST_AUFDIMMEN_HOCH =2,
|
||||
ST_WARTEN_HOCH =3,
|
||||
ST_ABDIMMEN_HOCH =4,
|
||||
ST_AUFDIMMEN_RUNTER =5,
|
||||
ST_WARTEN_RUNTER =6,
|
||||
ST_ABDIMMEN_RUNTER =7
|
||||
ST_AUFDIMMEN_LDR =1,
|
||||
ST_ABDIMMEN_LDR =2,
|
||||
ST_RUHEZUSTAND =3,
|
||||
ST_AUFDIMMEN_HOCH =4,
|
||||
ST_WARTEN_HOCH =5,
|
||||
ST_ABDIMMEN_HOCH =6,
|
||||
ST_AUFDIMMEN_RUNTER =7,
|
||||
ST_WARTEN_RUNTER =8,
|
||||
ST_ABDIMMEN_RUNTER =9
|
||||
};
|
||||
enum fsm_laufrichtung_t {
|
||||
LR_RUNTER=0,
|
||||
@ -83,8 +103,10 @@ public:
|
||||
|
||||
// Parameter section
|
||||
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 set_active_pwm(uint16_t _active_pwm);
|
||||
void set_time_per_stair(uint16_t _time_per_stair);
|
||||
};
|
||||
};
|
||||
|
||||
#endif // __TREPPE_H
|
@ -13,5 +13,5 @@ mem 0x60000000 0x60001fff rw
|
||||
set serial baud 460800
|
||||
|
||||
file .pio/build/debug/firmware.elf
|
||||
target remote /dev/ttyUSB0
|
||||
target remote \\.\COM23
|
||||
thb loop
|
||||
|
17
src/main.cpp
17
src/main.cpp
@ -55,17 +55,19 @@ void setup() {
|
||||
Serial.begin(76800);
|
||||
#endif
|
||||
|
||||
Serial.println(F("Booting ...."));
|
||||
Serial.println("Booting ....");
|
||||
|
||||
//pinMode(NODEMCU_LED, OUTPUT);
|
||||
pinMode(ESP12_LED, OUTPUT);
|
||||
|
||||
Wire.begin(); // Wire must be started first
|
||||
Wire.setClock(1000000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
|
||||
Wire.begin(); // Wire must be started first
|
||||
Wire.setClock(1000000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
|
||||
stairs.setup();
|
||||
Serial.println("PCA9685 connected !");
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
|
||||
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
|
||||
Serial.println("Connection Failed! Rebooting...");
|
||||
delay(5000);
|
||||
@ -79,17 +81,14 @@ void setup() {
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
ota_setup();
|
||||
|
||||
|
||||
httpServer.start();
|
||||
httpServer.start_apps();
|
||||
|
||||
Serial.println("HTTP server started !");
|
||||
|
||||
stairs.setup();
|
||||
Serial.println("PCA9685 connected !");
|
||||
//attachInterrupt(digitalPinToInterrupt(2), int_test, RISING);
|
||||
//attachInterrupt(digitalPinToInterrupt(12), int_test, RISING);
|
||||
|
||||
|
||||
os_timer_setfn(&timer1, timerCallback, &timer_flag);
|
||||
os_timer_arm(&timer1, 20, true);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user