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

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