Slightly odd behaviour with matlab - Any chance on someone checking the code? | LabJack
 

Slightly odd behaviour with matlab - Any chance on someone checking the code?

2 posts / 0 new
Last post
bill.connelly
bill.connelly's picture
Slightly odd behaviour with matlab - Any chance on someone checking the code?

I've written the following class in matlab as a wrapper for interacting with a U3. It works 99% of the time, but occasionally it the U3 does not respond by outputing the appropriate analog or digital steps. I can't see what the problem is. I was hoping there was a chance someone could look over it.

The class is below... the code snippet that operates it is:

        lj = MyLJ([4], [6, 7], [], [0]);

        lj.write_analog(5*randn(1,1));

        lj.write_digital( logical(1) );

        %some function that takes 3 seconds to operate

        lj.write_digital(logical([0, 0]));

        lj.write_analog(0);

classdef MyLJ

    properties

        ljasm

        ljudObj

        ljhandle

        

        num_input_chans

        

        

        dig_in_chans

        analog_in_chans

        

        dig_out_chans

        analog_out_chans

    end

    

    methods

        function obj = MyLJ(dig_in_chans, dig_out_chans, analog_in_chans, analog_out_chans)

            if intersect(dig_in_chans, dig_out_chans)

                error('Overlap between digital in and out pins. Pin cannot be both input and output')

            end

            

            %Make the UD .NET assembly visible in MATLAB

            obj.ljasm = NET.addAssembly('LJUDDotNet'); 

            obj.ljudObj = LabJack.LabJackUD.LJUD;

            

            %Open Labjack

            [ljerror, obj.ljhandle] = obj.ljudObj.OpenLabJack(LabJack.LabJackUD.DEVICE.U3, LabJack.LabJackUD.CONNECTION.USB, '0', true, 0);

            

            %Reset to default

            obj.ljudObj.ePut(obj.ljhandle, LabJack.LabJackUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0); 

            

            obj.num_input_chans = sum([length(dig_in_chans) length(analog_in_chans)]) ;

            

            %Set up input pins

            obj.dig_in_chans = dig_in_chans;

            obj.config_dig_in(dig_in_chans);

            obj.analog_in_chans = analog_in_chans;

            obj.config_analog_in(analog_in_chans)

            

            %Set up output pins

            obj.dig_out_chans = dig_out_chans;

            obj.analog_out_chans = analog_out_chans;

            

            %Push setup

            %obj.ljudObj.GoOne(obj.ljhandle);

        end

        

        function config_dig_in(obj, dig_in)

           for pin = dig_in

              obj.ljudObj.AddRequest(obj.ljhandle, LabJack.LabJackUD.IO.GET_DIGITAL_BIT, pin, 0, 0, 0); 

           end            

        end

        

        function config_analog_in(obj, analog_in)

            for pin = analog_in

                obj.ljudObj.AddRequest(obj.ljhandle, LabJack.LabJackUD.IO.GET_AIN, pin, 0, 0, 0);

            end

        end

        

        function read = read_all(obj)

            % Read a single value from every analog and digital channel in

            % order, digital channels first, then analog channels.

            

            obj.config_dig_in(obj.dig_in_chans);

            obj.config_analog_in(obj.analog_in_chans);

            obj.ljudObj.GoOne(obj.ljhandle);

            

            read = zeros(1,obj.num_input_chans);

            

            for i = 1:obj.num_input_chans

                if i == 1

                    [ljerror, ioType, channel, dblValue, dummyInt, dummyDbl] = obj.ljudObj.GetFirstResult(obj.ljhandle, 0, 0, 0, 0, 0);

                else

                    [ljerror, ioType, channel, dblValue, dummyInt, dummyDbl] = obj.ljudObj.GetNextResult(obj.ljhandle, 0, 0, 0, 0, 0);

                end

                read(i) = dblValue; 

            end 

        end

        

        function write_analog(obj, analog_vals)

            % Writes to analog channels in order. Can write to just first n

            % channels.

            if length(analog_vals) > length(obj.analog_out_chans)

                error('More values given than analog out channels configured')

            end

            

            for i = 1:length(analog_vals)

                obj.ljudObj.AddRequest(obj.ljhandle, LabJack.LabJackUD.IO.PUT_DAC, obj.analog_out_chans(i), analog_vals(i), 0, 0);

            end

            

            obj.ljudObj.GoOne(obj.ljhandle);

        end

        

        function write_digital(obj, digital_vals)

            % Write digital channels in order. Can write to just first n

            % channels

            if length(digital_vals) > length(obj.dig_out_chans)

                error('More values given than digital out channels configured')

            end

            

            for i = 1:length(digital_vals)

                obj.ljudObj.AddRequest(obj.ljhandle, LabJack.LabJackUD.IO.PUT_DIGITAL_BIT, obj.dig_out_chans(i), digital_vals(i), 0, 0);

            end

            

            obj.ljudObj.GoOne(obj.ljhandle);

        end        

    end

end

LabJack Support
labjack support's picture
Your calls look correct in

Your calls look correct in that you are setting digital and analog outputs for the most part. Going through it, it looks like you initialize your class with:

Digital Inputs: FIO4
Digital Outputs: FIO6, FIO7
Analog Inputs: None
Analog Outputs: DAC0

Then the following code:

Set DAC0 a random 0-5 V.
Set FIO6 to high.
%some function that takes 3 seconds to operate
Set FIO6 and FIO7 to low.
Set DAC0 to 0 V.

By "does not respond by outputing the appropriate analog or digital steps", are the outputs described above not correct when you detect the issue? How are you detecting this issue? Is it with the read/readAll function?

First make sure your U3 is running the latest, most stable version of the firmware:

https://labjack.com/support/firmware/u3

Second, double check that your "3 second function" is actually delaying for 3 seconds when your issue occurs. Perhaps if it is shorter than 3 seconds that is causing issues with your code's timings. Also, when changing U3 DAC outputs there is a slew rate of 0.4 V/ms, so keep that in mind when there are larger changes in voltages and performing readings on the DACs at a fast rate.