In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use raspberry pie to make smart cars. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
In the movie, you can see such scenes from time to time, a car with a camera on it, the car can be remotely controlled by a computer or a mobile phone, and the pictures captured by the camera on the car can be displayed on the computer or mobile phone in real time, like the following picture.
Friends who have not come into contact with this area may think that this is a very high-end technical work, in fact, it is not, this kind of car is actually very simple to do. So, how do you do a car like this?
In fact, we only need to prepare a circuit board (development board) to control the car, two to four motors (motors), a car rack, a camera and a camera head, these basic accessories, and then program and control the development board, the overall hardware cost adds up to less than 500 yuan.
* * Development board: * *
There are many kinds of development boards, such as 51 single chip microcomputer, raspberry pie, STM32, Arduino, micro:bit and so on, all of which can be used as the control board of the car. I use the raspberry pie development board. Then, there are many versions and models of raspberry pie. The cheapest raspberry pie zero can be bought for 68 yuan, but it is not recommended to buy this kind. Without a network card, you need to buy another network cable module. I use raspberry pie 3B, the price is 220 yuan. With wireless and wired network cards, as well as Bluetooth.
* * car shelves: * * there are many of these shelves on a treasure, all kinds of which can be found by searching for "smart car" on a treasure. With a whole set of motors, it costs only 50 or 60 yuan.
* * camera + head: * * there is also a lot of search on a treasure, such as the one I use below, 45 yuan.
When the accessories are ready, it is to develop the board installation system for the car, and then program and control the car.
The control of the car mainly has two aspects of control, one is the movement control of the front and back of the car, and the other is the camera shooting and the up and down transfer control.
The front, back, left and right control of the car:
#-*-coding:UTF-8-*-import RPi.GPIO as GPIOimport time# car motor pin definition LeftIn1 = 20LeftIn2 = 21LeftSpeed = 16RightIn1 = 26RightSpeed = 1 "set GPIO port to BCM encoding GPIO.setmode (GPIO.BCM) # ignore warning message GPIO.setwarnings (False) # Motor pin initialization operation def car_init (): global pwm_LeftSpeed global pwm_RightSpeed global delaytime GPIO.setup (LeftSpeed,GPIO.OUT,initial=GPIO.LOW) GPIO.setup (LeftIn1,GPIO.OUT) Initial=GPIO.LOW) GPIO.setup (LeftIn2,GPIO.OUT,initial=GPIO.LOW) GPIO.setup (RightSpeed,GPIO.OUT,initial=GPIO.LOW) GPIO.setup (RightIn1,GPIO.OUT,initial=GPIO.LOW) GPIO.setup (RightIn2,GPIO.OUT,initial=GPIO.LOW) # set pwm pin and frequency to 2000hz pwm_LeftSpeed = GPIO.PWM (LeftSpeed, 2000) pwm_RightSpeed = GPIO.PWM (RightSpeed 2000) pwm_LeftSpeed.start (0) pwm_RightSpeed.start (0) # car forward def run (delaytime): GPIO.output (LeftIn1, GPIO.HIGH) GPIO.output (LeftIn2, GPIO.LOW) GPIO.output (RightIn1, GPIO.HIGH) GPIO.output (RightIn2, GPIO.LOW) pwm_LeftSpeed.ChangeDutyCycle (80) pwm_RightSpeed.ChangeDutyCycle (80) time.sleep (delaytime) # car back def back (delaytime): GPIO.output (LeftIn1) GPIO.LOW) GPIO.output (LeftIn2, GPIO.HIGH) GPIO.output (RightIn1, GPIO.LOW) GPIO.output (RightIn2, GPIO.HIGH) pwm_LeftSpeed.ChangeDutyCycle (80) pwm_RightSpeed.ChangeDutyCycle (80) time.sleep (delaytime) # car turn left def left (delaytime): GPIO.output (LeftIn1, GPIO.LOW) GPIO.output (LeftIn2, GPIO.LOW) GPIO.output (RightIn1, GPIO.HIGH) GPIO.output (RightIn2 GPIO.LOW) pwm_LeftSpeed.ChangeDutyCycle (80) pwm_RightSpeed.ChangeDutyCycle (80) time.sleep (delaytime) # car turn right def right (delaytime): GPIO.output (LeftIn1, GPIO.HIGH) GPIO.output (LeftIn2, GPIO.LOW) GPIO.output (RightIn1, GPIO.LOW) GPIO.output (RightIn2 GPIO.LOW) pwm_LeftSpeed.ChangeDutyCycle (80) pwm_RightSpeed.ChangeDutyCycle (80) time.sleep (delaytime) # car turn left def spin_left (delaytime): GPIO.output (LeftIn1, GPIO.LOW) GPIO.output (LeftIn2, GPIO.HIGH) GPIO.output (RightIn1, GPIO.HIGH) GPIO.output (RightIn2 GPIO.LOW) pwm_LeftSpeed.ChangeDutyCycle (80) pwm_RightSpeed.ChangeDutyCycle (80) time.sleep (delaytime) # car turn right in place def spin_right (delaytime): GPIO.output (LeftIn1, GPIO.HIGH) GPIO.output (LeftIn2, GPIO.LOW) GPIO.output (RightIn1, GPIO.LOW) GPIO.output (RightIn2 GPIO.HIGH) pwm_LeftSpeed.ChangeDutyCycle (80) pwm_RightSpeed.ChangeDutyCycle (80) time.sleep (delaytime) # car stop def brake (delaytime): GPIO.output (LeftIn1, GPIO.LOW) GPIO.output (LeftIn2, GPIO.LOW) GPIO.output (RightIn1, GPIO.LOW) GPIO.output (RightIn2, GPIO.LOW) pwm_LeftSpeed.ChangeDutyCycle (80) pwm_RightSpeed.ChangeDutyCycle (80) time.sleep (delaytime)
The camera control has two parts, one is shooting, the other is the rotation of the head.
* * camera shooting: * * in the shooting part, I use mjpg-streamer. This tool can use the camera on the car as a remote camera (Web Camera), and the installation method is very simple:
# install the necessary library sudo apt-get updatesudo apt-get install libjpeg8-devsudo apt-get install imagemagicksudo apt-get install libv4l-devsudo apt-get autoremove cmakesudo apt-get install cmake
After the necessary libraries are installed, download mjpg-streamer to the raspberry pie and compile:
Cd mjpg-streamer/mjpg-streamer-experimentalmake allsudo make install
By doing the above, you can start the camera:
# run mjpg-streamer./mjpg_streamer-I ". / input_uvc.so"-o ". / output_http.so-w. / www"
After the camera is activated, you can use a browser on any computer in the local area network to enter: http:// raspberry pie IP:8080/?action=stream to see the picture captured by the camera.
* * head control: * * the main task of the head is to encode and control the upper and lower rudders and the left and right rudders:
Import RPi.GPIO as GPIOimport timeimport pygame# steering gear pin definition ServoUpDownPin = 9ServoLeftRightPin = 1 "set GPIO port to BCM encoding GPIO.setmode (GPIO.BCM) # ignore warning message GPIO.setwarnings (False) # initialize the upper and lower left and right angles to 90 degrees ServoLeftRightPos = 90ServoUpDownPos = 90def init (): global pwm_UpDownServo global pwm_LeftRightServo GPIO.setup (ServoUpDownPin, GPIO.OUT) GPIO.setup (ServoLeftRightPin GPIO.OUT) # set steering gear frequency and initial duty cycle pwm_UpDownServo = GPIO.PWM (ServoUpDownPin, 50) pwm_LeftRightServo = GPIO.PWM (ServoLeftRightPin 50) pwm_UpDownServo.start (0) pwm_LeftRightServo.start (0) # camera steering gear rotates left and right to the specified angle def leftrightservo_appointed_detection (pos): for i in range (1): pwm_LeftRightServo.ChangeDutyCycle (2.5 + 10 * pos/180) time.sleep (0.02) # wait for the end of the 20ms cycle pwm_ LeftRightServo.ChangeDutyCycle (0) # return to zero signal # camera steering gear rotates up and down to the specified angle def updownservo_appointed_detection (pos): for i in range (1): pwm_UpDownServo.ChangeDutyCycle (2.5 + 10 * pos/180) time.sleep (0.02) # wait for the end of the 20ms cycle pwm_UpDownServo.ChangeDutyCycle (0) # return to zero # camera steering gear upward movement def servo_up (): global ServoUpDownPos pos = ServoUpDownPos updownservo_appointed_detection (pos) # time.sleep (0.05) pos + = 0.7 ServoUpDownPos = pos if ServoUpDownPos > = 180: ServoUpDownPos = 18 camera steering gear downward movement def servo_down (): global ServoUpDownPos pos = ServoUpDownPos updownservo_appointed_detection (pos) # time. Sleep (.05) pos-= 0.7 ServoUpDownPos = pos if ServoUpDownPos = 180: ServoLeftRightPos = 18 "camera steering gear moves to the right def servo_right (): global ServoLeftRightPos pos = ServoLeftRightPos leftrightservo_appointed_detection (pos) # time.sleep (0.10) pos-= 0.7 ServoLeftRightPos = pos if ServoLeftRightPos
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.