Kanał Rolety sterowanie silnikiem DC

nebraska
Posts: 204
Joined: Thu Dec 08, 2022 3:00 pm
Been thanked: 1 time

Post

Witam
Chce wykorzystać kanał rolety a bardziej logike do sterowania silnikiem.
Nie potrzebuje wifi tylko sterowanie z przycisku jak w kodzie poniżej. Kod jeszcze nie skończony ponieważ chcę dodać krańcówki.
@klew pytanie do Ciebie czy w tym przykładzie mogę dopisać pamieć z supli? czy muszę pisać własną logikę na pamięć?
Chodzi o to żeby po utracie zasilania jeśli np ostatnie kliknięcie zapaliło mi diodę czerwoną i potem stop - wyłączyła się dioda. To po powrocie zasilania żeby pamiętał że kolejne kliknięcie ma zapalić diodę przeciwną czyli np zieloną.
Próbowałem z metodą rs->onSaveState(); ale nie działa. Poniżej logi, z tego co widze w logach ze odczyt nie ładuje.
Czy w ogóle uzycie metody onSaveState() da taką opcje?
Domyślam sie że związek jest z tym że iterate mam wywalone w loop?

Code: Select all

#define LED_WORK_CURTAIN 2
#define MOTOR_UP 4
#define MOTOR_DOWN 5
#define BUTTON_FLAP 14
#define ROLLER_SHUTTER_UP_RELAY_GPIO_VIRTUAL 0    // pin wirtualny
#define ROLLER_SHUTTER_DOWN_RELAY_GPIO_VIRTUAL 1  //  pin wirtualny

#include <Arduino.h>
#include <SuplaDevice.h>
#include <supla/storage/littlefs_config.h>
#include <supla/control/button.h>
#include <supla/device/status_led.h>
#include <supla/events.h>
#include <supla/storage/eeprom.h>
#include <supla/control/roller_shutter.h>
#include <supla/sensor/virtual_binary.h>
#include <supla/io.h>

#define STORAGE_OFFSET 100
Supla::Eeprom eeprom(STORAGE_OFFSET);


Supla::Control::RollerShutter* rs = nullptr;
Supla::Control::BlinkingLed* myLed = nullptr;

int lastDirection;

class VirtualExpander : public Supla::Io::Base {
public:
  VirtualExpander()
    : Supla::Io::Base(false) {}
  void customDigitalWrite(int channelNumber, uint8_t pin, uint8_t val) override {
    VIRTUAL_VARIABLE[pin] = val;
  }
  int customDigitalRead(int channelNumber, uint8_t pin) override {
    return VIRTUAL_VARIABLE[pin];
  }
private:
  bool VIRTUAL_VARIABLE[8];
};


void controllerMotor() {
  int8_t currentDirection = rs->getCurrentDirection();

  if (currentDirection != lastDirection) {
    switch (currentDirection) {
      case 0:  // STOP
        Serial.println("####### STOP #####################");
        digitalWrite(MOTOR_UP, LOW);
        digitalWrite(MOTOR_DOWN, LOW);
        myLed->setAlwaysOffSequence();
        break;

      case 1:  // DOWN
        Serial.println("####### Silnik w DOL #####################");
        digitalWrite(MOTOR_UP, LOW);  
        myLed->setCustomSequence(200, 200, 0, 0, 0);

        rs->onSaveState();  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1

        digitalWrite(MOTOR_DOWN, HIGH);
        break;

      case 2:  // UP
        Serial.println("####### Silnik w GORE #####################");
        digitalWrite(MOTOR_DOWN, LOW);  // Blokada przeciwstawna
        myLed->setCustomSequence(1200, 1200, 0, 0, 0);

        rs->onSaveState();  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
        digitalWrite(MOTOR_UP, HIGH);
        break;
    }
    lastDirection = currentDirection;
  }
}


void setup() {

  Serial.begin(115200);
  pinMode(MOTOR_UP, OUTPUT);
  pinMode(MOTOR_DOWN, OUTPUT);
  digitalWrite(MOTOR_UP, LOW);
  digitalWrite(MOTOR_DOWN, LOW);

  auto EX_OUTPUT = new VirtualExpander();
  myLed = new Supla::Control::BlinkingLed(LED_WORK_CURTAIN, true);
  rs = new Supla::Control::RollerShutter(EX_OUTPUT, ROLLER_SHUTTER_UP_RELAY_GPIO_VIRTUAL, ROLLER_SHUTTER_DOWN_RELAY_GPIO_VIRTUAL, true);

  Supla::Control::Button* one_button = new Supla::Control::Button(BUTTON_FLAP, true, true);
  one_button->addAction(Supla::STEP_BY_STEP, *rs, Supla::ON_PRESS);

  rs->onLoadState();  //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  SuplaDevice.begin();
}

void loop() {

  controllerMotor();
}
EEPROM zapis

Code: Select all

Button[0] pressed
RS[0] set target position: -4, tilt: -1
 **** Digital write[0], gpio: 0; value 0
 **** Digital write[0], gpio: 1; value 1
####### Silnik w DOL #####################
EEPROM: Wrote 9 bytes (offset 187)
Button[0] released
EEPROM Odczyt

Code: Select all

 *** Supla - starting initialization (platform 0)
Clock not configured, using default clock
SD: add flag DEVICE_CONFIG_SUPPORTED
SD: add flag CALCFG_RESTART_DEVICE
Storage initialization
EEPROM: Read 8 bytes [00 00 00 00 00 00 00 00 ] (offset 100)
Storage: missing Supla tag. Rewriting...
EEPROM: Wrote 7 bytes (offset 108)
EEPROM: Wrote 8 bytes (offset 100)
EEPROM: Commit
Config storage not configured

 *** Supla - Load state storage
Validating storage state section with current device configuration
Element state section size doesn't match current device configuration
 *** Supla - Load state storage done

 *** Supla - Init elements
 **** Digital write[0], gpio: 0; value 0
 **** Digital write[0], gpio: 1; value 0
Channel(0) value changed to -1
Button[0]: Initialized: pin 14, pullUp 1, invertLogic 1, state 1
 *** Supla - Init elements done

Current status: [2] Network Interface not defined!
User avatar
klew
Posts: 13905
Joined: Thu Jun 27, 2019 12:16 pm
Location: Wrocław
Has thanked: 134 times
Been thanked: 137 times

Post

Pytanie po co to robisz na SUPLI skoro nie chcesz używać SUPLI?

Aby zapis i odczyt stanu działały, to najprościej jest po prostu zostawić SuplaDevice.begin i iterate - one to ogarną.
Natomiast roleta zapisuje w storage stan (czyli % otwarcia). Jeśli chcesz tam zapisać coś innego to trzeba to zupełnie inaczej ogarnąć.

Jest też klasa RollerShutterInterface - ona powinna być prostsza do tego typu integracji
Najlepsze suple dla Twojego domu :mrgreen:
nebraska
Posts: 204
Joined: Thu Dec 08, 2022 3:00 pm
Been thanked: 1 time

Post

Z Supli chciałem sobie wziąć logikę sterowania silnika.
Faktycznie przejrzałem klase RollerShutterInterface i zrobiłem tak jak chciałem.
Dzięki za pomoc.

Return to “Arduino IDE”