Dr. Lawlor's Code, Robots, & Things

April 9, 2014

NightWeaver: a trivial live HTML editor

Filed under: Random Thoughts — Dr. Lawlor @ 11:38 am

One of my fellow professors is teaching an introduction to HTML in his CS 101 course, and I felt like he could use a simpler interface for students to get their first taste of HTML web programming.  So I built this trivial two-column editor:


Everything runs on the client side using a half dozen lines of quick and dirty javascript (basically just output.innerHTML = textarea.value).  It’s conceptually similar to my server-side C++ and assembly interface NetRun.  It’s called “NightWeaver” since I wrote it in one night, and is not related to Adobe Dreamweaver.

April 8, 2014

OpenCV: fix for V4L VIDIOC_S_CROP error

Filed under: Random Thoughts — Dr. Lawlor @ 5:00 pm

I’ve been playing with the excellent little OpenCV library ArUco for detecting the 3D position and orientation of printed QR-code style “markers” in live video feed from a webcam, but I kept getting this annoying error now and then:

VIDIOC_STREAMON: Invalid argument
VIDIOC_STREAMON: Invalid argument

What’s weird about this is it wouldn’t happen every time, normally things would work fine until this started, and then it’d keep happening until I unpluged and re-plugged the USB camera.  

Turns out, this happens when you press ctrl-C (interrupt) during the middle of a video capture.  Evidently this screws up something in the kernel (dmesg shows “xhci_hcd 0000:00:14.0: ERROR Transfer event for disabled endpoint or incorrect stream ring”), preventing further video streaming from that device.  Operationally it’s quite annoying, and for an autonomous robot, you can’t easily unplug and replug just to get things working again.  

A simple workaround is to catch SIGINT, the signal generated by ctrl-C, and delay shutdown until after the next video frame has fully arrived:

/* Keep the webcam from locking up when you interrupt a frame capture */
volatile int quit_signal=0;
#ifdef __unix__
#include <signal.h>
extern "C" void quit_signal_handler(int signum) {
if (quit_signal!=0) exit(0); // just exit already
printf("Will quit at next camera frame (repeat to kill now)\n");

... in main setup ...
#ifdef __unix__
signal(SIGINT,quit_signal_handler); // listen for ctrl-C
... in video capture loop ...
yourCaptureDevice >> videoImage;
if (quit_signal) exit(0); // exit cleanly on interrupt

My webcam now works reliably, even when I ctrl-C out of the analysis program!   (I’m running Ubuntu 13.11 and OpenCV 2.4.1, but this may be a bug in the Linux kernel 3.11.)

Create a free website or blog at WordPress.com.