Problem with a timer using Jquery, Flask, and nidaqmx

  flask, html, jquery, nidaqmx, windows

I have a timer that I’m selecting in html and passing as a variable, this variable starts a timer that calls a function in Flask which is using nidaqmx.

Afterwards, I’m using openpyxl to write and append some calculated values; however, I am facing a discrepancy in the time: when selecting any time value, sometimes it counts to 130 s, and some other times it counts up to 170 s. I don’t know what is happening, but the time variable is omitted and it somehow calculates its own time.

I tried a very basic program that generates random data, and it’s working perfectly, the problem seems to happen when using nidaqmx.

@app.route('/acount')
def count():
    
    filename = 'data_New.xlsx'                              # Initialize the file
    
    wb = Workbook()                                         
    ws = wb.worksheets[0]                                   
    ws.cell(row = 2, column = 1).value = 'time'           
    ws.cell(row = 2, column = 2).value = 'rms C1'
    ws.cell(row = 2, column = 3).value = 'rms C2'
    ws.cell(row = 2, column = 4).value = 'rms C3'
    ws.cell(row = 2, column = 5).value = 'rms C4'
    
    wb.save(filename)                                       # Saving the file
    return render_template('count.html')

@app.route('/aCont', methods = ['GET'])
def a_cont():
    sample_rateC = int(request.args.get('sample_rateC'))
    samples_to_acqC = int(request.args.get('samples_to_acqC'))
    wait_timeC = samples_to_acqC/sample_rateC                         
    cont_mode = AcquisitionType.FINITE                                
    global counterFlask
    counterFlask += 1            #I'm starting this variable on the main flask app. I know I shouldn't be using global variables, but is way simpler.                                
    
    with nidaqmx.Task() as task:
        now = datetime.now()
        t_militarC = now.strftime('%H:%M:%S')
        
        #Just the first two channels are active, the remaining are V channels
        task.ai_channels.add_ai_accel_chan(physical_channel = "cDAQ9181-1E3866DMod1/ai0", sensitivity = 100, current_excit_val = 0.002)
        
        task.ai_channels.add_ai_accel_chan(physical_channel = "cDAQ9181-1E3866DMod1/ai1", sensitivity = 100, current_excit_val = 0.002)
   
        task.ai_channels.add_ai_voltage_chan(physical_channel = "cDAQ9181-1E3866DMod1/ai2")
        
        task.ai_channels.add_ai_voltage_chan(physical_channel = "cDAQ9181-1E3866DMod1/ai3")
        
        task.timing.cfg_samp_clk_timing(sample_rateC, sample_mode = cont_mode, samps_per_chan = samples_to_acqC)               # Sets source of sample clock, its rate, and number of samples to aquire (buffer size)
         
        # Saving xdata and ydata to a xlsx file, this file is always overwritten, and can be saved as a different file name             
        filename = 'data_New.xlsx'       
    
        wb = load_workbook(filename)
        ws = wb.worksheets[0]                                  
        timeC = wait_timeC + 5
        data = np.ndarray((4, samples_to_acqC), dtype = np.float64)
        nidaqmx.stream_readers.AnalogMultiChannelReader(task.in_stream).read_many_sample(data, samples_to_acqC, timeout = timeC)  #time C is the time out for the NI9234 card... it works fine when I am doing only one acquisition
        
        x = np.linspace(0, wait_timeC, samples_to_acqC)
                
        rms_val_1C = np.sqrt(np.mean(np.power(data[0],2)))                
        rms_val_1C = np.round(rms_val_1C, 5)                           
        
        rms_val_2C = np.sqrt(np.mean(np.power(data[1],2)))
        rms_val_2C = np.round(rms_val_2C, 5)
        
        rms_val_3C = 'NONE'
        
        rms_val_4C = 'NONE'
        
        new_row = [t_militarC, rms_val_1C, rms_val_2C, rms_val_3C, rms_val_4C]
        
        ws.append(new_row)                                     
        wb.save(filename)
        
    return str(rms_val_1C)

This is the html+jquery:

{% extends "base.html" %}

{% block jscript %}

<script>

$(document).ready(function(){
    
    var tiempo = $("#tiempo").val();
    
    document.getElementById("btncaptCont").addEventListener("click", startTimer);
    
    function startTimer(){
        var seTime = setInterval(myTimer, tiempo);
    }
    
    function myTimer(){
        $.ajax({
            url: "/aCont",
            data : {
                sample_rateC : $('#f_muestreoCont').val(),
                samples_to_acqC : $('#m_canalCont').val()
            },
            type: "GET",
            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            success: function(result){
                $("#rms_1C").html(result);
            }
        });
    };
}); 
</script>

{% endblock %}

{% block leftsidebar %}

<div class="bs-component">
    <div class="card border-dark mb-3" style="max-width: 20rem;">
        <div class="card-header">Frequency and sample rate</div>
        <div class="card-body">
            <h6 class="mb-1"><i>frec. muestreo</i></h6>
            
                <select class="form-control" name="f_muestreoCont" id="f_muestreoCont">
                
                    <option value="2048">2048</option>
                    <option value="2560">2560</option>
                    <option value="3200">3200</option>
                    <option value="5120">5120</option>
                    <option value="6400">6400</option>
                    <option value="8192">8192</option>
                    <option value="10240">10240</option>
                    <option value="12800">12800</option>
                    <option value="25600">25600</option>
                    
                </select>
                
            <h6 class="mb-1"><i>Muestras/canal</i></h6>
                    
                <select class="form-control" name="m_canalCont" id="m_canalCont">
            
                    <option value="2048">2048</option>
                    <option value="4096">4096</option>
                    <option value="8192">8192</option>
                    <option value="16384">16384</option>
                    <option value="32768">32768</option>
                    <option value="65536">65536</option>
                    <option value="131072">131072</option>
                    
                </select>
                
            <h6 class="mb-1"><i>Intervalo de captura</i></h6>
                    
                <select class="form-control" name="tiempo" id="tiempo">
                    <!--Value in ms-->
                    <option value="90000">1.5 min</option>
                    <option value="120000">2 min</option>
                    <option value="240000">4 min</option>
                    <option value="300000">5 min</option>
                    <option value="600000">10 min</option>
                    
                </select>   
        </div>
    </div>
</div>

<div class="bs-component">
    <div class="card border-dark mb-3" style="max-width: 20rem;">
        <div class="card-header">Continuos capture</div>
        <div class="card-body">
            <button class="btn btn-primary" id="btncaptCont">Start</button>
        </div>
    </div>
</div>          

{% endblock %}

{% block center %}

<h2>rms value</h2>
<div class="bs-component">
    <p id="rms_val_1C"></p>
</div>

{% endblock %}

{% block rigthsidebar %}
    
{% endblock %}

I guess the timing goes crazy when the nidaqmx function is running. If so, how to fix it?

Source: Windows Questions

LEAVE A COMMENT