The raspberry pi has been used for so many things and robots are something that are included in that list. I wanted one of my own but I usually I started out with a project. My initial RPi Robot was a Minion Bot Mark I which was a telepresence robot and was controlled using OpenHAB. It had a camera but was assembled in a plastic caserol and I used an RPi SOC for that project.
In this article I go through the process of making MinionBot Mark VI which is much more stable, flexible and is based on the GrovePi+
The first thing we need is a chassis. Now you may buy one from anywhere but I chose to make one. I used up some scrap wood and made a hexagonal base plate. Then using some scrap sheet metal, I made clamps for the motors. In this case the motors and wheels are bought outs and you are free to use any kind you like.
As you can see, I used plastic bottle head and cut them to the correct height and used them as ball castors. On the other side, I used the Grove Motor driver and connected it to the motors. I used an NiMH battery pack to power things up.
This readies my chassis for work. I also 3D printed a mount for the Ultrasonic Range sensor and the RPi Camera. At this point I am missing some components and hence work is halted for now. I have designed a mount for the servo for 3D printing and will have to wait. This is still a work in progress you know...
Unlike my previous tutorials, we will work in the GUI and create a little guy of our own. Lets setup remote access to the RPi’s Windows env.
VNC (Virtual Network Computing) is one way we can control and monitor a computers desktop from another computer over a network. Which in our case is going to be useful for wireless remote teleoperation of the robot and basic control of Raspberry Pi. I am assuming that we have setup a static IP for the RPi and it is connected to our local network. On the RPi we need to install tightvnc by running the command
sudo apt-get install tightvncserver
once it is done, we move on to starting a server by issuing the command
vncserver :1 -geometry 1280x800 -depth 16 -pixelformat rgb565
It should ask you to enter a password which will be used for remote access. If not then run the following command
After the password is set its time to login to the sever from another computer.
If you are running windows or linux, then download the appropriate version of ultravnc.
On a Mac OS, use the screen sharing app.
The next thing to do is connect to the RPi and start writing some python scripts.
There are a number of ways you can control motors from an RPi but I have chosen to use the seeedstudios grove Pi + for this particular bot. In order to use it, we need to download some ready made scripts to test things out. Go to https://github.com/DexterInd/GrovePi and download the zip file.
There is a software folder which not only has python but also nodejs and C and Shell example code. Now you may choose to employ and arduino or even connect a motor driver directly for which you will have to write your own functions for movement. In another post, I will be using the gert board to control some stepper/servo motors but this time its gonna be the Grove Pi+ and friends.
I am not providing a tutorial but instead a step by step description of what I did and usually do when I come across a new platform. This should help you understand things a bit better.
Enabling I2C and SPI on the RPI
By default the I2C and SPI interfaces on the RPi are no enabled. We need to make some changes.
First type the following command in your command prompt
Go to Advanced Options -> I2C -> Yes
The screen will ask if you want the interface to be enabled :
The screen will ask if you want the module to be loaded by default : Select “Yes”
Repeat the same for the SPI module.
In addition to this, we need to edit the modules file. Execute the command
sudo nano /etc/modules
and it will open the modules file in the editor. Add the following line to the end.
Use Ctrl-X, Y and enter to save the file and exit.
Reboot. You should have the modules enabled and to check run the following command
lsmod | grep i2c_
This should list out i2c modules and the presence of i2c_bcm2708 in the list will indicate that all is as it should be.
Great so now we have the I2C and SPI all setup and we can move to testing out the motors
For details on I2C refer to https://learn.sparkfun.com/tutorials/i2c The first thing I need to
The motor driver in question looks something like the image shown below. In this case, we want to test out the motors first and for that the python script is as follows
#!/usr/bin/env python# Description: Grove Motor Drive via I2C# Author : Inderpreet Singhimport smbusimport timeimport RPi.GPIO as GPIO# Global Stuff HereDRIVER_ADDR = 0x0fMOTOR_SPEED_SET = 0x82PWM_FREQUENCE_SET = 0x84DIRECTION_SET = 0xaaMOTOR_SET_A = 0xa1MOTOR_SET_B = 0xa5NOTHING = 0x01ENABLE_STEPPER = 0x1aUNENABLE_STEPPER = 0x1bSTEPERNU = 0x1c# The Once Code Here# Check what revions for SMBus IDrev = GPIO.RPI_REVISIONif rev==2 or rev ==3: bus = smbus.SMBus(1)else: bus = smbus.SMBus(0)# Function Defs heredef MotorSpeedSetAB(SpeedA, SpeedB): bus.write_i2c_block_data(DRIVER_ADDR, MOTOR_SPEED_SET, [SpeedA, SpeedB])def MotorPWMFrequencySet(Freq): bus.write_i2c_block_data(DRIVER_ADDR, PWM_FREQUENCE_SET, [Freq, NOTHING])def MotorDirectionSet(Dir): bus.write_i2c_block_data(DRIVER_ADDR, DIRECTION_SET, [Dir,NOTHING])# Things to do oncetime.sleep(1.0) MotorSpeedSetAB(250, 250) time.sleep(1)# The Looping Code Heretry: while True: # Loop Things here MotorDirectionSet(0b00001010) time.sleep(5) MotorDirectionSet(0b00000000) time.sleep(2) MotorDirectionSet(0b00000101) time.sleep(5) MotorDirectionSet(0b00000000) time.sleep(2)except: print 'Somthing went wrong or you pressed Ctrl+C'finally: print 'Cleaning up Things...' GPIO.cleanup()
This works for me and the motors move forward and then backwards as they should. I need a better battery though.
You can replace the functions with your own to control the speed and direction control.
I wanted to upload a video but unable to… yet.
Lets have some fun with this one...
The world of windows has shiny buttons and stuff and you can use the mouse to interact with objects. We need a Graphical User Interface albeit a simple one here as well. Hence we start with TkInter.
Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a fast and easy way to create GUI applications.
This link is a lot of help. http://www.tutorialspoint.com/python/python_gui_pr...
I created a new script as follows:
# Description: Grove Motor Drive via I2C
# Author : Inderpreet Singh
import RPi.GPIO as GPIO
# Global Stuff Here
DRIVER_ADDR = 0x0f
MOTOR_SPEED_SET = 0x82
PWM_FREQUENCE_SET = 0x84
DIRECTION_SET = 0xaa
MOTOR_SET_A = 0xa1
MOTOR_SET_B = 0xa5
NOTHING = 0x01
ENABLE_STEPPER = 0x1a
UNENABLE_STEPPER = 0x1b
STEPERNU = 0x1c
# The Once Code Here
# Check what revions for SMBus ID
rev = GPIO.RPI_REVISION
if rev==2 or rev ==3:
bus = smbus.SMBus(1)
bus = smbus.SMBus(0)
# Function Defs here
def MotorSpeedSetAB(SpeedA, SpeedB):
bus.write_i2c_block_data(DRIVER_ADDR, MOTOR_SPEED_SET, [SpeedA, SpeedB])
bus.write_i2c_block_data(DRIVER_ADDR, PWM_FREQUENCE_SET, [Freq, NOTHING])
bus.write_i2c_block_data(DRIVER_ADDR, DIRECTION_SET, [Dir,NOTHING])
# Things to do once
GUI = Tk()
This code creates a window as shown below and allows easy control of our robot using a simple GUI
So now we have a little robot control and we should be able to use VNC to connect to it remotely.
More on this next time… Stay tuned ;)