极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12022|回复: 3

請問如何將MPU-6050(陀螺儀)控制28BYJ_48(步進馬達)?

[复制链接]
发表于 2014-1-3 16:19:25 | 显示全部楼层 |阅读模式
各位先進大家好!請問如何用ARDUINO的程式,用MPU-6050(陀螺儀)控制28BYJ_48(步進馬達)?
回复

使用道具 举报

发表于 2014-1-3 16:48:22 | 显示全部楼层
这个问题不好回答。。。。。。。没有明确的目标
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-3 17:32:10 | 显示全部楼层
本帖最后由 BOWIE 于 2014-1-22 01:59 编辑

謝謝您的答覆。我先試了一顆終於運轉了,但是我希望它轉動後會在我陀螺儀的角度固定,而不是一直轉!
以下是我的程序:

#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"

int motorPin1 = 8;         int motorPin2 = 9;   
int motorPin3 = 10;        int motorPin4 = 11;  

int motorSpeed = 1200;  //variable to set stepper speed
int count = 0;          // count of steps made
int countsperrev = 512; // number of steps per full revolution

int lookup[8]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09};//逆时针
int lookup1[8]={0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08};//顺时针

void anticlockwise()
{  for(int i = 0; i < 8; i++)
  { setOutput(i);    delayMicroseconds(motorSpeed);  }   }

void clockwise()
{  for(int i = 7; i >= 0; i--)
  { setOutput(i);    delayMicroseconds(motorSpeed);  }   }

MPU6050 mpu;

int16_t ax, ay, az;
int16_t gx, gy, gz;

int valy;
int valx;
int prevValy;
int prevValx;

void setOutput(int out)//set pins to ULN2003 high in sequence from 1 to 4
{ digitalWrite(motorPin1, bitRead(lookup[out], 0));
  digitalWrite(motorPin2, bitRead(lookup[out], 1));
  digitalWrite(motorPin3, bitRead(lookup[out], 2));
  digitalWrite(motorPin4, bitRead(lookup[out], 3));    }

void setup()
{
  pinMode(motorPin1, OUTPUT);    pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);    pinMode(motorPin4, OUTPUT);
Wire.begin();
Serial.begin(9600);

mpu.initialize();
}

void loop()
{
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
valy = map(ay, -34000, 34000, 0, 179);

  if(valy < 90 )
     {anticlockwise();}

  else if(valy > 90)
  {clockwise();}
  
  else
  { delayMicroseconds(motorSpeed*5);}

Serial.print(valy);
prevValy = valy;
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-22 01:57:03 | 显示全部楼层
我希望將陀螺儀的數據給步進馬達,並調到跟陀螺儀一樣的角度,請各位高手解答一下,謝謝!
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-9 16:54 , Processed in 0.035839 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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