├── 1.jpg ├── 2.jpg ├── 3.jpg ├── README.md ├── car.py ├── main.py └── 使用方法(必读).txt /1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maoshushuA/openmv-/171b973f48b41f1c361c6be2c78ab41718b5551c/1.jpg -------------------------------------------------------------------------------- /2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maoshushuA/openmv-/171b973f48b41f1c361c6be2c78ab41718b5551c/2.jpg -------------------------------------------------------------------------------- /3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maoshushuA/openmv-/171b973f48b41f1c361c6be2c78ab41718b5551c/3.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # openmv- 2 | 仅使用openmv驱动的寻黑线小车,使用PD控制差速 3 | -------------------------------------------------------------------------------- /car.py: -------------------------------------------------------------------------------- 1 | from pyb import Pin, Timer 2 | inverse_left=False #change it to True to inverse left wheel 3 | inverse_right=False #change it to True to inverse right wheel 4 | 5 | ain1 = Pin('P0', Pin.OUT_PP) 6 | ain2 = Pin('P1', Pin.OUT_PP) 7 | bin1 = Pin('P2', Pin.OUT_PP) 8 | bin2 = Pin('P3', Pin.OUT_PP) 9 | E = Pin('P4',Pin.OUT_PP) 10 | E.high() 11 | ain1.low() 12 | ain2.low() 13 | bin1.low() 14 | bin2.low() 15 | 16 | pwma = Pin('P7') 17 | pwmb = Pin('P8') 18 | tim = Timer(4, freq=1000) 19 | ch1 = tim.channel(1, Timer.PWM, pin=pwma) 20 | ch2 = tim.channel(2, Timer.PWM, pin=pwmb) 21 | ch1.pulse_width_percent(0) 22 | ch2.pulse_width_percent(0) 23 | 24 | def run(left_speed, right_speed): 25 | if inverse_left==True: 26 | left_speed=(-left_speed) 27 | if inverse_right==True: 28 | right_speed=(-right_speed) 29 | 30 | if left_speed < 0: 31 | ain1.low() 32 | ain2.high() 33 | else: 34 | ain1.high() 35 | ain2.low() 36 | ch1.pulse_width_percent(abs(left_speed)) 37 | 38 | if right_speed < 0: 39 | bin1.low() 40 | bin2.high() 41 | else: 42 | bin1.high() 43 | bin2.low() 44 | ch2.pulse_width_percent(abs(right_speed)) 45 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import sensor, image, time ,car#导入模块 2 | from pyb import Pin, Timer 3 | sensor.reset() 4 | sensor.set_pixformat(sensor.RGB565)#设置颜色格式 5 | sensor.set_framesize(sensor.QQQVGA)#设置分辨率 6 | sensor.skip_frames(time = 200) 7 | sensor.set_auto_gain(False)#关闭自动增益 8 | sensor.set_auto_whitebal(False)#关闭白平衡 9 | sensor.set_auto_exposure(False,21000)#关闭自动曝光设置为手动曝光 10 | tim = Timer(4, freq=1000)#设置pwm引脚频率 11 | Bline = (0, 22, -18, 15, -7, 35)#巡线LAB采样值 12 | Runroi = ( 0, 0, 80, 20 )#巡线检测区域 13 | yvanroi = ( 20, 0, 40, 60 )#判圆环区域 14 | Errors = 0#偏差 15 | LastErrors = 0#上次偏差 16 | Kp =3#比例系数 17 | Kd = 10#微分系数 18 | Center = 40#摄像头巡线中值 19 | PID = 0#pid输出量 20 | DErrors = 0#两次偏差变化率 21 | ValueL = 150#左电机速度 22 | ValueR = 150#右电机速度 23 | clock = time.clock() 24 | while(True): 25 | clock.tick() 26 | img = sensor.snapshot()#拍摄一幅图片进入缓存 27 | img = img.lens_corr(1.8)#镜头畸变矫正,使之更贴近现实 28 | for blob in img.find_blobs([Bline],roi=Runroi,merge=True):#色块检测函数,返回检测到的色块相关的值并存入blob中 29 | #详情链接:https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_blobs#image.find_blobs 30 | img.draw_rectangle(blob.rect())#在缓存中的图片内画方框,便于上位机查看识别情况 31 | Errors = blob.cx() - Center#pid偏差计算 32 | DErrors = Errors - LastErrors#微分项需要的两次偏差的差 33 | PID = Errors*Kp + DErrors*Kd#pid计算 34 | LastErrors = Errors#将本次偏差保存,用于微分系数的计算 35 | #print(PID) 36 | for yvan in img.find_circles(threshold = 2300, x_margin = 10, y_margin = 10, r_margin = 10,#检测圆形并返回相关信息 37 | r_min = 2, r_max = 50, r_step = 2):#threshold为检测敏感阈值,阈值越大越不容易误判,但不敏感,越小检测越准确但容易误判 38 | img.draw_circle(yvan.x(), yvan.y(), yvan.r(), color = (255, 0, 0))#在缓存中的图片内画方框,便于上位机查看识别情况 39 | if(yvan.y()>35):#当圆y轴方向大于某值时,停车充电,当单片机调试时卡死,建议先屏蔽本函数 40 | # car.run(80,80) 41 | # time.sleep(500) 42 | car.run(0,0)#停车 43 | time.sleep(3000)#充电时间设置,注意易导致单片机卡死 44 | 45 | # car.run(ValueL+PID,ValueR-PID) 46 | car.run(ValueR-PID,ValueL+PID)#差速后发向电机pwm 47 | # print(ValueL+PID) 48 | #作者@Mao叔叔啊 青岛恒星科技学院 2020.9.7 49 | #苏卡不列队 曹春秋 梁锦华 王福震 50 | -------------------------------------------------------------------------------- /使用方法(必读).txt: -------------------------------------------------------------------------------- 1 | 本程序需要配合TB6612FNG电机驱动模块使用 2 | 可用于openmv3和以上版本 3 | 将main.py和car.py考入单片机片上flash 4 | 接线如下 5 | AIN1=P0 6 | AIN2=P1 7 | BIN1=P2 8 | BIN2=P3 9 | STBY=P4 10 | PWMA=P7(右电机) 11 | PWMB=P8(左电机) 12 | 13 | --------------------------------------------------------------------------------