Advertisements

Archive for category Software

Tour Easy Front Derailleur Cable Clamp

In addition to sawing through the side of the cable ferrule, the front derailleur cable began breaking at the edge of the derailleur arm:

Tour Easy Front Derailleur Cable - frayed

Tour Easy Front Derailleur Cable – frayed

It wouldn’t have survived another ride!

Dan pointed out CNC machined aluminum cable clamps are a thing, but those are sized for larger frame tubes than the 1.0 inch steel used on our Tour Easy ‘bents and, although I’ve shimmed everything else on the frame, I wanted to tweak the cable angle to match the arm on the derailleur.

A bit of OpenSCAD wrangling produces a likely candidate:

Front Derailleur Cable Clamp - Slic3r

Front Derailleur Cable Clamp – Slic3r

That’s a bulked-up revision of the prototype:

Tour Easy Front Derailleur Cable Clamp - installed

Tour Easy Front Derailleur Cable Clamp – installed

Done up in orange PETG, it demonstrated the idea worked, but two perimeter threads wrapped around 15% infill isn’t quite up to the task. Note the split along the screw on the far half and various irregularities around the ferrule.

The cable angle isn’t quite right, either, as the proper compound angle would, alas, aim the cable into the pedal crank. The bulky bushings get in the way of putting the ferrule where it should be with the screws aligned in a tidy manner, so I must get used to the jaunty angle.

The bulkier version, done with 50% infill and four perimeter threads, has the same tilt angle, but the ferrule sits further from the screws:

Tour Easy Front Derailleur Cable Clamp V2 - rear quarter view

Tour Easy Front Derailleur Cable Clamp V2 – rear quarter view

The view from the left side shows the cable angles slightly to the rear, but the smaller angle should make it happier:

Tour Easy Front Derailleur Cable Clamp V2 - side view

Tour Easy Front Derailleur Cable Clamp V2 – side view

Probably should have used black PETG. Next time, for sure!

The OpenSCAD source code as a GitHub Gist:

Advertisements

, ,

Leave a comment

Siglent SDS2304X Screen Shot File

Poking the Print button on the front of the Siglent SDS2304X scope saves the screen to a BMP file (in the /BMP directory) on a USB flash drive plugged into its front-panel port:

Siglent SDS2304X Front Panel - Print Button - USB port

Siglent SDS2304X Front Panel – Print Button – USB port

Which produces files like these:

ll --block-size=1 /path-to-USB-stick/BMP/
total 2318336
drwxr-xr-x 2 ed ed    4096 May 23 13:13 ./
drwxr-xr-x 4 ed ed    4096 Dec 31  1969 ../
-rw-r--r-- 1 ed ed 1152054 May 23 13:13 SDS00001.BMP
-rw-r--r-- 1 ed ed 1152054 May 23 13:13 SDS00002.BMP

The files are 1152054 bytes long, as specified by the BMP header inside the file:

