top of page
Writer's pictureSiya Gupta

How to use PiCam to program robotic motion

Updated: Jul 21

In this project, we explore how to create a color-responsive robot using Raspberry Pi and OpenCV. The robot is designed to detect colored sticky notes on the floor and respond with specific movements. To achieve this, we use the HSV (Hue, Saturation, Value) color space, which separates color from intensity, making it easier to identify specific colors. The process begins by defining color ranges for each target color using numpy arrays. For example, pink is defined with lowerColorPi = np.array([160, 117, 0]) and upperColorPi = np.array([180, 255, 255]). These ranges create masks that filter the video frames, setting pixels within the color range to white and others to black using cv2.inRange. This mask helps the robot determine the presence and position of a color in the frame. Once a color is detected, the program associates it with a corresponding movement. For instance, if the center of the frame contains a sufficient number of pink pixels, the robot moves forward by activating both motors: GPIO.output(GPIO_Ain1, True) and GPIO.output(GPIO_Bin1, True). Similarly, detecting blue prompts a left turn by activating specific motor outputs: GPIO.output(GPIO_Ain1, True) and GPIO.output(GPIO_Bin2, True).



Each color triggers a unique action, such as turning, moving forward, or stopping, by altering the GPIO pin outputs and PWM duty cycles to control motor speed and direction. The code continuously captures frames using the Pi Camera, converts each frame to the HSV color space, and applies the masks. This real-time processing allows the robot to interact dynamically with its environment. Upon program termination, the system ensures all resources are released with cv2.destroyAllWindows(), camera.close(), and GPIO.cleanup(), maintaining system stability. This project serves as an engaging introduction to robotics and computer vision, demonstrating how hardware and software integration can create responsive, autonomous systems. By experimenting with additional patterns or sensors, you can expand the robot's capabilities even further.

An important aspect of this setup was ensuring the camera was angled downward to focus solely on the floor, preventing it from capturing multiple colors simultaneously and becoming confused. This careful positioning allowed the robot to accurately detect one color at a time, which is crucial for executing the correct movement commands. The angle was adjusted to maintain a consistent view of the colored notes while minimizing interference from surrounding objects. This setup not only enhances the accuracy of color detection but also ensures that the robot performs reliably in its environment. Adjusting the camera angle is a critical step in optimizing the robot’s performance, demonstrating how thoughtful hardware placement can significantly impact the effectiveness of computer vision applications.

Building this project came with its share of challenges and fun moments. One significant challenge was deciding how to associate the detected colors with robot motion. We considered various methods, such as using the centroid of the color blobs, analyzing shapes, or applying thresholding techniques. Ultimately, we decided that directly associating color with motion was the most straightforward and effective approach for this project. Fine-tuning the color ranges was another challenge, as lighting conditions could affect color perception. We experimented with different lighting setups and calibration techniques to improve detection accuracy. Managing the robot's movement was also crucial to ensure smooth transitions between different motions without abrupt stops or starts. Debugging these issues required patience and creativity, but it was rewarding to see the robot navigate the colored path successfully. The fun part was watching the robot come to life, responding correctly to the color cues. It was exciting to tweak the setup and see immediate results, allowing for hands-on learning and experimentation. This project highlights the importance of integrating both software and hardware solutions to create a smoothly functioning system, paving the way for more complex and capable robotics projects in the future.

15 views0 comments

Recent Posts

See All

Comments


bottom of page