Monday, 18 September 2017

Putting some of my Python knowledge to a good use: a Reddit reading bot!

One of the perks of knowing a programming language is that you can build your own tools and applications. Depending on what you need, it may even be a fast process since you usually do not need to write production grade code and a detailed documentation (although it might still be helpful in the future).

I’ve got used to read news stuff on Reddit, however, it sometimes can be a bit time consuming since it tends to keep you wandering through every and each rabbit hole that pops up. This is fine if you are commuting and have just some spare time to spend on browsing the web but sometimes I just need a quick glance at what’s new and relevant to my interests.

In order to automate this search process, I’ve written a bot that takes as input a list of subreddits, a list of keywords and flags and browses each subreddit looking for the given keywords.

If a keyword is found inside either the body or in the title of a post which has been submitted in one of the selected subreddits, the post title and the links are either printed in the console or saved in a file (in this case the file name must be supplied when starting the search).

The bot is written using praw.

Sunday, 3 September 2017

Let Python do the job for you: AutoCAD drawings printing bot

Recently I’ve been getting familiar with AutoCAD and at the same time I’m trying to improve my Python skills. Odd mix, huh?!

While trying to improve my Python skills I thought I could exercise myself on automating boring tasks. I remembered that a few years ago I was given a boring job which involved printing a lot of drawings directly in PDF format from Autocad. I was too lazy to print the drawings one by one and I knew the process could be automated. At the time I put together a Python script that did the job fine, but it was a bit messy. I thought I could make an improved version.

When nice APIs are not available, such as in the case of AutoCAD (at least that was the case a few years ago, nowdays things may have changed), using Pyautogui may help in the task of automating boring tasks.

Saturday, 2 September 2017

How would you make a very simple and rotating magnetic field starting from a three phase power supply?

Recently I had a brilliant idea :D why not make a rotating magnetic field that can rotate a needle of a compass? (or any other magnetic needle for that matter).

Ok but the design must be very basic. One possible option would be the following:

Image 34

Simulation of the electric field of a three phase cable using FEMM

Finding analytical solutions to physical problems is always nice and rewarding, however it can be a bit of a pain when the geometry of the problem (just to mention one possible hustle) becomes more complex than just a sphere, a cylinder or any other basic shape.

If you think of a simple cable, with a copper core and a PVC insulation, calculating the electric field generated in the insulation layer using pen and paper may still be feasible, however, using FEMM greatly improves your life when doing calculations as an engineer, student, or “just” curious person.

This simple cable can be modelled using FEMM as follows

Image 20

The cable has a total diameter of 7 cm, with a first PVC insulation with a radius of 2.6 cm, a 0.05cm air gap and another PVC insulation layer.

Friday, 1 September 2017

Magnetics simulation with FEMM

FEMM stands for Finite Element Method Magnetics, and it is a nice software for solving magnetics and electrostatics problems.

I’ve known FEMM for at least a couple of years but I’ve never tried  it out and used it at its full power! Now the time has come to do that!

Image 1

In this post I’m going to present the results of the following simulations:

  1. A C shaped electromagnet (detailed results).
  2. The magnetic field of the rotor of a 4 poles synchronous machine (brief overview).

Wednesday, 30 August 2017

Another typical control problem: balancing a ball on a beam

Balancing a ball on a beam is not a common problem you may face during your everyday life, however, this simple example of engineering can be extended to more complex problems and, in general, to other more interesting control problems such as

  • Control of temperature in a room
  • Control of robots
  • Control of automated cars
  • Control of industrial processes

The main problem tackled in this article is the design and implementation of a basic PID based controller to control the position of a ping pong ball on a beam. Ideally the controller should be able to set the position to whatever value of the x axis the user decides to apply. This is a simple scheme of the physical system

schema1

Modelling a DC motor using LTspice, Simulink and Matlab

Electrically speaking, a permanent magnet DC motor can be modelled as follows:

Image 1

applying LKT we obtain the following differential equation

$$v = Ri+L\frac{di}{dt}+e$$

where $R$ is the equivalent resistance of the brushes plus the windings, $L$ is the inductance as seen from the external terminals of the motor and $e$ is the back EMF. Usually R is very small and can be difficult to measure with a multimeter. The back EMF can be expressed as a function of the speed of the motor $e = k\phi\omega$.

Friday, 4 August 2017

Taylor series with Python and Sympy: Revised

More than 2 years ago I wrote a short post on Taylor series. The post featured a simple script that took a single variable function (a sine in the example), printed out the Taylor expansion up to the nth term and plotted the approximation along with the original function. As you can see on the right on the “Popular posts” bar, that post is one of the most popular and I’m told it appears among the first results on Google.

