Web ControlleD Multifunctional RoboT
by Ashu_d in Circuits > Raspberry Pi
4413 Views, 29 Favorites, 0 Comments
Web ControlleD Multifunctional RoboT

Make a web control multi functional robot built with minimal hardware and basic programming build on popular open source platform i.e raspberry pi. Apart from basic motor control, this tutorial aims to integrate popular sensor like distance, humidity/temp, RTC etc.This sensor apart from giving feedback can be used to control robots in many ways, a part of which is hereby brought here.
Parts List

















- Raspberry pi (Any model) but with the recent launch of pizero or pi 2 shall be a good option, my current model employs B+. (It is highly recommended to go with models pi 2 to get a nice browsing experience like our windows counterpart)
- 40 pin GPIO cable (if using pi B+ or pi 2)
- Breadboard (for quickly assembly various sensors)
- A 4wd chassis (so as to enough power to carry battery/sensor loads and cables)
- Distance sensor (Ultrasonic HC SR04)
- Humidity & Temp sensor (DHT11)
- PI Camera
- Buzzer module
- Powerbank (HP for powerng up the pi)
- Rechargable batteries AA (preferably 2100 mAH) 4 nos
- Jumper wires both Male & female type
- Resistors
- wifi adapter (EDUP/EDIMAX for communicating wirelessly with pi)
- Memory card (4GB and higher for running the OS on pi)
- Motor drivers (L293D or higher, but L293D are very cheap and robust can be used without any issues, preferably 2 nos for 4wd )
- servo motor
- servo pan
- Nylon cable ties (for tying the jumper wires) and foam tape (for holding servo or any other sensor where screw assembly cannot be used)
Getting Started - Making Raspberry Pi Ready
- The first step is to make memory card ready with the OS image, as a starter one can go for " wheezy" or now recently launched " Jessie" . The new version " Jessie" now provides GUI interface which gives experience like windows system.
- The start up guide as provided by Raspberry pi .org is included above or one can go do the following link https://www.raspberrypi.org/help/quick-start-guide... (As a preliminary you may need a monitor/TV HDMI cable, keyboard and mouse, for configuring but latter we would see that once we configure wifi, we can remotely control pi from our desktop/laptop/tablet or mobile)
Once configured now we are ready to for the next step.
Downloads
Setting Up " Wifi"

If you had downloaded the "Jessie" image on Pi SD card, than setting up the wifi is quite easy, follow the word document or go the link : https://www.raspberrypi.org/documentation/configur...
For older version of OS, please refer to the word document.
Once done, you would be able to connect to wifi, now its the time when you can connect pi from your remote computer, but remember the computer should be on the same network as pi, as web controlfor pi is still not enabled.
It should be also noted that your pi is assigned the IP address from your router, so each time you switch on and off your pi, the IP addr shall be different. In order to assign the static IP addr there are two ways
- Refer to link : http://www.modmypi.com/blog/tutorial-how-to-give-y... (this gives static IP to pi)
- OR its better to open your router page (most router default is at 192.168.1.1) and assign static address to pi by virtue of its mac address in DHCP reservation as shown above( I find this easier when moving on to different networks)
Still if you don't assign any IP to your pi , you can still connect to your pi by randomly connecting with IP for the series generated by your router i.e say your home router assigns 192.168.10.101 to your laptop...102 to your mobile and say 103 to your pi, so next time you connect the same may be 101 for your pi, so there is always a chance to guess the last number by two or three random strokes.
Downloads
Connecting - Remotely to Your Pi


