## Wednesday, 5 November 2014

### FPGA : A minor hiccup

So I was happily working on my FPGA for a while on my Linux system. One day, due to some work I had to do on a piece of software that only worked on Windows, I had to boot into my windows partition.

When one of my friends came over to take a look at the FPGA I plugged it right into the laptop because I already had a binary config file in the FPGA and all I needed was for the board to be powered up. Big mistake. The moment I plugged the fpga in, the red light on the board (which usually only comes on when the board i being programmed) came on and stayed on. And no matter how many times I re installed the drivers, the board just would not cooperate. I didn't really think much of it at the time. I thought that this was just a minor issue with the drivers on the windows side and thought that everything would be fine when I plugged my board in when I booted back into my Linux partition.

The next day, I booted back into Linux and the board was doing the same thing. The red programming led was coming on but the board wouldn't work.

I have no clue what happened but I wonder if it is related to that recent incident where the FTDI company pushed a driver update to windows that would brick fake ftdi chips that acted as a USB to UART bridge. The funny thing is, the Numato labs board doesn't have an ftdi chip on board. I think it has a PIC microcontroller programmed to do the USB to UART bridging. I wonder if the PIC device was emulating the ftdi chip? Or it could just be a coincidence. Whatever the case, the board was bricked.

I sent an email to the Numato labs customer service people about the issue without really expecting a response. To their credit however, not only did they respond, they offered to replace my board with a new one if I shipped my bricked board back to them! :D Never expected that! So the new board arrived today and I plugged it in and everything seems to be working fine.

Time to resume work on that project! :D

## Tuesday, 21 October 2014

### An 8 bit counter on an FPGA

So I finally got around to implementing an 8 bit counter on the FPGA that I recently acquired. Turned out to be a lot more difficult than I thought it would be mostly because I was a bit new to writing modules in verilog. I'm still at that stage of learning where I make silly mistakes which are simple but tedious to rectify.

Also, the clock of the FPGA is 100MHz. So I also had to divide the clock down to some frequency that was visible to the human eye. But in the end I got it working! The FPGA itself is only just barely visible but I couldn't turn up the lights without completely washing out the blinking LEDs. Oh well ...

I have this problem where I try to go to the advanced sections of new stuff too quickly and then give up because it looks too complicated. I have to keep telling myself to take things one step at a time and build up slowly to more advanced/complicated stuff.

So now that this counter is done I think I'll search around for something that's a little bit more complicated than the counter but still within my reach. And slowly build up to something more complicated like Finite State Machines or VGA controllers.

## Sunday, 14 September 2014

So it's common knowledge by now that if you want to see the most mind numbing, narrow minded side of humanity all you need to do is visit the comments section on an even slightly controversial YouTube video.

However, I've noticed that the comments are not so bad at the start. I subscribe to a lot of popular channels on YouTube. That means I sometimes get to see the videos as soon as they're out. I've noticed that the comments are usually really nice and supportive at the start. But as the video gets more and more popular the racist/sexist/homophobic/narrow minded comments start pouring in. It's probably because at the start, most of the people who see the video are those who subscribed to the channel. And these will be people who actually like the person who makes the videos and appreciates them for putting in effort to make good content. Once the video percolates through to the general public however, mean people start spamming the section with comments.

It's actually kind of sad to see the comment section slowly become filled with hateful negative comments and overwhelm the nice ones.

## Friday, 29 August 2014

### Getting Started with FPGAs

So after a lot of deliberation I've decided to start working with FPGAs. Ever since the Mojo came out I've been thinking about this. This semester I finally decided to go ahead and get an FPGA. I initially thought about getting the Mojo. I was reluctant because despite the claim that it is for hobbyists, it's not exactly cheap at Rs. 7500. So I decided to search for alternatives that used the same FPGA IC.

After a lot of searching I found this FPGA development board called "Mimas" from Numato Labs. They were selling this board at Rs. 3000 on Amazon. That was less that half the cost of the Mojo! So after a lot of searching and comparison of specs I finally decided that I'd get Mimas instead of Mojo. This board also has the additional advantage of 4 on board switches. The Mojo has none. Here's the FPGA in all it's glory! :D

Anyway, I started the installation after checking the md5sum of the download and everything seemed to be fine! I implemented some basic combinational logic circuits on the board to check if everything was working fine. I'll be implementing something a little bit more interesting (like a counter maybe?) and then I'll upload a video of the board working! :D

## Sunday, 13 July 2014

### Inverse Kinematics: Part 2

On to part two of exploring inverse kinematics! This time I think I will add two additional degrees of freedom to the arm and calculate the inverse kinematics.

$l_1$ - Length of the first link
$l_2$ - Length of the second link
$l_3$ - Length of the third link
$\theta_0$ - The angle of the base drum.
$\theta_1$ - The angle of the first link from the ground
$\theta_2$ - The angle of the second link w.r.t the first
$\theta_3$ - The angle of the third link w.r.t the second

