Kaynağa Gözat

Аналоговые входы. Тесты.

TelenkovDmitry 7 ay önce
ebeveyn
işleme
42127a7f3a

+ 66 - 95
fw/modules/io/analog_input.c

@@ -14,13 +14,15 @@
 #define DBG if(1)
 
 
-uint8_t input_mux; // выход сдвигового регистра U1010 (управляет MUX 301, 401)
-                   // мультиплексоры отвечат за коммутацию аналоговых входов и АЦП
+uint8_t ai_mux;     // выход сдвигового регистра U1010 (управляет MUX 301, 401)
+                    // мультиплексоры отвечат за коммутацию аналоговых входов и АЦП
 
-uint16_t input_mode; // режим измерения аналоговых каналов (ток или напряжение)
-                     // 0000 0000 0000 0000 
-                     // младшие 6 (с 1..6) бит - каналы с 1 по 6 соответственно
-                     // биты 9..14 - каналы с 7 по 12 соответственно
+uint16_t ai_mode;   // режим измерения аналоговых каналов (ток или напряжение)
+                    // 0000 0000 0000 0000 
+                    // младшие 6 (с 1..6) бит - каналы с 1 по 6 соответственно
+                    // биты 9..14 - каналы с 7 по 12 соответственно
+
+uint16_t ai_alarm;  // слово аварий входов
 
 ANALOG_IN_t channel_name[NUMBER_ADC_CH] = {AN_INP_1, AN_INP_2, AN_INP_3, 
                                            AN_INP_4, AN_INP_5, AN_INP_6,
@@ -36,7 +38,7 @@ uint16_t adc_raw_data[NUMBER_ADC_CH];
 
 
 
-// Установить режим измерения канало с 1 по 12.
+// Установить режим измерения каналов с 1 по 12.
 void ai_set_meas_mode(void)
 {
     uint8_t bit = 0;
@@ -44,7 +46,7 @@ void ai_set_meas_mode(void)
     for (uint8_t i = 1; i < 13; i++)
     {
         bit = settings.ai_mode_bits & (1 << (i - 1));
-        ai_set_mode((MEAS_CHAN_MODE_t)bit, i);  
+        ai_set_mode_word((MEAS_CHAN_MODE_t)bit, i);
     }
 }   
 
@@ -121,8 +123,6 @@ bool ai_adc_init(void)
     // 2 - ой канал
     MS5192T_Calibrate(MS5192T_MODE_CAL_INT_ZERO, MS5192T_CH_AIN2P_AIN2M); 
     MS5192T_Calibrate(MS5192T_MODE_CAL_INT_FULL, MS5192T_CH_AIN2P_AIN2M); 
-    
-    //MS5192T_SetChannel(MS5192T_CH_AIN1P_AIN1M);
           
     return true;
 }  
