Raspberry Pi Developer's Guide: Robots
Hardware:
- Raspberry Pi Model B R1 This will be the “brains” .
- Foscam FI8910W Wireless IP Camera This IP camera will be mounted on the robot and will be the primary “eyes” of the robot. I chose this IP camera because it has excellent documentation and is well built.
- X-Mi X Mini II 2nd Generation Capsule Speaker This provides the speaker output for the robot allowing the robot to talk.
- USB Virtual 7.1 Channel Sound Adapter Very cheap soundcard, primarily used to get around the issue of “popping” when using the Raspberry Pi’s onboard sound jack.
- Sabertooth Dual 12A Motor Driver The motor controller that controls the four drive motors (motors arranged in pairs as in a differential drive configuration.
- Arduino UNO This is involved in general sensing.
- Arduino Ethernet Shield Both Arduino’s are networked via Ethernet.
- Ultrasonic Sensors HC SR04 Ultrasonic Distance Sensor
- Infared Motion Sensor HC-SR501 Infrared Sensor Module (PIR ) Motion Detector
- Microphone A Microphone will be attached to the IP Camera
- Magnetic Sensor/Compass Three-Axis Digital Compass HMC5883L
- 12Vdc, 200 RPM Geared Motor I have four of these Drive Motors arranged in a differential drive configuration. The motors are driving the wheels which are mad up of a plastic hub and some 4.75″D x 2.375″W Off Road Tyres. These are attached to the 6mm motor shaft by four hexagonal Mounting Hubs.
- Decks
- TeckNet iEP1200 12000mAH USB Battery Packs x 2 These each have two 5 V outputs of 1A and 2.2A. 12.0 Volt Ni-MH 2800mAh Battery Pack – This pack will provide the power for the drive motors.
- 4 Port USB Hub (USB 2.0 with Mains Adaptor) A “Mains” powered hub which can be powered from the USB batter pack.
How to Build Your First Robot With Raspberry Pi
Required Materials
- Raspberry Pi B/B+ or 2 and basic peripherals: SD card, keyboard, mouse, etc.
- IR sensor modules.
- Geared DC motors.
- L293D driver board.
- Robot chassis and wheels.
- Caster wheel.
- Breadboard and double side tape.
- Male to male/Female to male jumpers.
- 9V Battery and connectors.
- Push button and 220R resistor.
How Does it Work?
This robot uses two IR sensor modules which can detect objects within a range of 5-6cm. This sensor outputs a digital LOW (0V) signal when there is an object within its range. And outputs a digital HIGH (5V) signal otherwise.
How do these IR sensors work?
These modules consist of a pair of receiver and transmitter IR LEDs. When an object gets in front of the IR sensor, the surface of the object reflects a part of the IR light back to the receiver. Thus, the receiver then outputs a LOW signal notifying that an object is in front of the sensor.
These sensors are wired to the GPIO input pins of the raspberry pi. The pi then using a python script checks whether the GPIO pins connected to the IR sensor modules goes low. If it does go low, then it commands the DC motors to move in the reverse direction first and then turn.
Connect the IR modules to your raspberry pi as shown in the following diagram. Power the sensor by providing 5V (+ pin), GND (- pin) from the raspberry pi. And connect the B pin on the sensors to raspberry pi’s GPIO pins 3 and 16.
irtest.py:
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.IN) #Right sensor connection
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Left sensor connection
while True:
i=GPIO.input(3) #Reading output of right IR sensor
j=GPIO.input(16) #Reading output of left IR sensor
if i==0: #Right IR sensor detects an object
print "Obstacle detected on Left",i
time.sleep(0.1)
elif j==0: #Left IR sensor detects an object
print "Obstacle detected on Right",j
time.sleep(0.1)
Connecting the Motors with L293D
Four output GPIO pins from the raspberry pi control the direction of rotation of the two motors. The two terminals of the motors are then connected to the 4 output terminals of the board.
The logic for controlling the motors from the raspberry pi is as given below:
Input A | Input B | Execution |
---|---|---|
1 | 0 | Turn clockwise |
0 | 1 | Turn counter-clockwise |
1 | 1 | brake |
0 | 0 | brake |
Thus each motor’s direction can be controlled by writing HIGH/LOW signals through two GPIO pins from the raspberry pi.
Use the code below to check the motors and the L293D. Make sure you have powered the driver board (L293D) and given the connections as per the above diagram.
motor.py:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(5,GPIO.OUT) #Left motor input A
GPIO.setup(7,GPIO.OUT) #Left motor input B
GPIO.setup(11,GPIO.OUT) #Right motor input A
GPIO.setup(13,GPIO.OUT) #Right motor input B
GPIO.setwarnings(False)
while True:
print "Rotating both motors in clockwise direction"
GPIO.output(5,1)
GPIO.output(7,0)
GPIO.output(11,1)
GPIO.output(13,0)
time.sleep(1) #One second delay
print "Rotating both motors in anticlockwise direction"
GPIO.output(5,0)
GPIO.output(7,1)
GPIO.output(11,0)
GPIO.output(13,1)
time.sleep(1)
Uploading the Code for the Raspberry Pi Robot
robot.py:
#DIYhacking obstacle avoiding robot. Check out: http://diyhacking.com for the tutorial
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.IN) #Right IR sensor module
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #Activation button
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Left IR sensor module
GPIO.setup(5,GPIO.OUT) #Left motor control
GPIO.setup(7,GPIO.OUT) #Left motor control
GPIO.setup(11,GPIO.OUT) #Right motor control
GPIO.setup(13,GPIO.OUT) #Right motor control
#Motor stop/brake
GPIO.output(5,0)
GPIO.output(7,0)
GPIO.output(11,0)
GPIO.output(13,0)
flag=0
while True:
j=GPIO.input(12)
if j==1: #Robot is activated when button is pressed
flag=1
print "Robot Activated",j
while flag==1:
i=GPIO.input(3) #Listening for output from right IR sensor
k=GPIO.input(16) #Listening for output from left IR sensor
if i==0: #Obstacle detected on right IR sensor
print "Obstacle detected on Right",i
#Move in reverse direction
GPIO.output(5,1) #Left motor turns anticlockwise
GPIO.output(7,0)
GPIO.output(11,1) #Right motor turns clockwise
GPIO.output(13,0)
time.sleep(1)
#Turn robot left
GPIO.output(5,0) #Left motor turns clockwise
GPIO.output(7,1)
GPIO.output(11,1) #Right motor turns clockwise
GPIO.output(13,0)
time.sleep(2)
if k==0: #Obstacle detected on left IR sensor
print "Obstacle detected on Left",k
GPIO.output(5,1)
GPIO.output(7,0)
GPIO.output(11,1)
GPIO.output(13,0)
time.sleep(1)
GPIO.output(5,1)
GPIO.output(7,0)
GPIO.output(11,0)
GPIO.output(13,1)
time.sleep(2)
elif i==0 and k==0:
print "Obstacles on both sides"
GPIO.output(5,1)
GPIO.output(7,0)
GPIO.output(11,1)
GPIO.output(13,0)
time.sleep(2)
GPIO.output(5,1)
GPIO.output(7,0)
GPIO.output(11,0)
GPIO.output(13,1)
time.sleep(4)
elif i==1 and k==1: #No obstacles, robot moves forward
print "No obstacles",i
#Robot moves forward
GPIO.output(5,0)
GPIO.output(7,1)
GPIO.output(11,0)
GPIO.output(13,1)
time.sleep(0.5)
j=GPIO.input(12)
if j==1: #De activate robot on pushin the button
flag=0
print "Robot De-Activated",j
GPIO.output(5,0)
GPIO.output(7,0)
GPIO.output(11,0)
GPIO.output(13,0)
time.sleep(1)
Building a Segway With Raspberry Pi
How to build a self balancing two-wheel robot (that is so-called segway).
In this project, you will learn how to access 6-axis motion sensor, motor control, and basic automatic control theorem.
Step 1: Materials
- Raspberry Pi AC-to-DC 5V power adapter, plastic enclosure I am using Raspberry Pi 2, with Raspbian installed. Cost $45.0
- USB WiFi adaptor, I’m using Edimax Wifi adaptor. Cost $10.0
- DC motors x2, wheels x2, car frame, acrylic sheet x2, motor bracket x2, brass stud x8, shaft coupling x2, screw xn I purchased them from Here The gear ratio of the motors I got is 1:34. I think the one of 1:21 should be better. You can use any other DC motors as long as the speed and the torgue is large enough for your robot. Cost $50.0
- AC-to-DC 12V power adapter Cost $4.0
- L293D IC I purchased it from Here. Cost $3.0
- MPU6050 module I purchased it from Here. Cost $3.0
- Breadboard: 830 tie-points Jumper wires, Male-to-Male wires xN, Male-to-Female wires xN Cable Tie x8
Step 2: Frame Installation
Step 3: Circuit Installation
Please follow the circuit diagram to install components to the breadboard and insert connection to you RPi.Let me have a short explaination of the circuit.
The interface of the MPU6050 module is i2C. There are four pins have to be connected to Raspberry Pi GPIO as below.
MPU6050 VCC --> RPi GPIO header pin#1 (3.3V),
MPU6050 SDA --> RPi GPIO header pin#3 (SDA)
MPU6050 SCL --> RPI GPIO header pin#5 (SCL)
MPU6050 GND --> RPi GPIO header pin#6 (GND)
To drive DC motors, I am using the popular L293D IC.
RPi GPIO header pins (pin#11, pin#13, and pin#15) are for the left wheel.
They have to be connected to L293D (pin#2, pin#7 and pin#1)
RPi GPIO header pins (pin#16, pin#18, and pin#22) are for the right wheel.
They have to be connected to L293D (pin#15, pin#10 and pin#9)
L293D pin#3 & pin#6 --> left motor M+ and M-.
L293D pin#14 & pin#11 --> right motor M- and M+.
To drive L293D, we should have extra power with enough voltage. What I'm using is a 110V AC-to-DC 12V adaptor and then connect to L293D.
L293D Vs --> 12V DC
L293D Vss --> 5V DC
L293D pin#4, #5, #13, #12 --> GND
Step 4: Tight Them Up
Step 5: Software Installation
I’m assuming that you already installed the Raspbian with WiFi adapter and you know how to use ssh to login to your RPi. We will start from installing i2C kernel module and wiringPi library.
The i2C kernel module is to help us to access MPU6050 and the WiringPi library is to help us to access GPIO.
Here are the instructions:
To install i2c kernel module,
$ sudo apt-get install libi2c-dev
To setup i2c kernel module,
$ sudo vi /etc/modules
Add following lines into the file.
i2c-bcm2708
i2c-dev
We also have to check a blacklist file.
$ sudo vi /etc/modprobe.d/raspi-blacklist.conf
Make sure the following two lines are commented, then save.
#blacklist spi-bcm2708
#blacklist i2c-bcm2708
Check raspi-config
$ sudo raspi-config
In Advanced Options -> I2C, please enabled it.
Then, reboot your RPi
$ sudo bash; sync;sync;reboot
When it goes back, check if the i2c driver kernel module is loaded automatically.
$ lsmod |grep i2c
i2c_dev 6027 0
i2c_bcm2708 4990 0
Then, let's install the wiringPi library.
Install git first.
$ sudo apt-get install git-core
Download and install wiringPi
$ cd
$ git clone git://git.drogon.net/wiringPi
$ cd wiringPi
$ sudo ./build
Step 6: Testing MPU6050 Sensor
Now, we will make sure the MPU6050 actually works.Let's install the i2c testing tool.
$ sudo apt-get install i2c-tools
Execute it to check if it saw the MPU6050 on i2c bus.
$ sudo i2cdetect -y 0 (for RPi Revision 1 board)
or
$ sudo i2cdetect -y 1 (for RPi Revision 2, Pi B, Pi B+, or Pi 2 boards)
If you saw the output as below, it saw the MPU6050. It means the mpu6050 device is on i2c bus and its i2c address is “0x68”
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Let's try some simple tools to get those acceleration and gyroscope information.
Download and compile following tool
$ cd
$ git clone https://github.com/wennycooper/mpu6050_in_c.git
$ cd mpu6050_in_c $ cat README.md
$ gcc -o mpu6050_accl ./mpu6050_accl.c -lwiringPi -lpthread -lm $ gcc -o mpu6050_gyro ./mpu6050_gyro.c -lwiringPi -lpthread -lm
To get acceleration information, please execute
$ sudo ./mpu6050_accl
You should see something like:
My acclX_scaled: 0.140625
My acclY_scaled: -0.031006
My acclZ_scaled: 0.994141
My X rotation: -1.768799
My Y rotation: -8.047429
The above acclX, acclY & acclZ are the measured acceleration in g(the g=9.8m/s^2) in X, Y and Z axis.
In fact, the accelerometer only tells us acceleration information. With some basic trigonometric functions calculation, we can obtain the orientation angles. They are “My X & Y rotation” and the unit is in degree.
Please try put the car frame a little bit forward or leanback, and watch the changes of the measurement. You will understand it more.
Let's also check gyroscope measurement. Please execute:
$ sudo ./mpu6050_gyro
It will tell you the velocity (in degree/s) of angle in each axis, like this.
My gyroX_scaled: -4.312977
My gyroY_scaled: 0.458015
My gyroZ_scaled: 0.366412
My gyroX_scaled: -4.053435
My gyroY_scaled: 0.427481
My gyroZ_scaled: -0.160305
Please try rotate the car frame and watch the changes by time. Then you will understand the gyroscope measurement more.
If we only use acceleration information to calculate the orientation angle, the angle will be not stable and not reliable. So, somebody invent the method to use the velocity of angle information to calculate the angle, and complement with the angle from acceleration. Then it will give us more stable and reliable angle.
It's so-called "Complementary filter". You will see the complementary filter in our code later. Check for more detail explanation in reference documents.
Step 7: Testing DC Motors
Let's download and compile the code to make the DC motors rotating.
$ cd
$ git clone https://github.com/wennycooper/dcMotor.git
$ cd dcMotor
$ cat README.md
$ gcc -o dcMotor0 dcMotor0.c -lwiringPi -lpthread
Now we are ready to make the motors go. Please make sure all the circuit components and 12V DC power are ready and connected. Bring your robot off the ground and execute:
$ sudo ./dcMotor0
How is that? If everthing works, the two motors should rotate in the same direction together. They should rotate for 5 seconds in forward direction and then for 5 seconds in backward direction, then stopped.
If anything went wrong, please go back to the previous steps to check. If both motors rotated in opposite direction, please swap the M+ & M- connections in one motor.
One more thing to mention is: Is the rotation speed fast enough??
If they rotated slowly, say two revolutions per second, it is too slow to balance the robot. The problem could be in the circuits to L293D. Please go back to check it.
Step 8: Put All Together
If you can go to here withour any problem. Congratulation, you are almost there!!
Let’s put all the our efforts together. Please download and compile the auto balancing control program
$ cd
$ git clone https://github.com/wennycooper/mySegway.git
$ cd mySegway
$ less README.md
$ gcc -o mySegway ./mySegway.c ./motors.c -lwiringPi -lpthread -lm
This program will try it's best to balance the robot. It measured the orientation infomation and drive the motors in 100 times per second. If the angle of orientation is too large (>60 degree) to be balanced, it will stop the motors for safety.
You can see the PID control in code. It is a very important method in automatic control theory. Check for more detail in the reference documents..
Step 10: References
http://blog.bitify.co.uk/2013/11/interfacing-raspberry-pi-and-mpu-6050.html
http://blog.bitify.co.uk/2013/11/reading-data-from-mpu-6050-on-raspberry.html
https://projects.drogon.net/raspberry-pi/wiringpi/software-pwm-library/
http://robotrabbit.blogspot.tw/2012/07/pid.html
http://www.bajdi.com/building-a-self-balancing-bot/
How to Build a Robot - Design and Schematic
Start building a robot that can follow lines or walls and avoid obstacles!
Overview
Choosing the components
Design the board for low cost PCB manufacturing and stay within the limits of the free version of Eagle CAD.
Mechanical: Motors, Gears, Wheels
- Tamiya 70168 Double Gearbox Kit
- Tamiya 70101 Truck Tire Set (4 tires)
- The front wheel is just a ball caster or plastic screw so that the robot can slide along the floor.
Brains: Microcontroller
The latest Teensy has a Cortex M4 which is plenty of power for a simple robot. A bonus is that the Teensy has an onboard 500mA regulator which can be used for all of the sensors.
Interaction: Sensors
line sensor
QTR-3RC Reflectance Sensor Array packs three IR LED/phototransistor pairs onto a 1.25″ × 0.3″ board.How much light is reflected from the ground.
distance sensor
The high brightness IR sensor,Power: Motor Driver, Battery
Line Follower
Follow a line?
A line follower is the easiest way to make a robot follow a pre-determined path. You only need a way to move and a sensor to determine if the robot is on the line or not. There have been may algorithms developed to keep the robot on the line. The field of engineering that covers these algorithms is called control theory. For this article, I'm going to make a really simple algorithm. In pseudo code:
is robot to the left of the line?
turn right
is the robot to the right of the line?
turn left
is the robot on the line?
move forward
Avoiding Obstacles
The algorithm I decided on will tell the robot to back up for 1 second if an obstacle is detected. It will then randomly turn left or right in an attempt to avoid the obstacle. While turning left or right, it continues to check for obstacles in the way. If it detects obstacles, it will stop turning and repeat the reverse/turn cycle until free of the obstacle.
The following sketch performs the logic:
- Start the robot driver and wait 5 seconds. This gives you time to put the robot where you want it before it starts moving.
- Move forward.
- If the middle sensor is activated, reverse for 1 second.
- Randomly choose left or right.
- Turn for up to 1 second as long as the middle sensor is not activated.
- If the middle sensor is activated, go to step 3.
- If the sensor is not activated, go to step 2.
Following Walls
In order to follow walls, you need at least two sensors (2 bits of information) to handle the four potential situations the robot could be in. One sensor has to be in the front, and the second could be on the left or right of the robot. The more sensors you use, the more information you have, so you can make better judgements about what is going on. For this example, I just used two. The robot cannot find the wall, so you have to place the robot next to the wall. If you placed it in the middle of the room, it would just drive in circles.
Front Sensor | Right Sensor | Situation | Action |
---|---|---|---|
Off | Off | Robot is driving away from wall. | Come back to wall, turn right. |
On | Off | Robot is away from wall but headed towards a wall or obstacle. | Turn hard left to get back parallel with the wall. |
Off | On | Robot is following the wall. | Drive forward. |
On | On | The robot is at a corner. | Turn hard left. |
留言