Saturday, 3 December 2016

Getting Started With STM32 and Simulink

The STM32 microcontrollers are powerful and full of awesome features but as anyone who has tried to work with the STM32 microcontrollers will know, setting them up and programming them can be a little difficult. I recently bought myself the STM32F4 Discovery development board and after trying a lot of new things, managed to setup a working development environment. I initially set up my development environment in Ubuntu. I found that the best option on linux is to use the System Workbench. But I found that getting anything more than a basic LED blink working was quite difficult. You have to pore over the ARM programmers manual and the StdPeripheral reference for hours and hours to get anything at all done. And if your focus is on the development of algorithms, maybe you don't want to spend so much time with the nitty gritty details of the implementation. 

Since I joined a PhD program a few months back, I have acess to Matlab from my university. So I decided to see if it was possible to use Simulink (which has support for quite a lot of hardware) would suit my workflow. I first tried the STM32 target for Simulink that's provided by ST themselves. After struggling with it for a week I gave up. I ran into multiple issues. First of all, the library was finnicky and not very easy to use. The user interface was set up in a confusing way. Second, I felt that the library was not complete because it did not implement a lot of features that were available on the processor like the option to read an incremental encoder using the timer. Of course, I guess it's possible to do it if you configure the timer and the pins yourself, but that defeats the purpose of having a Simulink block library in my opinion. Also, some of the blocks kept throwing errors and I was unable to run the examples because they were made for Matlab 2015b and I only had access to 2015a from my university. 

Instead I ended up using the waijung blockset for STM32. I've been trying it out for the last few hours and I think it's far better than the official library from ST. It implements most of the features on the STM32 (including incremental encoders) and the setup is a lot more intuitive. I managed to get an LED blinker setup in under an hour.



Thursday, 16 June 2016

New Website and Blog

As I mentioned in an earlier post (exactly a year ago! :D) I have my own website now. When I first bought the domain name and hosting package I opted to get a shared hosting server where I could build my own website using Wordpress. However, it never really satisfied me. I got a decent looking website up and running, but I had to work with the constraints of the Wordpress CMS. Whenever I wanted a part of my website to look a particular way, I had to go deep into menus and settings pages to try and tweak it to my liking. Also, most of the themes that were available didn't meet all of my requirements. Some of them would almost meet them but would be missing one critical feature or the other. In the end, I had to compromise by picking a theme that was good enough. I let my website be for about a year as I didn't have time in the middle of the semester to tweak everything to my liking. 

In May I finally finished my bachelor's degree. That left me with more time on my hands to tweak my website to my liking. I tried to keep the wordpress blog but I found working with Wordpress themes quite difficult if I wanted to modify them. In the end, I decided to go for something simple that worked: Bootstrap. It was a barebones solution that produced good results using simple HTML classes. I found myself a good theme from Startbootstrap and got started redesigning my website. 

Since I was using a static site generator, I no longer needed a hosting service which allowed disk access. So I decided that Github Pages would be the new home of my website. I canceled the hosting service but kept my domain name and pointed it to my brand new github pages site. 

I also discovered that it was possible to use Jekyll to produce static blogs that did not require databases. They also had support for importing blogger posts. Jekyll has a lot of cool in built markdown formatting features that Blogger lacks. So I decided to give it a shot. Although I got most of my important posts imported and running on a new Jekyll blog, I was a bit reluctant to leave the platform that I'd been using for so many years. In the end I decided to maintain both blogs separately. The new Jekyll blog will mirror this blog for the most part. However, the Jekyll blog will be curated a little bit more strictly to contain only "professional" posts. This blog will continue to be my outlet for both professional/technical and fun random posts. 

Go to ashwinnarayan.com to check out the new website.

New Blog

New Website

Wednesday, 30 March 2016

Unsupervised Learning: Self Organizing Maps

Most machine learning techniques like logistic regression, linear regression, and neural networks do something that is called supervised learning. This means that we need to supply examples of "correct" data to the machine learning algorithm for it to learn the patterns in the data. The goal of unsupervised learning is to automatically find clusters of similar data in a huge unlabeled set of data. Humans can do this easily with some types of data. If you saw a 2D scatter plot of points you'd easily be able to identify clusters in the data by visual examination. I've always considered unsupervised learning cooler than supervised mostly because it feels like that's what a general AI should be able to do. 

Self organizing maps (SOMs) are one technique used to find these clusters in the data. A self organizing map looks a little similar to a neural network at first. But the kind of operation that it does is very different. An SOM has a lattice of nodes. This lattice is usually one dimensional(arranged in a linear array) or 2 dimensional (arranged in a matrix). As the self organizing map is trained, the lattice of will be partitioned into separate classes. Since one of the main uses of self organizing maps is visualizing higher dimensional data, the lattice is is rarely more than two dimensions wide. The clusters in the higher dimensional data can be visualized on the 2D lattice.  Associated with each of the nodes in the lattice is a weight vector that has the same dimension as the input data vectors. To train the self organizing map we take each element in the data set and find the node with the weight that's closest to the element (Using the euclidean distance as the measure of closeness. Other measures can be used too.). Then we define a neighbourhood of nodes that are 'close' to this best matching unit and we adjust the weights of this entire neighbourhood of weights to be a little closer to the value of the element from the dataset. How much the weights are adjusted depends on the neighbour falloff function and on the learning rate. This can be expressed in one simple equation.
$$W_{uv}(t+1) = W_{uv}(t) + \Omega (u,v,t) \alpha (t)[x_n - W_{uv}(t)] $$
In each iteration, the learning rate and the size of the neighbourhood function is reduced by a small amount. The neighbourhood size is initially set to a large value. This allows the algorithm to initially affect the weights on a global scale and then over time adjust smaller and smaller regions of the map to train it.



