Monday, 8 December 2014

Using the ESP8266 WiFi module with an Arduino

So I was pretty excited when I discovered the new cheap ESP8266 wifi module. It seemed too good to be true. I kept feeling that it's got way too many features for it's cost. There had to be a catch somewhere.

But no! I had mine delivered to me a couple of days ago and it's the real thing! Of course, it's true that it's a bit difficult to get it working as the documentation available is mostly in Chinese but there are plenty of people working on a translation. I had a little trouble getting the thing working properly. So I'll give a short tutorial on how to get the module working with an Arduino so that others find it easier.

First of all, here's the pinout of the module:
That's the pinout when looking at the module from the side of the antenna. The antenna is that crazy trace that looks like a square wave.

The module works on 3.3v. So wire up the Vcc pin of the module to the 3.3v pin of the Arduino.
GND goes to ground.
Both RST and CH_PD also goes to 3.3v.

Now here's the tricky bit. The arduino is fine with reading incoming 3.3v signals on its digital input pins but the ESP8266 is NOT ok with incoming 5V signals from the Arduino. So use a simple voltage divider. (1k, 2k resistor) to step down the 5V signal from Arduino to 3.3V.

Another point to note: The RXD of the ESP8266 goes to the RX pin of the Arduino (pin 0) and the TXD of the ESP8266 goes to the TX pin of the Arduino (pin 1). It looks like the Arduino has the labels the other way round. This is because when the Atmega IC is removed, the TX and RX pins are the RX and TX pins of the USB to UART bridge. When the IC is actually in the socket, I suspect that the TX of the module should go to the RX and the RX of the module to the TX of the Arduino board.

I then removed the Atmega328 IC from the Arduino's socket and turned on the power. You should see a red LED light up on the ESP8266 indicating that the board is powered. A blue LED should briefly flash. That blue LED indicates serial communication. Now go to the Arduino IDE and open up the Serial monitor. Set the line ending to "Both NL and CR".

Some ESP8266 modules come with old firmware that requires you to set the baud rate to 115200. Some modules come with the new version of the firmware and a default baud rate of 9600. So try out different baud rates if you get gibberish on the monitor. Once you find the correct baud rate try typing in "AT+RST" into the serial monitor. You should get some output. Here's what I get:

The wifi module is controlled using "AT commands". There is a list of the available commands here. Say you want to connect to your home wifi network.

First type in "AT+CWMODE?". This gives you the current mode the module is operating in. You need to set it to mode 3 if you want to connect to your home router. To set the mode to 3 use the command, "AT+CWMODE=3".

Then type in "AT+CWLAP"(List Access Points). This will give you a list of available wifi networks. Hopefully, you see yours on the list.

To join an Access Point I have to use the command "AT+CWJAP"(Join Access Point).
The syntax is "AT+CWJAP="SSID","password".

And there you have it! You've connected to your router with the ESP8266. There's all sorts of cool things you can do after this. You can set up a TCP or UDP server and start streaming sensor data for example. Maybe I'll write about that in the next blogpost.

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

Monday, 20 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.

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

After I got the board the next step was getting the Xilinx ISE from their website. This was a big Pain In The Ass. The installation file was 6.1 GB and would take ages to download using my sort of slow internet dongle. The download was resumable but something would always go wrong at about 4 GB and the download would start over. In the end I had to start the download at 4.00 am in the night (When the college wifi is fast) and I had it downloaded in about 2 hours. I think it's a real shame that Xilinx doesn't distribute the software over a protocol that is more suited to resuming large downloads (maybe a torrent?). Getting my hands on the FPGA was way simpler that downloading the software.

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.

EDIT: Go here to see a processing simulation of the inverse kinematics script. :D

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. 

[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. 

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:
$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:

Friday, 4 July 2014

Completed Task 1!

I've completed task 1 of the Eudyptula Challenge! I know that's not really something to be excited about since it was a very basic task but I'm still excited! I've got a mail with details of task 2. This is getting more interesting.

I've read online that most people are getting stuck with task 6. I wonder what that task is. Hmmm...

I think starting this challenge was a brilliant idea. I'm learning way more than in any of the classes I've ever taken. Learning through solving a set of incrementally difficult practical challenge is the best thing ever!

The task recommends that I try out all the code with a regular system and not a virtual machine but I think I'm a bit too inexperienced to work with my regular laptop. If I somehow mess up the kernel and brick my laptop It's a huge loss. I can't afford to be without my laptop for extended periods of time at the moment. So after weighing the risks I think I'll just stick to running Ubuntu 14.04 in virtualbox.

Vritualbox is actually really handy when it comes to stuff like this. I even used that system to test out and become used to the i3 window manager before installing it on my main system. If anyone is thinking of trying out a new window manager it might be a good idea to use it in a virtual machine and tweaking all the config files in that before transferring it to your actual system.

So on to task 2!

Thursday, 3 July 2014

Switching to the i3 Window Manager

I've been thinking about switching to tiling window managers ever since I discovered the awesome tiling features of Emacs. But there were a lot of things that kept me bound to a desktop environment like Gnome. The network manager was the biggest thing. Since I use a mobile internet connection more often than WiFi I regularly had to use the network manager app in Ubuntu to configure and edit these connections (They're a bit temperamental). But after a while my curiosity got the best of me and I finally decided to give it a go.

