ESP8266 Treppenlichtsteuerung mit OTA zum Firmware Upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

treppe.cpp 11KB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #include "treppe.h"
  2. // #define DEBUG_TIMING
  3. /*
  4. - dimmer_tick: increment pwm jeden tick, bis anim beendet
  5. - return: fsm_pend.anim_beendet
  6. */
  7. bool Treppe::dimmer_tick(dimmer_t *dimmer, bool dim_type) {
  8. dimmer->pwm += dimmer->delta_pwm;
  9. Serial.printf("%.0f", dimmer->pwm);
  10. if (dim_type == DIM_STUFEN) {
  11. pwmController.setChannelPWM(dimmer->stufe,
  12. static_cast<uint16_t>(dimmer->pwm));
  13. } else { // DIM_LDR
  14. pwmController.setAllChannelsPWM(static_cast<uint16_t>(dimmer->pwm));
  15. }
  16. dimmer->tick++;
  17. if (dimmer->tick < dimmer->ticks) {
  18. Serial.print("-");
  19. return false;
  20. }
  21. Serial.println("");
  22. if (dim_type == DIM_LDR) {
  23. Serial.printf("DIM_LDR: start: %d, ziel: %d\n", dimmer->start_pwm,
  24. dimmer->ziel_pwm);
  25. return true;
  26. } else { // DIM_STUFEN
  27. Serial.printf("DIM_STUFEN: stufe: %d, start: %d, ziel: %d\n",
  28. dimmer->stufe, dimmer->start_pwm, dimmer->ziel_pwm);
  29. if (fsm_outputs.laufrichtung == LR_HOCH) {
  30. if (dimmer->stufe >= stufen - 1)
  31. return true;
  32. dimmer->stufe++;
  33. } else { // LR_RUNTER
  34. if (dimmer->stufe <= 0)
  35. return true;
  36. dimmer->stufe--;
  37. }
  38. dimmer->tick = 0;
  39. dimmer->pwm = dimmer->start_pwm;
  40. }
  41. return false;
  42. }
  43. // startbedingunen für animation
  44. void Treppe::start_animation(dimmer_t *dimmer, bool dim_type, uint16_t on_pwm,
  45. uint16_t off_pwm) {
  46. fsm_pend.anim_beendet = false;
  47. if (dim_type == DIM_STUFEN) {
  48. if (fsm_outputs.laufrichtung == LR_HOCH)
  49. dimmer->stufe = 0;
  50. else
  51. dimmer->stufe = stufen - 1;
  52. dimmer->ticks = param.time_per_stair / INT_TIME; // [ms]
  53. } else { // DIM_LDR
  54. dimmer->ticks = param.time_ldr / INT_TIME; // [ms]
  55. }
  56. if (fsm_outputs.dimmrichtung == DR_AUFDIMMEN) {
  57. dimmer->start_pwm = off_pwm;
  58. dimmer->ziel_pwm = on_pwm;
  59. dimmer->delta_pwm = (float)(on_pwm - off_pwm) / (float)dimmer->ticks;
  60. } else {
  61. dimmer->start_pwm = on_pwm;
  62. dimmer->ziel_pwm = off_pwm;
  63. dimmer->delta_pwm = (float)(off_pwm - on_pwm) / (float)dimmer->ticks;
  64. }
  65. dimmer->tick = 0;
  66. dimmer->pwm = dimmer->start_pwm;
  67. Serial.printf("stufe %d, ticks %d, delta %f, start %d, ziel %d\n",
  68. dimmer->stufe, dimmer->ticks, dimmer->delta_pwm,
  69. dimmer->start_pwm, dimmer->ziel_pwm);
  70. }
  71. void Treppe::print_state_on_change() {
  72. static FSMTreppeModelClass::ExtU_FSMTreppe_T last_in;
  73. static FSMTreppeModelClass::ExtY_FSMTreppe_T last_out;
  74. if (fsm_inputs.anim_beendet != last_in.anim_beendet ||
  75. fsm_inputs.sensor_oben != last_in.sensor_oben ||
  76. fsm_inputs.sensor_unten != last_in.sensor_unten ||
  77. fsm_inputs.ldr_schwelle != last_in.ldr_schwelle ||
  78. fsm_outputs.dimmrichtung != last_out.dimmrichtung ||
  79. fsm_outputs.laufrichtung != last_out.laufrichtung ||
  80. fsm_outputs.status != last_out.status) {
  81. last_in.anim_beendet = fsm_inputs.anim_beendet;
  82. last_in.sensor_oben = fsm_inputs.sensor_oben;
  83. last_in.sensor_unten = fsm_inputs.sensor_unten;
  84. last_in.ldr_schwelle = fsm_inputs.ldr_schwelle;
  85. last_out.dimmrichtung = fsm_outputs.dimmrichtung;
  86. last_out.laufrichtung = fsm_outputs.laufrichtung;
  87. last_out.status = fsm_outputs.status;
  88. Serial.printf("FSM IN: s_u: %d, s_o: %d, a_b: %d, l_s: %d => ",
  89. fsm_inputs.sensor_oben, fsm_inputs.sensor_unten,
  90. fsm_inputs.anim_beendet, fsm_inputs.ldr_schwelle);
  91. Serial.printf("OUT: LR: %d DR: %d ST: %d\n", fsm_outputs.laufrichtung,
  92. fsm_outputs.dimmrichtung, fsm_outputs.status);
  93. }
  94. }
  95. void Treppe::overwrite_sensors(bool s_oben, bool s_unten) {
  96. fsm_pend.web_ctrl_s_oben = s_oben;
  97. fsm_pend.web_ctrl_s_unten = s_unten;
  98. }
  99. void Treppe::read_sensors() {
  100. const bool s_oben = digitalRead(SENSOR_OBEN);
  101. const bool s_unten = digitalRead(SENSOR_UNTEN);
  102. fsm_pend.sensor_oben = false;
  103. fsm_pend.sensor_unten = false;
  104. // rising trigger => 1 cycle true !
  105. if (s_oben && !fsm_pend.last_s_oben) {
  106. fsm_pend.sensor_oben = true;
  107. }
  108. if (s_unten && !fsm_pend.last_s_unten) {
  109. fsm_pend.sensor_unten = true;
  110. }
  111. fsm_pend.last_s_oben = s_oben;
  112. fsm_pend.last_s_unten = s_unten;
  113. // check for manipulation over webserver
  114. if (fsm_pend.web_ctrl_s_oben) {
  115. fsm_pend.sensor_oben = true;
  116. fsm_pend.web_ctrl_s_oben = false;
  117. }
  118. if (fsm_pend.web_ctrl_s_unten) {
  119. fsm_pend.sensor_unten = true;
  120. fsm_pend.web_ctrl_s_unten = false;
  121. }
  122. }
  123. float Treppe::read_ldr() {
  124. /*
  125. Reads Illuminance in Lux
  126. FUTURE USE : show current Illuminance on Webserver in order to calibrate
  127. Voltage Divider 1 (R13, R14):
  128. R13 = 220k, R14 = 82k
  129. V(ADC) = V(in1) * R14/(R13+R14)
  130. -> V(in1) = V(ADC) * (R13+R14)/R14
  131. V(ADC) = analogRead(A0)/1023.00
  132. -> V(in1) = analogRead(A0)/1023.00 * (R13+R14)/R14
  133. = analogRead(A0) * (R13+R14)/(R14*1023.00)
  134. = analogRead(A0) * (220k+82k)/(82k*1023.00)
  135. = analogRead(A0) * 0.0036
  136. Voltage Divider 2 (LDR, R1 || (R13+R14))
  137. R1 = 47k, R13+R14 = 302k -> R1||(R13+R14) = 40,67k
  138. Vcc/V(in1) = R(LDR) / (R1||(R13+R14))
  139. -> R(LDR) = Vcc/V(in1) * (R1||(R13+R14))
  140. R(LDR) = 3.3V * 40.67k / V(in1)
  141. Join formulas:
  142. R(LDR) = 3.3V * 40.67k / (0.0036 * analogRead(A0))
  143. = 37280.00/analogRead(A0)
  144. ldr_ohm = R(LDR)
  145. E(LDR) = 6526.5 * R(LDR)^-2 (see Excel Regression)
  146. E(LDR) = 6526.5 / (R(LDR)^2)
  147. ldr_value = E(LDR)
  148. */
  149. //float ldr_ohm = 37280.00 / analogRead(A0);
  150. float voltage = analogRead(A0)*0.0036;
  151. float ldr_ohm = 40.57*(3.3-voltage)/voltage;
  152. float ldr_value = 6526.6 / (ldr_ohm * ldr_ohm);
  153. #ifdef LDRDEBUG
  154. Serial.printf("Ohm: %f lux: %f\n", ldr_ohm,ldr_value);
  155. #endif
  156. return ldr_value;
  157. }
  158. bool Treppe::check_ldr() {
  159. static uint8_t active = 0;
  160. #ifdef LDRDEBUG
  161. Serial.printf("R(LDR) = %f kOhm %f lux\n", ldr_value, lux);
  162. return true;
  163. #endif
  164. // follow up: averaging over many samples?
  165. float ldr = read_ldr();
  166. if (ldr < param.ldr_schwelle) {
  167. active = 1;
  168. }
  169. if (ldr > param.ldr_schwelle + LDR_HYS) {
  170. active = 0;
  171. }
  172. return active;
  173. }
  174. void Treppe::task() {
  175. #ifdef DEBUG_TIMING
  176. uint32_t m = micros();
  177. #endif
  178. // TODO wenn LDR geändert => idle_pwm_soll anpassen
  179. // fsm_pend.ldr_changed = true;
  180. fsm_inputs.ldr_schwelle = check_ldr();
  181. #ifdef DEBUG_TIMING
  182. Serial.print("1:");
  183. Serial.println(micros() - m);
  184. m = micros();
  185. #endif
  186. read_sensors();
  187. fsm_inputs.sensor_oben = fsm_pend.sensor_oben;
  188. fsm_inputs.sensor_unten = fsm_pend.sensor_unten;
  189. fsm_inputs.anim_beendet = fsm_pend.anim_beendet;
  190. #ifdef DEBUG_TIMING
  191. Serial.print("2:");
  192. Serial.println(micros() - m);
  193. m = micros();
  194. #endif
  195. FSMTreppe_Obj.setExternalInputs(&fsm_inputs);
  196. FSMTreppe_Obj.step();
  197. fsm_outputs = FSMTreppe_Obj.getExternalOutputs();
  198. #ifdef DEBUG_TIMING
  199. Serial.print("3:");
  200. Serial.println(micros() - m);
  201. m = micros();
  202. #endif
  203. print_state_on_change();
  204. #ifdef DEBUG_TIMING
  205. Serial.print("4:");
  206. Serial.println(micros() - m);
  207. m = micros();
  208. #endif
  209. if (fsm_outputs.status == ST_AUFDIMMEN_HOCH ||
  210. fsm_outputs.status == ST_ABDIMMEN_HOCH ||
  211. fsm_outputs.status == ST_AUFDIMMEN_RUNTER ||
  212. fsm_outputs.status == ST_ABDIMMEN_RUNTER) {
  213. if (fsm_pend.anim_beendet)
  214. start_animation(&dimmer_stufen, DIM_STUFEN, param.active_pwm,
  215. idle_pwm_ist);
  216. else
  217. fsm_pend.anim_beendet = dimmer_tick(&dimmer_stufen, DIM_STUFEN);
  218. } else if (fsm_outputs.status == ST_AUFDIMMEN_LDR ||
  219. fsm_outputs.status == ST_ABDIMMEN_LDR) {
  220. if (fsm_pend.anim_beendet)
  221. start_animation(&dimmer_ldr, DIM_LDR, idle_pwm_ist, 0);
  222. else
  223. fsm_pend.anim_beendet = dimmer_tick(&dimmer_ldr, DIM_LDR);
  224. } else if (fsm_outputs.status == ST_RUHEZUSTAND) {
  225. if (fsm_pend.ldr_changed) {
  226. fsm_pend.ldr_changed = false;
  227. fsm_outputs.dimmrichtung = DR_AUFDIMMEN;
  228. start_animation(&dimmer_ldr, DIM_LDR, idle_pwm_soll, idle_pwm_ist);
  229. idle_pwm_ist = idle_pwm_soll;
  230. }
  231. if (!fsm_pend.anim_beendet) {
  232. fsm_pend.anim_beendet = dimmer_tick(&dimmer_ldr, DIM_LDR);
  233. }
  234. if (param_changed) {
  235. param_changed = false;
  236. param = param_pend;
  237. save_param_to_eeprom();
  238. }
  239. }
  240. #ifdef DEBUG_TIMING
  241. Serial.print("5:");
  242. Serial.println(micros() - m);
  243. #endif
  244. }
  245. void Treppe::setup() {
  246. pwmController.resetDevices();
  247. // Deactive PCA9685 Phase Balancer due to LED Flickering
  248. // https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
  249. // see also lib/PCA9685-Arduin/PCA9685.h:204
  250. pwmController.init(PCA9685_PhaseBalancer_None);
  251. // pwmController.init(PCA9685_PhaseBalancer_Linear);
  252. pwmController.setPWMFrequency(100);
  253. // pwmController.setAllChannelsPWM(idle_pwm);
  254. // WARNING: before getting Parameters of Flash, make sure plausible parameters
  255. // are written in flash!
  256. EEPROM.get(EEP_START_ADDR, param); // get Parameters of flash
  257. pinMode(13, OUTPUT);
  258. pinMode(0, OUTPUT);
  259. digitalWrite(13, HIGH);
  260. digitalWrite(0, HIGH);
  261. pinMode(A0, INPUT);
  262. pinMode(SENSOR_OBEN, INPUT);
  263. pinMode(SENSOR_UNTEN, INPUT);
  264. pinMode(OE, OUTPUT);
  265. digitalWrite(OE, 0);
  266. Serial.printf("Treppe: stufen=%d\n", stufen);
  267. }
  268. void Treppe::save_param_to_eeprom() {
  269. EEPROM.put(EEP_START_ADDR,
  270. param); // copy Parameters so "EEPROM"-section in RAM
  271. EEPROM.commit(); // write "EEPROM"-section to flash
  272. }
  273. void Treppe::set_idle_pwm_max(const uint16_t value,
  274. const vorgabe_typ_t vorgabe_typ) {
  275. if (vorgabe_typ == VORGABE_PROZENT) {
  276. param_pend.idle_pwm_max = param_pend.active_pwm * value / 100;
  277. } else if (vorgabe_typ == VORGABE_12BIT) {
  278. param_pend.idle_pwm_max = value;
  279. }
  280. if (param_pend.idle_pwm_max > param_pend.active_pwm) {
  281. param_pend.idle_pwm_max = param_pend.active_pwm;
  282. }
  283. param_changed = true;
  284. Serial.printf("Treppe: param_pend.idle_pwm_max=%d\n",
  285. param_pend.idle_pwm_max);
  286. }
  287. void Treppe::set_active_pwm(const uint16_t value,
  288. const vorgabe_typ_t vorgabe_typ) {
  289. if (vorgabe_typ == VORGABE_PROZENT) {
  290. param_pend.active_pwm = 4095 * value / 100;
  291. } else if (vorgabe_typ == VORGABE_12BIT) {
  292. param_pend.active_pwm = value;
  293. }
  294. if (param_pend.active_pwm > 4095) {
  295. param_pend.idle_pwm_max = 4095;
  296. }
  297. param_changed = true;
  298. Serial.printf("Treppe: param_pend.active_pwm=%d\n", param_pend.active_pwm);
  299. }
  300. void Treppe::set_time_ldr(const uint16_t value) {
  301. param_pend.time_ldr = value;
  302. if (param_pend.time_ldr > TIME_MS_MAX)
  303. param_pend.time_ldr = TIME_MS_MAX;
  304. param_changed = true;
  305. Serial.printf("Treppe: time_ldr=%d\n", param_pend.time_ldr);
  306. }
  307. void Treppe::set_time_per_stair(const uint16_t value) {
  308. param_pend.time_per_stair = value;
  309. if (param_pend.time_per_stair > TIME_MS_MAX)
  310. param_pend.time_per_stair = TIME_MS_MAX;
  311. param_changed = true;
  312. Serial.printf("Treppe: time_per_stair=%d\n", param_pend.time_per_stair);
  313. }
  314. void Treppe::set_ldr_schwelle(const uint16_t value,
  315. const vorgabe_typ_t vorgabe_typ) {
  316. if (vorgabe_typ == VORGABE_PROZENT) {
  317. // ?!
  318. param_pend.ldr_schwelle = 10 * value / 100;
  319. } else if (vorgabe_typ == VORGABE_12BIT) {
  320. // param_pend.ldr_schwelle = value;
  321. }
  322. param_changed = true;
  323. Serial.printf("Treppe: ldr_schwelle=%d\n", param_pend.ldr_schwelle);
  324. }