Figure_1-1

The script I wrote originally was a bit clunky, and there surely was room for improvement. Last week I received an email from a reader, Josh, who sent me an improved version of the original post.

Thursday, 18 May 2017

Lighting your garden with LED lights and the sun: a DIY project, part 2.

Some time ago I wrote a short article on a small circuit I made to power on and off my garden lights using only a handful of components and some patience. Since then, however, I’ve dug deeper and found out some other good solutions to the problem.

A quick recap of the problem: At dusk and dawn I’d like my garden lights (powered by 12V DC batteries) to switch themselves on and off without me doing anything: a first step towards total automation ;)

Image 2

My first try at accomplishing this task was using a simple BJT with a voltage divider specifically design to allow a certain bias current when it gets dark. See here for more information on this first raw trial.

Tuesday, 2 May 2017

Sunday, 5 March 2017

Resizing spatial data in R

Here I am after a short break, writing again about R!

In december I worked on a project that required me to work on spatial data. This led me to learn about how R deals with this kind of data and to look around for ways to make my “spatial data experience” less painful. I discovered that R is, as always, full of options.

I’m used to dplyr for exploring data and manipulating it, however when it comes to spatial data, I think that gstat, raster and rasterVis are just some of the useful packages that will make your life a lot easier.
Rplot01

Wednesday, 30 November 2016

Short circuit currents calculation on high voltage lines

Short circuits are one of the most common failures that can happen when dealing with electrical circuits.

international-symbols---warning-53670-lg

Short circuits can be accidental, think of a tree branch leaning onto a high voltage powerline or due to the breakdown of the isolating material (this is often the case as it gets older and loses its isolating property). Whatever the cause, short circuits are, for sure, an enemy of your electrical systems mainly because the following effects:

Wednesday, 23 November 2016

Solving electrical radial lines with Python

Electrical transmission systems are something we all take for granted. They work in a reliable manner ensuring high quality of service and as few minutes lost per year as possible.

Low voltage lines and most of medium voltage lines are radial lines, that is they are like a branch of a tree with a unique power supply location. Radial lines are relatively easy to work with, you can solve a radial line problem (ie you can get currents and voltages) by applying Boucherot’s theorem to each section of the line. This is a good news for a Python enthusiast as myself, since repetitive tasks lends themselves to be automated with programming.

Since Boucherot’s theorem uses the absolute values of electrical quantities, it is useful when you are working with AC lines and you would like to know the magnitude (rms) of currents and voltages while at the same time you do not really care about the phase differences. The rms values are used, for instance, when you need to choose the protection systems to install and what kind of electrical wires to use.

Suppose you are given the following three phase balanced radial line:

Image 2

You can think of C1, C2 and C3 as industrial motors or any other kind of three phased balanced loads.

Wednesday, 26 October 2016

7th MilanoR meeting + talks live streaming

On 27th of October I’m going to attend the 7th MilanoR meeting featuring the following two talks:

1. Interactive big data analysis with R: SparkR and MongoDB: a friendly walkthrough  by  Thimoty Barbieri and Marco Biglieri

2. Power consumption prediction based on statistical learning techniques by Davide Pandini

This is my first official R event and I’m very much looking forward to it. There has been a strong positive reply from a growing number of people interested in R confirmed by the fact that the tickets were sold out pretty much a few hours after the event was announced.

However, due to this unexpected sudden success in the allocation of the tickets, many people will not be able to attend. Fear not though, the MilanoR staff has just decided to live stream the event on its brand new Facebook page. The event will start around 6.30 PM local time. Feel free to leave a comment and share any thoughts you may have on the topic.

If you would like to know more about the event, the talks and the speakers, check out the following articles:

1. 7th MilanoR Meeting: October 27

2. 7h MilanoR Meeting live on Facebook

Saturday, 24 September 2016

My first Shiny App: control charts

After having carefully followed the online official Shiny tutorial, I decided to make a quick try at making my very first Shiny App. I should say that I found myself very well with the explanation given and Shiny was definitely one of the libraries that took the less time for me to start using it. Of course I’m still not a master of Shiny by no means, but I feel more confident on how to use it and on what can be done with it.

Image 1

I’m working on an R project related to control charts and I was hinted to get to know Shiny, since it is very likely that the project will involve an interactive interface and Shiny fits the bill perfectly for this assignment. For this reason I took an afternoon for getting familiar with Shiny and build this App. Enough talk, let’s get to the App.