Implementing Killswitch and Reset pushbuttons in Python | LabJack
 

Implementing Killswitch and Reset pushbuttons in Python

5 posts / 0 new
Last post
JZ
MrYosuko's picture
Implementing Killswitch and Reset pushbuttons in Python

Hi,

I'm using python to program a motor-driven circuit that uses an L293D driver to control the motor rotation direction. The circuit has two push buttons called killswitch, and the other one is called reset. The killswitch is wired to FIO1, and the reset pushbutton is wired to FIO2. The input pins from the L293D that control the direction of motor rotation are wired to FIO3 and FIO4. 

I'm trying to program FIO1 as a kill switch and FIO2 as a reset switch, so when the kill switch is pressed while the motor is running, the motor and rest of the circuit will stop, and no matter whatever signal I send to FIO3 or FIO4, the circuit won't function unless I press the Reset pushbutton. I have been going through the documentation and the ue9.py library for some days now, but it seems I can't figure it out. Also, I couldn't find any examples with push buttons. I have a function that resets the state of FIO3 and FIO4 to default, but I don't know how to use it with the pushbuttons. 

So, I'm hoping for some help.

LabJack Support
labjack support's picture
The easiest way to set

The easiest way to set individual IO is with SingleIO:

https://github.com/labjack/LabJackPython/blob/master/src/ue9.py#L573

To set many IO at once, you could use the Feedback command:

https://github.com/labjack/LabJackPython/blob/master/src/ue9.py#L477

There is some more information about the feedback command in our low level docs and parameters like FIOState and FIODirection are bit-packed integers as described near the bottom of our DIO docs:

https://labjack.com/support/datasheets/ue9/low-level-function-reference/...

https://labjack.com/support/datasheets/ue9/hardware-description/dio

How do your buttons work? Do they latch shut, or just shut while pressed? The latter seems most common. In that case, the best way to control your buttons is likely with the IO set up as counters. Assuming you have the hardware and counter set up correctly, you would do your stop or reset routine when you see new counts on the respective counter (reading the counters periodically in software). Please see the following documentation:

https://labjack.com/support/datasheets/ue9/hardware-description/timers-c...

You would use the timerCounter function to set up the counters:

https://github.com/labjack/LabJackPython/blob/master/src/ue9.py#L647

Please see the following docs related to switch/button signal setup:

https://labjack.com/support/datasheets/ue9/hardware-description/dio/typi...

JZ
MrYosuko's picture
Thank you for the reply.

Thank you for the reply.

I used the Feedback command to reset the IOs back to logic LOW in my reset function, and I used the singleIO command on the two FIOs that control the direction of motor rotation. The buttons that I'm using are two SPST Momentary Push Buttons. So, I have tried to configure the FIO1 and FIO2 as follows:
 

pushbutton = ue9.timerCounter(UpdateConfig=True, NumTimersEnabled=2, Counter1Enabled=True, ResetTimer1=False, ResetTimer2=False, ResetCounter1=False, Timer1Mode=2, Timer1Value=3.3, Timer2Mode=3, Timer2Value=0)

I set timer1mode to the rising edge for killswitch and timer2mode to the falling edge for the reset button. However, I'm not so sure about this as I have configured a PWM signal before with timerCounter, but I have never tried to capture an input signal from a push-button when pressed. My push-buttons are connected the same way as Figure 2.9.1.3-1 but to FIO1 and FIO2. My reset function sets the FIO3 and FIO4 to input and state low. And reset function sets the FIO3 and FIO4 back to output as for its state; it depends on the user input.

Would it be possible for you to provide an example in python on setting up FIO1 and FIO2 as counters?

LabJack Support
labjack support's picture
Your configuration call looks

Your configuration call looks correct, but I do not think the period input measurement feature is appropriate here. The period measurement feature requires two edges in either direction to return a useful value. With your botton press, the ideal signal should look like a single pulse, one edge in each direction, so you would not get any useful info until you pushed the button twice or you had significant signal bounce that caused extra edges.

The following would configure two counters. Note that this would put them on FIO0 and FIO1; to put them on FIO1 and FIO2 you would need to enable one timer. If you do not need a timer for something else, I would recommend just moving the IO over to FIO0 and FIO1:

ue9.timerCounter(UpdateConfig=True, NumTimersEnabled=0, Counter0Enabled=True, Counter1Enabled=True)

This function is also how you read the timers and counters. To read and reset the counters:

timerCounterValues = ue9.timerCounter(UpdateConfig=False, ResetCounter0=True, ResetCounter1=True)

Counter0Val = timerCounterValues["Counter0"]

 

To help with debounce, you might use the firmware counter with debounce timer feature. TimerValue set to 1 will use an 87ms debounce period. This call also sets the features up on FIO0 and FIO1:

ue9.timerCounter(UpdateConfig=True, NumTimersEnabled=2, Timer0Mode=6, Timer0Value=1,  Timer1Mode=6, Timer1Value=1)

To read and reset both timers:

timerCounterValues = ue9.timerCounter(UpdateConfig=False, ResetTimer0=True, ResetTimer1=True)

Timer0Val = timerCounterValues["Timer0"]

 

Also, you cannot set the state of an input low. You would need to set the IO direction to output to set them low. The state of input pins depend on what you have connected to them. They are typically held high to 3.3V with a weak pullup resistor.

JZ
MrYosuko's picture
This explanation cleared up a

This explanation cleared up a lot of stuff for me. And I was able to figure out the pushbuttons. So, thank you :)