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

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