## Antenna Arrays And Python – Square Patch Element

As mentioned in my intro post, the individual antennas in an array are often known as “elements”. To compute the arrays performance each individual elements field contribution needs to be summed. For my initial investigation I focus on using a rectangular microstrip patch element and this post will cover the model that is used.

A microstrip or patch antenna is a low ­profile antenna that has a number of advantages over other antennas – it is lightweight, inexpensive, and easy to integrate with accompanying electronics because they can be printed directly onto a circuit board which makes them easy to fabricate.

A patch antenna usually consists of a conductive patch with width W, and length L, sitting on top of a substrate (i.e. a dielectric circuit board) with thickness h and relative permittivity Er. The substrate then sits on top of a conductive ground plane.

With the model used there is no account taken of mutual coupling between the elements. This can have a significant effect on array performance when array elements themselves are large, elements are closely spaced or large scan angles are used. It is also assumed the ground plane is infinite so the model is only valid over 0°<theta<90°, 0°<phi < 360°. The benefits of this model are the calculation is potentially very fast and despite the limitations the model still provides enough accuracy to give a useful insight into the potential performance of an array, before committing to more detailed modelling or prototyping.

The E theta and E phi components of the far-field radiation pattern are given by the equations below:

Using these equations allows us to create the following Python function to calculate the total E-field pattern for the patch as a function of theta and phi:

Before moving on to the array calculation itself next time I’ll detail the Python scripts I use to visualise the fields as it’s always nice to see what’s going on.

## Antenna Arrays And Python – ArrayCalc & Coordinate Systems

I want to start by acknowledging that the basis for most of the Python scripts came from Neill Tucker’s Antennas and Microwave page and more specifically his Phased Array Design Toolbox called ArrayCalc. He seems to really (really, really) know his antennas and his site has a lot of very useful and interesting theory and practical examples, totally worth checking out.

ArrayCalc is an extremely detailed and powerful toolbox written for Matlab that enables analysis of 2D and 3D antenna arrays with various built in antenna elements available such as dipoles, rectangular and circular microstrip patches and helixes. The code is all provided and the documentation is superb and really helped me understand the theory. The only issue for me is I don’t have access to Matlab! I decided to try and cherry pick the code that could be useful for me and rewrite it in Python.

Coordinate System

In ArrayCalc, Theory of Operation documentation Neill states that in his first antenna design job his mentor advised – “if you want to be a good antenna engineer, get your head around 3D coordinate geometry”. This is sound advice. Once I got it clear in my head the definitions of the spherical and cartesian coordinate systems and how they were related I found processing of the various output graphs, etc easier to understand and had a more intuitive feel for what was going on with the antennas.

The global coordinate system is established as a reference for the antenna array as a whole. It can be defined in both cartesian and spherical coordinates and the transforms between the two are shown in the diagram below. In my scripts an array of elements will be defined along the x-y plane with each element having its own x, y, z position with z always being zero. Neill provides a lot of detail regarding his geometrical set-up in the Theory of Operation documentation which is worth a read for more understanding.

## Antenna Arrays And Python – Introduction

I’ve been delving into the world of antenna arrays recently. The thing that really helped me understand the background theory was writing some Python scripts covering some design calculations. I wanted to detail some of these to help finalise my thoughts and to possibly assist anyone looking at this kind of thing.

There’s a lot of theory and terminology around antennas that could make some of the details unclear. If anyones interested and struggling let me know and I’ll do my best to help or at least point you in the right direction of further reading.

Antenna Array Overview

As a very simple overview an antenna array is a set of individual antennas which are connected together. Basically, it’s possible to design an array to be more sensitive in the desired direction than the individual antennas would be by themselves. When designed correctly the radio waves radiated by each individual antenna combine, adding together to enhance the power radiated in desired directions, and cancelling to reduce the power radiated in other directions. Due to reciprocity the same will also happen with radio waves being received.

Another useful function of array antennas can be electronic beam scanning. By phasing each antenna separately the beam of radio waves can be steered electronically to point instantly in any direction over a wide angle. This is most often referred to as a “phased array”.

Antenna Elements

The individual antennas in an array are often known as “elements”. The elements considered in most literature are microstrip patches but in theory an element can be any radiating antenna such as the dishes shown in the picture below.

