Sunday 11 November 2012

Raspberry Pi Network Programming

I was recently asked to create a simple networking program to demonstrate to school children during a Science and Technology open day. We used Raspberry Pi computers and I created a simple Instant Messenger program in Python using the Socket interface, which is quite easy. For simplicity sake I had a separate sender program and a receiver program which listens for incoming connections. This is the code for the receiver:

The code for the sender looks like this:
We ran these programs on 10 Raspberries connected together in an cabled LAN. To make this work the Raspberries needed static IP addresses. This is done by editing the interfaces configuration file.

The program works fairly well, although the receiver can only accept one connection at a time. The main aim of the program was to create something simple that was relatively easy to understand. The children enjoyed playing with the program and manged to send and receive messages but didn't really have time to get to understand how the program works.

Sunday 21 October 2012

Arduino with Blue tooth

I want to be able to communicate with the robot I'm planning, not to control it, I'm planning that it will be largely autonomous, but I think it will be useful for development and debugging to get messages from it. A wheeled robot can't really communicate via a cable so I needed a wireless method and my research suggested that Bluetooth was the most suitable and economic method (also I hope to eventually use my mobile phone to communicate with it). I therefore purchased the Blue SMiRF module (made by Sparkfun) from Hobbytronics in the UK (see here). The module does not come with specific instructions about how to connect it to the Ardunio but its not that difficult.
To start with I needed to solder wires to the Blue SMiRF module. These are:

  • RTS (Request to Send) - this is connected directly to the CTS pin
  • 5V+
  • Ground
  • TX (Transmit) - this is connected the the RX pin on the Arduino
  • RX (Receive) - this is connected to the TX pin on the Ardunio
  • CTS (Clear to send) - connected to RTS


Once I did this I supplied the module with 5V and looked to see if the Bluetooth radio on my laptop could see it. To do this I went to Start, then Devices and printers and clicked Add a new device. The Blue SMirF showed up like this:
Click on the device and choose Next. Choose 'Enter the Device's pairing code' then click Next and enter the default code (1234). The module should now be paired.
The module now appears in the Devices and printers window and if you right click on it and choose Properties you can see which COM port it uses (its COM 6 here):
Next I connected the module up to my Ardunio. It is set up to run the motor controller sketch (see previous Blog entry). This requires the user to send an F over the serial link to run the motor forward, S to sop and R to reverse the motor. Note that you must download your sketch to the Ardunio over the USB serial link, you can't do this using the  Bluetooth link. You also MUST set the link speed int he sketch to 115200. It won't work at other speeds.

Now I connect the module to the Ardunio, the RX wire on the module goes to the TX pin on the Ardunio and the TX wire from the module goes to the RX pin on the Ardunio. I power the Ardunio using a 9v battery.

On my laptop I have downloaded a free serial communication program called  PuTTY which I now run. In the Session option I choose Serial, set the COM port to the correct one (COM6 in my case) and the speed to 115 (for 115200), like this:
Then click Open and a serial terminal window should open and the Green LED on the module should light.
The motor can then be run forwards and in reverse and stopped over the Bluetooth link, with messages from the Ardunio displayed in the serial window.



Sunday 2 September 2012

H Bridge

The insect robot was interesting and simple to build but my plans are for a more sophisticated wheeled robot. The next step is to develop a motor controller that can run the motors forwards and backwards. To achieve this a circuit called an H bridge is required. This can be built using discrete transistors but its easier to use an IC. Ardunio suppliers Oomlut sell such a controller, the L293D (see here) which can control 2 motors. They also provide a circuit diagram and a sample sketch. I decided to start off simple and try the IC out with just one motor to start with. I found the circuit diagram supplied by Oomlut a bit difficult to follow so I made a version in Fritzling:
Basically the L293D has 3 digital inputs per motor:
The Enable pin is used to control the speed of the motor
Input pin 1 and 2 is used to control the direction of the motor. Set pin 1 high and pin 2 low to run the motor one way, pin 1 low and pin 2 high to run it the other way. Set both pins low to stop the motor.
The pin outs for the IC are as follows:
I found the sketch that Oomlut provided a bit complex so I wrote my own one:
This sketch uses the serial communication with the Arduino and allows the user to type an 'f' to run the motor forward, 'r' to reverse it and 's' to stop it.
The completed H bridge set up looks like this:
Next I need to add the second motor.




