NXT Self Driving Car


The Project

In this project I am making a self-driving car using Lego Mindstorms NXT and programming it using the Lego Mindstorms NXT programming software.


How To Do It Yourself

Step 1 Build the Car:
To build the car first you need a Lego Mindstorms NXT set. You need to build a car with two motors. The one on the right of the car should be connected to port A on the Mindstorms NXT brick and the one on the left of the car should be connected to port C. The ultrasonic sensor should be at the front of the car and should be connected to port 4 of the NXT Brick.
Here’s a short video explaining it:

Step 2 Write the Program:
The program is written using the NXT Software. In the program I tell the car to keep moving if there isn’t a obstacle in front of it and go backwards and turn in a completely different direction if there is an obstacle in front of it. The car also makes a noise when an obstacle is encountered.



The Project

The project is to take photos with a camera connected to my Raspberry Pi and stream live video from the camera via my Raspberry Pi to my iPad.

What is a Raspberry Pi?: http://www.raspberrypi.org/



July 24, 2014:

I finished the entire project today. This project was easy and wasn’t as challenging as the other projects I have done. The hardest part was finding out how to stream the video to my iPad. I researched on the internet and found an app called VLC which allows you to stream video. Then I installed VLC on the Raspberry Pi and typed in a command to stream the camera video. Then I entered the Raspberry Pi’s ip address to view the video on my iPad.

How To Do It Yourself

Step 1: Set up the camera

Step 2: How to take pictures and videos

To take a photo with the Raspberry Pi camera enter the following command into the LXTerminal after pointing the camera at what you want to take a picture of sudo raspistill -o filename.jpg This will save an image into your home directory. To view the image go to the file manager on your Pi and go to the home directory.

To take a video point at what you want to take a video of and enter sudo raspivid -o filename.h264 -t 10000  into the LXTerminal. This will save a video that is ten seconds long into your home directory. To change the duration of the video change the number after “-t” which is the duration of the video in milliseconds.

Step 3 Stream video to your iPad

To stream video first you need to install VLC on both the iPad and the Raspberry Pi. To install VLC on the Raspberry Pi enter
sudo apt-get install vlc  into the LXTerminal and wait for vlc to finish installing. To install VLC on the iPad go to the app store and download VLC.

Now to start streaming live video from the Raspberry Pi camera enter raspivid -o - -t 9999999 |cvlc -vvv stream:///dev/stdin --sout '#standard{access=http,mux=ts,dst=:8554}' :demux=h264 into the LXTerminal.

This will start the video stream. To view the video stream your Raspberry Pi must be connected to the internet with a wifi dongle.

Then you have to find out the ip address of your Raspberry Pi. This can be done by typing “ifconfig” into the LXTerminal.

Then go on VLC on your iPad and click “open network stream” and type in http://IPaddressOfRaspberryPi:8554 as the address. Replace where it says “IPaddressOfRaspberryPi” with your pi’s ip address. Then you should see video on your iPad.

Keyboard Controlled NXT Robot


The Project:

In this project I made a Lego Mindstorms NXT robot which is connected by USB to my Mac laptop. The robot moves and is controlled by my laptop’s keyboard. The program for this robot is written in the programming language Python and the Python module nxt-python.


July 22,2014:

I completed the project today.The hardest part of this project so far was finding out how to install all the software I needed such as nxt-python. I found out that to install nxt-python I had to install a python library called Py-USB. I installed the library but then I saw that it wasn’t working. After a couple hours of research I discovered that for Py-USB to work I had to install a C library called LibUsb which allows applications easy access to USB devices. After installing all the software writing the code actually turned out to be the easy part and there weren’t many bugs or problems in it. 

July 23, 2014:

After finishing the main project I have decided to go to the next step which is connecting the robot to the Raspberry Pi to be able to control it with a wireless keyboard. I have also decided to add a camera to the robot called the Raspicam which is connected to the Raspberry Pi and will move up and down when I press the keyboard keys. I will also stream the camera’s live video to be able to view it on the iPad. I have a separate project just for streaming from the Raspberry Pi camera which can be found here: https://vectorcalc.wordpress.com/2014/07/01/raspicam/ .

How To Do It Yourself:

Below are the steps for creating the robot but they only work if you have a Mac you can go to https://code.google.com/p/nxt-python/wiki/Installation for more information on how to install nxt-python on other operating systems.

Step 1: Build the the Lego Mindstorms Robot:

You need the Lego Mindstorms NXT 2.0 kit to build this robot. You can buy it on Amazon but it is pretty expensive (around $529 on Amazon).The robot is basically like a car with two motors which are connected to the NXT Brick. The instructions are included in the user manual of the NXT 2.0 kit. make sure the left motor is plugged in to PORT C and the right motor is plugged in to PORT A on the NXT brick.

Step 2: Install LibUSB:
LibUSB is a C library that gives applications easy access to USB devices on many different operating systems. To install it first you need to install homebrew. Do this by going to your Mac’s Terminal and copying and pasting the following command.
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
Then press enter.
Next enter the command brew install libusb into the Terminal and press enter.

Step 3: Install PyUSB:
PyUSB is a python module for communicating with USB devices.
To install it go to http://sourceforge.net/projects/pyusb/?source=dlp and download the file. Once the file is downloaded unzip it and copy and paste the folder onto your Desktop.
Then go to your Terminal and enter cd Desktop/pyusb-1.0.0b1 and press enter. This will take you inside the folder in the Terminal. Then enter python setup.py.install into the Terminal and press enter.

Step 4: Install nxt-python:
nxt-python is a Python module used to communicate with the NXT brick. To install it go to https://code.google.com/p/nxt-python/downloads/list and download nxt-python-2.2.2.zip. Unzip it and place the resulting folder onto your Desktop. Then go to your terminal and enter cd Desktop/nxt-python-2.2.2 and press enter. Then enter python setup.py.install into the terminal and press enter.

Step 5: Write your Python program:
You can copy my python program or look up the nxt-python documentation and make your own. Just create a text file in text edit and type the following code or create your own:

import nxt.locator

import sys

import tty, termios

from nxt.motor import *

b = nxt.locator.find_one_brick()

m_left = Motor(b, PORT_C)

m_right = Motor(b, PORT_A)

both = nxt.SynchronizedMotors(m_left, m_right, 0)
rightboth = nxt.SynchronizedMotors(m_left, m_right, 100)
leftboth = nxt.SynchronizedMotors(m_right, m_left, 100)

def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
ch = sys.stdin.read(1)
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch

ch = ‘ ‘
print “Ready”
while ch != ‘q’:
ch = getch()

if ch == ‘w’:
both.turn(100, 360, False)
elif ch == ‘s’:
both.turn(-100, 360, False)
elif ch == ‘a’:
leftboth.turn(100, 90, False)
elif ch == ‘d’:
rightboth.turn(100, 90, False)
print “Aborted!”

Step 6: Run the Code! :

When you are done with the code save it onto your Desktop with the exact name “robot.py” then make sure your NXT brick is connected via USB into your Mac and that the brick is turned on. Now go to the terminal and type cd Desktop and press enter. Then enter python robot.py into the terminal and press enter. Use the ‘w’,’a’,’s’,and ‘d’ keys to move your robot and press ‘q’ to stop the program.