From 6f257ebbd8840ef5df8c87752634d9ac1a12e216 Mon Sep 17 00:00:00 2001 From: Fisch Date: Sun, 12 Apr 2020 12:34:14 +0200 Subject: [PATCH] test in real hardware and motor bugs --- .../mixercontroller_w5100_pio/src/main.cpp | 89 +++++++++++-------- 1 file changed, 50 insertions(+), 39 deletions(-) diff --git a/controller/mixercontroller_w5100_pio/src/main.cpp b/controller/mixercontroller_w5100_pio/src/main.cpp index 4c88a85..ee36b57 100644 --- a/controller/mixercontroller_w5100_pio/src/main.cpp +++ b/controller/mixercontroller_w5100_pio/src/main.cpp @@ -2,8 +2,9 @@ * Ideas/TODO: * POT_MIN, POT_MAX as variable with calibration procedure. Drive slowly to both ends until value does not get lower. * Motor error checking. Timeout overall (if regulation fails or stuck). Timeout movement (motor is tunring but no change in poti value detected). Move right direction. - * Relais switching (selection and muting) - * MQTT topics + * Hardware: motorentstörkondensatoren einbauen direkt an motor. 47nF + zu - und zwei 10nF + zu case und - zu case + * PI Optimieren. aktuell overshoot + * Implement knob menu structure */ #include @@ -26,6 +27,7 @@ boolean getSelection(uint8_t pbit); boolean getMute(uint8_t pbit); void setSelectionChannel(uint8_t i, boolean state); void setMuteChannel(uint8_t i, boolean state); +void publishCurrentSetVolume(); #define LEDPIN 9 //PB1 = D9 = Pin15 Adafruit_NeoPixel leds = Adafruit_NeoPixel(9, LEDPIN, NEO_GRB + NEO_KHZ800); @@ -64,10 +66,6 @@ long last_serialdebug=0; Button button_knob; -boolean button_flag=false; //true if button pressed -boolean button_released=true; -long last_button_released=0; //last time button has been released (for debounce) - //Shift Register 595 //connections: https://www.arduino.cc/en/tutorial/ShiftOut #define SRLATCH PD4 //D4 = PD4 @@ -91,10 +89,11 @@ float encoderMultiplier=4.0; uint8_t motorspeed=0; #define PIN_POT A0 //A0 = PC0, reference potentiometer wiper -#define DEADZONE_POTI 5 //maximum allowed error. stop when reached this zone -#define POT_MIN 10 //minimum value pot can reach -#define POT_MAX 1010 //maximum value pot can reach +#define DEADZONE_POTI 10 //maximum allowed error. stop when reached this zone +#define POT_MIN 45 //minimum value pot can reach +#define POT_MAX 950 //maximum value pot can reach #define POTIFILTER 0.8 //0 to 1. 1 means old value stays forever +#define MAX_MOTOR_PWM 192 //0 to 255. Maximum pwm to output int poti_set; //set value, initial value will be read from poti int poti_read=0; //read value from poti @@ -121,10 +120,11 @@ long last_motorcheck=0; //#define MOTOR_FAILTIME 500 //in ms. if motor did not turn fox x amount of time at least with MINIMUM_MOTORVEL an error will initiate //long last_motorTooSlow=0; //typically 0 -float motorP=1.0; +float motorP=2.0; float motorI=0.1; float potidifference_integral=0; -#define MOTORI_ANTIWINDUP 32 //maximum value for (potidifference_integral*motorI). time depends on INTERVAL_MOTORCHECK +#define MOTORI_ANTIWINDUP 90 //maximum value for (potidifference_integral*motorI). time depends on INTERVAL_MOTORCHECK +//Motor starts moving at about speed=80 long last_potidifferenceLow=0; #define DEADZONETIMEUNTILREACHED 500 //time [ms] poti read value has to be inside of deadzone to set reachedposition flag (and stop regulating) @@ -145,7 +145,7 @@ void setup() { Serial.begin(9600); while (!Serial) {}; - Serial.println("Starting"); + Serial.println("Boot"); leds.begin(); leds.clear(); @@ -158,18 +158,17 @@ void setup() { if (useethernet) { - Serial.println("Setting up ethernet connection via DHCP"); + Serial.println("trying DHCP"); if (Ethernet.begin(mac) == 0) { // setup ethernet communication using DHCP useethernet=false; //Unable to configure Ethernet using DHCP - Serial.println("Unable to configure Ethernet using DHCP"); + Serial.println("DHCP Err"); delay(200); //for (;;); }else{ useethernet=true; - Serial.println("Ethernet configured via DHCP"); Serial.print("IP address: "); Serial.println(Ethernet.localIP()); Serial.println(); @@ -184,10 +183,10 @@ void setup() { //Serial.println(ip); // setup mqtt client - Serial.println("Configuring MQTT"); + Serial.println("Conf MQTT"); mqttClient.setClient(ethClient); mqttClient.setServer("10.0.0.1", 1883); - Serial.println("MQTT configured"); + Serial.println("MQTT ok"); mqttClient.setCallback(callback); } }else{ @@ -198,6 +197,7 @@ void setup() { poti_set=analogRead(PIN_POT); Serial.println("Ready"); + last_send = millis(); } @@ -205,10 +205,11 @@ void setup() { void reconnect() { // Loop until reconnected if (!mqttClient.connected()) { - Serial.print("Attempting MQTT connection..."); + Serial.print("Connect MQTT .."); // Attempt to connect if (mqttClient.connect(CLIENT_ID)) { Serial.println("connected"); + publishCurrentSetVolume(); mqttClient.publish("audiomixer/ip", ip.c_str()); //Publish own ip mqttClient.subscribe("audiomixer/volume/set"); //subscribe to /set, republish without /set mqttClient.subscribe("audiomixer/mute/set"); //without range @@ -221,6 +222,7 @@ void reconnect() { String sub_topic="audiomixer/select_"+String(i)+"/set"; mqttClient.subscribe((char*) sub_topic.c_str()); } + } else { Serial.print("failed, rc="); Serial.print(mqttClient.state()); @@ -291,6 +293,7 @@ void loop() { poti_set+=volEncVel*encoderMultiplier; //change poti set value poti_set=constrain(poti_set, POT_MIN,POT_MAX); poti_reachedposition=false; + publishCurrentSetVolume(); } @@ -371,7 +374,7 @@ void loop() { potidifference_integral+=potidifference*motorI; potidifference_integral=constrain(potidifference_integral,-MOTORI_ANTIWINDUP,MOTORI_ANTIWINDUP); //constrain _motormove=potidifference*motorP+potidifference_integral; - motorspeed=constrain(abs(_motormove), 0,255); + motorspeed=constrain(abs(_motormove), 0,MAX_MOTOR_PWM); if (poti_read<=POT_MIN && _motormove<0) { //stop motor if soft endstops reached and wants to turn that way MOTOR_STOP(); potidifference_integral=0; @@ -413,20 +416,19 @@ void loop() { last_serialdebug=loopmillis; + Serial.print(" set="); Serial.print(poti_set); Serial.print(" is="); Serial.print(poti_read); - Serial.print(" motorspeed="); + Serial.print(" mspeed="); Serial.print(motorspeed); - Serial.print(" iValue="); + Serial.print(" iVal="); Serial.print(potidifference_integral); - Serial.println(""); - - if (button_flag){ //TODO: remove hier if correct behaviour implemented - Serial.println("BUTTON Pressed"); - button_flag=false; //clear flag to reenable button triggering. + if (poti_reachedposition) { + Serial.print("!"); } + Serial.println(""); for(uint8_t i=0;i