Tuesday 28 August 2012

Robot Insect

I purchased a book called Make: Arduino Bots and Gadgets: Six Embedded Projects with Open Source Hardware and Software (Learning by Discovery) by Tero Karvinen and Kimmo Karvine. One of the projects in there was a simple robot insect which I decided to have a go at creating. The book is quite good with detailed explanations but I didn't follow the instruction exactly and the parts list are for US only. The insect uses two servo motors to move coat hanger wire front and rear legs. I looked for servo motors but most seemed quite expensive (£10 ea or more) but I found UK Arduino specialist oomlout.co.uk sell 'mini' servo motors for £5 each so I ordered a couple.
These are really quite small but appear to do the job. The book suggests using a hot glue gun to glue the two servos together with the front one facing forward and the rear one facing down. I don't have a hot glue gun so I used my old favorite, 'gaffer' tape.
This picture shows the two servos taped together and the rear legs made out of coat hanger wire, and attached to the servo mounting part using thin wire (I also taped a thin strip of gaffer taped over these). For Arduino board itself and the battery I used a small piece of MDF and used cable ties to attach the servos to the MDF. End result looked like this:
Before you attach the legs to the servos you need to center the servos using a very simple sketch. Once the legs are attached you can download the full walking program. I found the some adjustment to the shape of the legs is neeed to get it to walk. I also found it only works on carpet, not on smooth flooring such as laminate or lino as the legs get no grip. I added the wall plug feet to help with the grip, these are not mentioned in the book and some other sort of rubber feet might work better. This video shows it walking:
The sketch required to center the servos is shown below:
This must be uploaded to the Arduino with the servos attached but before the legs are attached to the servos, otherwise the legs may end up mis-aligned.
The sketch that makes the robot walk is as follows:
This basically goes though a loop of raising the front right leg, followed by the back left, then front left leg followed by back right.


Saturday 25 August 2012

Fritzing

Fritzing is a great free tool for creating breadboard diagrams. Much better than the photos I have used so far. You can download it here

Here is the diagram for the motor controller for a single motor, done in Fritzing:

I will add the two motor one shortly. Its a good way to document your projects as well as show other people what you did,

Wednesday 22 August 2012

Computer controlled vehicle

Having tested out the motor controller its now time to put them together to make a simple vehicle. To try this out I just stuck the two robot feet together with gaffer tape:
I used some longer wires to connect them up to the Arduino so they could move around a bit.

I wired up a second motor controller on the breadboard, same as the first one. I made a video of doing this, which you can see here

I then wrote a more sophisticated version of the original sketch which allows me to send commands to the Arduino to get the vehicle to stop and go (as before) and turn right and left. Here it is:


//left and right motors
int motorPinL = 9;
int motorPinR = 10;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pins as an output.
  
   pinMode(motorPinL,OUTPUT);
   pinMode(motorPinR,OUTPUT);
   Serial.begin(9600);
  } 

// the loop routine runs over and over again forever:
void loop() {
  
  if (Serial.available() > 0)
  {
    char ch = Serial.read();
    // press 'g' to go:
    if (ch == 'g')
    { digitalWrite(motorPinL, HIGH);
      digitalWrite(motorPinR, HIGH);
    }
    if (ch == 's')
    // press 's' to stop:
    {digitalWrite(motorPinL, LOW);
    digitalWrite(motorPinR, LOW);
    }
    if (ch == 'r')
    // press 'r' to run just one motor:
    {  digitalWrite(motorPinL, LOW);
      digitalWrite(motorPinR, HIGH);
    }
    if (ch == 'l')
    // press 'l' to run the other motor:
    {
    digitalWrite(motorPinR, LOW);
    digitalWrite(motorPinL, HIGH);}
  }
}

This video shows the vehicle working:

All very well but now I need to look into how to make the vehicle more autonomous, so for example it can avoid obstacles.

Better photo

Here is a better photo showing the wire connections from the breadboard controlling the single motor: