Browse Source

weitere anpassungen an pwm

tags/FSM1.0
Simon Schmidt 3 years ago
parent
commit
b47f778fc1
3 changed files with 66 additions and 60 deletions
  1. 54
    52
      lib/treppe/treppe.cpp
  2. 12
    5
      lib/treppe/treppe.h
  3. 0
    3
      src/main.cpp

+ 54
- 52
lib/treppe/treppe.cpp View File

else { else {
current_pwm += stepsize; current_pwm += stepsize;
} }
Serial.println((uint16_t)current_pwm);
// Serial.println((uint16_t)current_pwm);
pwmController.setChannelPWM(led, (uint16_t)current_pwm); pwmController.setChannelPWM(led, (uint16_t)current_pwm);
if(current_pwm > stopval - stepsize && current_pwm < stopval + stepsize){ if(current_pwm > stopval - stepsize && current_pwm < stopval + stepsize){
if(stopval == 0) pwmController.setChannelPWM(led, 0); if(stopval == 0) pwmController.setChannelPWM(led, 0);
lastbrightness = active_brightness; lastbrightness = active_brightness;
} }
status = status_build; // set parameter memory status = status_build; // set parameter memory
Serial.print("----Status Changed! onoff: ");
Serial.print(state);
Serial.print(" dir: ");
Serial.println(direction);


Serial.printf("----Status Changed! onoff: %d, dir: %d\n", state, direction);
} }
if(!finish){ // finish == 0 -> action pending if(!finish){ // finish == 0 -> action pending
if(!softstart_led(led,lastbrightness, brightness)){
Serial.print("one LED finished: ");
Serial.print(led);
Serial.print(" last: ");
Serial.print(lastbrightness);
Serial.print(" curr: ");
Serial.println(brightness);
if(direction){
led++;
if(led >= stairs) {
if(!softstart_led(led,lastbrightness, brightness)){
Serial.printf("one LED finished: led: %d, last: %d, curr %d\n",
led, lastbrightness, brightness);

if(direction){
led++;
if(led >= stairs)
finish = 1;
}
else{
led--;
if(led < 0)
finish = 1; finish = 1;
//lastbrightness = brightness;
}
}
else{
led--;
if(led < 0){
//lastbrightness = brightness;
finish = 1;
}
}
} }
}
} }
} }



void Treppe::task_2()
void Treppe::rampe()
{ {
if(state) { if(state) {
finish = 0;
state = 0; // set parameter memory
}

if(!finish) {
if(direction) { // aufwärts if(direction) { // aufwärts
if(tick >= ticks_treppe-1) { // ziel erreicht if(tick >= ticks_treppe-1) { // ziel erreicht
Serial.println("[Treppe] oberster tick !"); Serial.println("[Treppe] oberster tick !");
state = 0;
finish = 1;
return; return;
} }
tick++; // eins hoch tick++; // eins hoch
else { // abwärts else { // abwärts
if(tick <= 0) { // ziel erreicht if(tick <= 0) { // ziel erreicht
Serial.println("[Treppe] unterster tick !"); Serial.println("[Treppe] unterster tick !");
state = 0;
finish = 1;
return; return;
} }
tick--; // eins runter tick--; // eins runter
} }


stufe = tick / ticks_pro_stufe; stufe = tick / ticks_pro_stufe;
float new_pwm = differenz_pwm_pro_tick * (tick - ticks_pro_stufe*stufe);
if(direction)
new_pwm += differenz_pwm_pro_tick;
new_pwm += idle_brightness;

float new_pwm = 0.0;
if(an_aus) {
new_pwm = differenz_pwm_pro_tick * (tick - ticks_pro_stufe*stufe);
new_pwm += idle_brightness;
if(direction) new_pwm += differenz_pwm_pro_tick;
}
else {
new_pwm = active_brightness - differenz_pwm_pro_tick * (tick - ticks_pro_stufe*stufe);
new_pwm += idle_brightness;
if(direction) new_pwm -= differenz_pwm_pro_tick;
}


pwmController.setChannelPWM(stufe, (uint16_t) new_pwm); pwmController.setChannelPWM(stufe, (uint16_t) new_pwm);
Serial.printf("tick %04u, led %02d:%02u, pwm %4.1f\n", Serial.printf("tick %04u, led %02d:%02u, pwm %4.1f\n",
} }
} }


// if(stufe > stairs || stufe < 0 || tick < 0 || tick > ticks_treppe-1) {
// Serial.println("[Treppe] ERROR, Something went wrong !");
// state = 0;
// return;
// }

void Treppe::setup(){ void Treppe::setup(){
Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick); Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick);
current_sensor_state[1] = digitalRead(SENSOR2); current_sensor_state[1] = digitalRead(SENSOR2);
if(current_sensor_state[0] && !last_sensor_state[0] && state == 0){ if(current_sensor_state[0] && !last_sensor_state[0] && state == 0){
setTick(0);
setAnAus(1);
setDirection(1); setDirection(1);
setState(1); setState(1);
} }


