[tpyboard-micropython python can do hardware 9] five minutes of learning to use Tpyboard V102 to make a car to avoid the obstacle (upgrade Version)

Source: Internet
Author: User
Tags call back in degrees

Reprint please specify: @ Xiao Wu Yi Http://www.cnblogs.com/xiaowuyi

Welcome to join the discussion group64770604thank Shandong Radish Electronic technology company Authorized first, the experimental equipment1, Tpyboard V102 Plate 1 block 2, Motor Drive Module l298n 1 3, Motor 2 block 4, car chassis 1 5, ultrasonic Module 1 6, servo 1 7, SG90 Servo bracket a 8, ultrasonic gimbal bracket One

second, Ultrasonic Module1, What is the ultrasonic module

The ultrasonic sensor is a sensor developed using the characteristics of ultrasound, which works by transmitting an ultrasonic (much higher than the human hearing Range) and providing an output pulse corresponding to the time required for the burst echo to return to the Sensor. Ultrasonic sensors in the non-contact measurement of a wide range of applications, such as the detection of liquid water level (especially corrosive liquids, such as sulfuric acid, nitric acid liquid), car reversing collision avoidance system, Metal/non-metallic flaw detection, etc., can be used to ultrasonic distance Sensor.

2. The principle of ultrasonic module ranging

(1) the use of IO port trig trigger ranging, to a minimum of 10us high-level ordinary mail Presented.

(2) the module automatically sends 8 square waves of 40khz, and automatically detects if a signal is Returned.

(3) there is a signal return, through the IO port ECHO output A high level, the duration of the high level is the ultrasonic wave from the launch to the time of Return. Test distance = (high-normal * Sound velocity (340m/s))/2.

such as wiring, VCC for 5V power supply, GND for ground, TRIG trigger control signal input, The Board's x9,echo echo signal output, The board X10, four interface End.

three, the motor drive module1, What is the motor drive module

Motor Drive module is mainly able to control the operation of the motor: speed, operation, stop, step, uniform and other operations

2. How to use l298n

The l298n module is a 2-way H-bridge driver, so it can drive two motors at the same time, and after the method can be ENA enb, the speed and direction of the motor 1 can be driven from the IN1 IN2 input PWM signal, and the speed and direction of the motor 2 can be driven by the input PWM signal from IN3 IN4 respectively. We connect the OUT1 of the Motor 1 interface with the positive and negative poles of a motor of the OUT2 and the car, connect the OUT3 of the Motor 2 interface with the positive and negative poles of the OUT2 and the other motor of the car. Then the two sides of the terminal, namely the power supply positive (the Middle terminal is grounded) connection Tpyboard vin, The middle terminal is grounded, connecting the Tpyboard gnd,in1-in4 connection Tpyboard x1,x2,y1,y2, through x1,x2 and Y1, Y2 the high and low levels, to control the motor rotation, so that the car forward, back, left, to the Right.

Four Steering Machine1. overview

Rudder is also called Servo motor, the earliest for the ship to achieve its steering function, because it can be controlled continuously by the rotation of its corners, so the intelligent car is widely used to achieve the steering and various types of robot joint movement, as Shown.

2, the composition of the rudder machine

Generally speaking, the rudder is mainly composed of the following parts, helm, deceleration gear group, position Feedback potentiometer, DC motor, control circuit, etc., as Shown.

Servo input Line A total of three, red in the middle, is the power line, while Gray is the ground, this root line to the rudder to provide the most basic energy assurance, mainly motor rotation consumption. Power supply has two specifications, one is 4.8V, one is 6.0V, respectively, corresponding to different torque standards, that is, the output torque is different, 6.0V corresponding to a larger, specific to the application conditions; another line is the control signal line, Futaba is generally white, Jr is generally orange YELLOW. It is also important to note that certain models of the Sanwa lead power cords on the side, not the middle, need to be identified. But remember that red is the power, Gray is the ground, the remaining one for the signal line, generally will not be Mistaken. In this experiment, the servo red Tpyboard v102+ vin pin, the gray connection Tpyboard v102+ gnd pin, The remaining orange is the signal line, then Tpyboard v102+ the X3 pin. Tpyboard v102+ 's X1, X2, X3, and X4 are signal pins.

3. Working principle of Servo machine

The Control circuit board accepts the control signal from the signal line, controls the motor rotation, The motor drives a series of gear sets, slows down and drives to the output helm. Servo output shaft and position Feedback potentiometer is connected, helm rotation at the same time, driving position feedback potentiometer, potentiometer will output a voltage signal to the control circuit board, feedback, and then control the circuit board according to the position of the motor to determine the direction and speed of rotation, so as to achieve the target stop. The workflow is: control signal → control circuit board → motor rotation → gear Group deceleration → Helm rotation → position feedback potentiometer → Control circuit board FEEDBACK.

Five the construction function and method of servo motor1. constructor function

Pyb. Servo (id)

In this ID is 1-4, corresponding to the x1-x4 of the Tpyboard v102+.

2. methods

Servo.angle ([Angle, time= 0 ])

