Browse Source

Тесты уставок

unknown 3 tháng trước cách đây
mục cha
commit
69d4d6076e

+ 6 - 1
fw/modules/io/analog_input.c

@@ -10,6 +10,7 @@
 #include "monitoring.h"
 #include "mux.h"
 #include "filter.h"
+#include "preset_ai.h"
 #include <stdio.h>
 
 #undef DBG
@@ -246,8 +247,12 @@ void ai_processing(void)
                               &adc_add_raw_data[i], &adc_add_raw_data[i + 2]);
     }
     
+    // Работа с уставками
+    preset_process(adc_com_data);
+      
+    
     
-#if 0    
+#if 1    
     printf("end\r\n");
     adc_print_data();
     //adc_print_data_extend();

+ 1 - 1
fw/modules/modbus/modbus_ai_params.c

@@ -211,7 +211,7 @@ uint16_t mb_init_ai_params(uint16_t i)
     {
         mb_param[index].reg = addr;
         mb_param[index].size = 2;
-        mb_param[index].param = (uint8_t*)&settings.preset[i].hist;
+        mb_param[index].param = (uint8_t*)&settings.preset[i].hyst;
         mb_param[index].set = NULL;
         mb_param[index].get = NULL;
         mb_param[index].check_handler = mb_check_dummy;

+ 50 - 0
fw/modules/preset/preset_ai.c

@@ -2,10 +2,29 @@
 #include "FreeRTOS.h"
 #include "task.h"
 #include "settings_api.h"
+#include "triggers.h"
 #include <stdio.h>
 
 #if defined (MAI_12)
 
+#undef DBG
+#define DBG if(1)
+
+
+
+hyst_state_t hyst_state[AI_COMMON_NUMBER];
+
+
+
+// 
+void preset_init(void)
+{
+    for (int i = 0; i < AI_COMMON_NUMBER; i++)
+    {
+        hyst_state[i] = hyst_idle;
+    }
+}
+
 
 //
 void preset_set_state(void)
@@ -20,6 +39,37 @@ void preset_set_state(void)
 }
 
 
+//
+void preset_process(float *data)
+{
+    bool high = false;
+    bool low = false;
+
+    for (int i = 0; i < AI_COMMON_NUMBER; i++)
+    {
+        if (settings.preset[i].state == 0)
+            continue;
+        
+        // 0 - тип уставки - фиксированное значение
+        if (settings.preset[i].type == 0)
+        {
+            high = trig_high_value(data[i], 0, settings.preset[i].max, settings.preset[i].hyst, &hyst_state[i]);
+            low = trig_low_value(data[i], settings.preset[i].min, 0, settings.preset[i].hyst, &hyst_state[i]);
+            
+            // Запись в журнале событий
+            if (high) {
+                DBG printf("Channel %i, preset HIGH\r\n", i);
+            }
+            
+            if (low) {
+                DBG printf("Channel %i, preset LOW\r\n", i);
+            }
+        }
+        
+        
+    }
+}
+
 
 
 

+ 9 - 1
fw/modules/preset/preset_ai.h

@@ -4,21 +4,29 @@
 #include "at32f403a_407.h"
 
 
+
+//
 typedef struct
 {
     uint8_t state;  // 0 - выкл, 1 - вкл
     uint8_t type;   // тип, 0 - фиксированное значение
     float min;      // минимальное значение
     float max;      // максимальное значение
-    float hist;     // значение гистерезиса
+    float hyst;     // значение гистерезиса
 
 } preset_ai_t;
 
 
 
+// 
+void preset_init(void);
+
 //
 void preset_set_state(void);
 
+//
+void preset_process(float *data);
+
 
 
 #endif  // __PRESET_AI_H

+ 59 - 0
fw/modules/preset/triggers.c

@@ -0,0 +1,59 @@
+#include "triggers.h"
+#include "FreeRTOS.h"
+#include "task.h"
+#include "settings_api.h"
+#include <stdio.h>
+#include <math.h>
+
+
+
+// 
+bool trig_low_value(float value, float lowlevel, float highlevel, float hyst, 
+                    hyst_state_t *state) 
+{
+    (void)highlevel;
+    
+    if (*state == hyst_idle) {
+        if (value <= lowlevel) {
+            *state = hyst_down;
+            return true;
+        } else {
+            return false;
+        }
+    } else if (*state == hyst_down) {
+        if (value > (lowlevel + fabs(hyst))) {
+            *state = hyst_idle;
+            return false;
+        } else {
+            return true;
+        }
+    }
+
+    return false;
+}
+
+
+
+bool trig_high_value(float value, float lowlevel, float highlevel, float hyst,
+                     hyst_state_t *state) 
+{
+    (void)lowlevel;
+
+    if (*state == hyst_idle) {
+        if (value >= highlevel) {
+            *state = hyst_up;
+            return true;
+        } else {
+            return false;
+        }
+    } else if (*state == hyst_up) {
+        if (value < (highlevel - fabs(hyst))) {
+            *state = hyst_idle;
+            return false;
+        } else {
+            return true;
+        }
+    }
+
+    return false;
+}

+ 29 - 0
fw/modules/preset/triggers.h

@@ -0,0 +1,29 @@
+#ifndef __TRIGGERS_H
+#define __TRIGGERS_H
+
+#include "at32f403a_407.h"
+#include <stdbool.h>
+
+
+// Состояния для реализации алгоритма гистерезиса
+typedef enum
+{
+    hyst_idle = 0,
+    hyst_up,
+    hyst_down,
+
+} hyst_state_t;
+
+
+//
+bool trig_low_value(float value, float lowlevel, float highlevel, float hyst, 
+                    hyst_state_t *state);
+
+
+bool trig_high_value(float value, float lowlevel, float highlevel, float hyst,
+                     hyst_state_t *state);
+
+
+
+#endif  // __TRIGGERS_H
+

