【智控万物】回力车

2021-02-056822
AI 快速预览详细 收起
本项目介绍了如何使用掌控板和PID控制算法制作一个能够自动返回的回力车。通过调整Kp和Ki参数,小车能够沿设定方向前进并自动返回。项目中使用了掌控板的指南针功能,并通过Bylnk发送方向指令。整个过程不仅锻炼了孩子的动手能力,还培养了他们的逻辑思维。


【项目背景】

回力镖(Boomerang),又称回飞棒、飞旋镖、回旋镖,一种掷出后可以利用空气动力学原理飞回来的打猎用具,曾作为一些地区土著的狩猎工具。其中澳大利亚原住民的最为著名。飞去来器绕着弧形轨道飞行,在不使用其他的工具的情况下,以最短的时间飞出最长的距离。

【项目设计】
设计一个“回力车”,丢出去会自己回来。

【项目原理】
使用掌控板的“指南针”,将方向指向自己。利用“PID”控制小车沿着设定的方向前进。

【项目设想】
使用手机Bylnk,向掌控板发送方向,控制小车按方向前进。

【项目准备】
1、PID
PID即:Proportional(比例)、Integral(积分)、Differential(微分)的缩写。顾名思义,PID控制算法是结合比例、积分和微分三种环节于一体的控制算法,它是连续系统中技术最为成熟、应用最为广泛的一种控制算法,该控制算法出现于20世纪30至40年代,适用于对被控对象模型了解不清楚的场合。
【智控万物】回力车图4

2、Arduino PID库
介绍的很清楚:
使用PID库,轻松搞定PID:https://www.arduino.cn/thread-15176-1-2.html

【编写自己的PID】
1、添加“掌控宝”库
【智控万物】回力车图2


2、Mind+代码

  1. # MindPlus
  2. # mpython
  3. from mpython import *
  4. import parrot
  5. import math
  6. import time
  7. # 自定义函数
  8. def SetTunings(Kp, Ki, Kd):
  9.     global lastTime, Input, Output, Setpoint, kp, ki, kd, now, timeChange, error, SampleTime, SampleTimeInSec, ratio, lastInput, dInput, ITerm, OutMax, OutMin
  10.     SampleTimeInSec = (SampleTime / 1000)
  11.     kp = Kp
  12.     ki = (Ki * SampleTimeInSec)
  13.     kd = (Kd / SampleTimeInSec)
  14. def Compute():
  15.   global lastTime, Input, Output, Setpoint, kp, ki, kd, now, timeChange, error, SampleTime, SampleTimeInSec, ratio, lastInput, dInput, ITerm, OutMax, OutMin
  16.   now = time.ticks_ms()
  17.   timeChange = (now - lastTime)
  18.   if (timeChange >= SampleTime):
  19.     error = (Setpoint - Input)
  20.     ITerm = (ITerm + (ki * error))
  21.     if (ITerm > OutMax):
  22.        ITerm = OutMax
  23.     else:
  24.        if (ITerm < OutMin):
  25.            ITerm = OutMin
  26.     dInput = (Input - lastInput)
  27.     Output = ((kp * error) + (ITerm - (kd * dInput)))
  28.     if (Output > OutMax):
  29.            Output = OutMax
  30.     else:
  31.         if (Output < OutMin):
  32.             Output = OutMin
  33.     lastInput = Input
  34.     lastTime = now
  35. def SetSampleTime(NewSampleTime):
  36.   global lastTime, Input, Output, Setpoint, kp, ki, kd, now, timeChange, error, SampleTime, SampleTimeInSec, ratio, lastInput, dInput, ITerm, OutMax, OutMin
  37.   if (NewSampleTime > 0):
  38.     ratio = (NewSampleTime / SampleTime)
  39.     ki = (ki * ratio)
  40.     kd = (kd / ratio)
  41.     SampleTime = NewSampleTime
  42. oled.DispChar("准备开始", 0, (1-1)*16, 1)
  43. oled.show()
  44. magnetic.calibrate()
  45. oled.DispChar("校准完成", 0, (1-1)*16, 1)
  46. oled.show()
  47. lastTime = 0
  48. Input = 0
  49. Output = 0
  50. ITerm = 0
  51. OutMax = 50
  52. OutMin = -50
  53. lastInput = 0
  54. SampleTime = 100
  55. Setpoint = 190
  56. kp = 1
  57. ki = 0.2
  58. kd = 0
  59. while True:
  60.   Input = magnetic.get_heading()
  61.   Compute()
  62.   parrot.set_speed(parrot.MOTOR_1, (int((50 + Output))))
  63.   parrot.set_speed(parrot.MOTOR_2, -(int((50 - Output))))
  64.   oled.fill(0)
  65.   oled.DispChar((str(Input)), 0, (1-1)*16, 1)
  66.   oled.DispChar((str(Output)), 0, (2-1)*16, 1)
  67.   oled.DispChar((str(Setpoint)), 0, (3-1)*16, 1)
  68.   oled.show()
复制代码



3、图型
【智控万物】回力车图1

4、调整Kp,Kikp = 1
ki = 0.2
kd = 0
慢慢调
【回力车】
【智控万物】回力车图3

【演示视频】







创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(4)
DFB1omGQvjw
DFB1omGQvjw
老师,你好,我没有掌控版,可否用esp32代替,来做?
云天
云天
作者
回复DFB1omGQvjw
能算出方向才行,有[backcolor=rgb(255, 255, 255)][font=-apple-system, "]加速度传感器和地磁传感器共同计算,才能得出[/font][/backcolor]像掌控板算出指南针方向。
云天
云天
作者
越来越感觉PID是个好东西!
szjuliet
szjuliet
嘻嘻,甩不掉的牛皮糖
rzyzzxw
rzyzzxw
赞的
- 没有更多了 -