In my next post I plan to cover the work done on a rectangular microstrip patch design that will then go on to function as an element in an array.

## Deploying a Django app to Heroku

Note to self — before deploying a local Django app to Heroku I should do the following:

Install the wsgi HTTP server, gunicorn:

`pip install gunicorn`

Install package to help work with environment variables:

`pip install python-dotenv`

`pip install psycopg2`

Create requirements file:

`pip freeze > requirements.txt`

Configure settings.py to load environment variables and the database:

```import dotenv
import dj_database_url

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# load environment variables from .env
dotenv_file = os.path.join(BASE_DIR, ".env")
if os.path.isfile(dotenv_file):

# load database from the DATABASE_URL environment variable
DATABASES = {}
DATABASES['default'] = dj_database_url.config(conn_max_age=600)```

Also delete the default database config:

```DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}```

Add a .env file to project base directory and add the database URL:

`DATABASE_URL=sqlite:///db.sqlite3`

Make sure .env is added to .gitignore so it doesn’t get pushed to repo. My last .gitignore looked like:

```*.pyc
*~
__pycache__
myvenv
venv
db.sqlite3
.DS_Store
.env```

Assuming there is already a heroku account run:

`heroku login`

locally to associate machine with heroku account.

On the heroku dashboard create a new app. Under the “Settings” tab and under “Buildpacks” add a new buildpack called “heroku/python”.

Under “Config Variables” add “DISABLE_COLLECTSTATIC” with a value of “1”.

To set-up the database go back to command line and enter:

`heroku addons:create heroku-postgresql:hobby-dev -a YOUR_APP_NAME`

This provisions a new postgres database and should add the url to the Config Variables. Run command:

`heroku config -a YOUR_APP_NAME`

DATABASE_URL should be added to Config Variables.

Now create a new file called Procfile in the base directory of project and add:

`web: gunicorn YOURSITE.wsgi --log-level=info --log-file -`

Where YOURSITE is specific to the project.

Add another file in base directory called runtime.txt and add the version of python you want to use:

`python-3.5.2`

To serve static assets I use the WhiteNoise project. To install:

```pip install whitenoise
...
pip freeze > requirements.txt```

Add it to the app by changing wsgi.py to include:

```from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

application = get_wsgi_application()
application = DjangoWhiteNoise(application)```

Now that should be it! Commit the changes locally then run:

`git push heroku master`

It should display the progress of installation and eventually be up and running.

References

This was trying to put a bunch of stuff I read in the articles below into an order I’ve found works for me.

https://blog.heroku.com/in_deep_with_django_channels_the_future_of_real_time_apps_in_django

## Updating distributed RaspberryPI’s with automatic code updates

I’m working on a project that has multiple RaspberryPI’s, distributed in multiple locations, but all running the same code (Django Channels with Python background worker scripts). I wanted to be able to deploy any updates to the main code to all the PI’s with as minimal manual interaction as possible. I couldn’t find an obvious way to do this so thought I’d summarise what I’ve set-up in the hope I may get some feedback or it would help someone else.

Basic overview of solution

The main code base is stored in a Git repository in BitBucket.

I have a development PI set-up and when I’m ready I push code changes from it to the main branch of the BitBucket repo.

Each deployed PI has an updateWorker Python script running that periodically downloads the remote code from the BitBucket repository using the git fetch command. It then checks for changes using the git status command and finally updates the code if there are changes using git reset.

Running Git from Python

To run the Git commands from my Python script I’m using a Python Package called sh. It functions as a subprocess replacement that allows you to call any program as if it were a function. I plan to write another post about using it but the basics are intuitive. For example to do a git status call on the local directory and save the response in a variable called statusCheck:

```import sh
from sh import git

statusCheck = git("status")```

(See this tutorial for more details on how the login to BitBucket is handled)

updateWorker Script