+ 1 - 1
fw/modules/settings/settings_ai.c

@@ -76,7 +76,7 @@ void settings_ai_def(settings_t *settings)
     {
         settings->preset[i].state = 1;      // включена
         settings->preset[i].type = 0;       // тип уставки - фиксированное значение
-        settings->preset[i].hist = 5.0;     //
+        settings->preset[i].hyst = 5.0;     //
         settings->preset[i].min = 0;        // TODO пока условно
         settings->preset[i].max = 30000;    // TODO пока условно
     }

BIN
output/fw.bin


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 293 - 302
project/ewarm/iap/iap.dep


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 1068 - 1032
project/ewarm/module_universal_io.dep


+ 3 - 0
project/ewarm/module_universal_io.ewp

@@ -2282,6 +2282,9 @@
                 <file>
                     <name>$PROJ_DIR$\..\..\fw\modules\preset\preset_ai.c</name>
                 </file>
+                <file>
+                    <name>$PROJ_DIR$\..\..\fw\modules\preset\triggers.c</name>
+                </file>
             </group>
             <group>
                 <name>settings</name>

+ 3 - 0
project/ewarm/module_universal_io.ewt

@@ -2489,6 +2489,9 @@
                 <file>
                     <name>$PROJ_DIR$\..\..\fw\modules\preset\preset_ai.c</name>
                 </file>
+                <file>
+                    <name>$PROJ_DIR$\..\..\fw\modules\preset\triggers.c</name>
+                </file>
             </group>
             <group>
                 <name>settings</name>

+ 5 - 4
tools/ain_test.py

@@ -11,7 +11,7 @@ from analog_in import IO_AnalogInput
 
 def main():
     colorama.init(autoreset=True)
-    serial_port = Serial('COM7', 115200, timeout=0.05, parity='N', xonxoff=False)
+    serial_port = Serial('COM9', 115200, timeout=0.05, parity='N', xonxoff=False)
     modbus_tester = Modbus(serial_port, 1)
     modbus_tester.MB_DEBUG = False
     ai = IO_AnalogInput(modbus_tester) 
@@ -27,11 +27,12 @@ def main():
     # print(ai.get_presets_min())
     # for i in range(100):
         # print(ai.get_preset_max(1))
-    print(ai.get_presets_hist())
+    # print(ai.get_presets_hist())
 
-    for i in range(100):
-        print(ai.get_fil_inputs())
+    # for i in range(100):
+    #     print(ai.get_fil_inputs())
 
+    ai.set_inputs_state(0b0000_0000_0001)
 
     # print(ai.get_preset_min(2))
 

+ 84 - 0
tools/hyst.py

@@ -0,0 +1,84 @@
+import random
+from enum import Enum
+from math import fabs
+import matplotlib.pyplot as plt
+
+
+class Hyst(Enum):
+    
+    HYST_IDLE   = 0
+    HYST_UP     = 1
+    HYST_DOWN   = 2
+
+
+hyst_high_state = Hyst.HYST_IDLE.name
+hyst_low_state = Hyst.HYST_IDLE.name
+
+
+def grapth():
+    hyst_high_limit = 6.0
+    hyst_low_limit = 1.0
+    x = []
+    in_data = []
+    hyst_high = []    
+    hyst_low = []    
+    hyst_value_low = []
+    hyst_value_high = []
+
+    for i in range(200):
+        x.append(i)
+        in_data.append(random.uniform(0.0, 7.0))
+        hyst_high.append(hyst_high_limit)
+        hyst_low.append(hyst_low_limit)
+
+        hyst_value_low.append(trig_low_value(in_data[i], hyst_low_limit, 1.5))
+        hyst_value_high.append(trig_high_value(in_data[i], hyst_high_limit, 1.5))
+
+
+    plt.plot(x, in_data, x, hyst_high, x, hyst_low, x, hyst_value_low, x, hyst_value_high)
+    plt.title('Тестовые данные для гистерезиса')
+    plt.xlabel('X')
+    plt.ylabel('Y')
+    plt.show()
+
+
+def trig_low_value(value: float, lowlevel: float, hyst: float):
+    global hyst_low_state
+    if hyst_low_state == Hyst.HYST_IDLE.name:
+        if value <= lowlevel:
+            hyst_low_state = Hyst.HYST_DOWN.name
+            return True
+        else:
+            return False
+    elif hyst_low_state == Hyst.HYST_DOWN.name:
+        if value > (lowlevel + fabs(hyst)):
+            hyst_low_state = Hyst.HYST_IDLE.name
+            return False
+        else:
+            return True
+    return False
+
+
+def trig_high_value(value: float, highlevel: float, hyst: float):
+    global hyst_high_state
+    if hyst_high_state == Hyst.HYST_IDLE.name:
+        if value >= highlevel:
+            hyst_high_state = Hyst.HYST_UP.name
+            return True
+        else:
+            return False
+    elif hyst_high_state == Hyst.HYST_UP.name:
+        if value < (highlevel - fabs(hyst)):
+            hyst_high_state = Hyst.HYST_IDLE.name
+            return False
+        else:
+            return True
+    return False
+
+
+def main():
+    grapth()
+
+
+if __name__ == '__main__':
+    main()

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác