1875浏览
查看: 1875|回复: 5

【智控万物】回力车

[复制链接]
本帖最后由 云天 于 2021-2-5 20:30 编辑

【项目背景】

回力镖(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

【演示视频】







rzyzzxw  版主

发表于 2021-2-6 09:33:46

赞的
回复

使用道具 举报

szjuliet  版主

发表于 2021-2-6 09:57:04

嘻嘻,甩不掉的牛皮糖
回复

使用道具 举报

云天  初级技神
 楼主|

发表于 2021-2-6 10:06:58

越来越感觉PID是个好东西!
回复

使用道具 举报

DFB1omGQvjw  学徒

发表于 2021-2-6 16:08:19

老师,你好,我没有掌控版,可否用esp32代替,来做?
回复

使用道具 举报

云天  初级技神
 楼主|

发表于 2021-2-6 23:18:48

本帖最后由 云天 于 2021-2-6 23:22 编辑
DFB1omGQvjw 发表于 2021-2-6 16:08
老师,你好,我没有掌控版,可否用esp32代替,来做?

能算出方向才行,有加速度传感器和地磁传感器共同计算,才能得出像掌控板算出指南针方向。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail