Raspberry Pi Trolley (iii) Python control trolley

Source: Internet
Author: User

Before the text

Because recently busy review exam, so did not pick up the Raspberry Pi car, until yesterday, finally empty time to put the code down and share with you.

Body

In the second of the Raspberry Pi Trolley series, there are two ways to define the pin of the Raspberry Pi:

    • Physical numbering
    • GPIO numbering

I'm using the second way.

Start writing 1. Import Library
import RPi.GPIO as GPIOimport time
2. Define interface mode and interface location

In1-in4 for l298n access, IN5 and IN6 for infrared module access.

Wheel drive Mode:

IN1 and IN2 are responsible for driving wheel rotation (forward).
IN3 and IN4 are responsible for driving wheel rotation (back).

3. Initialization
def init():    GPIO.setup(IN1, GPIO.OUT)    GPIO.setup(IN2, GPIO.OUT)    GPIO.setup(IN3, GPIO.OUT)    GPIO.setup(IN4, GPIO.OUT)    GPIO.setup(IN5, GPIO.IN)    GPIO.setup(IN6, GPIO.IN)

The interface of the connecting l298n is set to output, because the output signal is required to drive the motor.
The interface connecting the infrared module is input, because the input signal is required to make a judgment.

4. Basic direction behavior
def up():    GPIO.output(IN1, GPIO.HIGH)          //右侧车轮前进    GPIO.output(IN2, GPIO.HIGH)          //左侧车轮前进    GPIO.output(IN3, GPIO.LOW)    GPIO.output(IN4, GPIO.LOW)def down():    GPIO.output(IN1, GPIO.LOW)    GPIO.output(IN2, GPIO.LOW)    GPIO.output(IN3, GPIO.HIGH)          //右侧车轮后退    GPIO.output(IN4, GPIO.HIGH)          //左侧车轮后退def turn_left():    GPIO.output(IN1, GPIO.HIGH)          //右侧车轮前进    GPIO.output(IN2, GPIO.LOW)    GPIO.output(IN3, GPIO.LOW)    GPIO.output(IN4, GPIO.LOW)def turn_right():    GPIO.output(IN1, GPIO.LOW)    GPIO.output(IN2, GPIO.HIGH)          //左侧车轮前进    GPIO.output(IN3, GPIO.LOW)    GPIO.output(IN4, GPIO.LOW)

What needs to be explained is: Because does not contain the rudder machine, the turn operation is the unilateral wheel drive, drives the body rotation

5. Infrared control
init()n = 5while (n > 0):                                  //总共转弯五次    in_left = GPIO.input(IN5)                   //左侧红外线接收器    in_right = GPIO.input(IN6)                  //右侧红外线接收器    up()                                        //未遇到障碍时直行    if (in_left == GPIO.LOW):        down()        time.sleep(1)        turn_right()        time.sleep(1)        n = n - 1        continue        if (in_right == GPIO.LOW):        down()        time.sleep(1)        turn_left()        time.sleep(1)        n = n - 1        continue        if (in_right == GPIO.LOW & in_left == GPIO.LOW):        down()        time.sleep(1)        turn_right()               //如果两侧都有障碍,就右转(个人喜好)        time.sleep(1)        n = n - 1        continuestop()                      GPIO.cleanup()                    //清空GPIO接口配置信息

If you encounter obstacles ahead, step back for a second, then turn and continue straight until you encounter the next hurdle.

What to note is the use of Time.sleep ():
Time.sleep (time) means that the next action is taken after a period of a second, which means sleeping for a while.
If you do not use Time.sleep (), the first step will be performed.

About running:
    • Due to the short effective distance of the infrared sensor (around 5cm) and the location of the sensor at the bottom of the vehicle, it is possible to delay the obstacle in the event of a rugged object when the vehicle is in motion.
Run video

Raspberry Pi Trolley

A bump in the face of a rugged object (rough environment)

About Raspberry Pi information on the first, and so I made a remote control to introduce, thank you for your attention.

Raspberry Pi Trolley (iii) Python control trolley

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.