In my implementation I used a Gaussian neighbourhood function whose variance decayed exponentially. The learning rate started off at 0.1 and decayed exponentially from there.

To check my implementation, I tested something that is a common use for SOMs. Clustering colors in an RGB color space. Colors are easy to visualize and I thought it would make a good demo. I first generated a random data set of bluish, reddish, greenish and dark bluish colors.

A random sampling of 4 different colors for the dataset.
I initialized the weights randomly and started the training. For this particular experiment I used a 2D 50x50 lattice of nodes. Since each node has a weight that is a 3 dimensional vector that represents a color, we can visualize the SOM node lattice as an RGB image. Stacking together the weight visualization after each epoch (One pass through the training set) of training gave me this really beautiful looking animation! :D
Animation that shows the self organizing map after each epoch of training.
 You can clearly see the different clusters of color slowly resolving as the training happens. The initial changes are more global due to the large neighbourhood. But as the training progresses the adjustments to the weights become more and more local. There is also a very nice separation of the different colors that were present in the first image. You can see that the brighter colors seem to be congregating towards the top of the image and the darker colors towards the bottom. The darkest blue (maybe even a little black) ended up in the bottom left corner surrounded by the darker hues of red, green and blue.
The final map after 30 epochs of training.





Tuesday, 22 March 2016

Machine Learning Part 2: Implementing Multi Class Logistic Regression

In my last post on machine learning I talked about how to implement simple linear regression in Python. This time I am going to implement logistic regression. This technique is used to classify input data into one of several classes.

First let's take a look at two class regression.

Let's have a set of input vectors $\{x_1, x_2, ... , x_N\}$ and a set of targets $\{t_1, t_2, t_3, ..., t_N\}$ as our training data. A logistic regression classifier defines a model $y(x,w) = h(w^Tx)$. The function $h(a)$ is a logistic sigmoid function or a "squashing" function that takes the output of the linear function $w^Tx$ and sqeezes it into a range of values from 0 to 1. Using the logistic sigmoid function allows us to interpret the output of the classifier as the probability that the input belongs to class 1 under certain assumptions.

So just like in linear regression we need to define cost function in terms of the parameters so that we have a measure of the performance of the model and a way to train the model. In the case of logistic regression there is a cost function - that is very effective and commonly used - called the cross entropy cost function. This cost function is again selected based on a probabilistic interpretation of the classifier.