The easiest way to connect to your pi, is through SSH, to get it going first enable SSH on pi as
sudo raspi-config
Than go to SSH, press enter and select enable " SSH Server"
You also need to install SSH client on your say windows system for downloading go to following link : http://www.putty.org/ and download putty client
After installation of the putty client, open it and the window as in above image shall pop up, input the IP addr of pi and you would be taken to the pi (you have to input username and password).
The SSH can give you command line control for pi but for GUI control we can install VNC viewer on pi as
sudo apt-get install tightvncserver
tightvncserver
Than enter the password if prompted (save this as this would be required for logging each time) further than you need to install tightvncviewer on your windows/mac system http://www.tightvnc.com/download.php
After installation, open the VNC viewer on your system and input the pi IP addr, but remember each time you boot your pi, you have to run tightvncserver on pi , which shall give the pi desktop a number, which shall than have to be input along with IP address on tightvncviewer on your windows system (pl check the image above)
For Auto boot of tightvncserver on your pi, follow the following link : https://www.raspberrypi.org/documentation/remote-a...
Setting Up Robot Platform



A good robot platform is the foremost requirement to support and carry our pi, sensors, battery bank, etc. There are variety of options available and one can go for 2wd or 4wd.
For 2wd there are two DC motors and torque output would be less than 4wd, so I have gone for 4wd, but remember to turn a 2wd is quite easy, whereas turning a 4wd might be a tricky.
Anyhow you can always experiment with the best platform. In my current tutorial I have gone with 4wd of Dagu
Assembling is quite easy for procurement and manual you can follow the following link http://www.dagurobot.com/goods.php?id=55
The 4wd of Dagu has two layers, i have added one extra layer so as to accommodate all of my sensors
Once the platform/chasis is ready we are now ready to integrate the sensors
Controlling the Robot Platform - Forward /Reverse and Turn


Here we would see how to control the 4 DC motors , for this we now need to assemble two nos L293 D boards
One L293 D boards can control two DC motors, you can assemble L293 D boards anywhere, i have assembled in the bottom layer of Dagu chassis to keep it firmly fixed, use foam tape (so need for screws are avoided).
For connecting the L293 D boards to motor as well as pi, we would need jumper wires. Now connect the motor to l293 as shown in above image. For PWM pin connect 5V from pi (We can use use PWM to vary the speed of motor, but right now we would keep it high)
Connect 4 nos batteries ( i have used GP2100) to the L293 as ahown.
For 5volts supply to L293, the same can be taken from raspberry GPIO output (GPIO image attached)
Now Connect 40 pin GPIO ribbon cable to pi and a suitable T-Cobbler or straight to read out the output from pi, wire the jumper wire to L293 board as shown in above image. To control the direction of motor now we just need to set the GPIO output low or high for the "A & B" pins
To set it straight, I have written a simple python program as below (You can of course make your python code and run it on pi)
from RPIO import PWM import RPi.GPIO as GPIO import time print"ok" GPIO.setmode(GPIO.BCM) GPIO.setup(23,GPIO.OUT) GPIO.setup(24,GPIO.OUT) GPIO.setup(17,GPIO.OUT) GPIO.setup(27,GPIO.OUT) GPIO.setup(20,GPIO.OUT) GPIO.setup(22,GPIO.OUT) GPIO.setup(18,GPIO.OUT) GPIO.setup(5,GPIO.OUT) GPIO.setup(12,GPIO.OUT) print"controls"</p> print"1:move forward"</p> print"2:move reverse"</p> print"3:stop robot"</p> print"4:move reverse with speed control"</p> print"5:move forward with speed control"</p> print"6: To turn the robot" print"press enter to send command" def fwd(): # For Moving in Forward Direction GPIO.output(23,True) GPIO.output(24,False) GPIO.output(17,True) GPIO.output(27,False) GPIO.output(22,True) GPIO.output(18,False) GPIO.output(5,True) GPIO.output(12,False) def rev(): # For Moving in reverse direction GPIO.output(23,False) GPIO.output(24,True) GPIO.output(17,False) GPIO.output(27,True) GPIO.output(22,False) GPIO.output(18,True) GPIO.output(5,False) GPIO.output(12,True) def stop(): # For Making the Robot stop GPIO.output(23,False) GPIO.output(24,False) GPIO.output(17,False) GPIO.output(27,False) GPIO.output(22,False) GPIO.output(18,False) GPIO.output(5,False) GPIO.output(12,False) def speedcont(): # For speed control if Required servo = PWM.Servo() servo.set_servo(13,10000) servo.set_servo(19,10000) def TL(): # For Turning the Robot GPIO.output(23,True) GPIO.output(24,False) GPIO.output(17,False) GPIO.output(27,False) GPIO.output(22,False) GPIO.output(18,False) GPIO.output(5,True) GPIO.output(12,False) while True: inp= raw_input() if inp =="1": fwd() print " robot moving in fwd direction" elif inp =="2": rev() print"robot moving in rev direction" elif inp=="3": stop() print"robot stopped" elif inp =="4": speedcont() rev() print"robot moving in rev direction at slow speed" elif inp =="5": speedcont() fwd() print"robot moving in fwd direction at slow speed" elif inp =="6": TL() print"robot TURNING" <br>
Controlling the Servos

Servos are important in robotic development, here you would see how to control servos from 0 - 180 deg in 3 steps (i.e 0 , 90 & 180 deg)
Here i have used Futuba s3003 servo - the connection is very simple the "+" & "-" goes to the our battery (in my case same as for my 4wd i.e set of 4 AA cells).
Its important to look at the operating voltage of servo ( in my case its 4.8 - 6 V see image above), the signal wire is to be connected to GPIO output ( see the pin no in python program below)
Well controlling servo motors in raspberry pi might be tricky but then there is very powerful library hosted @ RPIO.PWM , to install it on pi use the following code
<p>sudo apt-get install python-setuptools</p><p>sudo easy_install -U RPIO</p>
and here is the python code for turning the servos ( To know more about RPIO.PWM and the DMA used , please refer to the link : https://pythonhosted.org/RPIO/pwm_py.html )
from RPIO import PWM import RPi.GPIO as GPIO import time print"ok" GPIO.setmode(GPIO.BCM) GPIO.setup(19,GPIO.OUT) print"controls" while True: inp= raw_input() if inp =="1": # place the servo on 0 deg servo = PWM.Servo() servo.set_servo(19,1000) time.sleep(2) servo.stop_servo(19) elif inp =="2": # place the servo on 90 deg servo = PWM.Servo() servo.set_servo(19,1500) time.sleep(2) servo.stop_servo(19) elif inp=="3": # place the servo on 180 deg servo = PWM.Servo() servo.set_servo(19,2000) time.sleep(2) servo.stop_servo(19) <br>
Controlling the Pi Camera

Here I have used Pi camera module , the set up is quite easy and you can refer the link :https://www.raspberrypi.org/help/camera-module-set...
Further I have mounted this camera on my servo pan ( as shown above )
Video streaming : There a lots of tutorial for video streaming, but to begin with VLC streaming is quite easy please refer to link :http://raspberrypi.stackexchange.com/questions/231...
Taking Pictures :
Now here i have written a simple python code which ask for the image name and than stores the image by this name I have also added a buzzer so as to give me indication after pi clicks the picture ( you can use the buzzer module as shown in part list )
import RPi.GPIO as GPIO import time from time import sleep from subprocess import call GPIO.setmode(GPIO.BCM) GPIO.setup(21,GPIO.OUT) def takestillpic(num): # function block which accepts user defined photo character print" please enter photo character" num = raw_input() call ( ["raspistill -vf -hf -o " + str(num) + ".jpg" ],shell=True ) while True: inp = raw_input() if inp == "1": call ( ["raspistill -vf -hf -o image.51jpg" ],shell=True ) print" phot done please" elif inp == "2": num = raw_input() takestillpic(num) print " photo please" GPIO.output(21,True) # connect this output to buzzer sleep(1) GPIO.output(21,False)<br>
Calculating Distance and Avoiding Obstacles

Avoiding Obstacles is any Robot dream, here we would see how to calculate distance with Ultrasonic sensor
I have user HC-SR04 sensor along with 1K resistor, connection is quite simple (pl refer part list page for the image and connection of 1k resistor)
We need two GPIO output, to calculate the distance refer the below code, you can run this code straight to your pi
# ultrasonic distance measurement import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) TRIG = 13 ECHO = 26 print" Distance measurement in progress" GPIO.setup(TRIG,GPIO.OUT) GPIO.setup(ECHO,GPIO.IN) GPIO.output(TRIG,False) print" waiting for sensor to settle please" time.sleep(2) GPIO.output(TRIG,True) time.sleep(0.00001) GPIO.output(TRIG,False) while GPIO.input(ECHO)==0: pulse_start=time.time() while GPIO.input(ECHO)==1: pulse_end=time.time() pulse_duration = pulse_end - pulse_start distance = pulse_duration * 17150 distance = round(distance,2) print " Distance ", distance, "cm" if distance > 50: print " GOOD one " # these are just some reference of distance you can change it else: print " move on" GPIO.cleanup() # You can now integrate the above code with robotics control as mentioned in earlier steps to make more complex application, the same is now left as exercise :)
Reading Out Temperature and Humidity Sensor


Here we would used DHT 11 sensor to read out temperature and humidity sensor, both results can even be data logged.
Connect 4.7 K resistor to the pins as shown above ( remember to make the ground same for all)
Connect Vcc TO +5volts ( The same can be taken from pi GPIO output)
PIN 3 is NC and is to be left floating
There are python code to read out temp and humidity, but often the code fails to read out data, instead we would use "c" code to fetch out. We would use Adafruit DHT library to do so.
You can download the library from Github
git clone git://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
cd Adafruit-Raspberry-Pi-Python-Code/
cd Adafruit_DHT_Drive
# After above steps we can test whether the c code is working or not by below commands
sudo ./Adafruit_DHT 11 21 # Here "11" signifies DHT 11 & 21 is for GPIO OUTPUT pin
if drivers are working fine we can see the following results
using pin # 21
Data(39) : 0X3d 0X0 0x1d 0X0 0X5a
Temp = 20 *C, Hum = 35%
# we can also create a shell script to read the sensor value to avoid changing the root directory everytime
Learning to Chat With Your Robot
So who doesn't like to chat.
Well this section describes how we can put pi to chat with minimal requirement. To do so we would need a speaker system with a 3.5 mm jack Audio input.
Now step 1 is to connect Audio output of pi with speaker, basically here we are trying Text to speech conversion , with Espeak , there are other option also available and I recommend you to go through following link :http://elinux.org/RPi_Text_to_Speech_(Speech_Synth...
Now to begin with first Install following on your pi
sudo apt-get install espeak
# Here is a simple program which demonstrates the above
import os, time def robot(text): os.system("espeak ' " + text + " ' " ) robot("Hello Ashu") # Robot speak out this lines time.sleep(1) # sleep introduced for delay time.sleep(1) robot(" i am hungry") time.sleep(1) robot('what is your name') name = input('what is your name:') # input from user please type in name robot("Nice to meet you" + name ) time.sleep(1) robot("how old are you") age = input(' how old are you : ') robot(" well you look younger" + age ) time.sleep(1) robot('what do you want') want = input('what do you want') robot(" well i can give you" + want ) time.sleep(1) robot("bye bye") # Remember that above code would not run on command line as root user for this run code in python shell
The Final Step : Controlling Raspberry Pi From Web


Well I hope by now you should be able to connect run you robot pi in your home network and yes you can variety of other sensors too.
Now here we would see how run and control all our sensors from web.
With the advent of internet , we would use something called "IOT" to connect and monitor our devices
I have used "weaved" IOT to connect and manage my pi ( at presently 0.5hrs free session is provided for registered members) For installation on pi please refer to the link :https://www.weaved.com/installing-weaved-raspberry...
Once installation is complete, we just need to login to our weaved account, which would then show the "offline or "online" status of our robot pi, If you have selected SSH method to be connected to your pi than just copy the address provided by weaved to your SSH and than you are connected to your pi. Now you can run your any code.
# HAPPY CODING