First let's ignore the third link and consider the two links plus the rotating base. This extension is relatively simple to handle. If the value of $\theta_0$ is found then the problem can again be reduced to a planar 2D problem which already has a nice solution.

It's easy to see that:
$\theta_0 = atan2(z, x)$

Once this is found, then we can find the absolute distance in the x-z plane $d = \sqrt{x^2 + z^2}$. Then the inverse kinematics for the two links can be found by replacing $x$ with $d$ in all the equations.

Now it's time to add the third link into the equations. This third link is special. It's not really an additional degree of freedom. Two links and a rotating base are all that's needed to reach any point in 3D space. So why the third link? Imagine that there was a gripper attached to the third link. This gripper grabs a glass of water. If that third link were not there, this gripper would tilt with the angle of the second link and whatever liquid is in the glass would spill all over the place. This third link in the arm will adjust its angle to the second link so that it maintains a constant angle from the ground plane. Now if the arm is holding a glass of water and moving around, the glass will always be pointed in the right direction. It won't tilt and spill its contents all over the place. Let the constant angle between the third link and the ground be $\phi$ degrees. Applying this constraint we get: $\theta_3 = \theta_1 - \theta_2 - \phi$.

So all we need to do is subtract the vector of the third link from the target coordinates, apply the 2-link plus base inverse kinematics on this coordinates and then calculate phi using the value of $\theta_1$ and $\theta_2$ that we get from those calculations.

I've implemented all this in the invkin4() function in this python script.

I'll post a processing simulation of this soon. I'm still fiddling around with the P3D. I haven't got used to it yet.

### Interlude: The Electric Kettle Conundrum

So I decided to start using an electric kettle to prepare myself some coffee from this semester onwards. I naively thought that this was a simple matter of unpacking the electric kettle that I had stored away in my cupboard for the last two years, boiling some water in it and getting delicious, steaming coffee. Life is rarely that simple.

I first plugged the kettle into a spike buster that I have in my room. In about five minutes I hear a electric whizzle and see a quick burst of electric white sparks coming from my spike buster. Oops. I blew a fuse. I checked the electrical rating of my kettle. It was rated for 13A and 250V. I checked the fuse of the spike buster. It was rated for 13A and 250V. I guess I was driving the poor instrument at it's limit! Brushing aside the problem of getting a new fuse for the spike buster, I decided to see if it was possible to run the kettle directly off the wall socket.

But just before I connected the kettle and turned it on, I paused for a second. The memory of the white sparks was still fresh in my mind. So I decided to figure out whether the wall socket that I had in my room was capable of delivering the 250V 13A that the kettle required before I plugged it in and possible caused a fire that burned the whole building down.

So I started researching on plugs. Boring? Absolutely not! It turns out that engineers are very systematic creatures that draw up standards for pretty much everything under the sun. So I started hunting for the standards for electrical plugs in India. After a bit of googling I found that there are three standard types of plugs used for wall sockets in India; Types C, D, and M.

 Type C
 Type M
 Type D
Since I wasn't too interested in the Type C plug (I didn't have any in my room) I pulled up some specs on the plugs. I lifted all this stuff directly from this website.

[The Type D plug] has three round prongs that form a triangle. It is a 5A plug. The central earth pin is 20.6 mm long and has a diameter of 7.1 mm. The 5.1 mm line and neutral pins are 14.9 mm long, on centres spaced 19.1 mm apart. The centre-to-centre distance between the grounding pin and the middle of the imaginary line connecting the two power pins is 22.2 mm. Type M, which has larger pins and is rated at 15 amps, is used alongside type D for larger appliances in India, Sri Lanka, Nepal and Pakistan. Some sockets can take both type M and type D plugs.
Although type D is now almost exclusively used in India and Nepal, it can still occasionally be found in hotels in the UK. It should be noted that tourists should not attempt to connect anything to a BS 546 round-pin outlet found in the UK as it is likely to be on a circuit that has a special purpose: e.g. for providing direct current (DC) or for plugging in lamps that are controlled by a light switch or a dimmer.
Type D plugs are among the most dangerous ones in the world: the prongs are not insulated (i.e. the pin shanks do not have a black covering towards the plug body like type C, G, I, L or N plugs), which means that if a type D plug is pulled halfway out, its prongs are still connected to the socket! Little children run the risk of electrocuting themselves when pulling such a plug out and putting their fingers around it. Type D outlets are not recessed into the wall, so they do not provide any protection from touching the live pins either.
http://www.worldstandards.eu/electricity/plugs-and-sockets/d/