@@ -130,90 +130,36 @@ bool ai_adc_init(void)
 // Оцифровка всех каналов (входы + дополнительные каналы)
 void ai_processing(void)
 {
-    //unsigned long value;
-        
-#if 0 
-    ai_connect_channel(AN_INP_4);
-    vTaskDelay(10);
-    MS5192T_SetChannel(MS5192T_CH_AIN1P_AIN1M);
-    vTaskDelay(10);
-    value = MS5192T_SingleConversion();
-    printf("ADC channel [AN_INP_1] data raw: 0x%X, %f\r\n", value, (double)value*0.00001785305/0.0961538);
-    
-    ai_connect_channel(AN_INP_10);
-    vTaskDelay(10);
-    MS5192T_SetChannel(MS5192T_CH_AIN2P_AIN2M);
-    vTaskDelay(10);
-    value = MS5192T_SingleConversion();
-    printf("ADC channel [AN_INP_7] data raw: 0x%X, %f\r\n", value, (double)value*0.00001785305/0.0961538);
-#endif
-    
-    
-#if 0    
-    ai_connect_channel(AN_INP_2);
-    vTaskDelay(10);
-    MS5192T_SetChannel(MS5192T_CH_AIN1P_AIN1M);
-    vTaskDelay(10);
-    value = MS5192T_SingleConversion();
-    printf("ADC channel [AN_INP_1] data raw: 0x%X, %f\r\n", value, (double)value*0.00001785305/0.0961538);
-    
-    ai_connect_channel(AN_INP_8);
-    vTaskDelay(10);
-    MS5192T_SetChannel(MS5192T_CH_AIN2P_AIN2M);
-    vTaskDelay(10);
-    value = MS5192T_SingleConversion();
-    printf("ADC channel [AN_INP_7] data raw: 0x%X, %f\r\n", value, (double)value*0.00001785305/0.0961538);
-    
-    
-    ai_connect_channel(V_ISO_CL);
-    vTaskDelay(10);
-    MS5192T_SetChannel(MS5192T_CH_AIN1P_AIN1M);
-    vTaskDelay(10);
-    value = MS5192T_SingleConversion();
-    printf("ADC channel [V_ISO_CL] data raw: 0x%X, %f\r\n", value, (double)value*0.00001785305/0.0961538);
-    
-    ai_connect_channel(AN_INP_9);
-    vTaskDelay(10);
-    MS5192T_SetChannel(MS5192T_CH_AIN2P_AIN2M);
-    vTaskDelay(10);
-    value = MS5192T_SingleConversion();
-    printf("ADC channel [AN_INP_9] data raw: 0x%X, %f\r\n", value, (double)value*0.00001785305/0.0961538);
-#endif
-    
-#if 1    
     for (uint8_t i = 0; i < 8; i++)
     {
         adc_get_two_channles(channel_name[i], channel_name[i + 8]);
-        //printf("one: %u, two: %u\r\n", i, i + 8);
     }
 #if 0    
     printf("end\r\n");
     adc_print_data();
     printf("end\r\n");  
 #endif
-#endif        
-        
 }
 
 //
 void adc_get_two_channles(ANALOG_IN_t one, ANALOG_IN_t two)
 {
     ai_connect_channel(one);
-    vTaskDelay(10);
+    vTaskDelay(1);
     MS5192T_SetChannel(MS5192T_CH_AIN1P_AIN1M);
-    vTaskDelay(10);
     adc_raw_data[one] = MS5192T_SingleConversion();
-        
+    
     ai_connect_channel(two);
-    vTaskDelay(10);
+    vTaskDelay(1);
     MS5192T_SetChannel(MS5192T_CH_AIN2P_AIN2M);
-    vTaskDelay(10);
     adc_raw_data[two] = MS5192T_SingleConversion();
 }
 
 //
 void adc_print_data(void)
 {
+    printf("\033[2J"); // Очистить
+    printf("\033[H");  // Переместить курсор в левый верхний угол
     printf("AN_INP_1: 0x%X, %f\r\n", adc_raw_data[AN_INP_1], (double) adc_raw_data[AN_INP_1]*0.00001785305/0.0961538);
     printf("AN_INP_2: 0x%X, %f\r\n", adc_raw_data[AN_INP_2], (double) adc_raw_data[AN_INP_2]*0.00001785305/0.0961538);
     printf("AN_INP_3: 0x%X, %f\r\n", adc_raw_data[AN_INP_3], (double) adc_raw_data[AN_INP_3]*0.00001785305/0.0961538);
@@ -237,6 +183,7 @@ void adc_task(void *params)
 {
     for (;;)
     {
+        ai_alarm = sh_ai_mode(ai_mode);
         ai_processing();
         vTaskDelay(10);
     }
@@ -253,43 +200,43 @@ void ai_connect_channel(uint8_t channel)
 {
     if (channel < MUX_401_CH) 
     {
-        input_mux &= 0x70;
+        ai_mux &= 0xF0;
     
         switch (channel)
         {
-            case AN_INP_1: input_mux |= 0x03; break; // U301 Y3
-            case AN_INP_2: input_mux |= 0x00; break; // U301 Y0
-            case AN_INP_3: input_mux |= 0x05; break; // U301 Y5
-            case AN_INP_4: input_mux |= 0x07; break; // U301 Y7
-            case AN_INP_5: input_mux |= 0x06; break; // U301 Y6
-            case AN_INP_6: input_mux |= 0x04; break; // U301 Y4
-            case V_ISO_CL: input_mux |= 0x01; break; // U301 Y1
-            case V_ISO   : input_mux |= 0x02; break; // U301 Y2
+            case AN_INP_1: ai_mux |= 0x03; break; // U301 Y3
+            case AN_INP_2: ai_mux |= 0x00; break; // U301 Y0
+            case AN_INP_3: ai_mux |= 0x05; break; // U301 Y5
+            case AN_INP_4: ai_mux |= 0x07; break; // U301 Y7
+            case AN_INP_5: ai_mux |= 0x06; break; // U301 Y6
+            case AN_INP_6: ai_mux |= 0x04; break; // U301 Y4
+            case V_ISO_CL: ai_mux |= 0x01; break; // U301 Y1
+            case V_ISO   : ai_mux |= 0x02; break; // U301 Y2
             default: break;
         }
     }
     else
     {
-        input_mux &= 0x07;
+        ai_mux &= 0x87;
         
         switch (channel)
         {
-            case AN_INP_7: input_mux |= (0x04 << 4); break; // U401 Y4
-            case AN_INP_8: input_mux |= (0x06 << 4); break; // U401 Y6
-            case AN_INP_9: input_mux |= (0x07 << 4); break; // U401 Y7
-            case AN_INP_10:input_mux |= (0x05 << 4); break; // U401 Y5
-            case AN_INP_11:input_mux |= (0x02 << 4); break; // U401 Y2
-            case AN_INP_12:input_mux |= (0x01 << 4); break; // U401 Y1
-            case CRNT_LIM_U_BFR_R: input_mux  |= (0x00 << 4); break; // U401 Y0
-            case CRNT_LIM_U_ABFR_R: input_mux |= (0x03 << 4); break; // U401 Y3
+            case AN_INP_7: ai_mux |= (0x04 << 4); break; // U401 Y4
+            case AN_INP_8: ai_mux |= (0x06 << 4); break; // U401 Y6
+            case AN_INP_9: ai_mux |= (0x07 << 4); break; // U401 Y7
+            case AN_INP_10:ai_mux |= (0x05 << 4); break; // U401 Y5
+            case AN_INP_11:ai_mux |= (0x02 << 4); break; // U401 Y2
+            case AN_INP_12:ai_mux |= (0x01 << 4); break; // U401 Y1
+            case CRNT_LIM_U_BFR_R: ai_mux  |= (0x00 << 4); break; // U401 Y0
+            case CRNT_LIM_U_ABFR_R: ai_mux |= (0x03 << 4); break; // U401 Y3
             default: break;
         }
     }
     
-    sh_ai_connect(input_mux);
+    sh_ai_connect(ai_mux);
     
     //printf("Analog input connect register: ");
-    //print_binary_byte(input_mux);
+    //print_binary_byte(ai_mux);
 }
 
 // Утсновить режим измерения канала (ток или напряжение)
@@ -298,20 +245,44 @@ void ai_set_mode(MEAS_CHAN_MODE_t mode, uint8_t channel)
     if (mode == MEAS_CURRENT) 
     {
         if (channel < 7)
-            input_mode |= (1 << (channel - 1));
+            ai_mode |= (1 << (channel - 1));
         else
-            input_mode |= (1 << (channel + 1));
+            ai_mode |= (1 << (channel + 1));
     }
     else
         if (channel < 7)
-            input_mode &= ~(1 << (channel - 1));
+            ai_mode &= ~(1 << (channel - 1));
         else
-            input_mode &= ~(1 << (channel + 1));
+            ai_mode &= ~(1 << (channel + 1));
           
-    sh_ai_mode(input_mode);
+    sh_ai_mode(ai_mode);
     
     printf("Analog input mode: ");
-    print_binary_half_word(input_mode);
+    print_binary_half_word(ai_mode);
+}
+
+// Формируем слово режима работы каналов (ai_mode)
+void ai_set_mode_word(MEAS_CHAN_MODE_t mode, uint8_t channel)
+{
+    if (mode == MEAS_CURRENT) 
+    {
+        if (channel < 7)
+            ai_mode |= (1 << (channel - 1));
+        else
+            ai_mode |= (1 << (channel + 1));
+    }
+    else
+        if (channel < 7)
+            ai_mode &= ~(1 << (channel - 1));
+        else
+            ai_mode &= ~(1 << (channel + 1));
+}
+
+// Сигнал питания внешних датчиков
+void ai_ext_sens_power(uint16_t state)
+{
+    if (state)
+        ai_mux |= 0x80;
 }
 
 //

+ 7 - 0
fw/modules/io/analog_input.h

@@ -65,6 +65,12 @@ void ai_connect_channel(uint8_t channel);
 //
 void ai_set_mode(MEAS_CHAN_MODE_t mode, uint8_t channel);
 
+//
+void ai_set_mode_word(MEAS_CHAN_MODE_t mode, uint8_t channel);
+
+//
+void ai_ext_sens_power(uint16_t state);
+
 //
 void ai_connect_test(void);
 
@@ -75,6 +81,7 @@ void ai_mode_test(void);
 
 extern uint16_t adc_raw_data[];
 
+extern uint16_t ai_alarm; 
 
 #endif  // __ANALOG_INPUT_H
 

+ 23 - 0
fw/modules/modbus/modbus_ai_params.c

@@ -18,6 +18,16 @@ uint16_t mb_init_ai_params(uint16_t i)
     mb_param[index].reg = 0x0300;
 	mb_param[index].size = 1;
 	mb_param[index].param = (uint8_t*)&settings.ai_mode_bits;
+	mb_param[index].set = mb_set_ai_mode;
+    mb_param[index].get = NULL;
+    mb_param[index].check_handler = mb_check_dummy;
+    
+    index++;
+    
+    
+    mb_param[index].reg = 0x0301;
+	mb_param[index].size = 1;
+	mb_param[index].param = (uint8_t*)&ai_alarm;
 	mb_param[index].set = NULL;
     mb_param[index].get = NULL;
     mb_param[index].check_handler = mb_check_dummy;
@@ -173,4 +183,17 @@ uint16_t mb_init_ai_params(uint16_t i)
     return index;
 }
 
