# Embedding worker in Electron app using Webpack and electron-builder

Disclaimer: as per electron-boilerplate

Make webpack bundle your worker code. I’ll skip the entire code here because it’s too long.

Remeber the output path

Make electron-builder copy your worker bundle file .

This will place your worker bunlde file inside resources/app.asar.

Adjust worker path and working directory for fork in main.dev.ts.

# Installing Electron manually for electron-builder

This article tries to resolve an uncommon issue when you encounter the following error and it cannot be resolved automatically, e.g. behind proxy, etc.:

Download the right version (with consideration of package.json) of Electron from https://github.com/electron/electron/releases

Unpack the downloaded Electron and put all files under node_modules/electron/dist.

Create path.txt file under node_modules/electron with the following content:

• Windows: electron.exe
• Linux: electron
• macOS: Electron.app/Contents/MacOS/Electron

Note: Make sure there is no whitespace characters (including a newline character) after the filename. This is a problem that I struggled for minutes and hours.

# Quick start on ANTLR4 in Rust - part3

This is my note in adopting and learning ANTLR4Rust

Series

In the previous article, we implemented a parser with internal state variables. However, if a grammar is huge, it is practically impossible to manage a huge number of state variables. Alternatively, a visitor-like approach can be used.

In an exit method of the root rule in the Listener, we can grab the current context and its children and call custom parser methods:

The minimal working example can be found here.

# Quick start on ANTLR4 in Rust - part2

This is my note in adopting and learning ANTLR4Rust

Series

In the previous article, we built the minimal working example walking a parse tree using a listener. In this article, we are going to store a parsed CSV structure into a variable to use in the future.

Define a CSV structure:

csv will be a resultant CSV structure. add_to_header and row_to_add are internal state variables to generate a CSV structure.

Implement a listener:

Feed an input to a parse and extract the result:

A tricky part is that we have to extract csv from listener. And to this end, we have to extract listener with listener_id.

The minimal working example can be found here.

# Quick start on ANTLR4 in Rust - part1

This is my note in adopting and learning ANTLR4Rust

Series

Install nightly version of Rust (and make it default if you want for convenience).

Get ANTLR4 runtime for Rust from here:

Prepare a grammar. I will use an example grammar: https://raw.githubusercontent.com/antlr/grammars-v4/master/csv/CSV.g4

Generate a parser:

You will get csvlexer.rs, csvlistener.rs , csvparser.rs . Place them into your project src directory.

Add dependencies in Cargo.toml:

Add a feature and import lazy_static macros to the root module:

Import common and essential things:

Import grammar-specific things:

Implement ParseTreeListener, a supertraint of CSVListener:

Implement CSVListener:

Read and parse an input. Note that csvFile in the last line is a rule name in CSV.g4:

The minimal working example can be found here.

# Trilateration using the Levenberg-Marquardt method

True range multilateration is a method to determine the location of a movable vehicle or stationary point in space using multiple ranges (distances) between the vehicle/point and multiple spatially-separated known locations (often termed ‘stations’). — True range multilateration, Wikipedia

We can define a trilateration as an optimization problem to minimize a cost function $$S$$ for a given estimate $$\beta$$, a tuple of $$(x, y, z)$$

\begin{aligned} S(\beta)&=\sum_{i=1}^{N}f_i(\beta)\\ &=\sum_{i=1}^{N}\left[r_i-\sqrt{(x-X_i)^2+(y-Y_i)^2+(z-Z_i)^2}\right]^2 \end{aligned}

where $$r_i$$ is a measured distnace between an Anchor (i), whose coordinates are $$(X_i, Y_i, Z_i)$$.

Levenberg-Marquardt method is defined by:

$$\beta_{k+1}=\beta_k-\left(J^TJ+\mu_kdiag\left(J^TJ\right)\right)^{-1}J^Tf(\beta_k)$$

where $$J$$ is the Jacobian matrix and $$f$$ is a column vector composed of $$f_i$$:

\begin{aligned} J&=\begin{bmatrix} \frac{\partial f_1}{\partial x} & \frac{\partial f_1}{\partial y} & \frac{\partial f_1}{\partial z} \\ \frac{\partial f_2}{\partial x} & \frac{\partial f_2}{\partial y} & \frac{\partial f_2}{\partial z} \\ \vdots & \vdots & \vdots \\ \frac{\partial f_N}{\partial x} & \frac{\partial f_N}{\partial y} & \frac{\partial f_N}{\partial z} \end{bmatrix}\\ &=\begin{bmatrix} F_1(x-X_1) & F_1(y-Y_1) & F_1(z-Z_1) \\ F_2(x-X_2) & F_2(y-Y_2) & F_2(z-Z_2) \\ \vdots & \vdots & \vdots \\ F_N(x-X_N) & F_N(y-Y_N) & F_N(z-Z_N) \end{bmatrix} \end{aligned}

where $$F_i$$ is a shorthand of each derivative:

$$F_i=\frac{r_i}{\sqrt{(x-X_i)^2+(y-Y_i)^2+(z-Z_i)^2}}$$

Now, we can estimate $$\beta$$ by iterating the equation until it converges.

# Git Hooks with stderr

For example, it’s tricky to validate a commit with pre-commit hook if an app does not return a proper exit code. Here’s a way to validate with the stderr.

1. Make a temporary file
2. Redirect stderr to the temp file
3. Count the length of its content

You can understand it with code snippets below:

PowerShell

Unix/Linux shell

# Preventing privacy leak with TuringFonts (substitution cipher)

Download one of web fonts (.woff2) from TuringFonts. Assumed Zebra is used here

Put the font to the web directory and define the below in a stylesheet:

Assign zebra-substitution class to where it needs

