机器谱

R209】双轮平衡车

作者:机器谱

图文展示3264(1)

图文展示3264(1)

副标题

概述
自平衡

1. 运动功能说明

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

2. 结构说明

      该样机主要由两个 直流驱动轮模组 构成

概述

3. 电子硬件

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

主控板

Basra主控板(兼容Arduino Uno)

扩展板

Bigfish2.1扩展板

传感器加速度传感器
电池7.4V锂电池


/*------------------------------------------------------------------------------------

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

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

           https://opensource.org/licenses/MIT

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

  ------------------------------*/

#include <SignalFilter.h>

SignalFilter Filter;

char filtered;

int a,b,c,d;

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

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

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

void setup() {

  // initialize serial communications at 9600 bps:

  Serial.begin(9600);

  Filter.begin();

  Filter.setFilter('m');

  Filter.setOrder(2);

}

void loop() {

  // read the analog in value:

  sensorValue = analogRead(analogInPin);           

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

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

  filtered= Filter.run(outputValue);

  int a=abs(filtered);

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

    {   

     analogWrite(9,0);

     analogWrite(10,0);

     analogWrite(5,0);

     analogWrite(6,0);

     delay(5);}

     

   if(a>=101)

    {

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

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

     analogWrite(9,0);

     analogWrite(10,c);

     analogWrite(5,0);

     analogWrite(6,d);

     delay(5);   

     analogWrite(9,30);

     analogWrite(10,0);

     analogWrite(5,40);

     analogWrite(6,0);}  

   

   if(a<=90)

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

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

     analogWrite(9,c);

     analogWrite(10,0);

     analogWrite(5,d);

     analogWrite(6,0);

     delay(5);

     analogWrite(9,0);

     analogWrite(10,30);

     analogWrite(5,0);

     analogWrite(6,40);

   }   

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

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

  Serial.println(d);

}

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

4. 运动功能实现

   编程环境:Arduino 1.8.19

将参考例程(self_balance_car.ino)下载到主控板,小车将实现双轮自平衡功能,运动效果可参考实验演示视频。

5. 资料清单

序号

内容
1

R209-例程源代码

2R209-样机3D文件


文件下载
【整体打包】-【R209】双轮平衡车-概述-资料附件.zip
583.76KB下载3次下载
上一页 1 下一页

自平衡

1. 功能说明

      在双轮小车上安装一个 六轴陀螺仪传感器 本文示例将实现双轮小车自主平衡功能

2. 电子硬件

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

主控板

Basra主控板(兼容Arduino Uno)

扩展板

Bigfish2.1扩展板

传感器六轴陀螺仪
电池7.4V锂电池


物料

测试数据‍‍

陀螺仪的加速度包Y轴数据

沿Y轴俯仰陀螺仪时,数据从(-0.05,1.13)逐渐增大;

陀螺仪平放时的值为0.74;


/*------------------------------------------------------------------------------------

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

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

 https://opensource.org/licenses/MIT

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

 ------------------------------*/

void Get_gyroscope_And_Control()//读取陀螺仪参数,并判断平衡小车前倾、后仰或者是平放状态

{

 int gyroscope_acc_data= 0;

 int map_data= 0;

 if(sign)

 {  

 sign=0;

 if(Re_buf[0]==0x55)  //检查帧头

 {  

switch(Re_buf [1])

{

case 0x51:

 {

a[0] = (short(Re_buf [3]<<8| Re_buf [2]))/32768.0*16;

a[1] = (short(Re_buf [5]<<8| Re_buf [4]))/32768.0*16;

a[2] = (short(Re_buf [7]<<8| Re_buf [6]))/32768.0*16;

 //把陀螺仪的加速度包的Y轴数据转换为直流电机的速度

 map_data = fabs(a[1]) * 100;  //fabs()取浮点数的绝对值

 if( (map_data >= (map_to_int[2]-10)) && (map_data <= (map_to_int[2]+10)) )

 {

 //小车处于平衡态

 Motor_State(Stop,0,0);  

 }

 else if( (map_data < (map_to_int[2]-10)) && (map_data >= map_to_int[0]) )

 {

 /*当小车前倾,自行调整至平衡

 假如现在获取到陀螺仪数据0.50,转换为50并进行映射为直流的pwm值过程

 Motor_State(Forward,60+map(50,64,5,0,20), 70+map(50,64,5,0,20))

 */

 Motor_State(Forward,left_Motor_Speed_Init+map(map_data,(map_to_int[2]-10),map_to_int[0],Motor_Speed_Mix,Motor_Speed_Max),right_Motor_Speed_Init+map(map_data,(map_to_int[2]-10),map_to_int[0],Motor_Speed_Mix,Motor_Speed_Max) );

 }

 else if( (map_data <= map_to_int[1]) && (map_data > (map_to_int[2]+10)) )

 {

 //当小车后仰,自行调整至平衡

 Motor_State(Back,left_Motor_Speed_Init+map(map_data,(map_to_int[2]+10),map_to_int[1],Motor_Speed_Mix,Motor_Speed_Max),right_Motor_Speed_Init+map(map_data,(map_to_int[2]+10),map_to_int[1],Motor_Speed_Mix,Motor_Speed_Max) );

 }  

 }break;

}

 }

 }

}