hexdump -C /path-to-USB-stick/BMP/SDS00001.BMP | head
00000000  42 4d 36 94 11 00 00 00  00 00 36 00 00 00 28 00  |BM6.......6...(.|
00000010  00 00 20 03 00 00 e0 01  00 00 01 00 18 00 00 00  |.. .............|
00000020  00 00 00 94 11 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 01 01  01 01 01 01 01 01 01 01  |................|
00000040  01 01 01 01 01 01 01 01  01 01 01 01 01 01 01 01  |................|
*
00000880  01 01 01 01 01 01 01 01  01 01 01 01 01 01 1e 1e  |................|
00000890  1e 1e 1e 1e 1e 1e 1e 1e  1e 1e 1e 1e 1e 1e 1e 1e  |................|
*
00000990  1e 1e 1e 1e 1e 1e 01 01  01 01 01 01 01 01 01 01  |................|

The first 14 bytes contain the Bitmap file header, with the file size in Little-Endian order in the four bytes at offset +0x02: 0x00119436 = 1152054.

The four bytes at offset +0x0A give the offset of the pixel data: +0x36. That’s the series of 0x01 bytes in the fourth row. Unlike most images, BMP pixel arrays start at the lower left corner of the image and proceed rightward / upward to the last pixel at the upper right corner.

The data between the Bitmap file header and the start of the pixel data contains at least a Device Independent Bitmap header, identified by its length in the first four bytes at offset +0x0E. In this case, the length of 0x28 = 40 bytes makes it a Windows (no surprise) header.

The two bytes at +1C give the bits-per-pixel value: 0x18 = 24 = 3 bytes/pixel, so parse the pixels in RGB order.

The four bytes at +0x12 give the bitmap width in pixels: 0x320 = 800. Each pixel row must be a multiple of 4 bytes long, which works out fine at 2400 bytes.

The tail end of the file shows one dark pixel at the upper right:

hexdump -C /path-to-USB-stick/BMP/SDS00001.BMP | tail
00118330  00 cc 00 00 cc 00 00 cc  00 00 cc 00 00 cc 00 00  |................|
00118340  cc 00 00 cc 00 00 cc 00  00 cc 00 00 cc 00 00 cc  |................|
00118350  00 00 cc 00 00 cc 00 00  cc 0f 0f 75 1e 1e 1e 1e  |...........u....|
00118360  1e 1e 1e 1e 1e 1e 1e 1e  1e 1e 1e 1e 1e 1e 1e 1e  |................|
*
00118ad0  1e 1e 1e 01 01 01 1e 1e  1e 1e 1e 1e 1e 1e 1e 1e  |................|
00118ae0  1e 1e 1e 1e 1e 1e 1e 1e  1e 1e 1e 1e 1e 1e 1e 1e  |................|
*
00119430  1e 1e 1e 01 01 01                                 |......|

Which looks like this, expanded by a factor of eight (clicky for more dots to reveal the situation):

Screenshot - upper right corner - 8x expansion

Screenshot – upper right corner – 8x expansion

The scope can also transfer a screenshot over the network:

lxi screenshot -a 192.168.1.42 /tmp/lxi-shot.bmp 
Loaded siglent-sds screenshot plugin
Saved screenshot image to /tmp/lxi-shot.bmp

Which has the same header:

hexdump -C /tmp/lxi.bmp | head
00000000  42 4d 36 94 11 00 00 00  00 00 36 00 00 00 28 00  |BM6.......6...(.|
00000010  00 00 20 03 00 00 e0 01  00 00 01 00 18 00 00 00  |.. .............|
00000020  00 00 00 94 11 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 01 01  01 01 01 01 01 01 01 01  |................|
00000040  01 01 01 01 01 01 01 01  01 01 01 01 01 01 01 01  |................|
*
00000880  01 01 01 01 01 01 01 01  01 01 01 01 01 01 1e 1e  |................|
00000890  1e 1e 1e 1e 1e 1e 1e 1e  1e 1e 1e 1e 1e 1e 1e 1e  |................|
*
00000990  1e 1e 1e 1e 1e 1e 01 01  01 01 01 01 01 01 01 01  |................|

But the resulting file is three bytes = one pixel (!) too large:

ll --block-size=1 /tmp/lxi.bmp
-rw-rw-r-- 1 ed ed 1152057 May 23 19:09 /tmp/lxi.bmp

The tail end of the file:

hexdump -C /tmp/lxi.bmp | tail
00118330  00 cc 00 00 cc 00 00 cc  00 00 cc 00 00 cc 00 00  |................|
00118340  cc 00 00 cc 00 00 cc 00  00 cc 00 00 cc 00 00 cc  |................|
00118350  00 00 cc 00 00 cc 00 00  cc 0f 0f 75 1e 1e 1e 1e  |...........u....|
00118360  1e 1e 1e 1e 1e 1e 1e 1e  1e 1e 1e 1e 1e 1e 1e 1e  |................|
*
00118ad0  1e 1e 1e 01 01 01 1e 1e  1e 1e 1e 1e 1e 1e 1e 1e  |................|
00118ae0  1e 1e 1e 1e 1e 1e 1e 1e  1e 1e 1e 1e 1e 1e 1e 1e  |................|
*
00119430  1e 1e 1e 01 01 01 01 01  0a                       |.........|

Because the file header doesn’t include those three bytes, they don’t go into the image and the resulting screenshot is visually the same.

Which looks like a picket-fence error, doesn’t it? I’d lay long odds the erroneous loop runs from 0 to NUMPIXELS, rather than 0 to NUMPIXELS-1. Raise your hand if you’ve ever made that exact mistake.

I have no practical way to determine whether the error is inside the scope or the LXI network code, but given Siglent’s overall attention to software fit-and-finish, I suspect the former.

One can convert BMP files to the much more compact PNG format:

convert /tmp/lxi.bmp /tmp/lxi.png
convert: length and filesize do not match `/tmp/lxi.bmp' @ warning/bmp.c/ReadBMPImage/829.

Yes. Yes, there is a mismatch.

The space savings is impressive, particularly in light of PNG being a lossless format:

ll /tmp/lxi.*
-rw-rw-r-- 1 ed ed 1.1M May 23 19:09 /tmp/lxi.bmp
-rw-rw-r-- 1 ed ed  14K May 23 19:17 /tmp/lxi.png

You can eliminate the nag by truncating the file:

truncate --size=1152054 /tmp/lxi.bmp

One could wrap it all up in a script:

#!/bin/bash
lxi screenshot -a 192.168.1.42 /tmp/"$1".bmp
truncate --size=1152054 /tmp/"$1".bmp
convert /tmp/"$1".bmp "$1".png
echo Screenshot: "$1".png

And then It Just Works:

getsds2304x.sh "Test Shot Starfish"
Loaded siglent-sds screenshot plugin
Saved screenshot image to /tmp/Test Shot Starfish.bmp
Screenshot: Test Shot Starfish.png
Test Shot Starfish

Test Shot Starfish

SpaceX uses Test Shot Starfish tracks for pre-launch background music; the actual test shot was spectacular.

,

2 Comments

LXI-Tools for Siglent SDS2304X Oscilloscope and SDM3045X Multimeter

For whatever reason, my Siglent SDS2304X Oscilloscope and SDM3045X Multimeter partially implement their documented command sets through partial implementations of the VXI instrumentation driver network protocol. The Linux command-line side comes from lxi-tools, which one must fetch from its repository and compile from source(do liblxi first, then lxi-tools)  through the usual ./configure - make - sudo make install process, after tediously installing whatever dependencies might be revealed by incremental progress through the configuration(s) on your system(s).

The alternative, of course, is Labview on Windows.

The SDS2304X scope doesn’t respond to the LXI discover broadcast, so you must know and specify its IP address in the command. It’s easiest to configure the Siglent instruments at fixed IP addresses and be done with it:

lxi scpi -a 192.168.1.41 "*idn?"
Siglent Technologies,SDM3045X,SDM34whatever,5.01.01.03
lxi scpi -a 192.168.1.42 "*idn?"
*IDN SIGLENT,SDS2304X,SDS2Xwhatever,1.2.2.2 R10

Although the LXI tools also come in a Snap package, installing them that way prevents storing files outside the user’s home directory; having evolved a fairly extensive NFS filesystem, Snaps seem basically useless for my purposes. I don’t see much more security exposure from downloading and running a Snap than from downloading, compiling, and running the source code, but they obviously know what’s best for me.

,

3 Comments

MPCNC: Tweaked GRBL Config

These GRBL configuration constants seem to work well with the DW660 router in the MPCNC gantry:

The overall XY travel is slightly smaller than the initial configuration, because the router sticks out further than the penholder I’d been using. Increasing the $27 Homing Pulloff distance to 3 mm leaves a comfortable space beyond the limit switches after homing to the positive end:

MPCNC - X-axis endstop - home

MPCNC – X-axis endstop – home

Adjusting the $13[01] XY travel distances and switch positions on the other end of the rail leaves a similar comfort zone at the negative end:

MPCNC - X-axis endstop - X min

MPCNC – X-axis endstop – X min

Both switches now live on the rear X-axis rail and appear as seen from behind the bench; they just look backwards. The Y-axis switches are on the left rail and look exactly the same.

The XY travel works out to 630 × 460 mm = 24.8 × 18.1 inch, which is Good Enough.

Some fiddling with the Z axis limit switch tape mask produces a nice round 100 mm = 3.9 inch vertical travel. The Z-axis rails just barely clear the table at the lower limit and just barely stay in the bottom bearings at the upper limit, so it’s a near thing. In practical terms, the rails or the tool will smash into the workpiece sitting atop the table before the limit switch trips.

Setting both $20=1 Soft Limits and $21=1 Hard Limits may be excessive, but I vastly prefer having the firmware detect out-of-range moves and the hardware forcibly shut down if the firmware loses track of its position, rather than letting it grind away until I can slap the BRS. The steppers aren’t powerful enough to damage anything, of course, so it’s a matter of principle.

The $N0=F150 sets the initial speed, as the default F0 seems to (sometimes) confuse bCNC’s auto-level grid probing.

The $N1=G10L2P1X-633Y-463Z-3 sets the default G54 coordinate origin to the front-left corner, with Z=0 at the home position up top, so as to prevent surprises. I expect to use G55 for most work holder touchoffs, although we’ll see how that plays out.

The G28 and G30 settings depend on the tool change location and the Z-axis probe location, so they’re still not cast in concrete.

Leave a comment

Teensy 3.6 USB Serial Startup

The Arduino Serial doc says the USB hardware on the (now obsolescent) Leonardo requires a test-for-open before using the serial port:

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }
}

As it happens, you must also use that test on the ARM-based Teensy 3.6.

The gotcha happens when the USB port doesn’t become available, in which case the conditional remains true and the loop continues forever, which is precisely what happened when I powered the Teensy from a USB battery pack on the Squidwrench Operating Table.

After some flailing around, this startup snippet falls through after ahem awhile:

#define BUILTIN_LED 13

... snippage ...

Serial.begin(115200);

int waited = 0;
while (!Serial && waited < 3000) {
  delay(1);
  waited++;
  if (! (waited % 50))
    FlipPin(BUILTIN_LED);
}

... snippage ...

Serial.printf(" serial wait: %d ms\n\n",waited);

The serial startup delay seems to vary unpredictably between 800 and 1800 ms, so 3000 ms may be too short:

serial wait: 1033 ms
serial wait: 899 ms
serial wait: 907 ms

The ARM Teensy connects the board's built-in LED to the same SPI clock as on the AVR Arduinos, so it's only useful during startup, but having some hint will come in handy the next time it jams for another reason.

,

3 Comments

FM DDS: SPI Mock 2

Doing the DDS calculations in full-frontal double floating point turns out to be maybe fast enough:

DDS Mock - 0 VAC - SPI

DDS Mock – 0 VAC – SPI

I set the ADC to HIGH_SPEED conversion and sampling, reducing the time between the start of conversion (first pulse in D1) and the ADC end-of-conversion interrupt (rising edge in D2) from 4.7 μs to 2.6 μs, more-or-less, kinda-sorta.

The ADC hardware can return the average of several sample taken in quick succession, so I set it to average four samples. The vertical cursors show the combination of fast conversion and averaging requires 7 μs (-ish) from start to finish: long enough to justify separating the two by an interrupt and short enough to allow calculations after fetching the result.

The purple trace shows the analog input voltage hovering close to a constant VCC/2 (about 1.6+ V), rather than the sine-wave I used earlier, again courtesy of the scope’s arbitrary function generator. The loop() dumps the min and max ADC values (minus half the ADC range (4096/2= 2048):

    -4 to     2
    -3 to     2
    -3 to     2

A span of half a dozen counts = 3 bits means the 12 bit ADC really delivers 9 bits = 0.2% resolution = 54 dB dynamic range = probably not good enough. However, the “circuit” is an open-air hairball on the bench, driven from the scope’s arbitrary waveform generator in high-Z mode, so things can only get better with more any attention to detail.

The 1.9 μs gap between the first and second burst of SPI clocks contains all the floating-point calculations required to convert an ADC sample to DDS delta-phase bits:

void adc0_isr(void) {

  int Audio;

  digitalWriteFast(PIN_ANALOG,HIGH);

  AnalogSample = adc->readSingle();             	  // fetch just-finished sample
  Audio = AnalogSample - 2048;                      // convert to AC signal

  DDSBuffer.Phase = 0;

  SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
  digitalWriteFast(PIN_DDS_FQUD, LOW);

  SPI.transfer(DDSBuffer.Phase);

  DDSBuffer.DeltaPhase = (uint32_t)((((double)Audio / 2048.0) * Deviation + Crystal) * CountPerHertz);

  SPI.transfer((uint8_t)(DDSBuffer.DeltaPhase >> 24));      // MSB first!

  if (Audio > AudioMax)                                     // ignore race conditions
    AudioMax = Audio;
  if (Audio < AudioMin) AudioMin = Audio; SPI.transfer((uint8_t)(DDSBuffer.DeltaPhase >> 16));

  SPI.transfer((uint8_t)(DDSBuffer.DeltaPhase >>  8));
  SPI.transfer((uint8_t)DDSBuffer.DeltaPhase);

  SPI.endTransaction();                         // do not raise FQ_UD until next timer tick!

  digitalWriteFast(PIN_ANALOG,LOW);
}

A closer look lets the scope decode and present the SPI data:

DDS Mock - 0 VAC - SPI detail

DDS Mock – 0 VAC – SPI detail

The program calculates and displays various “constants” I set for convenience:

FM Modulated DDS
Ed Nisley KE4ZNU
 serial wait: 890 ms

DDS clock:     180000000.000 Hz
CountPerHertz:        23.861 ct
HertzPerCount:         0.042 Hz

Crystal:    20000000.000 Hz
Deviation:      5000.000 Hz

You can confirm the SPI data by working backwards with a calculator:

  • DDS delta-phase register bytes: 1C 71 C6 E2 = 477218530 decimal
  • Multiply by 180 MHz / 2^32 to get frequency: 1999997.5506 Hz
  • Subtract nominal 20.0 MHz crystal to get modulation: -2.4494 Hz
  • Divide by nominal 5.0 kHz deviation to get fractional modulation: -4.89.9e-6
  • Multiply by half the ADC range (4096/2) to get ADC counts: -1.003
  • Add 2048 to get the actual ADC sample: 2047

Nicely inside the range of values reported by the main loop, whew.

Which means I can avoid screwing around with fixed-point arithmetic until such time as clawing back a few microseconds makes a meaningful difference.

Now, to begin paying attention to those pesky hardware details …

The TeensyDuino source code as a GitHub Gist:

Leave a comment

FM DDS: Floating Point Timing

Inserting a few simple floating point operations between the SPI transfers provides a quick-n-dirty look at the timings:

Math timing - double ops

Math timing – double ops

The corresponding code runs in the ADC end-of-conversion handler:

void adc0_isr(void) {

  digitalWriteFast(ANALOG_PIN,HIGH);

  AnalogSample = adc->readSingle();                     // fetch just-finished sample

  SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
  digitalWriteFast(DDS_FQUD_PIN, LOW);

  SPI.transfer(DDSBuffer.Phase);                // interleave with FM calculations
  FlipPin(GLITCH_PIN);
  TestFreq += DDSStepFreq;
  FlipPin(GLITCH_PIN);
  SPI.transfer(DDSBuffer.Bits31_24);
  TestFreq -= DDSStepFreq;
  SPI.transfer(DDSBuffer.Bits23_16);
  TestFreq *= DDSStepFreq;
  SPI.transfer(DDSBuffer.Bits15_8);
  FlipPin(GLITCH_PIN);
  TestFreq /= DDSStepFreq;
  FlipPin(GLITCH_PIN);
  SPI.transfer(DDSBuffer.Bits7_0);
  SPI.endTransaction();                         // do not raise FQ_UD until next timer tick!

  digitalWriteFast(ANALOG_PIN,LOW);
}

The FlipPin() function twiddling the output bit takes a surprising amount of time, as shown by the first two gaps in the blocks of SPI clocks (D4). Some cursor fiddling on a zoomed scale says 300 ns = 50-ish cycles for each call. In round numbers, actual code doing useful work will take longer than that.

Double precision floating add / subtract / multiply seem to take about 600 ns. That’s entirely survivable if you don’t get carried away.

Double precision division, on the other paw, eats up 3 μs = 3000 ns, so it’s not something you want to casually plunk into an interrupt handler required to finish before the next audio sample arrives in 20 μs.

Overall, the CPU utilization seems way too high for comfort, mostly due to the SPI transfers, even without any computation. I must study the SPI-by-DMA examples to see if it’s a win.

,

Leave a comment