Cornell ECE5725 Project
Yizhou Sun, Yunjie Li
We’ve designed a security robot based on Raspberry Pi which can navigate itself in your home avoiding obstacles and also detect suspicious events (motion) when you leave your house. Once a suspicious event is detected, the robot will send an email to the host which includes a photo to notify that.
In this report we will introduce our design process in details. The security robot mainly consists of two parts: Motion Detection and Navigation. In Motion Detection, a Pi-Camera and the OpenCV module are used to detect moving objects. In the Navigation, we used three ultrasound distance sensors in front, right, left side of the robot to detect the distance to obstacles and the Raspberry Pi (R-Pi) will analyze the and decide what to do in next step.
Figure 1. System overview and photo of the Security Robot
We used Raspberry Pi Camera Module V2 as the detection device. Following the instruction , we first connected the camera to Pi via the camera port. Then we started the Pi and and config the system by opening the Raspberry Pi Configuration Tool and setting the camera interface to be enabled. After that we reboot system. And we wrote a simple Python program to test that it works and can caputure video successfully.
Figure 2. Pi-Camera and connection 
The wheels of our robot were implemented by two Parallax continuous rotation servo motors. Figure 3 is the circuit of the servo. We used pin GPIO 6 and 26 as the outputs pins for two servos and the GPIO python library to generate PWM input signal to control them.
The direction and speed of the servo is determined by the duty cycle of the output signal. After carefully calibration, we set the frequency of the signal to 46.51Hz, to control the robot more easily we used a low speed for the servo motor. The duty cycle for clockwise direction is 6.71%, the duty cycle of counterclockwise is 7.17%.
Figure 3. Circuit of the servo and its sample image 
Ultrasonic Distance Sensors
To detect the distance between the robot to obstacles, we used three HC-SR04 ultrasonic distance sensors, one was placed at the front, and two at left and right side of the robot respectively.Each distance sensor has four pins, one is connected to 5V power supply and one is connected to ground. The other two pins, ECHO and TRIG, are used to send and receive ultrasonic pulse.
Figure 4. Ultrasonic Distance Sensors HC-SR04
The voltage generated by pin ECHO is 5V, but the input pin of Raspberry Pi can only accept 3.3V signal. Therefore, it is necessary to use a voltage divider to bring down the voltage to 3.3V. The final connection can be found in System Design & Schematic.
Let t denote the time interval between the time when a pulse is generated pin TRIG and the time when the pulse is received by pin ECHO. Because the speed of sound is about 343m/s, and then we can calculate the distance using the following equation.
Distance = 34300 * t / 2 (cm) = 17150 * t (cm)
For the distance sensor at the front, it connected ECHO to GPIO 18 and TRIG to GPIO 24; For the distance sensor at left, it connected ECHO to GPIO 17 and TRIG to GPIO 22 and for the distance sensor at right, it connected ECHO to GPIO 13 and TRIG ti GPIO 22. While testing the distance sensor, we found that we need to give a small period of time before testing the distance. For example if we test the distance to the front obstacle immediately after the testing the distance to the left, the result would be inaccurate, because the ultrasonic pulse generated by left sensor will affect the sensor at the front, which will give us a wrong answer.
We used Python OpenCV library to implemented the motion detection function. To be able to detect moving object the robot need to be stationary, then we use the Pi-Camera to capture the video and process it. First step is to install OpenCV using following command:
sudo apt-get update
However, we met one issue when installing OpenCV, when we ran command "sudo apt-get install libopencv-dev python-opencv", the terminal shows an error that "The value "stable" is for APT::Default-Release is invalid". But this problem is easy to, we can edit the configuration file “/etc/apt/apt.conf/10defaultRelease” by replacing the word “stable” by “oldstable”. Then ran command “sudo apt-get install libopencv-dev python-opencv”, the OpenCV would be installed successfully.
sudo apt-get install libopencv-dev python-opencv
Next step is to use OpenCV to implement motion detection module. We used the picamera.capture_continuous to capture the image frames from the camera. Then for those frames, we need to do some preprocessing about the frames before detecting the moving objects. We first converte them into grayscale frames, and apply a Gaussian filter to remove the high frequency noise. This can make the detection more precise.
For the first frame we save it as the base frame, then all the next frames i, we will compute a new frame n by the differences between base and frame i. We threshold the new frame n to keep the pixels that have large changes. Then we apply a morphological filter to fill the hole on the new frame. After that we will count the area of the detected object, once the it is larger than the defined minimum area, we view it as a detected object, else we view it as noise. In the end we will add frame i to base frame as a new base frame for next calculation. 
In order to increase the accuracy, only detect moving object in three continuous frames, we view it as a suspicious event, then we mark the object with a green square, save it and send it in email to the host. We tested this algorithm with different parameters to find the best threshold value that has a proper sensitivity that can detect moving object without report the noise of the environment. Following is the sample result generated by our robot, the moving person is marked by a green square.
Figure 5. Sample result of the motion detection
We've also designed the algorithm to navigate the robot inside a house. The basic idea is to let the robot always move along the left wall. Since this robot is designed to move inside a house, we can always place the robot's left side close to a wall before starting it. And moving along the left wall guarantees that the robot can go through all rooms in most cases.
The first step is the measurement, we measured the exact speed of our robot when moving forward, and the exact angular velocity when it rotates clockwise and counter-clockwise under the duty cycles defined above. We found that its speed is 15 cm/s when moves forward and backward and the angular velocity is 86.12 degree/sec when rotating clockwise and 76.92 degree/sec when rotating counter-clockwise.
In order to do implement the 'left wall' navigation , we've written several functions in python, moveForward(), moveBackward(), turnLeft(), turnRight(), stop() and getDistance(), detail can be seen in code appendix. These functions would change the duty cycle of GPIO output signal to control the servos.
We used a while true loop to control the movement of the robot in a house. At the beginning of each iteration, we call moveForward() and then call time.sleep(1), which let the robot go straight for about 15 cm. Then we call getDistance() to get the distance of the robot to left wall, right wall and front wall. If the distance to 3 wall are all very small, it means a dead end in front, we call function turnRight() and time.sleep(2.09) to let the robot turn 180 degree. Else if the distance to front wall is less than frontLowerBound, we call turnRight() and time.sleep(1.045) to rotate 90 degrees clockwise (turn right) and then move forward as shown in figure below.
Figure 6. Navigation algorithm, U turn and turn right
If it is non of the situation above, it will check leftDist (distance to left wall), if leftDist is greater than leftLowerBound and less than leftHigherBound, we know the robot is in a good situation and continue to move forward. If leftDist is smaller than leftLowerBound, which means that the robot is too close to the left wall, we'll call turnRight(), time.sleep(1), moveForward(), time.sleep(1), turnLeft(), time.sleep(1) continuely to control the robot move apart from the left wall. If leftDist is greater than leftHigherBound but less than leftThreshSpace, which means that the robot is far from the left wall, we call these functions in similar way to make the robot close to the left wall. And if leftDist is greater than leftThreshSpace, which means that there may be a 'door' at the left side, we will make the robot turn left.
Figure 7. Navigation algorithm, adjust distance and ture left
After we implemented the first version discussed above, we started to test it. However, we found a problem during the test process: the above algorithm works only when the robot moves strictly straight after the moveforward() is called. But as we can’t control servos very precisely, and the friction on different areas of the ground is not the same, it’s really hard to make the robot move strictly straight.
Then we wrote some new codes to fix the problem. In the beginning of each movement, we first record the oldLeftDist. After the robot moves forward for about l = 15 cm, we check if oldLeftDist is as same as leftDist. If abs(oldLeftDist - leftDist) is greater than some threshold, we rotate the robot for arcsin((oldLeftDist - leftDist)/l) degrees to make sure the robot moves straight. As shown in figure
Also, we added simple recovery function to our navigation algorithm. The recovery part is made to handle the possible collision to wall. In each iteration, the robot will check if frontDist is too smaller, as it already touched the wall, the robot will move backwards, then turn right and move forward.
Figure 8. Navigation algorithm, new adjust distance and recovery from collision
We’ve successfully build the security robot as expected. First we assembled the robot according using two servo motors, one Pi-Camera and three ultrasound distance sensors. The servos at left and right side are used to control 2 wheels control motion of the robot. Pi-Camera is used to detect suspicious events is mounted at the top of the robot. Three ultrasound distance sensors are mounted at the front, left and right side. These sensors give distance information to the Raspberry-Pi which helps navigation.
The second part is the software part which consists of motion detection and navigation. In motion detection part, we installed OpenCV library and used it to implemented a motion detection algorithm, and it will save a picture of the detected object and send a email to the host. Navigation part implemented by a ‘along left wall’ algorithm using the distance feedback from threee distance sensors. Moving along the left wall guarantees that our robot can go through all rooms in the house in most cases. The robot can also navigate itself to avoid obstacles and recover from collision to walls. We’ve also designed a function to help the robot move strictly straight.
During the whole project, we faced some issues. The first issue is the OpenCV installation issue, we solved it by changing the variable in the version configuration file. Second issue is the distance sensor issue, we solved it by writing our own function to calculate distance instead of using third-party library. Third issue is that the robot doesn't not always move strictly straight, which makes the movement control difficult, we solved it by carefully calculate the angle between the wall, and adjust the direction.
In the future, we would like to implement a mapping function to save the path. Therefore, after the first traversal of your home, the robot can go much faster in the next traversals because we don’t need to detect the distance any more. We can improve the motion detect to write a more complicated algorithm so that we can detect the moving object while moving. And we can add a microphone on the robot to detect the sound of footsteps or other large noise in the house and we want it to generate alert sound when something is detected.
Yizhou Sun (firstname.lastname@example.org) Yunjie Li (email@example.com)