Binary Logic Circuits

Logic circuits are used to build the digital computers, ones that are enabling your engagement with this very blog post. These perform operations on digital signals, which is essentially restricting the signal from electric circuits to a few discrete values. For example, decimal logic circuits have values from 0 to 9. The discrete values are fewer in the case of binary logic circuits, where the result is either a 1 or a 0.

Let us address the case of binary logic circuits in this post.

The simplest binary logic that we interact with on a regular basis is a switch. It has two values – ON or OFF.

Think of our control input to be a variable, s. If we provide no input, the value of s = 0 or s = LOW and the circuit is considered “open”. If we provide an input, the value of s = 1 or s = HIGH and the circuit is now “closed”.

If we apply this to the case of our switch, think of no input as leaving your switch at OFF (s = 0). In this case, our circuit is open and the bulb or fan you want to control stays turned off. If we decide to switch our electric equipment ON (s = 1), we end up closing the circuit and setting the fan in motion.

States of a switch
Table 1: States of a switch

Table 1 provides a simple representation of our logic, an approach called the Truth Table representation. As we can gather from the Truth Table, our output is equal to the control input we provide our circuit. In the case of digital logic circuits, we define the output (also called Logic Function) to be a function of our control input variable. For our switch, the logic function equals the control input and is represented as:

F(s) = s

Now we have our first binary logic expression!

This idea can be extended to introduce basic logic functions (AND, OR, NOT) and subsequently build a logic network that has a combination of these gates.

 

Advertisements

Color tracking using webcam, OpenCV and Python

In this post, a simple method to obtain continuous video from a standard webcam is described. It also provides code to track a certain color from the camera feed. For this purpose, we make use of OpenCV functions in python.

To test if you are able to capture the live video feed, use the code snippet provided below.

# import the necessary packages
import cv2
#Declare variables for window
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
# try to get the first frame
if vc.isOpened():
rval, frame = vc.read()
else:
rval = False
while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # Exit when ESC key is pressed
break
vc.release() #To unlock camera on windows OS
cv2.destroyWindow("preview")

If you are unable to close the window, simply press ‘Esc’. It has been assigned as the exit key in the code.

Now, to verify color tracking using HSV values, use the following code:

#Import necesessary packages
import cv2
import numpy as np
#Declare variables for window
vc = cv2.VideoCapture(0)
while(1):
# Take each frame
ret, frame = cap.read()
if(ret):
# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of blue color in HSV
lower_blue = np.array([50, 100, 100], dtype=np.uint8)#[110,50,50]
upper_blue = np.array([70,255,255], dtype=np.uint8)#[130,255,255]
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_blue, upper_blue)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(frame,frame, mask= mask)
cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.imshow('res',res)
k = cv2.waitKey(5) & 0xFF
if k == 27: #Exit when ESC key is pressed
break
vc.release()
cv2.destroyAllWindows()

If you would like to determine HSV values for any color, please use the example below:

>> green = np.uint8([[[0,255,0]]])
>> hsv_green = cv2.cvtColor(green,cv2.COLOR_BGR2HSV)
>> print hsv_green

Now the value obtained (here, [[[60 255 255]]]) offers the base to set the range of colors to determine. Use [H-10, 100, 100] as lower bound and [H+10, 255, 255] as upper bound.

Hope this helps.