+
+// -------------------------------------------------------------------------- //
+//                          Установка параметров
+// -------------------------------------------------------------------------- //
+
+//
+mb_delay_action_t mb_set_ai_mode(void)
+{
+    ai_set_meas_mode();  
+    return MB_SAVE_SETTINGS;
+}
+
+
 #endif

+ 8 - 0
fw/modules/modbus/modbus_ai_params.h

@@ -17,6 +17,14 @@ uint16_t mb_init_ai_params(uint16_t i);
   
 
 
+// -------------------------------------------------------------------------- //
+//                          Установка параметров
+// -------------------------------------------------------------------------- //
+
+//
+mb_delay_action_t mb_set_ai_mode(void);
+
+
 #ifdef __cplusplus
 }
 #endif

+ 9 - 0
fw/modules/settings/settings_ai.c

@@ -8,10 +8,19 @@ void settings_ai_set_all_default(void)
 {
     // Аналоговые входы
     settings_ai_def(&settings);
+    
+    // Питание внешних датчиков
+    settings_ext_sens_power_def(&settings);
 }
 
 //
 void settings_ai_def(settings_t *settings)
 {
     settings->ai_mode_bits = 0;
+}
+
+//
+void settings_ext_sens_power_def(settings_t *settings)
+{
+    settings->ext_sens_power = 0;
 }