$$J  = -\sum_{n=1}^{N}t_n ln(y(x_n) + (1-t_n)ln(1-y(x_n))$$

The gradient of this cost function is surprisingly the same as the gradient of the square error cost function.

$$\frac{\partial J}{\partial W} = \sum_{n=1}^{N}(y(x_n) - t_n)x_n$$

The weights of the model can be adjusted using the gradient descent algorithm just like in linear regression. The classifier will generate a straight that will separate the two classes.




Multiclass logistic regression can be done using many logistic regression units if the goal is to perform multiple binary classifications. However, if you have a vector that needs to be assigned to one of K classes, the procedure is slightly different. Multiclass regression is performed using the model $y(x) = softmax(W^Tx + \vec{b})$ where x is the input vector, W is a matrix with K columns where each column is a parameter vector, $\vec{b}$ is a vector of biases and the softmax function is defined as follows.

$$softmax(a_j) = \frac{e^{a_j}}{\sum_{k=1}^{K}e^{a_k}}$$

Each of the targets to train the classifier will be an array of size K. If an input belongs to class k, the kth position of the array will be a one. The rest of the elements in the array will be zero. This is called 'one-hot encoding'. It is easy to see from the definition of the softmax function that the output of the classifier will be an array of values  that are between zero and one and that the sum of all the elements in the output array will be 1. This means that we can interpret the output array as a probability distribution that tells you the probabilities that the input vector belongs to each of the K classes.

Like in two class classification, the input data (with N vectors and dimension M) set can be arranged in a NxM array $X$ where each row is an input vector. The corresponding targets can be arranged in a matrix T that has N rows and K columns. If the input vector $x_n$ belongs to class K the element $T_{nk}$ will be one. The rest of the elements of the matrix will be zero.

The cost function is the same cross entropy function extended to work with the matrix T.
$$J = -\sum_{n=1}^{N}\sum_{k=1}^{K}T_{nk}ln(y_k(x_n))$$
 However, since we are using a softmax function instead of the logistic sigmoid function, the gradient of the cost function will be different. The derivation to obtain expression for the gradient gets very hairy but the expression for the gradient is surprisingly simple.

$$\frac{\partial J}{\partial W_{uv}} = \sum_{n=1}^{N}X^T_{vn}(t_{nu} - y_{nu})$$

Once we have this gradient, gradient descent can be performed on the parameters in the same way. I gathered all the vectorized implementations of the equations I've mentioned into a classifier class in Python for ease of usage.



To test the classifier I created a scatter of normally distributed points centred around different means on a 2D plane and applied the classifier. With a little bit of help from stackoverflow I was able to figure out how to color the regions of the plot according to the trained classifier.

This is a scatter plot of the unclassified data. The points were generated using multivariate normal distributions using different means.

This is a plot of the error or the cost over time as the gradient descent algorithm runs.

A plot of the decision regions learned by the classifier. If a region is shaded a particular color, that means that the classifier identifies all the points in that region as belonging to the same class.



Saturday, 27 February 2016

My First Build of BB-8

Being a robotics person, the part of Star Wars: The Force Awakens that gave me the most delight was BB-8! The cute little droid who rolls along on a sphere. The moment I saw it on screen I knew that I had to build it! This blogpost documents my entire build process.

I decided that the first thing I should do is come up with a good control mechanism. The first thing I thought of was a pendulum hanging from a diametric bar inside the sphere. The pendulum would be free hanging and an actuator would apply torque on it and make the sphere roll. This seemed a simple enough system to work with. So I started the process of simulating the robot and coming up with a good control system.

The Mathy Stuff

The easiest way to model a sort of complex system like this is to use Lagrangian mechanics. Write down the difference between the kinetic and potential energies of the system, apply the Euler-Lagrange equations to get the equations of motion and plug the equations into a differential equation with initial conditions. This general procedure can be used to simulate a wide range of mechanical systems. It's an essential part of any roboticist's toolkit.

In the case of the BB-8 sphere pendulum system the equations of motion come out as:

$\frac{5}{3} m_b \ddot{x}(t)+m_p \left(l \ddot{\theta}(t) \cos (\theta (t))-l \dot{\theta}(t)^2 \sin (\theta (t))+\ddot{x}(t)\right)=\frac{u(t)}{r}$
$l m_p \left(g \sin (\theta (t))+l \ddot{\theta}(t)+\cos (\theta (t)) \ddot{x}(t)\right)=-u(t)$

Here $m_b$ is the mass of the ball, $m_p$ is the mass of the pendulum, $l$ the length of the pendulum rod and $r$ the radius of the sphere. The state of the robot is represented by the state vector $\left[ \begin{array}{c} x \\ \theta \\ \dot{x} \\ \dot{\theta} \end{array} \right]$ where $x$ is the distance the sphere has rolled and $\theta$ is the angle the pendulum makes with the vertical.

When the motor applies a torque on the pendulum, it also applies an equal and opposite torque on the shaft on the spherical body. This is the control torque we're going to use to make BB-8 roll back and forth. For the mass of the pendulum I am using another motor with a weighted disk. This motor can react against the weighted disk to make the robot turn left and right. These two mechanisms are enough to make BB-8 go anywhere! Since this robot is an open chain manipulator, the equations of motion can be written in the manipulator equation form $H(q)\ddot{q} + C(q,\dot{q})\dot{q} + G(q) = Bu$.

$\left[\begin{array}{cc}\frac{5}{3}m_b + m_p & m_pl\cos\theta \\ m_pl\cos\theta & m_pl^2\end{array}\right] \left[\begin{array}{c}\ddot{x} \\ \ddot{\theta}\end{array}\right] + \left[\begin{array}{cc}0 & -m_pl\dot{\theta}\sin\theta \\ 0 & 0 \end{array}\right] \left[\begin{array}{c}\dot{x} \\ \dot{\theta}\end{array}\right] + \left[\begin{array}{c}0 \\ m_pgl\sin\theta\end{array}\right] = \left[\begin{array}{c} \frac{1}{r} \\ -1 \end{array}\right] u $

This is what the system looks like without any motors.


We can easily write the manipulator equation to the standard form ( $\dot{x} = f(x,u)$ ), linearize it around any equilibrium point to get a linear state space model and implement a linear controller. I'm so glad that I had Mathematica to help me with the modeling. It made things very simple and I could get a simple LQR for controlling the sphere working without too much hassle. Here's what the motion of the sphere looks like with the controller. 



The Body

Now it's time to build! First I was stuck on what to make the sphere out of. I didn't want the robot to be too small and at first, I couldn't think of any place to find a sphere. So i started designing a spherical shell in Blender. I had to do it in parts because of size constraints on the 3D printer. This was very time consuming. And then it struck me! I could use on of these! :D



I bought a 20 cm diameter globe off amazon. The sphere is made out of ABS plastic which is really nice. I used a hacksaw to cut the sphere in half. The next challenge is to mount the motors. I tried reusing empty potato chip cans but most of them didn't have the dimensions that I wanted. So in the end I decided to 3D print a motor housing and some parts to couple the sphere to the motors. I used Blender (Open Source! Yay! :D) to design the parts.

Initial Render of what the internals were going to look like.
However, it turned out that 3D printing parts is a bit time consuming. Also, the motor couplings were difficult to print without using a lot of support material. After a few iterations of redesigning and checking, the final motor coupling was printed. I made a decent looking motor housing out of some wood and metal L-shaped motor mounts.

The motor assembly

The Electronics

An Arduino Nano with an Atmega328 is the brain of the robot. A GY521 accelerometer/gyro board helps me measure the angle of the motor assembly with respect to the ground and a rotary encoder coupled to the dead axle helps me measure the speed and position of the ball. A HC05 Bluetooth module communicates with an Android app that I made using MIT App Inventor so that I can control the robot. 


Putting it all together lead to this huge mess of wires.


After multiple attempts at rewiring, I got to this slightly less messy (but still quite messy) state.

The battery is also included in this one.
Despite the messiness, it actually works! :D I made it roll around for a bit.

Since this was my first build, I decided that it should more of a proof of concept than a robust build. So I built it very fast. Now  that I know that it works, I can spend some time designing a nice PCB to make the wiring neater and more compact. I also need to fine tune the control loop so that the whole thing moves smoothly. No point in tuning it on this since I'll be rebuilding a part of it anyway.

Watch out for the next post about a more robust build!

Friday, 20 November 2015

Prime Spirals in Python


I watched this really fascinating video on prime spirals recently and decided to see if I could write my own code to generate them. It's not terribly efficient but it works! Code is on github! :D


Machine Learning: Implementing Linear Regression and Gradient Descent in Python

It's been a while since I went through Andrew Ng's Machine Learning course on Coursera. The course is very good at introducing the basic concepts but it lacks the depth of knowledge that I need if I really want to understand how to implement and use some of the algorithms on my own. So I decided to work through a more difficult textbook on machine learning. One of the first things I decided to do was implement the machine learning algorithms from scratch using my language of choice: Python.

The most simple idea out there that can be called machine 'learning' is linear regression. I first learned about linear regression in high school math class. So when I saw that linear regression was the first topic in the machine learning course, I was a bit skeptical. I felt that linear regression was too simple to be called machine learning. But it turns out that linear regression is quite a powerful tool to make predictions from data. The term 'linear' in linear regression is a bit misleading because it makes you think about straight line graphs and gives you the impression that trying to fit straight lines to data can't really be that useful. The trick to levelling up the power of linear regression lies in how you choose your variables (or features)! So you can have features that are crazy functions of your initial variables. 

To understand this think about how we can use a straight line to fit a set of data points that you think might be exponential in nature. Say yoususpect that output $y$ is related to the variable $x$ by $y = ke^{cx}$. You can rearrange this equation to $log(y) = cx + k_1$ by taking the natural logarithm of both sides of the equation. Now we can plot $log(y)$ vs $x$ on a graph and try to fit a straight line to it. Here the parameter that we 'learn' is $c$ and $k_1$. The variable $x$ is called a feature.

In a similar way you can add features that are functions of the existing features if you think that the relationship is not linear. In a sense we are choosing a 'basis' of functions which we can mix together in different amounts to (hopefully) accurately model the output $y$. The way a Fourier Series builds up a periodic sequence using sine waves of different frequencies is exactly the same as how linear regression works with non linear basis functions.

So I decided to make the system 'learn' the square wave in my first implementation of linear regression. First some math:

The output prediction $\hat{y}$ can is represented by the linear model below. Here 'n' is the number of feature vectors.

$$ \hat{y} = \sum_{i=1}^{n} \theta_i x_i$$

To use gradient descent to train this model we need two things: a cost function and a gradient function.

The cost function is a function that takes the values of theta, and the features and the data samples and calculates a cost based on the error between the prediction that the model makes and the actual value. The most commonly used cost function is the mean square error cost function.

In this equation, m is the total number of training samples that we have and $x^{(i)}$ represents a feature vector that is ith training example. The cost function sums over all the training examples available to give one real number that represents the cost of the current values of the parameters.

$$
J = \frac{1}{m} \sum_{i=1}^{m}(\hat{y}^{(i)} - y^{(i)})^2 \\
\frac{\partial J}{\partial \theta_j} = \frac{2}{m} \sum_{i=1}^{m}(\hat{y}^{(i)} - y^{(i)})x^{(i)}_j
$$

The gradient of a function is a vector that points in the direction in which the function changes the most. So if I want to find the place where the cost is minimum all I have to do is to start out at a point, find the gradient at that point and go in the opposite direction. I have to keep doing this until the gradient is zero (or very close to it). Think of it like a ball rolling down slopes until it comes to rest at a place where there is no slope. So in Python I have to make a loop and each time the loop is run, I update the parameters theta using the rule. Here alpha is the learning parameter.
$$ \theta_j := \theta_j - \alpha \frac{2}{m} \sum_{i=1}^{m}(\hat{y}^{(i)} - y^{(i)})x^{(i)}_j$$

So I got a large set of points that were in the shape of a square wave and I used sine wave basis functions as features. After running gradient descent for a thousand iterations I got coeffecients that were really close to the actual Fourier Series expansion of a square wave!
The learned function superimposed over the original square wave

How the error decreases over time.



You can find my implementation on my github page.

Sunday, 15 November 2015

The One Thing that my College Gets Wrong About Exams

I'll be honest. Despite it's shortcomings, many of the courses that are offered actually do contain material that I find interesting. In fact, when you're learning something at the level of a college course, there are few subjects that aren't interesting. It's like Richard Feynman said; everything is interesting if you go deep enough into it. My biggest complaint about the system isn't the lack of depth in the courses or for that matter the professor's depth of knowledge (although I have had a fair share of instructors who had a less than optimal grasp of the course material themselves). My biggest complaint is the focus of the examinations that they conduct that are supposed to evaluate how well the students know the material. The exams  - in addition to having a low correlation to the students' grasp of the material - also actively discourage them from gaining a true understanding of the course material.

Typically, the exams I've written (particularly exams of courses that are highly mathematical) test only the ability of the students to memorize two things: derivations and formulae. I'm not saying that people should be exempted from memorizing formulae. There are plenty of formulae that are so important that they must be memorized. The problem is when you have to memorize formulae that look like this:

$L_F = -10 log(\cos{\theta} \{\frac{1}{2} - \frac{1}{\pi} p(1-p^2)^{\frac{1}{2}} - \frac{1}{\pi}arcsin(p) - q[\frac{1}{\pi}y(1-y^2)^{\frac{1}{2}} + \frac{1}{\pi}arcsin(y) + \frac{1}{2}]\})$

$p = \frac{\cos{(\theta_c)}(1-\cos{(\theta)})}{\sin{(\theta_c)}\sin{(\theta)}}$

$q = \frac{\cos^3(\theta_c)}{(\cos^2(\theta_c) - \sin^2(\theta_c))^\frac{1}{2}}$

$y = \frac{\cos^2(\theta_c)(1 - \cos \theta) - \sin^2(\theta)}{\sin{(\theta_c)} \cos{(\theta_c)} \sin{(\theta)}}$

For anyone interested it's a formula that tells you the coupling loss between two fiber optic cables when they're misaligned by an angle theta. The book doesn't even attempt to derive this formula because the authors know that the derivation on its own doesn't really add to a person's understanding of fiber optics. It may be a nice exercise that tests the student's algebraic manipulation but that's pretty much it. In the real world, if there is a situation in which this formula needs to be used, it's pretty much guaranteed that the engineer will just look it up. It's completely pointless to memorize it. 

Why is this focus bad? Firstly, it does not actually test understanding and leads to students not getting the score that they deserve. There have been countless instances where I knew  how to solve a problem but didn't get any credit because I had to remember some stupidly long equation. Secondly, it forces examiners to limit their questions to really simple ones because they know that remembering the formula or it's contrived derivation is half the question. So the number of questions that test my understanding of the material is next to zero. Thirdly, it actively discourages students from trying to understand the material. Once people figure out that they can get scores by just memorization, they will start focusing on memorization. Sometimes, there is so much to memorize that there isn't enough time to try and understand even if I wanted to.  

If I were the person who was in charge of the setting the examinations, I would do the sensible thing and give every student a booklet full of the formulae that need to be used for the questions. This allows the questions to be far more interesting and test how the student is able to use the information at his disposal to solve actual problems that practicing engineers face. The brain is a processor with a limited amount of cache! It's not a hard disk! Introducing this simple change of focus will go a long way towards courses that are actually useful. It will also give the professors the freedom to ask tougher questions! 

Now I know that this approach actually works! Before college I did my Cambridge IGCSEs and A-Levels. Both these exams were very focused on understanding rather than recall. Exams either had all the required formulae included as part of the questions or (as was the case for Further Mathematics at A-Levels) a large booklet of formulae. This didn't affect the difficulty of the exam at all. It's no use knowing formulae if you don't know how to use it. This might sound a bit strange but I actually enjoyed writing my IGCSEs and my A-Levels. The exams were designed well enough that sometimes I came out of the exam hall with a new understanding of what I had learned. I still feel that I understood and learned more in class during my high school than I did in most of my college courses. Any understanding that I have about my course material is a result of my independent reading and efforts rather than the course instruction. 

Now I'm not the first person to talk about this. People have been complaining about the education system for a very very long time. And many have even tried to change it by approaching the administration. Invariably though, I've seen that - at least in my college - the people in charge are very reluctant to bring about any of these changes. The biggest roadblock is probably the fact that a lot the of the professors in my college disagree with what I have said above. They believe that memorization is very important perhaps because that is how they were taught. I've seen that things are changing, but not nearly fast enough. I don't really think that the changes that I've suggested will be implemented any time soon. So why am I writing all this down? I really just really needed to vent. XP

Thursday, 15 October 2015

Simulating a Double Pendulum in Mathematica

I've been playing around with Mathematica's Non-Linear Control Systems toolbox over the past few days and it's been brilliant! One of the first systems that I tried to simulate is the double pendulum since it's such a commonly used sample problem in non-linear controls.

The first step is to write down the total kinetic and potential energies of the system and find the Lagrangian. Once you have that, you just need to run the Lagrangian through Mathematica's EulerEquations function and then set up a non-linear state space model using the differential equations. 

Once you have the system model it's really simple to get the response and make a simple simulation.

And out pops a wonderful demonstration of chaos! Here are are three simulations with slightly different initial conditions showing diverging trajectories. 

Initial conditions: {160, -160}
Initial Conditions: {161, -160}

Initial Conditions: {161,-161}


The next thing I tested was whether I could make a simple controller to stabilize this double pendulum pointing straight up. I went with a standard Linear Quadratic regulator. In this simulation there is only one actuator at the elbow joint. So this is also an example of an underactuated robotic system. Works great! :D


Sunday, 11 October 2015

Simulating Mechanical Systems in Mathematica

I've been trying a lot of different software for simulating mechanical systems for my project. By far, the most fun I've had is when I was simulating it on Mathematica. Mathematica is a seriously cool piece of software. I got a simulation of the simple pendulum up and running almost 3 times faster than I did when I was working with Python and Matplotlib. Although, to be fair, the time I did it in Python was the first time I was doing the simulation. So I was learning about how to do the simulation as well as trying to make it. So that might have affected the amount of time I took.


Friday, 9 October 2015

Movie Review - Interstellar



Interstellar is a movie that I had been looking forward to for quite a while. After watching the movie I started going through a lot of the movie reviews that people started posting. I was quite disappointed to see so much negativity and nitpicking directed towards the movie. 

The movie is set in a dreary post apocalyptic earth ravaged by dust storms. Food is scarce and blight is creeping in on the last of humanity's food supplies. To survive, people are forced to take up farming. In one of the farms Cooper - a NASA engineer turned unwilling farmer reminisces about the time humanity used to look up at the stars and wonder about our place in the universe.

Cooper soon discovers that all hope is not lost when a gravitational anomaly leads him to a hidden NASA base full of people who are working on one last mission to save humanity. A wormhole has appeared near Saturn that put a new galaxy full of potentially habitable planets within the reach of humans. Cooper is asked to help NASA with visiting the most promising of these worlds to investigate. They visit a world with tidal waves the size of mountains and a world of ice and snow all the while having to deal with the time dilation of a super massive black hole.

The movie excelled in its depiction of space travel and the realities of time dilation. It very realistically depicted the science of gravitational time dilation. The graphics of the worm hole was brilliant, dwarfed only by that of the absolutely gorgeous black hole. 

The AI of the robots TARS and CASE were an unexpected delight! The movie did not try to make the AI look humanoid or impossibly advanced. The shape and movements of TARS were practical and realistic. The highly advanced natural language processing and fluid intelligence that the robots displayed was truly remarkable. As a person who is reasonably familiar with robotics, I know that these are areas where active research is going on. In fact, quite recently robotics research has started shifting from conservative control systems - that are very slow and careful - to control systems that make full use of the dynamics of the body of the robot - very similar to how TARS controls itself - to create robots that can make quick and acrobatic movements. The humor settings were a nice touch.

The harshest criticisms were leveled at the slightly unusual finale with Cooper floating around in the black hole and sending messages back in time. Many say that this ending was unsatisfactory and ruined what was otherwise a good movie. I disagree. Science fiction movies and novels can be characterized as a spectrum with hard science fiction on one end (like the work of Arthur C. Clarke) and soft science fiction on the other (like Doctor Who). Maybe people were complaining because most of the movie can be classified as hard science fiction while the black hole scene is more 'soft'. However, I find the idea of an advanced species manipulating a black hole to send messages back in time no more outlandish than the idea of them creating a wormhole at will. If there are species that are capable of the kind of engineering required to construct a stable wormhole, I think it's safe to assume that they have also figured out the mechanics of a black hole. 

Also, a lot of the reviews that I've seen of interstellar keep looking at it from the  point of view of a movie critic. They look at the movie as though it was designed to give them some sort of intense entertainment experience and that the movie fell short of giving it to them. I think it's unfair to expect Interstellar to be like that. Movies like interstellar are not good because they are packed full of action or witty dialog. Movies like interstellar are good because they make us dream about a better future. Just like Carl Sagan's COSMOS and Neil deGrasse Tyson's awe inspiring monologues on space, this movie has the power to inspire us. Perhaps one day in the future we'll become a space faring civilization and some of the people working on it will talk about how this really old movie about space travel captured their imagination.

So Interstellar is not a movie to watch if you're looking for action or comedy (although the movie has its fair share of both). It's a philosophical movie that is intended to make us think and wonder; and at least in my case, I think it succeeded in doing exactly that judging by the unusually long post-film trance I was in.

Thursday, 8 October 2015

The Magical Euler-Lagrange Equation and the Calculus of Variations

I've been learning a lot about simulating and controlling mechanical systems for one of my projects. Of all the math that I learned, the most amazing was the Euler-Lagrange equation. 

$\frac{d}{dt} \frac{\partial L}{\partial \dot{q_i}} - \frac{\partial L}{\partial q_i} = Q_i$

The overarching theme of the control systems that I've been reading about is optimality. And wherever we need optmization, Calculus of variations tends come into the picture. Calculus of Variations is a branch of mathematics that studies the problem of finding functions that minimize the value of a functional (or cost function) in a particular range. As an example, take the range $x \in [0,1]$ on the real line and assume that $f(x)$ is a function that is defined in this range. There are an uncountably infinite different possible functions $f(x)$. A functional is an operation that maps the function $f(x)$ on to a real number. Think of this as a "cost" for the path that the function draws on the graph. The main problem that calculus of variations tries to solve  is finding the function $f(x)$ that minimizes the "cost".

This area of mathematics has applications in a huge variety of fields and arguably the most important of which is in the field of Physics. Long back some physicists found that the Newtonian Mechanics that everyone knows and loves can be reformulated as an optimization problem. They found that there is a quantity known as "action" (which is defined as kinetic energy - potential energy) that is minimized as particles and other things in the real world go about their daily business. This is commonly known as the "Principle of Least Action". Coming back to the Euler-Lagrange equation, mathematicians figured out that plugging in the functional into this equation gives the constraints of the system that cause it minimize the functional. This means that every single dynamic problem in Newtonian physics can be reformulated as an optimization problem! Pretty neat.

So say you have a simple system like a simple pendulum and you want to figure out the equations of motion of system. The way people are used to doing it is drawing a diagram that shows the net forces acting on the system and then deriving the equations of motion using Newton's  three laws of motion and energy constraints. Using the Euler-Lagrange equation, the procedure is slightly different and arguably simpler. The first step is to write down the Kinetic and Potential energies of the system.

$T = \frac{1}{2} m l^2 \dot{\theta}^2$
$V = -m g l \cos{\theta}$
$L = \frac{1}{2} m l^2 \dot{\theta}^2 + m g l \cos{\theta}$

To get the equations of motion of the system,

$\frac{d}{dt}\frac{\partial L}{\partial \dot{\theta}} - \frac{\partial L}{\partial \theta} = -b\dot{\theta} + u$
Where U is the torque an actuator (motor) at the base of the pendulum gives to the pendulum.

This gives:

$I\ddot{\theta} + b\dot{\theta} + m g l \sin{\theta} = u$

No fiddling around with forces and no need to try and figure if the net force is zero or not. Just works with pure energies! This works every time as long as you write down the Lagrangian correctly. The first time I saw this, I couldn't believe it worked! For the following few weeks I'd take Lagrangians of random things for fun and check to see if it was giving the correct answers.

This also makes me think about the amount of information we need to know 'everything' about a system. Before I learned about the Lagrangian, I had a 'gut-feeling' that in physics, fields were everything. Maybe this is because school and engineering level education in physics focuses a lot on field theories in physics. However, the fact that we can get the complete behaviour of a system from an expression that just encodes the total energy of the system implies that somehow, the energy of the system encodes all the necessary information to describe it completely. The equation suggests to me that potentials and energies are more 'fundamental' in a sense than fields.

Monday, 5 October 2015

Movie Review - Real Steel



Two weeks ago I discovered pure awesomeness in the form of Battle Bots. I spent my whole evening watching every episode of the 2015 season. Seeing my excitement, my friend suggested that I watch this movie called Real Steel which was released in 2011.

The movie is set in a future where human boxing is outlawed (Good!). Instead people watch insanely cool robots go all out against each other for entertainment (Fantastic!). A retired human boxer (Charlie Kenton)  who is trying to make it big in the robot boxing (and failing miserably at it) has to spend two months looking after his son (who he hasn't seen in years). Though they are initially cold to each other, the duo eventually learn to work together with a little scrapyard bot called Atom and make it to the Real Steel Tournament, repairing their relationship along the way. The movie is surprisingly heart warming despite the fact that it is about robots fighting violently to their deaths while people cheer (Or maybe it's heartwarming because of that :D ).

Of course, what I liked most about the movie was were the robots! Being a robotics enthusiast and having built quite a few robots myself, it's really nice to see more realistic robots being depicted in film. Far too many movies get robots wrong. Most of them vastly over estimate their abilities or completely ignore the laws of physics. I liked the fact that the robot fighters in the movie were controlled via joysticks and had the sort of jerky, non-human movements that characterize robots. I get the feeling that the movie was aiming for realism - which is a goal that I really appreciate, especially when the movie is about a subject like robotics which I'm really passionate about.

The fight scenes between the robots were brilliantly choreographed and genuinely exciting. The urge to cheer on little Atom during the final fight was completely overwhelming. 

The movie also gave me a lot to think about. It set my mind a-buzz with ideas about how to implement a robot boxing tournament like that in real life. It also got me thinking about how I would go about designing robots like that. The problem is harder than most people think. This amusing compilation of robots attempting the DARPA Challenge gives you a good idea of how good our robots are in real life. The movie was a satisfying blend of science fiction, robots and very human, emotional moments. Definitely a worthwhile watch. 

Friday, 28 August 2015

Book Review - The Martian by Andy Weir




Fantasy books were my gateway drug into the world of reading. Starting with Harry Potter, The Inheritance Cycle and the Lord of the Rings, fantasy was a staple of the steady diet of books. Then I discovered Famous Five, Secret Seven and other children's adventure novels. I started reading science fiction quite late into my life. Although I read a few books here and there I didn't really get deep into the genre until I read Douglas Adams' "The Hitchhiker's Guide to the Galaxy". Nowadays I tend to prefer science fiction over other genres. So when I found out the the upcoming movie "The Martian" was based on a novel by the same name, I immediately hunted down the book so I could read it before the movie came out. Turns out the book was pretty short and I finished it by reading on and off during my free time over the past couple of days. So here are my thoughts on the book.

Mark Watney is an astronaut on the third manned mission to Mars. Due to an unfortunate and unexpectedly strong dust storm five days into the mission, NASA had to abort. When the crew was making an emergency evacuation from the artificial habitat, the dust storm blows a piece of debris into Mark Watney, impaling him with a piece of metal and taking out the electronics that monitored his vital signs. With no vital signs from Mark's suit and the storm threatening to tip over the vehicle that they needed to lift off from the martian surface, the crew had no choice but to leave his (supposedly) dead body behind. Mark wakes up on Mars after the storm with a slightly leaky suit and the stark realization that he is now stranded on an alien planet. He needs to use his knowledge and resourcefulness to survive on a dusty red wasteland till someone can send help from Earth. 

The first thing I noticed about the book was how realistic a lot of the mission terms and procedures seemed to be. The actual structure of the Mars Mission had a lot of the idiosyncrasies of a real mission. The science presented in it appeared to be accurate as well. I decided to look up the author after finishing the novel. It turns out the that the author is a computer programmer who put quite a lot of effort into making sure that the science in the book was reasonably accurate. From his pictures on Wikipedia I think he probably hung around NASA a lot and learned as much as he could about the procedures they follow and how they handle communication with the media and the general public before writing in those parts of the book. The book really gave me an insight into how difficult it can be to communicate science and technology effectively with the public and  how important it is to do it right. 

As I was reading the book it struck me that Matt Damon is the perfect person to play Mark Watney. With the incessant swearing and wisecracking, it was almost as though the character of Mark Watney was written with Matt Damon in mind. Of course, this is probably confirmation bias on my part but that's what was going through my mind as I read the book.

The Martian is also in the Goldilocks Zone with regards to the level of technical detail that the author decided to add in - not too much to turn away a general audience and just enough juicy detail to draw in the science nerds. Being a science nerd myself, I thought the density of science was ideal. 

A lot of the story was written from the perspective of Mark Watney as excerpts from his log. The reader is for the most part experiencing the story from the point of view of the man stuck on Mars. This made the novel feel very personal and all the more thrilling. Those brief moments where the writing switched to third person were absolutely spine chilling because in general, the book followed the pattern of switching to third person when things are about to go south. 

'The Martian' is an optimistic and very heartwarming exploration of what humans can do together if they really put their minds to it. A lot of people sacrificed a lot of things and a lot of money so that one man could come back to earth from Mars. Rivalries were set aside when the life of another human being was at stake. Would the events have played out similarly in real life? I don't know. But I believe - as the author probably did - that most humans are good at heart. So if asked about the most important thing I learned from the book, I'd say it is the maxim: "Be excellent to each other!". :D

Tuesday, 18 August 2015

Dung Beetles are Extremely Cool

I've noticed little bugs scurry across the road quite a lot during my time at this college. Most of the time I've been too busy to actually go down and examine these bugs. Today, when I saw one of these critters crawling beside the road I decided to take a closer look.




What I found was the Dung Beetle in the process of rolling a ball of dung and trying to find a nest. Dung beetles are extremely cool creatures. From Wikipedia:

The "rollers" roll and bury a dung ball either for food storage or for making a brooding ball. In the latter case, two beetles, one male and one female, stay around the dung ball during the rolling process. Usually it is the male that rolls the ball, while the female hitch-hikes or simply follows behind. In some cases, the male and the female roll together. When a spot with soft soil is found, they stop and bury the ball, then mate underground. After the mating, both or one of them prepares the brooding ball. When the ball is finished, the female lays eggs inside it, a form of mass provisioning. Some species do not leave after this stage, but remain to safeguard their offspring. The dung beetle goes through a complete metamorphosis. The larvae live in brood balls made with dung prepared by their parents. During the larval stage, the beetle feeds on the dung surrounding it.

Here's a video from National Geographic that shows what the Dung beetles do.




Tuesday, 28 July 2015

Creating a Simple Bot using the Telegram Bot API

So Telegram recently released an API for creating bots on Telegram! It's quite cool. Already, people have released nice Python libraries for it. For this bot I'm using one of them.

I decided to create a simple Telegram bot that I can use to control the music on my laptop. It's a testament to the power and simplicity of Python and the convenience of the linux terminal that it only took me an hour to develop this. Head over to my repo at gihub if you want to look at the code!

Here's a video of it working!


Movie Review - Whiplash

Note: This post may contain spoilers.


I used to watch sci-fi and animated films exclusively but now every once in a while I watch a random movie just for the fun of it. Yesterday that random movie turned out to be Whiplash - a movie about an aspiring drummer and his well intentioned but abusive mentor.

I loved the movie but I disagree with the opinions of Terence Fletcher. There is a scene in the movie where he is asked if there is a line. If there is a limit which if he crosses he might actually discourage his student from becoming the next great musician that the world loves. And he replies that there is no such limit because the next great won't be discouraged. While that's a cute sentiment, I think he's wrong. The world isn't divided into 'The Greats' and talentless people. There are plenty of people who fall in between the spectrum. And I think that by not caring about those who are not destined to be 'Great' and even actively doing things that could damage them mentally he is doing more harm than good. Additionally, I think that there are a lot of people who respond much better to constructive criticism and positive reinforcement than to abuse hurled at them. For every person with a story about an abusive mentor who made them who they are, I think there are a few dozen people with stories about how their incredibly supportive and positive mentor inspired them to be their best.

So while the story was inspiring, I really hope that people don't use Terence as an inspiration. 

Saturday, 18 July 2015

Maker Faire Singapore

The day before yesterday I found out that there was a Maker Faire going on in Singapore this weekend! I immediately scrapped all my other plans for the weekend to get there. It was definitely worth it. I owe a lot to the maker movement. It's what got me interested in engineering and I think it's a big part of the reason I am the person I am today!

The highlight of the Maker Faire was definitely the whole hall dedicated to Intel Edison projects! There were so many cool things in that hall! I have to admit that I only had a vague idea of what the Intel Edison did before saw all those demos. It's a pretty capable SoC. My favorite demo were these dancing hexapods. They were really cool to watch. The second best demo was the robot with omni directional wheels.

The Dancing Hexapods



There was also quite an impressive display of 3D printing at the Faire. There were so many exhibits related to them! Apart from the conventional designs using steppers to move the extruder in the X-Y plane I also saw a 3D printer based on a delta robot.

Saw this 3D printer that could print huge parts

The delta robot 3D printer

Why Self-Driving Cars are the Future of Personal Transport

So Google's self driving cars are getting really good at self driving. A lot of people I know are skeptical about self driving cars. Some say that they will never use them because they just love to drive. I think that once self driving cars are actually on the road, they won't have a choice.

From everything the the testing of the self driving cars have shown so far, they seem to be much safer than human drivers. They don't get tired and they never break the rules. Human drivers on the other hand, cause millions of deaths every year due to carelessness and driving while sleepy, drunk or under the influence of drugs. Even if self driving cars only cut down the number of car accidents by half we have a moral obligation to switch over. By making the choice to continue driving manually we are making the choice for millions of deaths to continue happening every year.

I think that sometime in the distant future people will talk with horror about the time that people were allowed to drive their own cars. Just like we now talk with horror about the horrifying state of medical practices before we knew about germs and how exactly the human body worked.
 

Friday, 3 July 2015

Terminator Genisys – Review

I watched the movie a few days ago. And I thought I’d write down some thoughts I had about the movie.
 
First of all, I have to admit that although I roughly know what happens in the earlier movies I have only watched one of the previous three movies.So this is the first Terminator film the I’ve watched fully.

For me, the movie was just OK. Nothing spectacular. Visual effects were nice and that was probably the best part of the movie.

Apart from the fact that the movie uses a completely overused plot of using time travel to reset what happened in the earlier movies, my main complaint about the film is the portrayal of the new enhanced symbiote John Connor as evil. By merging with the machines, John Connor has finally solved two of humanity’s greatest problems: aging and disease! Why does the movie portray that as bad?
In fact, one of the most most interesting thing researchers in medicine are working on right now is using nanobots for targeted drug delivery. Imagine having an army of nanobots inside you making sure you don’t age and never get any disease.

If everyone were to merge with the machines like John Connor did there would actually be peace again and human civilization would have taken the next logical step of using their now immortal bodies to explore the vastness of interstellar space!

For once the AI in the movie has a goal to work together with humans and they just had to portray that as evil. I’d love to see a Terminator film where the humans and the AI (it’s supposed to be super intelligent!) finally realize that the best thing to do is cooperate.