My initial choice was the awesome window manager. I picked it because it seemed popular and a lot of people on Linux forums were recommending it. Despite all the praise people seemed to have for it, I didn't quite like it. I'd spend ages trying to get all the windows in exactly the order and tiling configuration I wanted and even then it was a bit tricky to navigate. There was also the fact that I needed to learn the Lua programming language to configure it. 

Then I came across the i3 window manager. I went to YouTube and watched a couple of videos of people trying it out and was reasonably impressed with the degree of flexibility it offered. I liked the tree based structure they used to implement their containers and loved the way I could open new tiled windows in any orientation I wanted by using a simple keyboard shortcut. I found that I preferred it to the weird interface that awesome provided.

So now that I was stuck with i3, I needed to tackle the huge challenge of getting all the monitoring programs working inside it. I use a laptop that is quite prone to overheating. So I needed a temperature monitoring program to update the CPU's current temperature to a bar that's visible across all workspaces. I needed to know the current temperature of the system with a quick glance. I also needed the network manager applet and a Guake terminal running. Also, the default font that i3 uses looks really ugly. So I needed to figure out how to change that as well. i3 doesn't require you to know some weird obscure programming language like Lua to configure it. It used simple text files for configuration! 

Once I had the basic stuff configured I needed to tackle the biggest annoyance with simple window managers - no simple interface for suspend, lock, shutdown and logout. Ok logout is simple. I just need to press Mod4+Shift+e to exit the window manager but suspending the laptop and locking its screen were a bit tricky.

After hours of googling I found a really neat script online which handled everything. It even had a neat pyGtk GUI. I downloaded the script, modified a little bit to suit my needs and bound it to Mod4+Esc. And there's this really neat program called xautolock which puts the computer to sleep when I don't use it for a while. 

I've uploaded all my config files to my github account here:

So finally here's how my laptop looks now:

It was a bit of a pain to fiddle around with config files for hours to get all the basic functionality up and running but I quite like the freedom it gives me to make my desktop look however I want it to look! :D

Tuesday, 17 June 2014

Microcontrollers and Random Stuff

It's really been ages since I wrote anything significant on my blog. So I think it's time that I breathed some new life into it. I couldn't find much time to write anything during the semester but I think I'll put in some effort to keep this blog updated at least during the holidays.

I've been working with microcontrollers for over a year now and I've become reasonably familiar with  them. One thing I've discovered after working with them for so long is that it's really really annoying to get it to work with libraries that other people wrote. It's not that they don't work but it's not easy to figure out exactly what's going on. And sometimes, the function names can be a bit weird. This is just my personal opinion though. Maybe others have had different experiences with libraries.

Anyway, I was recently working on a little project in which I had to display text on a 20x4 LCD module and I was finding it difficult to get libraries to work exactly the way I wanted them to. So I decided to go ahead and write my own library for it. And yes, I realize that what I'm doing is probably best described by this:


But it's a good learning experience and if this library helps someone (who thinks like me) else out then that's a good thing. The library is just a single file and all you need to do is include it in your main source file to use its functions.

So if anyone's interested they can go here to download the library.

And finally, here's a short video of the library in action.

Monday, 16 June 2014

The Eudyptula Challenge

So I've decided to take the Eudyptula Challenge!!

The challenge is a set of tasks of slowly increasing difficulty that teaches a person how to write kernel modules. I've always wanted to learn about the linux kernel. I just didn't know where to start. This looks like the perfect opportunity for me to start learning.

The challenge is very strict about not posting the code that I write on websites. So I won't be doing that. I think I will give general updates about my progress though.

The first task is to write a simple hello world kernel module. Looks like I need to start off by finding out how to write a kernel module.

Looks like I'm going to have an interesting time ahead!