See how it looks.

TuringFonts also provides Encoder to get a ciphered text.

# Quick start on sending email with Amazon SES on Node.js

Although Amazon provides great documentation, this is what all I needed to send an email with Amazon SES (Simple Email Service) on Node.js.

## Get Started with Amazon SES

To start, visit Amazon SES and hit Get started with Amazon SES. Then Amazon asks which region you want to use SES. Note that region for SES doesn’t need to be the same with your other Amazon services.

After choosing the region, you will see the page above. I highlighted menus you need. You can use two types of identities, domains and email addresses. If you have your own domain and you want to send an email from the domain, the first one needs to be configured. If you want to send an email from your existing email account such as Gmail and Outlook, the second one needs to be configured. The first one and the second one is not mutually exclusive. You can use both.

### Option 1: Verify Your Domain

After entering Domains menu, hit Verify a New Domain.

In the popup, type your domain in interest, check Generate DKIM Settings and hit Verify This Domain.

For a given domain, SES requests you to configure DNS records in order to verify whether the domain is owned by you. SES will try to verify the domain constantly. If verification succeeds, you will see Verification: verified, DKIM Status: verified, Enalbed for: Yes in the dashboard as shows in the above. Then it’s all set.

Verifying an email address is simpler. After entereing Email Addresses menu, hit Verify a New Email Address and type an email address in interest. And then SES will send a verification email to a given email address. Once you visit a link given in the verification email, the email address will be verified. Then it’s all set.

## Build Node.js Application Sending Email with SES

### Configure AWS Credentials

To verify whether your Node.js application has the right permissions to send an email with SES, you need to provide the right credentials to AWS SDK.

Visit IAM Management Console and hit Create New Access Key.

Don’t worry. The keys have been deleted

Remember the created access key and write it down to the following file:

• Windows: C:\Users\*USERNAME*\.aws\credentials
• Linux, Unix, or macOS: ~/.aws/credentials

Now AWS SDK will read access key from the credential file and prove your application has the right permissions.

Note: Access key allows full access to your Amazon AWS. Do not disclose access key publicly. I recommend to create an IAM user and allows only SES

### Script Sending Email with SES

The simplest Node.js script sending an email with SES is provided in Amazon Documentation or its GitHub repository, so I’ll skip the entire script here:

## Requst a Sending Limit Increase

Initially, your SES operates in sandbox mode andcan send an email only to domains or email addresses which you verified in the above. To be able to send an email to anyone, you need to Request a Sending Limit Increase.

Visit Create Case and create a case to request a sending limit increase.

# Positioning system using DecaWave DW1000

## DecaWave’s DW1000

DW1000 is a UWB communication and ranging module manufactured by DecaWave.
It provides a fairly good ranging accuracy (< 2 cm). Not only a simple anchor-tag ranging system, let’s make a practical centimeter-level precision indoor positioning system using it

## System Design

### Overview

A setup is shown in a figure below:

• A tag
• A tag is composed of two components—Raspberry Pi and Arduino Pro Mini
• Raspberry Pi controles a higher level indoor positioning system
• It triggers Arduino Pro Mini to probe anchors and measure distance between it and each anchor
• It requests distance measurements on Arduino Pro Mini and locate itself through trilateration
• Arduino Pro Mini
• It is required due to similar reasons of anchors
• It receives commands from Raspberry Pi and executes the corresponding process
Processes are:
• Probing anchors nearby and exchanging series of frames with each anchor
to measure distance
• Reporting probed anchors and measured distance values
• Anchors
• They are deployed in a site of interest and their 2D/3D coordinates are known
• They are implemented on Arduino Pro Mini for the following reasons:
• Arduino Pro Mini’s low power consumption
• Anchor’s fast response requirement (order of milliseconds)
• Anchor’s simple tasks (receiving and transmitting UWB frames)

An overall sequence diagram is illustrated below. Tag (Arduino) is actually composed of Arduino Pro Mini and DW1000.

### State Machine: Tag

A figure below is a state diagram of a tag:

When a Raspberry Pi commands via I2C to scan, it goes into SCAN state and broadcasts a PING message. Going into PONG state immediately, it waits for PONG messages from anchors nearby for a certain duration. I set it to 100 ms.

If it discovers 3 or more anchors during the PONG state, it goes into ROUNDROBIN state. It exchanges 4 messages with each discovered anchor sequentially, i.e. POLL to an anchor, POLL_ACK from an anchor, RANGE to an anchor, and RANGE_REPORT from an anchor. After sending POLL and RANGE message, it waits for a response message for a certain timeout of 10 ms.

After 4 messages are exchanged with each anchor, it calculates distance between an anchor using Tx/Rx timestamp. It adopts two-way ranging algorithm, which is a bit computational intensive, but I think it is not that much intensive. For more information about two-way ranging, please refer the official appliclation note APS013 by DecaWave.

### State Machine: Anchor

A state diagram of an anchor is simple:

• It normally stays at IDLE state
• If it receives a PING message, it tries to send a PONG message with a random delay between 0 and 90 ms to avoid the collision and goes into PENDING_PONG state
• If it receives a POLL message and sends a POLL_ACK message, it goes into RANGE state and waits for a RANGE message is received
• If timeout of 10 ms expires, it returns to IDLE state
• If it receives a RANGE message, it sends a RANGE_REPORT message to a tag

## Implementation

The whole implementation can be found in my GitHub repository. I used arduino-dw1000 for controlling DW1000 using Arduino. I followed wiring between Arduino Pro Mini and DW1000 as defined in examples/Ranging{Anchor,Tag} of the library. Only I2C interface is needed to connect Raspberry Pi and Arduino Pro Mini