If no argument is given, the function returns the current Angle.

If a parameter is given, the function sets the servo angle:

    • The angle is the angle that moves in Degrees.
    • Time is the number of milliseconds required to reach the specified Angle. If omitted, the server moves as quickly as possible to its new Location.

Servo.speed ([speed, Time=0])

If no argument is given, the function returns the current Speed.

If a parameter is given, the function sets the speed of the Servo:

    • Speed is the rate of change between 100 and 100.
    • Time is the number of milliseconds required to reach the specified Speed. If omitted, the server accelerates as fast as Possible.

Servo.pulse_width([value])

If no argument is given, the function returns the current value of the original pulse Width.

If a parameter is given, the function sets the original pulse width Value.

Servo.calibration([pulse_min,pulse_max,pulse_centre [ ,pulse_angle_90,pulse_speed_100])

If no argument is given, the function returns the current calibration data as a 5-tuple.

If a parameter is given, the function sets the timing calibration:

    • The pulse_min is the minimum allowable pulse width.
    • The Pulse_max is the maximum allowable pulse width.
    • The pulse_centre is the pulse width corresponding to the center/0 Position.
    • The pulse_angle_90 is a pulse width corresponding to 90 degrees.
    • The pulse_speed_100 is the pulse width corresponding to the speed 100.

VI. requirements of the experiment

This experiment is an intelligent obstacle avoidance trolley experiment, The main realization of the Car's obstacle avoidance function, The current square distance is too close, tpyboard v102+ will control the rudder rotation ultrasonic gimbal support to judge the front, left front, right front distance, thus control the car to the left, turn right or forward, to achieve the function of avoiding the Obstacle.

Seven Experimental Results

Video Address: http://player.youku.com/player.php/sid/XMjY2MTQ1NDA2MA==/v.swf

Eight Source Code

Variable naming is a little low, let's change it ourselves.

# Main.py--put Your code here!
#www. tpyboard.com
#turnipsmart. taobao.com
Import pybfrom pyb import pinfrom pyb import timerfrom pyb import servox1 = Pin (' X1 ', pin.out_pp) x2 = Pin (' X2 ', pin.out_pp ) Y1 = Pin (' Y1 ', pin.out_pp) y2 = pin (' Y2 ', pin.out_pp) Trig = Pin (' X9 ', pin.out_pp) Echo = Pin (' X10 ', pin.in) num=0flag=0run= 1zuo=0qian=0you=0distance=0def start (t): global flag Global num if (flag==0): num=0 else:num=num +1def Stop (t): Global run if (run==0): run=1start1=timer (1,freq=10000,callback=start) stop1=timer (4,freq=2,call Back=stop) #小车左转def left (): x1.high () x2.low () y1.high () y2.low () #小车前进def go (): x1.high () x2.low () y1. Low () Y2.high () #小车后退def back (): x1.low () x2.high () y1.high () y2.low () #小车右转def right (): x1.low () X2.hi GH () y1.low () y2.high () #小车停止def stop (): x1.low () x2.low () y1.low () y2.low () #控制舵机向右, left, forward def Servo (): Global Distance Global Zuo Global you global Qian Servo3=pyb. Servo (3) #向右75旋转 servo3.angle ( -75,500) pyb.delay (+) Ceju () You=distance print (' you ', you) #向左转75度 servo3.angle (75,500) pyb.delay (+) ceju () zuo=distance Print (' Zuo ', distance) #转到0度 servo3.angle (0,500) pyb.delay (+) ceju () qian=distance print (' qian ', distance) #返回正前方, left front, right front distance return qian,zuo,you# ranging method def ceju (): global flag Global NUM Global run global distance If (run==1): trig.value (1) pyb.udelay (+) trig.value (0) while (echo.value () ==0): Trig.value (1) pyb.udelay (+) trig.value (0) flag=0 if (echo.value () ==1): Flag=1 while (echo.value () ==1): flag=1 if (num!=0): #测距 D ISTANCE=NUM/10000*34299/2 #print (' Distance: ') #print (Distance, ' cm ') pyb.delay (500) Flag=0 run=0 return distancedef main (): global distance global Zuo Global you global Qian Servo3 =pyb. Servo (3) servo3.Angle (0,500) pyb.delay (+) ceju () while 1==1:ceju (), #前方距离大于40cm前进 if distance > 40: Go () ceju () print (' juli ', distance) #前方距离小于40cm if distance <= 40: Stop () back () pyb.delay () stop () Servo () #如果右前方距离大于左前方 If You>zuo:right () pyb.delay (+) ceju () #如果左前方距离大于右前方 If Zuo>you:left () pyb.delay (+) ceju () #如果所有方向距离全部小 At 15cm if zuo<15 and you< and Qian<15:stop () back () p Yb.delay () Stop () ceju () #有一个方向距离小于5CM if zuo<5 or you <5 or Qian<5:stop () back () pyb.delay () stop () Ceju () Main ()

  

[tpyboard-micropython python can do hardware 9] five minutes of learning to use Tpyboard V102 to make a car to avoid the obstacle (upgrade Version)

Related Article

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.