+ 1 - 0
fw/modules/settings/settings_api.h

@@ -119,6 +119,7 @@ typedef struct
 
 #if defined (MAI_12)
     uint16_t    ai_mode_bits;       // режим работы входов, 0 - зимерение напряжения, 1 - тока
+    uint16_t    ext_sens_power;     // питание внешних датчиков, 0 - выкл, 1 - вкл
 #endif    
 
     

+ 1 - 1
fw/user/main.cpp

@@ -144,7 +144,7 @@ void init_task(void *argument)
     //ai_connect_channel(AN_INP_7); 
     
     sh_init();          // Сдвиговые регистры
-    ai_set_meas_mode(); // Режим зимерения из настроек
+    ai_set_meas_mode(); // Режим измерения из настроек
     ai_adc_init();      // Внешинй ADC
     
     // Режим зимерения напряженния

BIN
output/fw.bin


Dosya farkı çok büyük olduğundan ihmal edildi
+ 302 - 323
project/ewarm/iap/iap.dep


Dosya farkı çok büyük olduğundan ihmal edildi
+ 791 - 792
project/ewarm/module_universal_io.dep


BIN
tools/__pycache__/mb_registers.cpython-312.pyc


+ 34 - 20
tools/analog_in.py

@@ -9,6 +9,10 @@ from mb_registers import AI_REGS
 import matplotlib.pyplot as plt 
 import matplotlib.animation as animation 
 
+ai_name = {'AIN_1': 0, 'AIN_2': 1, 'AIN_3': 2, 'AIN_4': 3, 'AIN_5': 4, 
+           'AIN_6': 5, 'AIN_7': 6, 'AIN_8': 7, 'AIN_9': 8, 'AIN_10': 9, 
+           'AIN_11': 10, 'AIN_12': 11, 'V_ISO_CL': 12, 'V_ISO': 13,
+           'CRNT_LIM_U_BFR_R': 14, 'CRNT_LIM_U_ABFR_R': 15}
 
 class IO_AnalogInput(IO_Module):
 
