1.循迹小车

循迹小车,我这里使用openmv来实现图像识别功能,接下来我会通过对pid.py,car.py,main.py的文件进行一一阐释并如何使用,做出解释。

1.car.py

定义openmv的引脚来来控制stm32,这里的openmv作为上位机,而stm32作为下位机接收信息传输来做出实际操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from pyb import Pin,Timer
inverse_left = False
inverse_right = False

#推挽输出
ain1 = Pin('P0',Pin.OUT_PP)
ain2 = Pin('P1',Pin.OUT_PP)
bin1 = Pin('P2',Pin.OUT_PP)
bin2 = Pin('P3',Pin.OUT_PP)

ain1.low()
ain2.low()
bin1.low()
bin2.low()

"""
Pin()--->是对这个引脚的设置 'Px' x为Openmv的引脚口
上面是对四个引脚的设置
------------------------------
下面是定义PWM
"""

#这两个引脚用来与stm32f103进行串口通信
pwma = Pin('P7')
pwmb = Pin('P8')
tim = Timer(4,freq = 1000) #设置定时器


#ch1 ch2 这是我们设置的定时器的两个通道,pwm模式,引脚为'P7','P8'
ch1 = tim.channel(1,Timer.PWM,pin = pwma)
ch2 = tim.channel(2,Timer.PWM,pin = pwmb)
ch1.pulse_width_percent(0)
ch2.pulse_width_percent(0)

"""

上面是对openmv引脚的初始化
------------------------------
以下是函数设置
@para left_speed 设置左轮的速度
@para right_speed 设置右轮的速度

"""

def run(left_speed,right_speed):
if inverse_left == True:
left_speed = (-left_speed)
if inverse_right == True:
right_speed = (-right_speed)


#轮子的速度设置
if left_speed < 0: