meetup tonight

I’ll be giving another lightning talk this evening downtown at the San Diego JS meetup. I get to talk about the Autonomous Tank project and the code behind that.

SDJS

Since I no longer have a corporate-sponsored license of Microsoft PowerPoint, I had to improvise for my overhead slideshow this time. So I did what most coders would do in this scenario: I coded something for the task.

Screen Shot 2018-10-02 at 2.45.50 PM

tanks a lot

I decided to build a very cool-looking robotic tank kit which is made by OSEPP. They have a variety of grown-up toys like this in the geekspace.

I guess I’ve been inspired lately by some of the local meetups which involve races with autonomously-driven cars.

To build this, I find that a surprising amount of hardware is going into this project as well as several programming languages all at once. I’ve had to bounce back and forth between Python and C as I interface the Raspberry Pi Zero W computer with the Arduino Mega 2560 R3 Plus board. This Arduino doesn’t come with Bluetooth, wi-fi or even an Ethernet jack so I opted to add in the Pi since it’s inexpensive and comes with a full operative system. The Pi of course includes a webcam for initially allowing the remote control features to be easy. Later, that same camera will be used to generate images to be processed for autonomous driving.

DSC_0072

Repository

Update:

I decided to design some plastic parts for the tank. It’s now looking awesome, has some quick-release pins and I’ve purchased a 12-battery AA charger and batteries for the project since it seems to be hungry for power.

DSC_0073

DSC_0074

Screen Shot 2018-09-12 at 2.00.44 PM

The first three attempts at managing the tracks for steering didn’t seem accurate enough for my own driving-related expectations at least. I finally had to resort to trigonometry in the last set of calculations; this appears to be a more natural steerage interface.

It looks like the first two phases of the project are now complete and I’m well into the third (autonomous) phase now.

Autonomous (Self-Driving) Mode

Next up is the part where a service is taking snapshots from the camera and then using this to make steering decisions. The strategy here is to use data image processing to find the road, so to speak, our position relative to the path ahead as well as any competitors also on the track.

The first interesting piece of the data processing involves some linear algebra and a variety of matrices which perform distinct functions, if you will. You basically multiply a particular matrix for a 3×3 array of pixels to replace the center pixel’s color in each case. The first and most useful matrix is named findEdgesKernel and looks like this:

-1 -1 -1
-1  8 -1
-1 -1 -1

This will allow a new, simpler image which should highlight only the track (masking tape) for the path ahead. This part is working quite well so the next step is to process this resulting path image to determine how the tank should steer both now and in the near future.

Screen Shot 2018-09-12 at 2.03.10 PM

understanding blockchain

Bitcoin is a relatively new phenomenon, getting its start only nine years ago. Since that time, it is now enjoys a market cap of about US$150B at the time of this post. That’s an admittedly-large sum of money for something that’s completely digital and you can’t hold in your hand at the end of the day. I guess it shares the same status then with stocks, bonds, mutual funds and other trading instruments that are a bit ethereal, if you ask me. That said, though, it has come a long way since its inception and there are a thousand or so copycats in one form or another. And yet, does the average person know anything about the underlying code and how it’s implemented?

I thought I’d put together a collection of terms to shed some light on the underlying technology to make it easier to understand.

Node

A node is a single identifiable unit in a variety of storage systems. It often includes a unique ID of some kind to make it distinguishable from its neighboring data. A node could be as simple as the following:

1,This is the data part
2,And this is more data

Linked List

A list is usually something like a comma-separated list of data. A linked list is one in which each new bit of data points back to the previous one in some way. And there are even varieties of this in which the new data points backward and the existing data points forward as well. Here is another example which includes backward-facing pointers:

{ID=1,Data="This is the data part",Previous=0},
{ID=2,Data="And this is more data",Previous=1}

Hash

In the cryptographic world, a hash is usually a large number which was generated from a collection of data. You should assume that the same collection of data as seen later would continue to produce the exact same hash as an output. Furthermore, no other collection of data should produce the same hash. Here’s another example with short versions of hashes included:

{ID=1,Data="This is the data part",Hash="5a285ab1945",PrevHash="00000000000",PrevID=0},
{ID=2,Data="And this is more data",Hash="b6f7f023f02",PrevHash="5a285ab1945",PrevID=1}

This storing-of-the-previous-hash-value in each record is important to prevent any tampering by someone else.

Digital Wallet

Now that you know what a hash is, it’s not too difficult to imagine that a digital wallet (financial account) is represented by one of these as well.

Transaction Ledger

A financial ledger in the form of two paper-based books is something that accountants used to fill out to maintain a company’s finances. A ledger is a history of all the adding and subtracting for the different accounts.

If the data part in our examples above actually represent those +/- activities and correctly identify a pair of financial accounts, then this might be an excellent way of publicly keeping a record of how much different accounts hold in some form of currency.

Block Number

Now that you know what a hash is, that unique identifier/number for each node or block of data is yet another hash number.

Merkle Tree

Since a block can store not one but several transactions in it, the method of storing the data is important. A merkle tree is a way of storing data with individual hashes on each branch plus combined hashes as well at the point of intersection of those branches.

So as we described above, a block can have multiple transactions stored in it. Each individual transaction would include a hash and the accumulation of transactions also gets a combined hash for the sake of safety.

Miners

A Bitcoin miner is a computer whose job it is to create and add blocks to this public ledger. More accurately, though, a miner is usually a single graphics card in a computer which has been setup to mine coins. It requires a fair bit of processing power to do what should be a simple task.

Nonce

A nonce is usually an integer and in our context, it means a special integer which—when added to the contents of a block—produces a hash which begins with four zeroes. As you might have guessed, it requires a bit of work to produce a working hash for a block of transactions and this is the “work” which Bitcoin miners do, for example.

When the correct nonce is guessed and a valid hash produced, it is published along with the block. If this is unique work and was done first then the miner might be rewarded with an amount of digital coin.

Blockchain

A blockchain, then, is a public, distributed ledger of groups of transactions stored in blocks. Each block has been assigned a hash which was programmatically difficult to produce. Each block includes information about the previous block in the chain, making it nearly impossible to alter after-the-fact.

A blockchain allows a digital wallet (as identified by a hash) to have a balance which is the collection of +/- transactions into that account.

 

add comments to a gcode file

I’ve just written a new command-line tool (CLI), this time in NodeJS/JavaScript but as usual, it’s open-source. The program will create a new version of your 3D printer’s GCODE file, adding comments along the way which describe what each command does.

repository

I would suggest that it’s best to install it somewhere in your path and then you should be able to just invoke it easily in your working directory where the GCODE file(s) live:

 

$ gcode-comments file.gcode

;FLAVOR:RepRap
;TIME:11265
;Generated with Cura_SteamEngine 2.3.1
M104 S205            ; Set extruder temperature
M109 S205            ; Set extruder temperature and wait (blocking)
;LAYER_COUNT:28
;LAYER:0
M107                 ; Turn off fan
M205 X10             ; Adjust jerk speed
G1 F2400 E-1         ; Move and/or extrude to the indicated point
...

Input:  file.gcode
Output: file_commented.gcode

hello

Since I’m now an instructor, I thought I would create a repository which demonstrates  code for the many languages out there which could produce a command line tool/interface (CLI).

HelloCommandLine

Currently, there are nine languages represented but I may add more later. Note that everything here is decidedly OSX-specific. Each subsection includes the instructions for running and/or compiling each, noting that some are compiled languages and some are not.

talking at the speed of lightning

I give so-called “lightning talks” at San Diego JS, a four-times-per-month local group on Meetup.com. Each talk only lasts five minutes so there’s time for several speakers within the span of a single event.

The venue is typically packed. Here’s a photograph of a typical turnout—there were about 120 attendees this month alone.

I suppose you can communicate a lot in a mere five minutes. It is a bit challenging to try to distill down all the things you need to say into this timeframe. There’s really no room for story-telling, just tell the straight facts and details as you race through your slides and screenshots and nothing more. At best, you can hope that someone will ask a relevant question which may allow you to go into some detail you’d earlier hoped to have included.

Challenges

Many of my projects involve more than one computer. Unfortunately, the security settings on most wi-fi routers at venues like this don’t want you to connect from one computer to the next. The router would actively prevent your demo from working. So I’ve learned to bring along my own networking, which is a hassle. This is especially difficult with IoT projects, for what it’s worth.

Another challenge is related to power. It seems like each of the speakers needs to setup prior to the event and so they all want to bring along their power adapters and plug in. This means that the venue would need to accommodate all those brick-style adapters and they usually forget this.

And I suppose, a recurring problem is that of screen resolution compromises that you have to put up with. You will have formatted all your screens for one resolution while creating your content, only to find that you’re now presenting in a smaller resolution. This then threatens to clip off content or the font size is now too small to be seen by those near the back.

Regardless, it’s a rewarding experience and I hope to give more talks in the months to come. I would encourage others to do the same. It’s a great opportunity to give back to the community of like-minded coders.

mobile app for the robo

I’ve written a new mobile app for the Robo C2 and Robo R2 set of printers by Robo 3D, a local San Diego—based company.

Robo-Home

Details

It’s written in the Adobe PhoneGap (Cordova) platform with Framework7 for the styling and scaffolding. It communicates to the underlying OctoPrint interface inside the printer itself. Rather than building several smartphone apps and being subject to the recurring annual developer fees by Apple/Google/Microsoft, I intend to serve it up in a more economical way: embed another single-board computer inside the printer.

PiZero

