Saturday, March 3, 2018

Raspberry Pi USB Input Device Data Monitor (Part 2)

USB Mouse/Keyboard Data Monitoring Using Raspberry Pi


In the previous post here, I was using python evdev module to capture the USB data.
This time I used python pyusb module since I found a great post by Gourmet Web Design

Setup
Install pip:
$ sudo apt-get update$ sudo apt-get install python-pip
Install puusb module:
$ sudo pip install pyusb
Check input events:
$ ls /dev/input
If you do not know which event is mouse or keyboard, follow this.
Install input-utils:
$ sudo apt-get install input-utils
$ lsinput

If you want to use pyusb to check the input devices, you can refer to this post by Gourmet Web Design



                  



Python Code
This is the python example code.

Example:
import usb.coreimport usb.util
dev = usb.core.find(idVendor=0x093a, idProduct=0x2510)
# was it found?if dev is None:    print usb.core.find()    raise ValueError('Device not found')
# first endpointinterface = 0endpoint = dev[0][(0,0)][0]print(endpoint)
# tell the kernel to detachdev.detach_kernel_driver(interface)
while True:    try:        data = dev.read(endpoint.bEndpointAddress,endpoint.wMaxPacketSize)        print data    except usb.core.USBError as e:        data = None        if e.args == ('Operation timed out',):            continue
# release the deviceusb.util.release_interface(dev, interface)# reattach the device to the OS kerneldev.attach_kernel_driver(interface)

Crate a python script file as below and copy&paste the above code.
nano test.py
Run the python script.
python test.py
Press any buttons on the keyboard or mouse and you will see the outputs in the terminal.
Done!




Wednesday, February 28, 2018

USB Host Shield for Arduino

USB Host Shield for Arduino to Monitor USB data


Sparkfun has USB host shield that can work with Arduino.
I think this is great for the user who want to monitor USB data during the prototyping phase.
It is also great for my latency test project that I posted previously.
It seems like this is original done by Circuits@Home and it is super great work!
This post will focus on the setup of the tool and hopefully I can use this for other projects that I am working on.

Setup
Software preparation:
Launch the Arduino IDE.
There is library available through the library manager.
To launch the Library Manager, follow this.

Sketch > Include Library > Manage Libraries...

In the Library Manager, there is a search bar at the upper right.
Type "USB host shield" and you will find the library by Oleg from Circuit@Home.
In my case "USB Host Shield Library 2.0", and here is the screenshot  after the installation.










After the installation, restart the Arduino IDE and you can now refer to the example codes using the library from "File" menu.

File > Examples > USB Host Shield Library 2.0

So many examples here!

Hardware preparation:
Before you use this board, you need to solder the header pins so that you can stack the board on top of the Arduino.
Once you connect the board to Arduino, make sure the power switch is ON position and you will see the red LED is on. 


Test

For my test, I just used USB mouse.
Connect a USB mouse to the USB connector on the shield board.
From the Arduino IDE, select the exmaples.
I used USB HID descriptor example.

File > Examples > USB Host Shield Library 2.0 > HID > USBHID_desc

Upload the code to the Arduino.
Then, open the "serial monitor" from the Arduino IDE (small icon at the upper right).
You will see the data read from USB mouse as you move the mouse.




















Done!



                  

Sunday, February 25, 2018

Raspberry PI USB Keyboard/Mouse Latency Test

USB Keyboard/Mouse Latency Test Using Raspberry Pi


Latency, latency, latency...
People ask those questions and I am also one of the guy who is interested in the latency...
From the USB test here and GPIO test here I have done before, I now decided to do quick latency test of keyboard or mouse.
In this test, I do the simple button press test and check how quickly the raspberry pi detect the USB input event.


Setup
Connect GPIO input to an external button:
I was too lazy to open up the unit and wire the button signal directly to the raspberry pi... sorry for the people who are interested in this post...
so I decided to wire a tact switch to the raspberry pi.
Then attach the switch to the one of the keyboard button.
  