if(current_sensor_state[1] && !last_sensor_state[1] && state == 0){ if(current_sensor_state[1] && !last_sensor_state[1] && state == 0){
setTick(0);
setAnAus(0);
setDirection(0); setDirection(0);
setState(1); setState(1);
} }


// first switch - off approach, use timer later // first switch - off approach, use timer later
if(!current_sensor_state[0] && last_sensor_state[0] && state == 1){ if(!current_sensor_state[0] && last_sensor_state[0] && state == 1){
setTick(ticks_treppe);
setAnAus(1);
setDirection(1); setDirection(1);
setState(0); setState(0);
} }


if(!current_sensor_state[1] && last_sensor_state[1] && state == 1){ if(!current_sensor_state[1] && last_sensor_state[1] && state == 1){
setTick(ticks_treppe);
setAnAus(1);
setDirection(0); setDirection(0);
setState(0); setState(0);
} }


last_sensor_state[0] = current_sensor_state[0]; last_sensor_state[0] = current_sensor_state[0];
last_sensor_state[1] = current_sensor_state[1]; last_sensor_state[1] = current_sensor_state[1];
ledsequence();


ledsequence();
} }




return time_per_stair; return time_per_stair;
} }


uint8_t Treppe::setDirection(uint8_t _direction){
void Treppe::setDirection(uint8_t _direction){
switch_direction = _direction; switch_direction = _direction;
Serial.println("Treppe: Direction changed!");
Serial.printf("Treppe: switch_direction=%d!\n", switch_direction);
if(finish) Serial.println("apply direction request immediately"); if(finish) Serial.println("apply direction request immediately");
else Serial.println("currently active, dir change afterwards"); else Serial.println("currently active, dir change afterwards");
// to do: implement state command variable to determine dimm-state // to do: implement state command variable to determine dimm-state
return switch_direction;
} }


uint8_t Treppe::setState(uint8_t _state){
if(state == _state) return 1;
else{
switch_state = _state;
Serial.println("Treppe: State Request changed!");
if(finish) Serial.println("apply state request immediately");
else Serial.println("currently active, state changes after activity");
}
return 0;
void Treppe::setState(uint8_t _state){
if(state == _state) return;
else {
switch_state = _state;
Serial.printf("Treppe: switch_state=%d!\n", switch_state);
if(finish) Serial.println("apply state request immediately");
else Serial.println("currently active, state changes after activity");
}
} }

+ 12
- 5
lib/treppe/treppe.h View File

uint8_t switch_state = 0; uint8_t switch_state = 0;
uint8_t finish = 1; uint8_t finish = 1;


// alternative
uint32_t tick = 0; uint32_t tick = 0;
uint32_t stufe = 0; uint32_t stufe = 0;
uint8_t an_aus = 0;


uint32_t ticks_treppe = 0; uint32_t ticks_treppe = 0;
uint32_t ticks_pro_stufe = 0; uint32_t ticks_pro_stufe = 0;
float differenz_pwm_pro_tick = 0.0; float differenz_pwm_pro_tick = 0.0;
// alternative



// initialize with i2c-Address 0, use Wire Library // initialize with i2c-Address 0, use Wire Library
PCA9685 pwmController; PCA9685 pwmController;
uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval); uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval);
void ledsequence(); void ledsequence();
void rampe();


public: public:
Treppe(uint8_t _stairs) : stairs(_stairs){ Treppe(uint8_t _stairs) : stairs(_stairs){
/ (float) ticks_pro_stufe; / (float) ticks_pro_stufe;
} }


void task(); // call periodically

void setup(); void setup();
void task_2();
void task(); // call periodically


// Parameter section // Parameter section
uint16_t setIdle(uint16_t _idle_brightness); uint16_t setIdle(uint16_t _idle_brightness);
} }


// Runtime Parameter section // Runtime Parameter section
uint8_t setDirection(uint8_t _direction);
uint8_t setState(uint8_t _state);
void setDirection(uint8_t _direction);
void setState(uint8_t _state);
void setAnAus(uint8_t _an_aus) {
an_aus = _an_aus;
}


uint8_t getState() { return state;}; uint8_t getState() { return state;};
uint8_t getFinished() { return finish;};
uint8_t getDirection() {return direction;}; uint8_t getDirection() {return direction;};
}; };

+ 0
- 3
src/main.cpp View File



os_timer_setfn(&timer1, timerCallback, &timer_flag); os_timer_setfn(&timer1, timerCallback, &timer_flag);
os_timer_arm(&timer1, 20, true); os_timer_arm(&timer1, 20, true);

stairs.setState(1);
stairs.setDirection(1);
} }


#include <random> #include <random>

Loading…
Cancel
Save