The Smell of Molten Projects in the Morning

Ed Nisley's Blog: Shop notes, electronics, firmware, machinery, 3D printing, laser cuttery, and curiosities. Contents: 100% human thinking, 0% AI slop.

Category: Software

General-purpose computers doing something specific

  • HAL Pin Names for a Bone-Stock USB Mouse

    I’ve always wondered what the LinuxCNC HAL pin names would be for an ordinary mouse, particularly nowadays when an Arduino Leonardo can become a USB HID gadget without much effort at all. If one had a Leonardo and l337 programming skillz, one might receive far more interesting data than just fast-twitch muscle movement…

    Logitech Optical Mouse - LinuxCNC box
    Logitech Optical Mouse – LinuxCNC box

    So. We begin…

    From less /proc/bus/input/devices:

    ... snippage ...
    I: Bus=0003 Vendor=046d Product=c077 Version=0111
    N: Name="Logitech USB Optical Mouse"
    P: Phys=usb-0000:00:1d.0-1/input0
    S: Sysfs=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1:1.0/input/input10
    U: Uniq=
    H: Handlers=mouse3 event10
    B: EV=17
    B: KEY=ff0000 0 0 0 0 0 0 0 0
    B: REL=143
    B: MSC=10
    

    From ll /dev/input:

    ... snippage ...
    crw-r-----   1 root root 13, 74 2013-02-23 07:46 event10
    ... snippage ...
    crw-r-----   1 root root 13, 35 2013-02-23 07:46 mouse3
    

    Manually beat the permissions into shape, because this is a one-off affair:

    sudo chgrp users /dev/input/event10
    sudo chgrp users /dev/input/mouse3
    sudo chmod g+w /dev/input/event10
    sudo chmod g+w /dev/input/mouse3
    

    Find the USB address from lsusb:

    Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 004 Device 006: ID 06f2:0011 Emine Technology Co. KVM Switch Keyboard
    Bus 004 Device 005: ID 046d:c401 Logitech, Inc. TrackMan Marble Wheel
    Bus 004 Device 004: ID 04d9:1203 Holtek Semiconductor, Inc. MC Industries Keyboard
    Bus 004 Device 003: ID 046d:c216 Logitech, Inc. Dual Action Gamepad
    Bus 004 Device 002: ID 0451:2046 Texas Instruments, Inc. TUSB2046 Hub
    Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 002 Device 002: ID 046d:c077 Logitech, Inc.
    Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    

    Query the attributes with udevadm:

    udevadm info --query=all --attribute-walk --name=/dev/bus/usb/002/002
    ... snippage ...
      looking at device '/devices/pci0000:00/0000:00:1d.0/usb2/2-1':
        KERNEL=="2-1"
        SUBSYSTEM=="usb"
        DRIVER=="usb"
        ATTR{configuration}==""
        ATTR{bNumInterfaces}==" 1"
        ATTR{bConfigurationValue}=="1"
        ATTR{bmAttributes}=="a0"
        ATTR{bMaxPower}==" 98mA"
        ATTR{urbnum}=="13"
        ATTR{idVendor}=="046d"
        ATTR{idProduct}=="c077"
        ATTR{bcdDevice}=="6700"
        ATTR{bDeviceClass}=="00"
        ATTR{bDeviceSubClass}=="00"
        ATTR{bDeviceProtocol}=="00"
        ATTR{bNumConfigurations}=="1"
        ATTR{bMaxPacketSize0}=="8"
        ATTR{speed}=="1.5"
        ATTR{busnum}=="2"
        ATTR{devnum}=="2"
        ATTR{version}==" 2.00"
        ATTR{maxchild}=="0"
        ATTR{quirks}=="0x0"
        ATTR{authorized}=="1"
        ATTR{manufacturer}=="Logitech"
        ATTR{product}=="USB Optical Mouse"
    

    Fire up halrun, load hal_input, and dump the pins:

    halrun
    halcmd: loadusr -W hal_input -KRAL Optical
    halcmd: show all
    Loaded HAL Components:
    ID      Type  Name                                      PID   State
         5  User  hal_input                                  1693 ready
         3  User  halcmd1692                                 1692 ready
    
    Component Pins:
    Owner   Type  Dir         Value  Name
         5  bit   OUT         FALSE  input.0.btn-back
         5  bit   OUT          TRUE  input.0.btn-back-not
         5  bit   OUT         FALSE  input.0.btn-extra
         5  bit   OUT          TRUE  input.0.btn-extra-not
         5  bit   OUT         FALSE  input.0.btn-forward
         5  bit   OUT          TRUE  input.0.btn-forward-not
         5  bit   OUT         FALSE  input.0.btn-middle
         5  bit   OUT          TRUE  input.0.btn-middle-not
         5  bit   OUT         FALSE  input.0.btn-mouse
         5  bit   OUT          TRUE  input.0.btn-mouse-not
         5  bit   OUT         FALSE  input.0.btn-right
         5  bit   OUT          TRUE  input.0.btn-right-not
         5  bit   OUT         FALSE  input.0.btn-side
         5  bit   OUT          TRUE  input.0.btn-side-not
         5  bit   OUT         FALSE  input.0.btn-task
         5  bit   OUT          TRUE  input.0.btn-task-not
         5  s32   OUT             0  input.0.rel-hwheel-counts
         5  float OUT             0  input.0.rel-hwheel-position
         5  bit   IN          FALSE  input.0.rel-hwheel-reset
         5  float IN              1  input.0.rel-hwheel-scale
         5  s32   OUT             0  input.0.rel-wheel-counts
         5  float OUT             0  input.0.rel-wheel-position
         5  bit   IN          FALSE  input.0.rel-wheel-reset
         5  float IN              1  input.0.rel-wheel-scale
         5  s32   OUT             0  input.0.rel-x-counts
         5  float OUT             0  input.0.rel-x-position
         5  bit   IN          FALSE  input.0.rel-x-reset
         5  float IN              1  input.0.rel-x-scale
         5  s32   OUT             0  input.0.rel-y-counts
         5  float OUT             0  input.0.rel-y-position
         5  bit   IN          FALSE  input.0.rel-y-reset
         5  float IN              1  input.0.rel-y-scale
    ... snippage ...
    

    Hmmm, that was interesting…

  • Leaked Spam Template

    A wannabe spammer inadvertently sent me a nice comment-spam template:

    {{You must|You need to|You have to|You should} {take advantage of|make the most of|benefit from|take full advantage of} {all the|all of the|each of the|every one of the} software advancements that {happen to be|are actually|are|are generally} {a successful|an effective|an excellent|a prosperous} {Internet marketer|Online marketer|Internet entrepreneur|Affiliate marketer}. {If your|In case your|Should your|When your} work {begins to|starts to|actually starts to} suffer, {the competition|your competition|competition|your competitors} could {leave you|make you|create} {in the|within the|inside the|from the} dust. Show {that you are|that you will be|that you are currently|you are} always {on the|around the|in the|about the} {cutting edge|innovative|leading edge|really advanced}, {and they will|and they can} {learn to|learn how to|figure out how to|discover how to} trust {you and your|both you and your|you and the|your} products.

    Multiplying the number of choices together gives a tidy 4.8×109 different comments, each one heartbreakingly close to making sense.

    It’s now in my spam collection, along with some other nuggets snatched from the Internet’s outfall pipe

  • Poking Around in Upstart

    The hack I added to the lightdm startup script occasionally causes it to hang, which suggests a timing problem. The result leaves the default text-mode login screen active on VT1, after which I can log in and issue sudo lightdm start to produce the usual GUI screen. Using startx doesn’t (seem to) start the display manager, resulting in all manner of weird behavior.

    The definitive Upstart info seems to be in the Upstart Intro, Cookbook, and Best Practises document.

    The stanza I modified looks like this:

    start on ((filesystem
               and runlevel [!06]
               and started dbus
               and (drm-device-added card0 PRIMARY_DEVICE_FOR_DISPLAY=1
                    or stopped udev-fallback-graphics)
               and mounted MOUNTPOINT=/mnt/bulkdata)
              or runlevel PREVLEVEL=S)
    

    According to the timing diagram in section 10.1.8, the filesystem event should happen after all the remote filesystems have been mounted, which seems like that stanza might produce a race condition. So just waiting for the filesystem event should suffice, but it doesn’t; that’s why I had to add the mounted event.

    According to the example in section 11.14, that stanza should probably look like:

    start on ((filesystem
               and (runlevel [!06]
               and (started dbus
               and (drm-device-added card0 PRIMARY_DEVICE_FOR_DISPLAY=1
                    or stopped udev-fallback-graphics))))
              or runlevel PREVLEVEL=S)
    

    The additional parentheses around successive conditions seem to serialize them, so that they need not all occur at the same time.

    At least I think that’s how it should work…

    Unfortunately, it doesn’t.  The good news is that it’s converted the intermittent failure into a hard fault, which is generally a step in the right direction.

    Changing the stanza to:

    #start on ((filesystem
    start on ((mounted MOUNTPOINT=/mnt/bulkdata
               and runlevel [!06]
               and started dbus
               and (drm-device-added card0 PRIMARY_DEVICE_FOR_DISPLAY=1
                    or stopped udev-fallback-graphics))
    #           and mounted MOUNTPOINT=/mnt/bulkdata))
              or runlevel PREVLEVEL=S)
    

    … also fails hard.

    At this point, I have no idea what to do, so I’ve restored the original stanza.

  • Eagle HAL Configuration: Sherline HAL File

    More hal-config.lbr tweakage produced enough HAL blocks to completely define the Sherline CNC mill’s HAL connections, all wired up in a multi-page schematic (Eagle-LinuxCNC-Sherline.zip.odt) that completely replaces all the disparate *.hal files I’d been using, plus a new iteration of the hal-write-2.5.ulp Eagle-to-HAL conversion script.

    The first sheet (clicky for more dots) defines the manually configured userspace and realtime modules:

    Sherline Schematic - 1
    Sherline Schematic – 1

    That sheet has three types of Eagle devices:

    • Generalized LoadRT – devices like trivkins that require only a loadrt line
    • Dedicated LoadRT – devices like motion that require functions connected to a realtime thread
    • Generalized LoadUsr – devices like hal_input with a HAL device, but no function pins

    The device’s NAME field contains either the module name (for the specialized devices with functions) or a generic MODULE for everything else, preceded by an optional index that imposes an ordering on the output lines. The device’s VALUE field contains the text that will become the loadrt or loadusr line in the HAL file. Trailing underscores act as separators, but are discarded by the conversion script.

    The immensely long line is the VALUE field that plugs a bunch of variables from the Sherline.ini file into the motion controller.

    The conversion script doesn’t do anything special for those devices, other than transfer the VALUE field to the HAL file. Ordinary HAL devices, the ones with functions that don’t require any special setup, must appear in the conversion script’s list of device names, so that it can recognize them and deal with their connections.

    That sheet produces this part of the HAL file:

    ####################################################
    # Load realtime and userspace modules
    loadrt trivkins
    loadrt [EMCMOT]EMCMOT key=[EMCMOT]SHMEM_KEY num_joints=[TRAJ]AXES base_period_nsec=[EMCMOT]BASE_PERIOD servo_period_nsec=[EMCMOT]SERVO_PERIOD traj_period_nsec=[EMCMOT]SERVO_PERIOD
    loadrt probe_parport
    loadrt hal_parport cfg="[PARPORT]ADDRESS out"
    loadrt stepgen step_type=0,0,0,0
    loadrt pwmgen output_type=0
    loadusr -W hal_manualtoolchange
    loadusr -W hal_input -KA Dual
    loadrt logic count=1 personality=0x104
    

    The conversion script counts the other schematic devices and automagically produces these lines to load their corresponding modules:

    loadrt constant		count=13
    loadrt and2		count=17
    loadrt conv_float_s32		count=1
    loadrt flipflop		count=4
    loadrt mux2		count=5
    loadrt mux4		count=1
    loadrt not		count=8
    loadrt or2		count=14
    loadrt scale		count=7
    loadrt timedelay		count=1
    loadrt toggle		count=1
    

    Next, the parallel port configuration, which uses the D525’s system board hardware:

    Sherline Schematic - 2
    Sherline Schematic – 2

    The stepconf configuration utility buries the parallel port configuration values in the default HAL file as magic numbers. I moved them to a new stanza in the INI file, although the syntax may not be robust enough to support multiple cards, ports, and configurations. This, however, works for now:

    [PARPORT]
    ADDRESS = 0x378
    RESET_TIME = 10000
    STEPLEN = 25000
    STEPSPACE = 25000
    DIRSETUP = 50000
    DIRHOLD = 50000
    

    That LOGIC block is new and serves as an AND gate that produces a combined enable signal for the parallel port. The stepconf utility uses the X axis enable signal, but, seeing as how the Sherline controller doesn’t use the result, none of that matters on my system.

    The tool height probe and manual tool change wiring:

    Sherline Schematic - 3
    Sherline Schematic – 3

    I’m not convinced the Emergency Stop polarity is correct, but it matches what was in the original HAL file. As before, the Sherline driver box ignores that output, so none of that matters right now.

    Four very similar pages define the XYZA step-and-direction generators. This is the X axis driver:

    Sherline Schematic - 4
    Sherline Schematic – 4

    You can imagine what the next three pages for the YZA logic look like, right? There are also a few blank pages in the schematic, so the numbers jump abruptly.

    The magic part of this is having Eagle manage all the tedious renumbering and counting. If you remember to adjust the name of the first module from, say, AXIS.1 to AXIS.0, then the rest get the proper numbers as you go along.

    The remainder of the schematic implements the Joggy Thing’s logic, much as described there. I discovered, quite the hard way, that copy-and-pasting an entire schematic from elsewhere does horrible things to the device numbering, but I’m not sure how to combine two schematics to limit the damage. In any event, manually adjusting a few pages wasn’t the worst thing I’ve ever had to do; starting with a unified schematic should eliminate that task in the future.

    The miscellaneous buttons:

    Sherline Schematic - 11
    Sherline Schematic – 11

    The joystick and hat values:

    Sherline Schematic - 12
    Sherline Schematic – 12

    The joystick deadband logic now uses the (new with HAL 2.5, I think) input.n.abs-x-flat pins, which eliminated a tangle of window comparator logic.

    The jog speed adjustment logic that sets the fast and crawl speeds:

    Sherline Schematic - 13
    Sherline Schematic – 13

    I should probably put the speed ratios in the INI file, but that’s in the nature of fine tuning.

    The lockout logic that remembers which axis started moving first on a given joystick and locks out the other axis, which greatly simplifies jogging up to an edge without bashing into something else:

    Sherline Schematic - 14
    Sherline Schematic – 14

    Combine all those signals into values that actually tell HAL to jog the axes:

    Sherline Schematic - 15
    Sherline Schematic – 15

    The last page connects all the realtime function pins to the appropriate threads:

    Sherline Schematic - 16
    Sherline Schematic – 16

    The LinuxCNC documentation diverges slightly from the implementation, but a few iterations resolved all the conflicts and had the additional benefit that I had to carefully think through what was actually going on.

    A deep and sincere tip o’ the cycling helmet to the folks making LinuxCNC happen!

    Although the Sherline mill doesn’t have more than a few minutes of power-on time with the new HAL file, the Joggy Thing behaves as it used to and the axes move correctly, so I think the schematic came out pretty close to the original HAL file.

    The next step: draw a new schematic to bring up and exercise a different set of steppers…

  • Eagle HAL Configuration: Nostromo N52 Controller

    Combining some of the pin names generated by hal_input with the recipe for creating HAL devices, here’s a test configuration that hitches an old Nostromo N52 controller to a LinuxCNC system (clicky for more dots):

    Nostromo N52 Controller - HAL config
    Nostromo N52 Controller – HAL config

    The F01 key lights the red LED, the Orange Button lights the green LED, and a oneshot timer pulses the blue LED for half a second after the Button closes. The Thread block defines the connections from the functions to the main timing routine, and the loadrt block defines the thread timing. The hal_input module takes care of its own input sampling in userspace.

    Now, for the classic embedded system “Hello, world!” test:

    Nostromo N52 Controller - F01 test
    Nostromo N52 Controller – F01 test

    It’s amazing how good an LED can make you feel…

    A halscope shot shows the timing relation between the Orange Button (confusingly hitched to the greenkey signal) and the oneshot pulse:

    HalScope - oneshot triggering
    HalScope – oneshot triggering

    That schematic produces this HAL configuration file:

    # HAL config file automatically generated by Eagle-CAD ULP:
    # [/mnt/bulkdata/Project Files/eagle/ulp/hal-write-2.5.ulp]
    # (C) Martin Schoeneck.de 2008
    # Charalampos Alexopoulos 2011
    # Mods Ed Nisley KE4ZNU 2010 2013
    # Path        [/mnt/bulkdata/Project Files/eagle/projects/LinuxCNC HAL Configuration/]
    # ProjectName [Nostromo]
    # File name   [/mnt/bulkdata/Project Files/eagle/projects/LinuxCNC HAL Configuration/Nostromo.hal]
    # Created     [12:28:04 14-Feb-2013]
    
    ####################################################
    # Load realtime and userspace modules
    loadrt threads name1=test-thread period1=1000000
    loadusr -W hal_input -K +Nostromo:0 -KRL +Nostromo:1
    loadrt constant		count=1
    loadrt oneshot		count=1
    
    ####################################################
    # Hook functions into threads
    addf oneshot.0		test-thread
    addf constant.0		test-thread
    
    ####################################################
    # Set parameters
    
    ####################################################
    # Set constants
    setp constant.0.value	0.5
    
    ####################################################
    # Connect Modules with nets
    net bluepulse input.1.led-scrolll oneshot.0.out
    net duration constant.0.out oneshot.0.width
    net greenkey input.0.key-leftalt oneshot.0.in input.1.led-capsl
    net redkey input.0.key-tab input.1.led-numl
    

    A snapshot of the Nostromo.sch, Nostromo.hal, hal_config.lbr, and hal-write-2.5.ulp files is in Nostromo-N52.zip.odt. Rename it to get rid of the ODT suffix, unzip it, and there you go.

  • Conficker vs. Library: The Rest of the Story

    Well, here’s how the story of picking up Conficker at the library played out:

    Yes, thank you so much! Everything you said was true. Apparently someone’s USB drive was infected and infected many computers here. We are very appreciative for your technological detective work. The head of IT was very incredulous because everything is deep frozen after it is shut down. But it was all true and I am very grateful

    The part about “many computers here” seems worrisome; they’re apparently not running any defensive software at all.

    ‘Nuff said…

  • Monthly Science: Patio Ground Temperature

    The patio at the rear of our house is a six inch concrete slab with a five foot “crawlspace” below it, with a HOBO datalogger pendant in the dirt near the pole at the middle of the I beam supporting the slab. The pendant is very close to the surface, so there’s a big diurnal temperature variation, but it still gives a reasonable picture of seasonal change.

    A configuration setting in Hoboware determines whether it stores / exports dates with years having two digits or four digits. As you might expect, over the course of five years the dates have both formats, but there’s always a blank separating the date and the time:

    grep "/12 " /tmp/data.csv | head -1
    1,11/09/12 08:45:00 ,49.050,,,,
    grep "/2012 " /tmp/data.csv | head -1
    2235,01/01/2012 00:00:00,44.560,0.0,,,,
    

    This burst of sed regex line noise normalizes all years to four digits:

    sed 's/\/\([0-9][0-9]\) /\/20\1 /' whatever.csv
    

    The parenthesized subexpression matches the digits of a two digit year preceding a blank, then the \1 plugs it into the right spot in the output. This suffers from the usual failure when the century rolls over, but frankly, my dear readers, I don’t give a damn. The backslashes escape forward slashes and parentheses, in addition to making the regex pretty much write-only.

    The plot shows the expected annual variation:

    Under patio - Ground - Center
    Under patio – Ground – Center

    The periodic upward spikes happen when I carry the logger to the Token Windows Laptop and read it out; the air temperature upstairs is always warmer than the dirt under the patio.

    The Bash and gnuplot script that produced the graph:

    #!/bin/sh
    #-- overhead
    export GDFONTPATH="/usr/share/fonts/truetype/"
    base="${1%.*}"
    echo Base name: ${base}
    ofile=${base}.png
    tfile=$(tempfile)
    echo Input file: $1
    echo Temporary file: ${tfile}
    echo Output file: ${ofile}
    #-- prepare csv Hobo logger file
    sed 's/^\"/#&/' "$1" | sed 's/^.*Logged/#&/' | sed 's/ ,/,/' | sed 's/\/\([0-9][0-9]\) /\/20\1 /' > ${tfile}
    #-- do it
    gnuplot << EOF
    #set term x11
    set term png font "arialbd.ttf" 18 size 950,600
    set output "${ofile}"
    set title "${base}"
    set key noautotitles
    unset mouse
    set bmargin 4
    set grid xtics ytics
    set timefmt "%m/%d/%Y %H:%M:%S"
    set xdata time
    set xlabel "Date"
    set format x "%Y-%m"
    #set xrange [1.8:2.2]
    set xtics font "arial,12"
    #set mxtics 2
    #set logscale y
    #set ytics nomirror autofreq
    set ylabel "Temperature - F"
    #set format y "%4.0f"
    #set yrange [30:90]
    #set mytics 2
    #set y2label "right side variable"
    #set y2tics nomirror autofreq 2
    #set format y2 "%3.0f"
    #set y2range [0:200]
    #set y2tics 32
    #set rmargin 9
    set datafile separator ","
    #set label 1 "label text" at 2.100,110 right font "arialbd,18"
    #set arrow from 2.100,110 to 2.105,103 lt 1 lw 2 lc 0
    plot	\
        "${tfile}" using 2:3 with lines lt 3
    EOF