void Motor_State(int _mode, int _left, int _right)//电机状态函数

{

 switch(_mode)

 {

 case Forward: {analogWrite(motor_pin[0],_right);analogWrite(motor_pin[1],0);analogWrite(motor_pin[2],_left);analogWrite(motor_pin[3],0);}break;

 case Back:  {analogWrite(motor_pin[1],_right);analogWrite(motor_pin[0],0);analogWrite(motor_pin[3],_left);analogWrite(motor_pin[2],0);}break;

 case Stop:  {analogWrite(motor_pin[0],0);analogWrite(motor_pin[1],0);analogWrite(motor_pin[2],0);analogWrite(motor_pin[3],0);}break;

 }

}

/*------------------------------------------------------------------------------------

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

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

 https://opensource.org/licenses/MIT

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

 ------------------------------*/

/*  

 功能:自平衡小车

 接线:陀螺仪传感器(GND、VCC、RX、TX)接在扩展板的( Gnd、Vcc(3.3v)、RX、TX);

 直流电机分别接在(5,6),(9,10)

*/

#include<Math.h>

#define Gyroscope_left_LimitAngle_Y  -0.05  //读取到陀螺仪 Y 轴向前的极限数值

#define Gyroscope_Right_LimitAngle_Y  1.13  //读取到陀螺仪 Y 轴向后的极限数值

#define Gyroscope_Middle_LimitAngle_Y  0.74  //读取到陀螺仪 Y 轴平放时的数值

#define Motor_Pin_Count 4


/*

 由于直流电机的物理差异,左右电机的速度值不同;

 需要先微调两电机的pwm值,保证小车能走直线(否则会出现小车原地打转)

*/

#define left_Motor_Speed_Init 60  //左侧电机的初始速度(0-255)

#define right_Motor_Speed_Init 70  //右侧电机的初始速度(0-255)

#define Motor_Speed_Mix 0  //电机速度增量最小值

#define Motor_Speed_Max 20  //电机速度增量最大值


unsigned char Re_buf[11],counter=0; //存储陀螺仪数据的变量

unsigned char sign=0;  //定义是否接收到陀螺仪数据的标志位

float a[3];  //用于存储x、y、z三轴的角速度包数值

int motor_pin[4] = {5,6,9,10};//定义电机引脚

int map_to_int[3] = {0,0,0};  //用于存储Y轴向左偏、向右偏、平放的数值

enum{Forward = 1,Back,Stop};  //定义电机前进、后退、停止三种状态

void setup()

{

 delay(1000);Serial.begin(115200);//打开串口,并设置波特率为115200

 for(int i=0;i<Motor_Pin_Count;i++){

 pinMode(motor_pin[i],INPUT);  //将电机的四个引脚设置为输出模式

 }

 map_to_int[0] = Gyroscope_left_LimitAngle_Y*100;  //重新赋予陀螺仪 Y 轴向前的极限数值

 map_to_int[1] = Gyroscope_Right_LimitAngle_Y*100;  //重新赋予陀螺仪 Y 轴向后的极限数值

 map_to_int[2] = Gyroscope_Middle_LimitAngle_Y*100; //重新赋予陀螺仪 Y 轴平放时的数值

}


void loop()

{

 Get_gyroscope_And_Control();//读取陀螺仪参数,并判断平衡小车前倾、后仰或者是平放状态

}


//实时读取陀螺仪发送的数据(serialEvent()函数会自动运行)

void serialEvent() {  

 while (Serial.available()) {

 Re_buf[counter]=(unsigned char)Serial.read();

 if(counter==0&&Re_buf[0]!=0x55) return;  //第0号数据不是帧头  

 counter++;  

 if(counter==11)  //接收到11个数据

 {  

 counter=0;  //重新赋值,准备下一帧数据的接收

 sign=1;

 }  

 }

}

电路连接:

      ① 六轴陀螺仪传感器(GND、VCC、RX、TX)连接在Bigfish扩展板的Gnd、Vcc(3.3v)、RX、TX;

      ② 2个直流电机分别连在Bigfish扩展板的(5,6)、(9,10)。

3. 功能实现

      编程环境:Arduino 1.8.19

      实现思路:实现小车的自平衡。当人为去打破小车的平衡,小车能够自行恢复到平衡状态。这里当俯视小车时,小车处于水平位置代表平衡态;前倾或后仰等均为非平衡状态

3.1 测试数据

     ① 需要先测出陀螺仪沿Y轴的状态数据本实验中测试出的实验数据大家可参考格式,测试出自己实验数据。

     ② 找准双轮小车的平衡状态(大家可尝试把锂电池安装在车底部,让小车默认为平衡态)

3.2 示例程序

     将参考例程(Gyroscope_Control_Car.ino)下载到主控板:

判断双轮小车前倾、后仰或者是平放状态的参考程序(Gyroscope_Device.ino)如下:

4. 资料清单

序号

内容
1

【R209】-自平衡-程序源代码


文件下载
【整体打包】-【R209】双轮平衡车-自平衡-资料附件.zip
5.18KB下载0次下载
上一页 1 下一页
© 2022 机器时代(北京)科技有限公司  版权所有
机器谱——机器人共享方案网站
学习  开发  设计  应用