@@ -19,12 +23,14 @@ class IO_AnalogInput(IO_Module):
         super().__init__(self.modbus)
         self.log = AnalogInputLogReader(self.modbus)
 
-        self.fig = plt.figure()
-        self.in1 = self.fig.add_subplot(1, 1, 1)
-        # self.in2 = self.fig.add_subplot(1, 1, 2)
+        self.fig = plt.figure(1)
+        self.input = self.fig.add_subplot(1, 1, 1)
+        self.input.set_title('input_1')
+
         self.x = [0]
-        self.data_in1 = []
-        # self.data_in2 = []
+        self.data = []
+
+        self.graph_input = ai_name['AIN_1']
 
     '''Чтение параметров'''
 
@@ -33,6 +39,10 @@ class IO_AnalogInput(IO_Module):
         data = self.modbus.read_holding_registers(AI_REGS['ain_mode'], 1)
         return format(data[0], '012b')
 
+    def get_inputs_alarm(self):
+        data = self.modbus.read_holding_registers(AI_REGS['ain_alarm'], 1)
+        return format(data[0], '012b')
+
     def get_raw_inputs(self):
         data = self.modbus.read_holding_registers(AI_REGS['ain_raw'], 16)
         return data
@@ -53,23 +63,22 @@ class IO_AnalogInput(IO_Module):
 
     '''Вывод данных на график'''
 
-    
+    def show_graph(self, channel: str):
+        self.graph_input = ai_name[channel]
+        ani = animation.FuncAnimation(self.fig, self.draw_raw_inputs, interval=50)
+        plt.show()
+
 
     def draw_raw_inputs(self, i):
         data = self.get_raw_inputs()
 
-        self.data_in1.append(data[0]) 
-        # self.data_in2.append(data[1])
+        self.data.append(data[self.graph_input]) 
 
+        self.input.clear()
+        self.input.plot(self.x, self.data)
 
-        self.in1.clear()
-        self.in1.plot(self.x, self.data_in1)
-        # self.in2.clear()
-        # self.in2.plot(self.x, self.data_in2)
-
-        if len(self.data_in1) == self.GRAPTH_LEN:
-            self.data_in1.pop(0)
-            # self.data_in2.pop(0)
+        if len(self.data) == self.GRAPTH_LEN:
+            self.data.pop(0)
             self.x.pop(0)
 
         self.x.append(self.x[-1] + 1)
@@ -93,13 +102,18 @@ def main():
 
     '''Режим работы аналоговых входов'''
     # print(ai.get_inputs_mode())
-    # ai.set_inputs_mode(0b00011000)
-    
+    ai.set_inputs_mode(0b000000001)
+    # print(ai.get_inputs_mode())
+
+    '''Аварии аналоговых входов'''
+    for i in range(100):
+        print(ai.get_inputs_alarm())
+        sleep(1)
     # ai.get_raw_inputs()
     # ai.print_raw_inputs()
 
-    ani = animation.FuncAnimation(ai.fig, ai.draw_raw_inputs, interval=100)
-    plt.show()
+    # ai.show_graph('AIN_1')
+
 
 if __name__ == '__main__':
     main()

+ 1 - 1
tools/mb_registers.py

@@ -10,6 +10,6 @@ reg_table = {'out_cur': 0x0200, 'out_mode': 0x0202, 'out_mode_save': 0x0203, 'pw
 DIO_REGS = {'in_bits': 0x0100, 'in_cnt': 0x0102,'in_mode': 0x0120, 'in_norm': 0x0122,
             'in_deb_start': 0x124, 'load_bits': 0x0130, 'cred_bits': 0x0131, 'out_cur': 0x0200}
 
-AI_REGS = {'ain_mode': 0x0300, 'ain_raw': 0x0302}
+AI_REGS = {'ain_mode': 0x0300, 'ain_alarm': 0x0301,'ain_raw': 0x0302}
 
 LOG_REGS = {'log_info': 0x0900, 'log_ent': 0x0901, 'arch_cap': 0x0902, 'arch_ent': 0x0903, 'arch_per': 0x0904}

Bu fark içinde çok fazla dosya değişikliği olduğu için bazı dosyalar gösterilmiyor