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 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include "treppe.h"
  2. /*
  3. dimm_stufe
  4. - dimmt stufe (0 - 15, PCA9685 outputs) mit linearen ticks
  5. von idle bis active brightness
  6. - return false solange gedimmt wird
  7. - return true bei nächster stufe
  8. */
  9. bool Treppe::dimm_stufe(uint8_t stufe)
  10. {
  11. if (fsm_outputs.dimmrichtung == DR_AUFDIMMEN)
  12. current_pwm += differenz_pwm_pro_tick;
  13. else
  14. current_pwm -= differenz_pwm_pro_tick;
  15. Serial.printf("dimm_stufe %d %f\n", stufe, current_pwm);
  16. pwmController.setChannelPWM(stufe, static_cast<uint16_t>(current_pwm));
  17. current_tick++;
  18. if (current_tick >= ticks_pro_stufe)
  19. return false;
  20. return true;
  21. }
  22. /*
  23. animation tick
  24. - nach dem dimmen einer stufe wird die stufe weitergezählt
  25. - abbruch am ende => anim_beendet = true;
  26. */
  27. void Treppe::anim_tick()
  28. {
  29. if (!dimm_stufe(stufe))
  30. {
  31. Serial.printf("anim_tick(): stufe: %d, start: %d, ziel: %d, current %f\n",
  32. stufe, start_pwm, ziel_pwm, current_pwm);
  33. if (fsm_outputs.laufrichtung == LR_HOCH)
  34. {
  35. if (stufe >= stufen - 1)
  36. {
  37. anim_beendet = true;
  38. return;
  39. }
  40. stufe++;
  41. }
  42. else
  43. {
  44. if (stufe <= 0)
  45. {
  46. anim_beendet = true;
  47. return;
  48. }
  49. stufe--;
  50. }
  51. current_tick = 0;
  52. current_pwm = start_pwm;
  53. }
  54. }
  55. // startbedingunen für animation
  56. void Treppe::start_animation()
  57. {
  58. anim_beendet = false;
  59. if (fsm_outputs.laufrichtung == LR_HOCH)
  60. stufe = 0;
  61. else
  62. stufe = stufen - 1;
  63. if (fsm_outputs.dimmrichtung == DR_AUFDIMMEN)
  64. {
  65. start_pwm = idle_bright_internal;
  66. ziel_pwm = active_brightness;
  67. }
  68. else
  69. {
  70. start_pwm = active_brightness;
  71. ziel_pwm = idle_bright_internal;
  72. }
  73. current_tick = 0;
  74. current_pwm = start_pwm;
  75. }
  76. void Treppe::print_state_on_change()
  77. {
  78. static FSMTreppeModelClass::ExtU_FSMTreppe_T last_in;
  79. static FSMTreppeModelClass::ExtY_FSMTreppe_T last_out;
  80. if (
  81. fsm_inputs.anim_beendet != last_in.anim_beendet ||
  82. fsm_inputs.sensor_oben != last_in.sensor_oben ||
  83. fsm_inputs.sensor_unten != last_in.sensor_unten ||
  84. fsm_outputs.dimmrichtung != last_out.dimmrichtung ||
  85. fsm_outputs.laufrichtung != last_out.laufrichtung ||
  86. fsm_outputs.status != last_out.status)
  87. {
  88. last_in.anim_beendet = fsm_inputs.anim_beendet;
  89. last_in.sensor_oben = fsm_inputs.sensor_oben;
  90. last_in.sensor_unten = fsm_inputs.sensor_unten;
  91. last_out.dimmrichtung = fsm_outputs.dimmrichtung;
  92. last_out.laufrichtung = fsm_outputs.laufrichtung;
  93. last_out.status = fsm_outputs.status;
  94. Serial.printf("FSM IN: s_u: %d, s_o: %d, beendet: %d =>",
  95. fsm_inputs.sensor_oben, fsm_inputs.sensor_unten, fsm_inputs.anim_beendet);
  96. Serial.print(" step => ");
  97. Serial.printf("OUT: LR: %d DR: %d ST: %d\n",
  98. fsm_outputs.laufrichtung, fsm_outputs.dimmrichtung, fsm_outputs.status);
  99. }
  100. }
  101. bool Treppe::read_sensor(int sensor)
  102. {
  103. /*
  104. reads sensors with edge detection
  105. returns true if motion was detected
  106. returns false if no motion was detected
  107. returns false if motion was detected, but state did not change back to not detected
  108. */
  109. uint8_t pegel = digitalRead(sensor);
  110. static uint8_t pegel_alt[2] = {0, 0};
  111. uint8_t index = 0;
  112. if (sensor == SENSOR_OBEN)
  113. index = 0;
  114. else
  115. index = 1;
  116. if (pegel == 1 && pegel_alt[index] == 0)
  117. {
  118. pegel_alt[index] = pegel;
  119. return true;
  120. }
  121. else
  122. {
  123. pegel_alt[index] = pegel;
  124. return false;
  125. }
  126. //return static_cast<bool>(pegel);
  127. }
  128. float Treppe::read_ldr()
  129. {
  130. /*
  131. Reads Illuminance in Lux
  132. FUTURE USE : show current Illuminance on Webserver in order to calibrate
  133. Voltage Divider 1 (R13, R14):
  134. R13 = 220k, R14 = 82k
  135. V(ADC) = V(in1) * R14/(R13+R14)
  136. -> V(in1) = V(ADC) * (R13+R14)/R14
  137. V(ADC) = analogRead(A0)/1023.00
  138. -> V(in1) = analogRead(A0)/1023.00 * (R13+R14)/R14
  139. = analogRead(A0) * (R13+R14)/(R14*1023.00)
  140. = analogRead(A0) * (220k+82k)/(82k*1023.00)
  141. = analogRead(A0) * 0.0036
  142. Voltage Divider 2 (LDR, R1 || (R13+R14))
  143. R1 = 47k, R13+R14 = 302k -> R1||(R13+R14) = 40,67k
  144. Vcc/V(in1) = R(LDR) / (R1||(R13+R14))
  145. -> R(LDR) = Vcc/V(in1) * (R1||(R13+R14))
  146. R(LDR) = 3.3V * 40.67k / V(in1)
  147. Join formulas:
  148. R(LDR) = 3.3V * 40.67k / (0.0036 * analogRead(A0))
  149. = 37280.00/analogRead(A0)
  150. ldr_ohm = R(LDR)
  151. E(LDR) = 79.735 * R(LDR)^-0.498 (see Excel Regression)
  152. ldr_value = E(LDR)
  153. */
  154. float ldr_ohm = 37280.00 / analogRead(A0);
  155. float ldr_value = 79.735 * pow(ldr_ohm, -0.498);
  156. return ldr_value;
  157. }
  158. bool Treppe::check_ldr()
  159. {
  160. static uint8_t active = 0;
  161. #ifdef LDRDEBUG
  162. Serial.printf("R(LDR) = %f kOhm %f lux\n", ldr_value, lux);
  163. return true;
  164. #endif
  165. // follow up: averaging over many samples?
  166. float ldr = read_ldr();
  167. if (ldr < ldr_schwelle)
  168. active = 1;
  169. if (ldr > ldr_schwelle + LDR_HYS)
  170. active = 0;
  171. activate_idle_pwm(active);
  172. return active;
  173. }
  174. void Treppe::task()
  175. {
  176. fsm_inputs.ldr_schwelle = check_ldr();
  177. fsm_inputs.sensor_oben = read_sensor(SENSOR_OBEN);
  178. fsm_inputs.sensor_unten = read_sensor(SENSOR_UNTEN);
  179. fsm_inputs.anim_beendet = static_cast<bool>(anim_beendet);
  180. FSMTreppe_Obj.setExternalInputs(&fsm_inputs);
  181. FSMTreppe_Obj.step();
  182. fsm_outputs = FSMTreppe_Obj.getExternalOutputs();
  183. print_state_on_change();
  184. if (fsm_outputs.status > ST_RUHEZUSTAND)
  185. {
  186. if (anim_beendet == true &&
  187. (fsm_outputs.status == ST_AUFDIMMEN_HOCH ||
  188. fsm_outputs.status == ST_ABDIMMEN_HOCH ||
  189. fsm_outputs.status == ST_AUFDIMMEN_RUNTER ||
  190. fsm_outputs.status == ST_ABDIMMEN_RUNTER)
  191. )
  192. {
  193. start_animation();
  194. }
  195. if (!anim_beendet)
  196. anim_tick();
  197. }
  198. }
  199. void Treppe::berechne_dimmer()
  200. {
  201. ticks_pro_stufe = time_per_stair / INT_TIME; // [ms]
  202. differenz_pwm_pro_tick = (float)(active_brightness - idle_bright_internal) / (float)ticks_pro_stufe;
  203. }
  204. void Treppe::setup()
  205. {
  206. pwmController.resetDevices();
  207. // Deactive PCA9685 Phase Balancer due to LED Flickering
  208. // https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
  209. // see also lib/PCA9685-Arduin/PCA9685.h:204
  210. pwmController.init(PCA9685_PhaseBalancer_None);
  211. //pwmController.init(PCA9685_PhaseBalancer_Linear);
  212. pwmController.setPWMFrequency(100);
  213. //pwmController.setAllChannelsPWM(idle_brightness);
  214. pinMode(A0, INPUT);
  215. pinMode(SENSOR_OBEN, INPUT);
  216. pinMode(SENSOR_UNTEN, INPUT);
  217. pinMode(OE, OUTPUT);
  218. digitalWrite(OE, 0);
  219. Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick);
  220. Serial.printf("Treppe: initial parameters: stufen=%d\n", stufen);
  221. }
  222. void Treppe::set_idle_prozent(int prozent) {
  223. uint16_t new_pwm = 0xFFF * prozent / 100;
  224. set_idle_pwm(new_pwm);
  225. }
  226. void Treppe::set_idle_pwm(uint16_t _idle_brightness)
  227. {
  228. if(_idle_brightness > active_brightness) {
  229. idle_bright_internal = active_brightness;
  230. } else {
  231. idle_bright_internal = _idle_brightness;
  232. }
  233. Serial.printf("Treppe: idle_bright_internal=%d\n", idle_bright_internal);
  234. berechne_dimmer();
  235. pwmController.setAllChannelsPWM(idle_bright_internal);
  236. }
  237. void Treppe::activate_idle_pwm(bool active)
  238. {
  239. static uint8_t former_active = 0;
  240. if (fsm_outputs.status == ST_RUHEZUSTAND || fsm_outputs.status == ST_INAKTIV_LDR)
  241. {
  242. if (active != former_active)
  243. {
  244. idle_bright_internal = idle_brightness * active;
  245. // Dimming Function for all LEDS ?
  246. pwmController.setAllChannelsPWM(idle_bright_internal);
  247. former_active = active;
  248. }
  249. }
  250. }
  251. void Treppe::set_active_pwm(uint16_t _active_brightness)
  252. {
  253. active_brightness = _active_brightness;
  254. berechne_dimmer();
  255. Serial.printf("Treppe: active_brightness=%d\n", active_brightness);
  256. }
  257. void Treppe::set_time_per_stair(uint16_t _time_per_stair)
  258. {
  259. time_per_stair = _time_per_stair;
  260. berechne_dimmer();
  261. Serial.printf("Treppe: time_per_stair=%d\n", time_per_stair);
  262. }