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.