极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2517|回复: 0

双轮平衡车实现自主平衡功能

[复制链接]
发表于 2023-4-25 10:05:18 | 显示全部楼层 |阅读模式
本帖最后由 机器谱 于 2023-4-25 10:05 编辑

1. 运动功能说明
       R209样机是一款双轮小车,适合作为自平衡车的实验模型。本文示例将实现双轮小车自主平衡功能,当人为的去打破小车的平衡点时,小车会自动调节至平衡点并稳定下来。


2. 结构说明
       该样机主要由两个 直流驱动轮模组【https://www.robotway.com/h-col-117.html】构成。

3. 电子硬件

       在这个示例中,我们采用了以下硬件,请大家参考:

主控板
Basra主控板(兼容Arduino Uno)
扩展板
Bigfish2.1扩展板
传感器加速度传感器
电池7.4V锂电池

       电路连接:将两个直流电机分别连接在Bigfish扩展板的(D5,D6)以及(D9,D10)接口上;加速度传感器连接在Bigfish扩展板的A0端口上。


4. 运动功能实现

编程环境:Arduino 1.8.19
将参考例程(self_balance_car.ino)下载到主控板,小车将实现双轮自平衡功能,运动效果可参考官网的实验演示视频。
  1. /*------------------------------------------------------------------------------------

  2.   版权说明:Copyright 2023 Robottime(Beijing) Technology Co., Ltd. All Rights Reserved.

  3.            Distributed under MIT license.See file LICENSE for detail or copy at

  4.            https://opensource.org/licenses/MIT

  5.            by 机器谱 2023-04-12 https://www.robotway.com/

  6.   ------------------------------*/

  7. #include <SignalFilter.h>

  8. SignalFilter Filter;

  9. char filtered;

  10. int a,b,c,d;

  11. const int analogInPin = A0;   // Analog input pin that the potentiometer is attached to

  12. int sensorValue = 0;        // value read from the pot

  13. int outputValue = 0;        // value output to the PWM (analog out)

  14. void setup() {

  15.   // initialize serial communications at 9600 bps:

  16.   Serial.begin(9600);

  17.   Filter.begin();

  18.   Filter.setFilter('m');

  19.   Filter.setOrder(2);

  20. }

  21. void loop() {

  22.   // read the analog in value:

  23.   sensorValue = analogRead(analogInPin);           

  24.   // map it to the range of the analog out:

  25.   char outputValue = map(sensorValue, 0, 1023, 0, 255);  

  26.   filtered= Filter.run(outputValue);

  27.   int a=abs(filtered);

  28.    if((a>=91)&&(a<=100))

  29.     {   

  30.      analogWrite(9,0);

  31.      analogWrite(10,0);

  32.      analogWrite(5,0);

  33.      analogWrite(6,0);

  34.      delay(5);}

  35.      

  36.    if(a>=101)

  37.     {

  38.      c=a+(a-100)*5;

  39.      d=a+20+(a-100)*5;     

  40.      analogWrite(9,0);

  41.      analogWrite(10,c);

  42.      analogWrite(5,0);

  43.      analogWrite(6,d);

  44.      delay(5);   

  45.      analogWrite(9,30);

  46.      analogWrite(10,0);

  47.      analogWrite(5,40);

  48.      analogWrite(6,0);}  

  49.    

  50.    if(a<=90)

  51.     {c=a+(90-a)*3+40;

  52.      d=a+20+(90-a)*3+40;

  53.      analogWrite(9,c);

  54.      analogWrite(10,0);

  55.      analogWrite(5,d);

  56.      analogWrite(6,0);

  57.      delay(5);

  58.      analogWrite(9,0);

  59.      analogWrite(10,30);

  60.      analogWrite(5,0);

  61.      analogWrite(6,40);

  62.    }   

  63.   Serial.print(a);Serial.print("/");

  64.   Serial.print(c);Serial.print("/");

  65.   Serial.println(d);

  66. }
复制代码

5. 资料下载

资料内容:
​①例程源代码
​②样机3D文件
资料下载地址:https://www.robotway.com/h-col-207.html

想了解更多机器人开源项目资料请关注 机器谱网站 https://www.robotway.com

回复

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-29 02:22 , Processed in 0.037815 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表