Jump to content

As of July 17, 2015, the LabJack forums here at forums.labjack.com are shut down. New registrations, topics, and replies are disabled. All forums are in a read-only state for archive purposes.

Please visit our current forums at labjack.com/forums to view and make new posts. To post on the current forums, use your labjack.com login account. Your old LabJack forums login credentials have been retired. There are no longer separate logins for labjack.com and LabJack forums.


Photo

Reading and sending PWM


  • Please log in to reply
2 replies to this topic

#1 Susi

Susi
  • Members
  • 31 posts

Posted 01 November 2012 - 11:10 AM

Hello, I have made a windows form aplication in C#, and at first at conected to the labjack U3 perfectly, I was sending a diferente PWM signal every 1 seg, and now I need to read from the AIN0 every 1ms. so when added the code to take the sample every 1ms I started to have problems whit the PWM signal I was sending, I'm able to read every 1ms perfectly but now when I send the PWM servomotro it is not moving. So this the code to generate a pwm signal: [codebox]LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double dblValue = 0; int dummyInt = 0; double dummyDouble = 0; double[] dummyDoubleArray = { 0 }; double ns,tao; tao = (0.00001 * angulo + 0.000565); ns = (0.01911 - tao) * (65536 / 0.01911); try { u3 = new U3(LJUD.CONNECTION.USB, "0", true); LJUD.ePut(u3.ljhandle, LJUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0); LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_COUNTER_PIN_OFFSET, 4, 0, 0); LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_BASE, (double)LJUD.TIMERCLOCKS.MHZ48_DIV, 0, 0); LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_DIVISOR, 14, 0, 0); LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 1, 0, 0); LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_TIMER_MODE, 0, (double)LJUD.TIMERMODE.PWM16, 0, 0); LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_TIMER_VALUE, 0, ns, 0, 0); LJUD.GoOne(u3.ljhandle); } catch (LabJackUDException e) { showErrorMessage(e); } try { LJUD.GetFirstResult(u3.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { showErrorMessage(e); } bool finished = false; while (!finished) { try { LJUD.GetNextResult(u3.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { if (e.LJUDError == UE9.LJUDERROR.NO_MORE_DATA_AVAILABLE) finished = true; else showErrorMessage(e); } }[/codebox] So I call this method when I press a button and send a diferrent angulo to move the servomotor. And the next code I'm putting start to read since I run the application. [codebox] double dblDriverVersion; LJUD.IO ioType=0; LJUD.CHANNEL channel=0; double dblValue=0; int dummyInt = 0; double dummyDouble = 0; dblDriverVersion = LJUD.GetDriverVersion(); try { u3 = new U3(LJUD.CONNECTION.USB, "0", true); // Connection through USB LJUD.ePut(u3.ljhandle, LJUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0); LJUD.AddRequest(u3.ljhandle, LJUD.IO.GET_AIN, 0, 0, 0, 0); } catch (LabJackUDException e) { } try { LJUD.GoOne(u3.ljhandle); LJUD.GetFirstResult(u3.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { } try { LJUD.GetNextResult(u3.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { } double AIN0; AIN0 = dblValue; textBox3.Text = AIN0.ToString(); }[/codebox]

#2 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 01 November 2012 - 02:40 PM

Hello, I have made a windows form aplication in C#, and at first at conected to the labjack U3 perfectly, I was sending a diferente PWM signal every 1 seg, and now I need to read from the AIN0 every 1ms. so when added the code to take the sample every 1ms I started to have problems whit the PWM signal I was sending, I'm able to read every 1ms perfectly but now when I send the PWM servomotro it is not moving.

So this the code to generate a pwm signal:

LJUD.IO ioType = 0;
            LJUD.CHANNEL channel = 0;
            double dblValue = 0;

            int dummyInt = 0;
            double dummyDouble = 0;
            double[] dummyDoubleArray = { 0 };
            double ns,tao;
            tao = (0.00001 * angulo + 0.000565);
            ns = (0.01911 - tao) * (65536 / 0.01911);

            try
            {
                u3 = new U3(LJUD.CONNECTION.USB, "0", true);

                LJUD.ePut(u3.ljhandle, LJUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0);
                
                LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_COUNTER_PIN_OFFSET, 4, 0, 0);

                LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_BASE, (double)LJUD.TIMERCLOCKS.MHZ48_DIV, 0, 0);
              
                LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_DIVISOR, 14, 0, 0);
               
                LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 1, 0, 0);

                LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_TIMER_MODE, 0, (double)LJUD.TIMERMODE.PWM16, 0, 0);

                LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_TIMER_VALUE, 0, ns, 0, 0);

                LJUD.GoOne(u3.ljhandle);
            }
            catch (LabJackUDException e)
            {
                showErrorMessage(e);
            }

            try { LJUD.GetFirstResult(u3.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); }
            catch (LabJackUDException e) { showErrorMessage(e); }
            bool finished = false;
            while (!finished)
            {
                try { LJUD.GetNextResult(u3.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); }
                catch (LabJackUDException e)
                {
                    if (e.LJUDError == UE9.LJUDERROR.NO_MORE_DATA_AVAILABLE)
                        finished = true;
                    else
                        showErrorMessage(e);
                }
            }

So I call this method when I press a button and send a diferrent angulo to move the servomotor.

And the next code I'm putting start to read since I run the application.

double dblDriverVersion;
	    LJUD.IO ioType=0;
	    LJUD.CHANNEL channel=0;
	    double dblValue=0;
	    int dummyInt = 0;
            double dummyDouble = 0;
           
            dblDriverVersion = LJUD.GetDriverVersion();
            try
            {
                u3 = new U3(LJUD.CONNECTION.USB, "0", true); // Connection through USB

                LJUD.ePut(u3.ljhandle, LJUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0);

                LJUD.AddRequest(u3.ljhandle, LJUD.IO.GET_AIN, 0, 0, 0, 0);

            }
            catch (LabJackUDException e)
            {
            }
             try
                {
                    LJUD.GoOne(u3.ljhandle);
                    LJUD.GetFirstResult(u3.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble);
                }
                catch (LabJackUDException e)
                {
                }

                try { LJUD.GetNextResult(u3.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); }

                catch (LabJackUDException e) {
                }
                double AIN0;
                AIN0 = dblValue;
                textBox3.Text = AIN0.ToString();
                 
        }


Generally these functions should only be called once:

u3 = new U3(LJUD.CONNECTION.USB, "0", true); // Connection through USB
LJUD.ePut(u3.ljhandle, LJUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0);

The first function is to open the U3 and get a handle to it. Once you have that you should pass it around to various parts of your code, rather than opening it new each time. Thus, if the U3 is a member of your class and both those functions are in the same class they will use the same object and you only have to call the new U3() call once, at the beginning of your program.

The LJUD.IO.PIN_CONFIGURATION_RESET function sets the U3 to the default values. This is useful to do at the beginning of your program to make sure that when the code runs the first time the U3 is set up properly each time. That function will disable timers and counters and other things, so it could be what is causing your problem.

I would try calling those two functions just once at the beginning of your program and see if it fixes your issue.

#3 Susi

Susi
  • Members
  • 31 posts

Posted 01 November 2012 - 05:46 PM

That was the problem, I fixed it and now works perfect Thanks for your help


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users