This will fit nicely on a $10 Raspberry Pi Zero W computer, as shown. It’s then powered by the USB 5V supply inside the printer and would be powered on every cycle. I’d then use my iPhone’s or iPad’s browser to simply connect to the app.

Progress

The app is fully-functional for the Robo C2 printer and sports a slick-looking interface.

Repository

Screenshots

And here are some obligatory screenshots of the app.

Robo-LeftPanel

Robo-Motors

Robo-Files

 

keeping your pi cool

An average computer’s operating system maintains some logistics about the cpu, like its input voltage, temperature and the like. The Raspberry Pi single board computer is no exception and will even scale back its speed if it determines that its internal temperature is getting too high. That’s a good thing but another approach is to proactively cool the cpu with a fan when it’s approaching that threshhold.

pi-temp.png

Since I’m creating a cube-like chassis to hold four of these Raspberry Pi 3 computers, I’ll soon need this functionality. So I’ve just created a new repository with JavaScript code to return the cpu’s temperature in fahrenheit/celsius as a string or a number. One could then programmatically turn on/off a fan using the GPIO pins using this information.

Here’s that repository:  raspi-temp

to type or not to type…

that is the question.  Rather than a Shakespeare reference, I’m here referring to a term in software development which determines how a language deals with variables, for example.

Define: type

When you create a variable in a computer language, it’s usually something like this:

var someVarName = 1;

In a case like this, we might infer that someVarName stores a number (an integer).  We might say that the someVarName‘s type is integer.  Using a pet-ownership metaphor, it’s like purchasing a dog house first (“someVarName”) and then next buying a dog to put into it (“1”).  You wouldn’t buy a fish bowl to store a dog… although this seems to work out great if you own a cat.  JavaScript, e.g., is like this picture:  it doesn’t seemingly care if you want to store a cat in a fish bowl.

cat-in-a-bowl

Two Schools of Thought

There are two camps out there:  those who like languages which force the variable type and those who don’t.

A statically-typed language usually involves a step in which your code is converted into something else (compiling) and any type-related issues must be fixed before a program can be created.

A dynamically-typed language is run “as is” and the code is evaluated at the moment of truth—determinations about the type of a variable are made at this time.  If there is a type-related issue, your end-user could be the first person to see the error.

Statically-Typed Dynamically-Typed
Java JavaScript
C++ Python
C# PHP
C Objective-C

The Pendulum Swings

Over the past three decades, the popularity of either approach has waxed and waned.  It’s safe to suggest for the moment that the less-strict languages are gaining rapidly in popularity over their stricter counterparts.

most-popular

We have the world of open source to thank for the popularity and speed of development we’re currently seeing in these dynamically-typed languages like JavaScript and Python.

Seeing the Future

Honestly, though, there are too many people in that strict-is-better camp and their influence is felt within software development companies.  If I were to guess at the future of JavaScript, I’d probably have to say that TypeScript and Flow will gain in popularity as larger development teams look to lower the number of bugs in their code.

I don’t know, though.  Maybe it’s time that we just relax and let the cat hang out in the fish bowl.

 

blinking the raspi’s built-in LED

I’ve just added a repository of some JavaScript code to take over and exercise the built-in activity LED on a Raspberry Pi Zero W (and presumably other models). It’s called gpiozero-toggle-led and it’s a pretty simple interface with installation instructions and some sample code. It works with the underlying js-gpiozero JavaScript port of the popular original Python code. This would be an excellent way of simply demonstrating GPIO without any additional wiring, components, breadboards, extra power supplies or electrical knowledge (like finding a 330-ohm resistor using its color bands).

zero-wireless

Note that the “zero” in the title of the repository and in js-gpiozero does not refer to the Raspberry Pi Zero but to the original gpiozero Python library.

This should remove some of the guess work when attempting to use the relatively-new library since their documentation examples at the moment are taking a back seat to their code port from the more-extensive Python offering.

This approach can easily be modified to instead exercise external LEDs (as soldered or otherwise attached to the header pin locations seen below).  Note that you’ll use “BCM numbering” for APIs such as this one. For external LEDs, you would need to connect it inline with a resistor from a selected pin to one of the grounds for this to work with correct orientation of the LED’s anode/cathode, of course.

raspberry-pi-pinout

If you’re trying to use this with a Raspberry Pi of a different model, you’ll likely want to adjust the JavaScript slightly as seen below.

/routes/index.js:

// Existing code, for a Raspberry Pi Zero
var ledActivity = new LED(47, false);
// For Raspberry Pi 3, for example
var ledActivity = new LED(47);

And that’s it. Since the Raspberry Pi Zero assumes an opposite value for true/false than the bigger models, it’s necessary to configure this in the device constructor to make things work as expected. Since BCM pin 47 is the activity light on the board itself, this will allow you to control it.