Same as my previous GPIO test post, I am using GPIO20(pin #38) as interrupt input.
Also same as previous USB device input test post, I am using wireless keyboard with touchpad.
For this test, I used left button of the touchpad.


Python Code

Python code is pretty simple.
You call "import RPi.GPIO as GPIO" in the beginning and set the mode.
This is the python example code.
Example:
import RPi.GPIO as GPIO
import time
from evdev import InputDevice, categorize, ecodes
dev = InputDevice('/dev/input/event0')
print(dev)
GPIO.setmode(GPIO.BCM)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def my_callback(channel):
              global start
              start=time.time()
raw_input("Press Enter\n")
GPIO.add_event_detect(20, GPIO.FALLING, callback=my_callback, bouncetime=300)
try:
              for event in dev.read_loop():
                           if event.code == 272 and event.value ==1:
                                       end=time.time()
                                       elapsed=(end-start)*1000
                                       print(str(elapsed) + " ms")
except KeyboardInterrupt:
              GPIO.cleanup()

Crate a python script file as below and copy&paste the above code.
nano test.py
Run the python script.
python test.py

Result

The result was not that great...
why?
Because the my setup was too lazy to measure...
The contact of external button and touchpad button do not happen at the same time.
It is totally up to how you press button.
In my case, I needed to press a bit strong so that both external button and touchpad button were pressed nearly at the same time...
Anyway, the number I got was around 8-10 ms.
Old logitech whitepaper says around 10ms depending on the environment, so maybe result is reasonable for this simple test??
"The typical latency of an advanced 2.4 GHz device operating in a clean environment is below 10 ms. In a noisy environment, this latency may increase depending on the strength, type and occurrence of the interference. "
This is wireless keyboard with touchpad so wired mouse/keyboard would be faster I would assume.

Done!



                  

Saturday, February 24, 2018

Raspberry PI GPIO Test

GPIO Test Using Raspberry Pi


This post will talk about how fast raspberry pi can trigger GPIO. 
I did not want to investigate it deeply here but just wanted to quickly check it.
So, I decided to use the well maintained python GPIO library for raspberry pi.
For this test, I wired the output pin to input pin and measure the loopback time using timers.


Setup
Connect GPIO pins:
In my case, using GPIO16(pin #36) as output and GPIO20(pin #38) as input.
I used female jumper wires to connect those pins.
GPIO pin numbering diagram

Notes:

The GPIO software module for python is installed by default if you are using Raspbian.
If you want to double check the versions and/or install the latest version, refer to this site.


Python Code

Python code is pretty simple.
You call "import RPi.GPIO as GPIO" in the beginning and set the mode.
This is the python example code.
Example:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def my_callback(channel):
              end=time.time()
              elapsed=(end-start)*1000
              print(str(elapsed) + " ms")
raw_input("Press Enter\n")
GPIO.add_event_detect(20, GPIO.FALLING, callback=my_callback, bouncetime=300)
try:
              while True:
                           GPIO.output(16, 1)
                           time.sleep(0.5)
                           start=time.time()
                           GPIO.output(16, 0)
                           time.sleep(0.5)
except KeyboardInterrupt:
              GPIO.cleanup()

Notes:
There are 2 types of mode settings.
GPIO.BOARD means it is referring to the pins by the number of the pin.
GPIO.BCM means it is referring to the pins by the "Broadcom SOC channel" number.
For the details, you can refer to this great article here.

Crate a python script file as below and copy&paste the above code.
nano test.py
Run the python script.
python test.py

Result

The result was quite interesting.
It was around 0.2ms.
I think this is not a problem for the projects I do.

Done!



                  

Raspberry Pi USB Input Device Data Monitor (Part 1)

USB Mouse/Keyboard Data Monitoring Using Raspberry Pi


I am working on getting the raw USB mouse or keyboard data using raspberry pi.
To start with, I just decided to grab the data from the Logitech K400R wireless keyboard that I use for my raspberry pi.
This keyboard has the touchpad and size is good, not too small, not too large, and price is very reasonable. 

Logitech Wireless Touch Keyboard K400 with Built-In Multi-Touch Touchpad



Setup
Install pip:



$ sudo apt-get install python-pip python-dev build-essential 
$ sudo pip install --upgrade pip 
$ sudo pip install --upgrade virtualenv 
Install evdev module:

$ sudo pip install evdev
Check input events:

$ ls /dev/input
If you do not know which event is mouse or keyboard, follow this.
Install input-utils:

$ sudo apt-get install input-utils
$ lsinput



Python Code

This is the python example code.
Example 1:
from evdev import InputDevice, categorize, ecodes
dev = InputDevice('/dev/input/event0')
print(dev)
for event in dev.read_loop():
             print(event)
Example 2:
from evdev import InputDevice, categorize, ecodes
dev = InputDevice('/dev/input/event0')
print(dev)
for event in dev.read_loop():
             if event.type == ecodes.EV_KEY:
                        print(event)
Crate a python script file as below and copy&paste the above code.
nano test.py
Run the python script.
python test.py
Press any buttons on the keyboard or mouse and you will see the outputs in the terminal.
Done!



                  

Sunday, January 7, 2018

Raspberry PI Car Accessory

Car Accessory Ideas


I am thinking about the next raspberry pi based project for car accessory.

One of the key features are dashcam type of drive recording support.

I think this is very important feature especially when accident like car crash or brek-in happens. 

I have bought the following product from Xiaomi and this one is really good considering the low price.



YI 2.7" Screen Full HD 1080P60 165 Wide Angle Dashboard Camera




Thanks to DASHCAMTALK  site and they provide a great review of this product.

This feature requires video streaming, audio streaming, and storage management.

Can I mimic the similar features of this product using raspberry pi?
My guess is yes, although buying this product would be cheaper than DIY...
Well the point is to enjoy the project and learn something 😊
Luckily I have learned many from the previous blog, and streaming video and audio can be done.
Forget the quality for now... I am sure I can't exceed the actual product quality 😰


I can also add inertial sensors to detect the collision.

Maybe I can use the same sensor I used, BMI160 previous blog or BNO055  previous blog.

This sensor can trigger the recording and also provide the inertial data like angular velocity and acceleration.



Having GPS would also be helpful and I am sure there would be lots of information from all the DIY experts. I need to research that.



Since it is DIY, what else can I add or what else do I want??

Once I thought about message board on my rear window to show the other driver a message.
It is very simple message like "Thank you" or "Sorry" to show my quick response to the driver who gave me the space to change the lane or who got unhappy with my driving behavior.
I think this could be done by raspberry PI as well as long as I can get reasonable power, size, and price LED matrix board.
Maybe something like this work but might require multiple...
I would like to keep 5V power supply so that it is easier for the hardware integration.

Digital Flexible LED Matrix Panel DC5V

Well, it will not be cheap if I need to use 2-3 of these to expand the viewing range.
The another method is may be using HUD type of method using display panel, but this may cost as well. Using magnifying mirror with the small display might be also a candidate, but the viewing angle would be a problem. 
Any rate, maybe using LED matrix panel can be the first route.

I think it is also possible to add motor to rotate the camera angle remotely.
With the above panel, it could be very power hungry device... Need to control well.

Hmm... let's think more later.

Features
Okay now the summary of features.
  • Video and audio recording using "picam + USB microphone" or "USB webcam"
    • Format and resolution are up to the camera
  • Video and audio streaming to smartphone
    • It can be old android phone
  • Video and audio playback from smartphone
  • Detect collision and monitor the inertial data
  • GPS data logging
  • Message board with remote operation support
  • TBD


Planning

I guess first thing is to review the old video and audio streaming project in the previous blog, and implement sensor data and others.
I wonder how long it takes to complete but it would be fun.

Thank you for reading!
I will update this project as I make progress.



                 

Saturday, January 6, 2018

Raspberry PI Stereo Camera

Raspberry PI Stereo Camera


There are more demands on the stereo camera, I think.
I am not computer vision engineer but I hear people asking for the stereo camera.
You can get the USB stereo cameras but not many cameras are available for DIY people.


Then, I wondered if it is possible to do the stereo camera streaming using raspberry pi.
The raspberry pi camera module v2 has Sony image sensor and the performance is so nice.
The raspberry pi site has the comparison chart between camera module v1 and v2.
The camera module v2 supports 8M pixel resolution, 1080p@30, 720p@60, and 640 × 480p@60/@90. As an eye of the stereo camera, it has quite good spec.
Unfortunately, raspberry pi can only supports 1 camera module for now, so the potential solution to develop the stereo camera system is either using something like this.


Thanks to ArduCam and they have great camera products. I have been using there products in my previous blogs here and here.
The above adapter is basically multiplexer and it can easily switch the multiple camera views.
I think it is great module for the camera surveillance projects or my car accessory projects here (in progress now).
However, I would like to try different method for this project which is using 2 raspberry pi and each raspberry pi use the 8MP camera module v2.
The reason why I wanted this is to record the 2 camera views simultaneously and maximize the image resolution and frames.

Using 2 Raspberry PI with Camera Module V2
For the first attempt, I came up with this method.

  1. Add external button and wire the signal to both raspberry pi
  2. Use the hardware interrupt to trigger the recording start event
  3. Launch script to star the stream at the input trigger
    1. The script just waits for camera recording until the input trigger is detected
  4. Offline operation to merge 2 recorded video files


Synchronize the Frames from 2 Raspberry PI and Camera Module V2
There are another trick I did for this test which is to try synchronizing the 2 individual camera timing as much as possible.
What I did was to share the clock signals on the camera module v2.
Fortunately, the camera module use CMOS output clock device to feed the clock signal to the image sensor chip, so it was really easy to wire and share the 1 clock signal to the other.

Result
Here is the result. (Not great quality, sorry...)
The camera was not aligned well, but the shifted image is due to the left and right side camera view angle.



Thank you for reading!
I will update this project as I make progress.