Introduction: RaspRob, the Raspberry Robot
This is my guide how-to create a robot that is controlled via Internet.
The built in webcam makes the robot easy to control from any PC or smartphone! We will use python to control the GPIO ports on our Pi and .php to access the python scripts.
Parts needed:
Raspberry Pi (up and running)
LEGO.
2 LEGO motors
Motor driver (TB6612FNG Dual Motor Driver Carrier)
Some cables
WiFi dongle
Batterypack
Webcam
Cardboard, wallpaper paste and paint
Step 1: Step 1: Preparations and Get Rid of All Cables.
This instructable assumes the Raspberry Pi is installed with Rasperian, configured and up and running.
Our goal is to the rid of all cables so let’s start with the network cable;
WiFi:
I suggest that you use Wicd to manage the wifi. Really easy to setup and works without problem for me. Install:
sudo apt-get install wicd-curses
Run and setup Wicd:
sudo wicd-curses
- Highlight the network you want to connect to and press the right cursor key to configure it
- Scroll down and check ‘Automatically connect to this network‘
- Scroll down to the bottom and enter the wireless key
- Press [F10] to save.
Disconnect your network cable, reboot and now you should be wireless.
Mouse and Keyboard:
To get rid of the mouse and keyboard connect via SSH to your Raspberry. I use PuTTY.
Power:
The final cable that we need to remove is the power cable. I simply use a battery pack designed for mobile phones. And that's it! We are truly wireless!
Step 2: Step 2: Install Webserver and PHP
We will now install a webserver and PHP on our Pi.
We will later create a .php-file with buttons to control our Robot and also a live feed from our webcam.
Install and start with the following commands;
sudo apt-get install apache2 php5 libapache2-mod-php5sudo
service apache2 restart
Enter the I.P. address of your Raspberry Pi into your web browser. You should see a simple page that says "It Works!"
Step 3: Step 3: Webcam
Now it’s time to get the webcam up and running. We will start with the installation of motion;
sudo apt-get install motion
We will need to configure some things to get it working as we want. Let’s start with changing motion to run as daemon and change Webcam_localhost to off. We will do this in the file motion.conf so we edit it with;
sudo nano /etc/motion/motion.conf
Find the rows “DAEMON = OFF” and “Webcam_localhost = ON” and chage them to;
DAEMON = ONWebcam_localhost = OFF
Then edit /etc/default/motion and change start_motion_daemon = no to yes so it looks like;
start_motion_daemon = yes
Now start the service;
sudo service motion start
Connect the webcam and enter the I.P. address of your Raspberry Pi followed by :8081 (ex: http://192.168.0.17:8081/ ) into your web browser and you will see a live feed from your webcam!
Step 4: Step 4: Motor Drivers
I use TB6612FNG Dual Motor Driver to control my LEGO motors. We need a motor driver
to be able to switch the + and – to make the LEGO motors to run in two different directions. It’s easy to connect the motor driver and the best way is to use a break out board. The motor driver TB6612FNG is connected like this;
Left side
- GND - Raspberry ground
- VCC - VCC from Raspberry
- AO1 - Output to (-) lead of motor A
- AO2 - Output to (+) lead of motor A
- BO2 - Output to (+) lead of motor B
- BO1 - Output to (-) lead of motor B
- VMOT - positive pole of motor battery
- GND - negative pole of motor battery
Right side
- PWMA - VCC from Raspberry
- AIN2 - digital pin on Raspberry(pin 18)
- AIN1 - digital pin on Raspberry (pin 17)
- STBY - VCC from Raspberry
- BIN1 - digital pin on Raspberry (pin 24)
- BIN2 - digital pin on Raspberry (pin 23)
- PWMB - VCC from Raspberry
- GND - GND of Raspberry
I use a 9v battery to power my motors.
I don’t use PWM and connect it to VCC.
When pin 17 is high and pin 18 is low motor A will turn clockwise and when pin 17 is low and pin 18 is high motor A will turn counter-clockwise. The same thing goes for Motor B with pin 23 and 24.
Step 5: Step 4: GPIO and Python
We now have our motors connected and it’s time to get control over them. We will do this with Python scripts. We will create four different scripts, one for each direction.
- Forward – Both motors forward – Pin 17 High, Pin 18 low, Pin 23 High, Pin 25 low
- Backwards – Both motors backwards - Pin 17 low, Pin 18 high, Pin 23 low, Pin 25 high
- Turn Right – Motor A forward and Motor B backwards - Pin 17 high, Pin 18 low, Pin 23 low, Pin 25 high
- Turn left – Motor A backwards and Motor B forwards - Pin 17 low, Pin 18 high, Pin 23 high, Pin 25 low
To get started we begin with installing the Python Development toolkit and GPIO libraries for Raspberry Pi;
sudo apt-get install python-dev
sudo apt-get install python-rpi.gpio
Now we are ready to write our scripts.
Crete the first one, named forward.py, with this command;
sudo nano forward.py
And paste the following code;
import RPi.GPIO as GPIO
import timedef main():
print "executing script"
# tell the GPIO module that we want # to use the chip's pin numbering scheme GPIO.setmode(GPIO.BCM) # setup pin 17 as an output GPIO.setup(17,GPIO.OUT) GPIO.setup(24,GPIO.OUT)
# turn pin on GPIO.output(17,True) GPIO.output(24,True) # sleep for 0,2 seconds time.sleep(0.2) # turn the pin off GPIO.output(17,False) GPIO.output(24,False)
GPIO.cleanup()
if __name__=="__main__": main()
Test it with;
sudo python forward.py
Now we do the same thing for the
other 3 scripts;
backwards.py:
import RPi.GPIO as GPIO
import timedef main():
print "executing script"
# tell the GPIO module that we want # to use the chip's pin numbering scheme GPIO.setmode(GPIO.BCM) # setup pin 17 as an output GPIO.setup(18,GPIO.OUT) GPIO.setup(23,GPIO.OUT)
# turn pin on GPIO.output(18,True) GPIO.output(23,True) # sleep for 0,2 seconds time.sleep(0.2) # turn the pin off GPIO.output(18,False) GPIO.output(23,False)
GPIO.cleanup()
if __name__=="__main__": main()
left.py:
import RPi.GPIO as GPIO
import timedef main():
print "executing script"
# tell the GPIO module that we want # to use the chip's pin numbering scheme GPIO.setmode(GPIO.BCM) # setup pin 17 as an output GPIO.setup(18,GPIO.OUT) GPIO.setup(24,GPIO.OUT)
# turn pin on GPIO.output(18,True) GPIO.output(24,True) # sleep for 0,2 seconds time.sleep(0.2) # turn the pin off GPIO.output(18,False) GPIO.output(24,False)
GPIO.cleanup()
if __name__=="__main__": main()
right.py:
import RPi.GPIO as GPIO
import timedef main():
print "executing script"
# tell the GPIO module that we want # to use the chip's pin numbering scheme GPIO.setmode(GPIO.BCM) # setup pin 17 as an output GPIO.setup(17,GPIO.OUT) GPIO.setup(23,GPIO.OUT)
# turn pin on GPIO.output(17,True) GPIO.output(23,True) # sleep for 0,2 seconds time.sleep(0.2) # turn the pin off GPIO.output(17,False) GPIO.output(23,False)
GPIO.cleanup()
if __name__=="__main__": main()
Perfect! Now we can control our motors with our four python scripts!
Step 6: Step 5: Cretating Our .php File
Now it’s time to create our .php
file with buttons that start our four different python scripts. The page will also show the live feed from our webcam!
The first thing we need to do is to set permissions so the .php file can start our python scripts. This is done by adding this line..
www-data ALL=(root) NOPASSWD:ALL
…at the end in the file sudoers. So run;
cd /etc/
sudo nano sudoers
and paste the line “www-data ALL=(root) NOPASSWD:ALL” at the bottom of the file.
Now we are ready to create our .php page. This is pretty straight forward. We create a button and connect our script with it. Se the attached file control.php (in Files.rar together with the python scripts).
Save the file as control.php in /var/www .
I use Cyberduck to copy files from ant to my Pi. It's a small application that you install on your PC and it uses port 22 (same as SSH).
Download, install and run Cyberduck. Then choose “Open connection” and type in;
- SFTP
- IP-adress to your Pi
- Port 22
- User = Pi
- Your password
Then press “connect” and you will have a file manager on your Pi up and running.
Copy control.php to /var/www on ypur Pi.
Now you can test it! Enter the I.P. address of your Raspberry Pi followed by /control.php (ex: http://192.168.0.17/control.php ) into your web browser and you will see your buttons!
Now create a webpage with two frames and add the live feed (ex: http://192.168.0.17:8081/ ) in one of them and the control.php in the other (ex: http://192.168.0.17:8081/ Save the file with whatever name you want ( .htm). I use robot.htm.
Now you can access the control.php and live feed from your network and control the motors remote! If you forward port 80 to your PI’s IP address in your router you will be able to remote it from anywhere in the world as long as you have internet access!
Step 7: Step 6: Building the Robot
Let’s build! I started with the motors and after some testing I found
the best gearing for speed vs. torque.
Then I just continued building to get all components it the right place. Hard to explain how, see my pictures for a better guide. I wanted the robot to have legs, even if it don’t use them to walk… But I think it looks better and more like a Robot.
Then I used some waste cardboard and papier mache and after that some paint. For the neck I used a Coco cola bottle. Again, see my pictures!
That's it! The Robot is ready for use!
The latest add-on is a Ranging Distance Measurement Module (RCW-0002) that will prevent the robot from running into things and added a self-driving script that run forward until it gets around 10 cm from anything. Then the robot stops, goes backwards 1 second and turn left for about 45 degrees and forward again. It works fine with for example walls, but chair legs and other things are still a problem. I thing I need a few more RCW-0002 attached and aimed in different angels to get it to work perfect.
Participated in the
Hand Tools Only Contest 2016
Participated in the
Raspberry Pi Contest 2016
Participated in the
Full Spectrum Laser Contest 2016