2761浏览
查看: 2761|回复: 1

[项目] 【智控万物】PID平衡球

[复制链接]
基于Arduino的简易PID控制实验

1、arduino中pid库安装:https://www.arduino.cc/en/Guide/Libraries#toc3

2、演示亮度积分控制
【智控万物】PID平衡球图1
【智控万物】PID平衡球图9【智控万物】PID平衡球图2
  1. #include <PID_v1.h>
  2. double input;
  3. double output;
  4. double Setpoint;
  5. double Kp=0, Ki=1, Kd=0;
  6. PID myPID(&input, &output, &Setpoint, Kp, Ki, Kd, DIRECT);
  7. void setup() {
  8.   Serial.begin(9600);
  9.   pinMode(5, OUTPUT);
  10.   Setpoint =75;  //设置PID的输出值
  11.   myPID.SetMode(AUTOMATIC);//设置PID为自动模式
  12.   //myPID.SetSampleTime(200);//设置PID采样频率为200ms
  13.   //myPID.SetOutputLimits(0,255);
  14.   myPID.SetTunings(Kp, Ki, Kd);
  15. }
  16. void loop() {
  17.   input = analogRead(A0);            // reads the value of the potentiometer (value between 0 and 1023)
  18.   input=map(input, 0, 1024, 0, 255);
  19.   myPID.Compute();//PID转换完成返回值为1
  20.   analogWrite(5,output);            
  21.   Serial.print(input, 0);Serial.print("     ");Serial.print(output, 0);Serial.print("     ");Serial.println(Setpoint, 0);
  22. }
复制代码
【智控万物】PID平衡球图3

Ki=1
【智控万物】PID平衡球图4

Ki=2


【平衡球实验】
平衡球实验需要控制舵机,180度大舵机。单独供电。
1、测试舵机
DSS-P05 5.1Kg 机器人专用舵机:舵机采用金属齿轮和双轴承结构,耐磨耐用。在一些负载大或者长期运作的项目上面,能够充分保证项目的稳定性。附带的舵机盘和齿轮能够让你省去很多固定的麻烦。https://www.dfrobot.com.cn/goods-230.html
【智控万物】PID平衡球图8

  1. /*
  2. Controlling a servo position using a potentiometer (variable resistor)
  3. by Michal Rinott <[url]http://people.interaction-ivrea.it/m.rinott>[/url]
  4. modified on 8 Nov 2013
  5. by Scott Fitzgerald
  6. [url]http://www.arduino.cc/en/Tutorial/Knob[/url]
  7. */
  8. #include <Servo.h>
  9. Servo myservo;  // create servo object to control a servo
  10. int potpin = 0;  // analog pin used to connect the potentiometer
  11. int val;    // variable to read the value from the analog pin
  12. void setup() {
  13.   myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  14. }
  15. void loop() {
  16.   val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  17.   val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  18.   myservo.write(val);                  // sets the servo position according to the scaled value
  19.   delay(15);                           // waits for the servo to get there
  20. }
复制代码
2、传感器使用超声波平整墙面的有效测距量程为2–500cm
【智控万物】PID平衡球图7

URM09-模拟量超声波测距传感器:https://www.dfrobot.com.cn/goods-1954.html
测试程序
  1. // # Editor : roker
  2. // # Date : 18.02.2019
  3. // # Product name: URM09 Ultrasonic Sensor(Gravity Analog)(V1.0)
  4. // # Product SKU : SEN0307
  5. // # Version : 1.0
  6. #define MAX_RANG (520)//模块测距极值为520cm(比有效最大量程值略大)
  7. #define ADC_SOLUTION (1023.0)//Arduino UNO 的ADC精度为10bit
  8. int sensityPin = A0; // select the input pin
  9. void setup() {
  10. // Serial init
  11. Serial.begin(9600);
  12. }
  13. float dist_t, sensity_t;
  14. void loop() {
  15. // read the value from the sensor:
  16. sensity_t = analogRead(sensityPin);
  17. // turn the ledPin on
  18. dist_t = sensity_t * MAX_RANG / ADC_SOLUTION;//
  19. Serial.print(dist_t, 0);
  20. Serial.println("cm");
  21. delay(500);
  22. }
复制代码

3、制作平衡装置

【智控万物】PID平衡球图5


【智控万物】PID平衡球图6

4、程序编写
  1. #include <PID_v1.h>
  2. #include <Servo.h>
  3. #define  MAX_RANG      (520)//模块测距极值为520cm(比有效最大量程值略大)
  4. #define  ADC_SOLUTION      (1023.0)//Arduino UNO 的ADC精度为10bit
  5. int sensityPin = A1;    // select the input pin
  6. Servo myservo;  // create servo object to control a servo
  7. int val;    // variable to read the value from the analog pin
  8. double input;
  9. double output;//用于提供给舵机角度值。
  10. double Setpoint;
  11. double Kp=4, Ki=0.05, Kd=1.5;
  12. PID myPID(&input, &output, &Setpoint, Kp, Ki, Kd,REVERSE);
  13. void setup() {
  14.   myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  15.   Serial.begin(9600);
  16.   Setpoint =15;  //设置PID的输出值
  17.   myPID.SetMode(AUTOMATIC);//设置PID为自动模式
  18.   myPID.SetSampleTime(200);//设置PID采样频率为200ms
  19.   myPID.SetOutputLimits(0,180);
  20.   myPID.SetTunings(Kp, Ki, Kd);
  21. }
  22. float dist_t, sensity_t;
  23. void loop() {
  24.   //val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  25.   //val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  26.   
  27.   sensity_t = analogRead(sensityPin);
  28.   // turn the ledPin on
  29.   input = sensity_t * MAX_RANG  / ADC_SOLUTION;//
  30.   myPID.Compute();//PID转换完成返回值为1
  31.   myservo.write(output);                  // sets the servo position according to the scaled value
  32.   Serial.print(input);Serial.print("     ");Serial.print(output);Serial.print("     ");Serial.println(Setpoint, 0);
  33.                   
  34. }
复制代码
4、调试PID
PID(&Input, &Output, &Setpoint, Kp, Ki, Kd, Direction)
各参数解释请参考:Arduino PID 库函数指南https://blog.csdn.net/ie_jeton/article/details/76913229
调了一个小时,先确定为:double Kp=4, Ki=0.05, Kd=1.5;
5、结果演示视频

szjuliet  版主

发表于 2021-2-3 18:39:22

厉害!
回复

使用道具 举报

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

本版积分规则

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

硬件清单

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

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

mail