[The Type M plug] the Indian type D plug, but its pins are much larger. Type M is a 15 amp plug, which has three round prongs that form a triangle. The central earth pin is 28.6 mm long and has a diameter of 8.7 mm. The 7.1 mm line and neutral pins are 18.6 mm long, on centres spaced 25.4 mm apart. The centre-to-centre distance between the grounding pin and the middle of the imaginary line connecting the two power pins is 28.6 mm.
http://www.worldstandards.eu/electricity/plugs-and-sockets/m/

So I got out my ruler and decided to go ahead and measure my wall socket to see which plug type it was. First I measured the holes. My earth pin had a diameter of 8.7 mm and my live and neutral pins had a diameter of 7.1 mm. I thought that was it! I'd determined that my plug was of Type M! But there was a little unexpected twist! I then measured the spacing between the centres of the live and neutral pins just for fun and found that it was 19.1 mm! This thoroughly confused me. Here was a wall socket that had the pin diameters of a Type M socket but had the pin spacings of a Type D socket! I was unsure what to do! What I wanted to know was the current rating of the wall socket. If the socket had the current rating of Type M then it can supply up to 15A and I can safely run my kettle from it. However, if the socket had the current rating of Type D then it could only supply 5A and I would risk setting something on fire.

So I dived back into the internet and dug deeper. The Bureau of Indian Standards (BIS) is the organization that keeps track of the standards for all the stuff in India and I found that the code for the electrical plug standards is "IS1293". Weird.
So I dug up the pdf of that standard and started reading through it. After skimming through the material at high speed I found what I was looking for on page 83 of that document. The page had actual measurements of the wall sockets. They did not have a plug type with the weird combination of measurements that I measured. So I decided to think about it a bit. Obviously, Type M plugs which are 25.4 mm apart won't fit into this socket that I have even if the pin diameter matches. Since the socket pin spacing was 19.1 mm I assume/deduce that it was not meant to supply power to devices with Type M plugs. So from that page on the standards, this wall socket that I have must either supply 6A or 10A. Since both those values are below the rated 13A for my electric kettle, I think that I should not use it at the moment. I think I'll ask some people in the college about the actual current ratings of the plugs in my room but for now, no kettle.

TL;DR: Dammit! I can't use my kettle!

## Friday, 11 July 2014

### Inverse Kinematics for 2DOF Arm

When I first came across the problem of inverse kinematics I thought - quite naively - that it would be a simple matter to find a solution to the problem because the forward kinematics problem was so simple to solve. I decided to start out with a 2 link arm and see if I could work out the solution on my own from scratch without having any initial knowledge about inverse kinematics. After filling pages and pages with calculations that didn't quite seem to be going anywhere I realized why it was such a difficult problem.

Of course a 2 link arm had a complete close form solution. After a bit of online research I found the solution. However, the solution for arms with more than 2 degrees of freedom turns out to be not so simple. It requires using numerical methods to slowly converge on a solution. This approach doesn't solve everything either. There's still the problem of choosing the right set of angles because there are often multiple solutions for a particular location of the end of the arm and some of these solutions are invalid because they are outside the range of motion of the motors at the joints of the arm.

I've decided to slowly familiarize myself with all the different ways of finding inverse kinematics solutions. I think I'll make blogposts each with a higher degree of freedom and eventually I'll talk about the solution for the 6DOF arm. Why stop at 6? Because it turns out that in 3D space, you don't need more than 6DOF to reach a particular location/orientation. Any more than that and your arm becomes redundant. 3DOF for location in space plus 3DOF for orientation in space are all we need from an arm.

So in this post I'll talk about the solution to 2 link robotic arm. And this arm will have hinge type joints only. I think that the technical term used is "revolute joint".

A 2 link planar robotic arm looks like this:

The forward kinematics equations for this arrangement are simple enough.
$x = l_1 cos(\theta_1) + l_2 cos(\theta_1 + \theta_2)$
$y = l_1 sin(\theta_1) + l_2 sin(\theta_1 + \theta_2)$

The inverse kinematics equations are quite monstrous. The full derivation is given in this fantastic pdf. I'll just write down the final equations here.

Given a value of x and y, the inverse kinematics equations are:
Let
$k_1 = l_1 + l_2 cos(\theta_2)$
$k_2 = l_2 sin(\theta_2)$
$\gamma = atan2(k_2, k_1)$

$\theta_2 = atan2(\sqrt{1 - (\frac{x^2 + y^2 - l_1^2 - l_2^2}{2l_1 l_2})^2}, \frac{x^2 + y^2 - l_1^2 - l_2^2}{2l_1 l_2})$
$\theta_1 = atan2(y, x) - \gamma$

I wrote a simple python script to calculate the values of theta. You can get the code here. The invkin2() function calculates the values of $\theta_1$ and $\theta_2$ for an arm with 2 links.

It was a bit difficult to test the code this way though. Can't see if I'm right or wrong in most cases. So I wrote this processing sketch to visualize the calculations so I can easily verify their correctness.

You can view the processing simulation here: http://rationalash.github.io/invkin/