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.8KB

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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) = 6526.5 * R(LDR)^-2 (see Excel Regression)
  161. E(LDR) = 6526.5 / (R(LDR)^2)
  162. ldr_value = E(LDR)
  163. */
  164. float ldr_ohm = 37280.00 / analogRead(A0);
  165. float ldr_value = 6526.6/(ldr_ohm*ldr_ohm);
  166. return ldr_value;
  167. }
  168. bool Treppe::check_ldr()
  169. {
  170. static uint8_t active = 0;
  171. #ifdef LDRDEBUG
  172. Serial.printf("R(LDR) = %f kOhm %f lux\n", ldr_value, lux);
  173. return true;
  174. #endif
  175. // follow up: averaging over many samples?
  176. float ldr = read_ldr();
  177. if (ldr < ldr_schwelle)
  178. active = 1;
  179. if (ldr > ldr_schwelle + LDR_HYS)
  180. active = 0;
  181. activate_idle_pwm(active);
  182. return active;
  183. }
  184. void Treppe::task()
  185. {
  186. fsm_inputs.ldr_schwelle = check_ldr();
  187. fsm_inputs.sensor_oben = read_sensor(SENSOR_OBEN);
  188. fsm_inputs.sensor_unten = read_sensor(SENSOR_UNTEN);
  189. fsm_inputs.anim_beendet = static_cast<bool>(anim_beendet);
  190. FSMTreppe_Obj.setExternalInputs(&fsm_inputs);
  191. FSMTreppe_Obj.step();
  192. fsm_outputs = FSMTreppe_Obj.getExternalOutputs();
  193. print_state_on_change();
  194. if (fsm_outputs.status > ST_RUHEZUSTAND)
  195. {
  196. if (anim_beendet == true &&
  197. (fsm_outputs.status == ST_AUFDIMMEN_HOCH ||
  198. fsm_outputs.status == ST_ABDIMMEN_HOCH ||
  199. fsm_outputs.status == ST_AUFDIMMEN_RUNTER ||
  200. fsm_outputs.status == ST_ABDIMMEN_RUNTER)
  201. )
  202. {
  203. start_animation();
  204. }
  205. if (!anim_beendet)
  206. anim_tick();
  207. }
  208. }
  209. void Treppe::berechne_dimmer()
  210. {
  211. ticks_pro_stufe = time_per_stair / INT_TIME; // [ms]
  212. differenz_pwm_pro_tick = (float)(active_pwm - idle_pwm_internal)
  213. / (float)ticks_pro_stufe;
  214. }
  215. void Treppe::setup()
  216. {
  217. pwmController.resetDevices();
  218. // Deactive PCA9685 Phase Balancer due to LED Flickering
  219. // https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
  220. // see also lib/PCA9685-Arduin/PCA9685.h:204
  221. pwmController.init(PCA9685_PhaseBalancer_None);
  222. //pwmController.init(PCA9685_PhaseBalancer_Linear);
  223. pwmController.setPWMFrequency(100);
  224. //pwmController.setAllChannelsPWM(idle_pwm);
  225. pinMode(A0, INPUT);
  226. pinMode(SENSOR_OBEN, INPUT);
  227. pinMode(SENSOR_UNTEN, INPUT);
  228. pinMode(OE, OUTPUT);
  229. digitalWrite(OE, 0);
  230. Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick);
  231. Serial.printf("Treppe: initial parameters: stufen=%d\n", stufen);
  232. }
  233. void Treppe::set_idle_prozent(int prozent) {
  234. uint16_t new_pwm = active_pwm * prozent / 100;
  235. set_idle_pwm(new_pwm);
  236. }
  237. void Treppe::set_idle_pwm(uint16_t new_idle_pwm)
  238. {
  239. if(new_idle_pwm > active_pwm) {
  240. idle_pwm = active_pwm;
  241. } else {
  242. idle_pwm = new_idle_pwm;
  243. }
  244. Serial.printf("Treppe: idle_pwm=%d\n", idle_pwm);
  245. berechne_dimmer();
  246. activate_idle_pwm(true);
  247. }
  248. void Treppe::activate_idle_pwm(bool active)
  249. {
  250. static uint16_t last_pwm = 0;
  251. if (fsm_outputs.status == ST_RUHEZUSTAND || fsm_outputs.status == ST_INAKTIV_LDR)
  252. {
  253. idle_pwm_internal = idle_pwm * active;
  254. if (idle_pwm_internal != last_pwm)
  255. {
  256. // Dimming Function for all LEDS ?
  257. berechne_dimmer();
  258. pwmController.setAllChannelsPWM(idle_pwm_internal);
  259. last_pwm = idle_pwm_internal;
  260. }
  261. }
  262. }
  263. void Treppe::set_active_pwm(uint16_t _active_pwm)
  264. {
  265. active_pwm = _active_pwm;
  266. berechne_dimmer();
  267. Serial.printf("Treppe: active_pwm=%d\n", active_pwm);
  268. }
  269. void Treppe::set_time_per_stair(uint16_t _time_per_stair)
  270. {
  271. time_per_stair = _time_per_stair;
  272. berechne_dimmer();
  273. Serial.printf("Treppe: time_per_stair=%d\n", time_per_stair);
  274. }