```import sh
from sh import git
import time
import os, sys

aggregated = ""

def CheckForUpdate(workingDir):
print("Fetching most recent code from source..." + workingDir)

# Fetch most up to date version of code.
p = git("--git-dir=" + workingDir + ".git/", "--work-tree=" + workingDir, "fetch", "origin", "master", _out=ProcessFetch, _out_bufsize=0, _tty_in=True)

print("Fetch complete.")
time.sleep(2)
print("Checking status for " + workingDir + "...")

statusCheck = git("--git-dir=" + workingDir + ".git/", "--work-tree=" + workingDir, "status")

if "Your branch is up-to-date" in statusCheck:
print("Status check passes.")
print("Code up to date.")
return False
else:
print("Code update available.")
return True

def ProcessFetch(char, stdin):
global aggregated

sys.stdout.flush()
aggregated += char

if __name__ == "__main__":
checkTimeSec = 60
gitDir = "/var/testupdate/"

while True:
print("*********** Checking for code update **************")

if CheckForUpdate(gitDir):
print("Resetting code...")
resetCheck = git("--git-dir=" + gitDir + ".git/", "--work-tree=" + gitDir, "reset", "--hard", "origin/master")
print(str(resetCheck))

print("Check complete. Waiting for " + str(checkTimeSec) + "seconds until next check...", True)
time.sleep(checkTimeSec)```

updateWorker uses the –git-dir and –work-tree options to define where the local git repo is for the code.

The script parses the response from the git status command to determine if there are updates. If an update is available we will see:

```On branch master

Your branch is behind ‘origin/master’ by 2 commits, and can be fast-forwarded.
(use “git pull” to update your local branch)
nothing to commit, working directory clean```

On my PIs the updateWorker then kills all the running scripts which are automatically restarted by another background process, this should mean the scripts are all running up to date versions. So far it’s working well but not sure if I’m doing something crazy wrong!

## The Rule Of 3

Since the start of the year I’ve been trying out “The Rule Of 3” to try and prioritise tasks in a better way. I’m kind of using it in a dumbed down way – at the end of the day I just pick the three tasks I think are most important for the following day, but so far I do think it’s helping me focus. Worth a try.

## Exercise is goooooood

So today was meant to be the “blueist” day of the year and to be honest when I woke up this morning I didn’t feel keen to get out of bed, let alone get working. Even after our evening meal I was still feeling a bit meh and could happily have sat on the sofa watching crap on Netflix all night. But I didn’t, I dragged myself to boxercise and put in a shift! It was super tough but I felt so good after, exercise really is a natural high. That and the sweetest most lovely people on Undateables makes the world seem a better place!

## Zcash

Zcash (ZEC) is another form of crytocurrency with its main differentiation being that it provides methods to make private transactions. Personally I can’t really think of when I’d need to use this feature but from a speculation point of view I’m interested because I’ve seen how much Bitcoin and Ethereum went up in value and I can see something similar happening with Zcash.

So I’m tempted to try and buy in early and see if the value rises. But I haven’t bitten the bullet yet and I’m not entirely sure why.

I guess one reason is it’s not super easy to buy, it’s not hard but it would still take a bit more effort than going to coinbase to buy BTC or ETH and that seems to stop me.

Also whenever I look online for guides on how to buyZEC there are usually guides on how to mine it too which looks like a “free” way to get some (ignoring the cost of time and electricity) and of course I’d rather get it for free!

Another reason is I’m afraid to loose money, I wouldn’t be buying a lot but I think I’m naturally risk averse when it comes to parting with cold hard cash.

Current price at this time is \$43.91, will I miss out…

## Blockchains & Digital Currency

Warning, this is a bit of a brain spew!

Digital currency such as Bitcoin and Ethereum and the blockchains they are built on are exciting and interesting technologies. I’ve been following various blogs about them for some time and there are often comparrisons to the early days of the internet. If that’s the case I want to be involved! But I’m not sure of the best way. And is it going to live up to the hype?

There appear to be two obvious ways to get into it. Firstly develop using the technologies or secondly – speculate on the associated currencies/tokens. I don’t know if I have the creativity or the time to dive into the development side of things although I do find it interesting, especially Ethereum. Speculating on the currency is easy enough to do, just purchase some, but it’s really just guess work, think I need to come up with some kind of strategy.

## Git calls on another directory

Today I learned that it’s possible to call git commands on a directory/repository other than the one you are currently in. For example to call git status on a repo in /var/app just type:

`git --git-dir=/var/app/.git/ --work-tree=/var/app status`

I used this in a Python script I’ve been writing at work that automatically checks for updates in a repo and updates the code if there are any. More on that another time.