In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
What this article shares with you is about how the raspberry pie car can move forward and backward. The editor thinks it is very practical, so I share it with you. I hope you can learn something after reading this article. Let's take a look at it with the editor.
The advance of raspberry pie car
Bought a raspberry pie car accessories, of course, there is a secondary development board, but the principle is the same. The board I bought is Chuanglebo's smart car kit. The following code comes from its guidance file, but the documents are all in English and there is no code explanation, which makes it difficult to understand. I rewrote it, and gave my own understanding, the code is also slightly different, but from scratch, I can really understand the principle.
1. First of all, according to the circuit diagram, set the import RPi.GPIO as GPIOimport time# binding pin corresponding to the raspberry pie pin, which comes from the drawing PWMA=18AIN1 = 22AIN2 = 27PWMBpie 23BIN1 = 25BIN2 = 24.
18 pins control the left two motors 23 pins control the right two motors AIN1, AIN2, BIN1, BIN2 controls the forward rotation and flipping of the left and right motors # # 2, setting pin mode and input and output
GPIO.setmode (GPIO.BCM) GPIO.setwarnings (False) # set pin to output GPIO.setup (PWMA, GPIO.OUT) GPIO.setup (AIN1, GPIO.OUT) GPIO.setup (AIN2, GPIO.OUT) GPIO.setup (PWMB, GPIO.OUT) GPIO.setup (BIN1, GPIO.OUT) GPIO.setup (BIN2, GPIO.OUT) # Motor leftMotor = GPIO.PWM (PWMA, 100) rightMotor = GPIO.PWM (PWMB, 100) leftMotor.start (0) rightMotor.start (0)
In this way, the setup is basically complete and the motor is on standby.
3. Forward and backward functions
To make the car move forward, we need to turn the four motors. Since it is moving forward, of course the motor is turning forward.
Def forward (speed,runtime): leftMotor.ChangeDutyCycle (speed) GPIO.output (AIN1, True) # AIN1 High level forward GPIO.output (AIN2, False) # if True flips the time rightMotor.ChangeDutyCycle (speed) GPIO.output (BIN1, True) GPIO.output (BIN2, False) time.sleep (runtime) # maintains the state, if nothing else is given to the command execution, execution will continue
When the four wheels are turning positively, the basic forward function is realized. Of course, it's the same thing to go back, you just need to set it in reverse.
Def backword (speed, backtime): leftMotor.ChangeDutyCycle (speed) GPIO.output (AIN2, True) # AIN2 GPIO.output (AIN1, False) # AIN1 rightMotor.ChangeDutyCycle (speed) GPIO.output (BIN2, True) # BIN2 GPIO.output (BIN1, False) # BIN1 time.sleep (backtime)
At this point, the functions of forward and backward have been realized. Finally, the car is run by calling the function.
If\ _ name\ _\ _ ='\ _ _ main\ _\ _': try: while True: forward (50Magne3) except KeyboardInterrupt: GPIO.cleanup ()
Be sure to call the GPIO.cleanup () function to clear the relevant settings on the pin, otherwise the pin will be occupied, which may affect the next run of the program.
4. General ideas
The motor is connected to the circuit board, and ChangeDutyCycle allows us to set the speed by changing the duty cycle. When the speed is set and the forward or flip is set, it is realized through the connected AIN1 and AIN2 interfaces. (here is the name of the circuit module, which can be given a different name or connected to a different interface in practice.)
This is how the raspberry car moves forward and backward. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.