查看: 5660|回复: 11
收起左侧

步进电机,走起!

2013-8-19 20:52:27 | 显示全部楼层 |阅读模式
本帖最后由 wjb711 于 2013-8-19 20:59 编辑

参考文档http://www.codelast.com/?p=5232
所需工具 步进电机一个, 步进驱动板一个, 如图(树老大的店有售, 本人不是托, 本人用的系从树老大的店采购)
友情给个树老大的广告:步进电机购买地址
http://raspi.taobao.com/category ... cene=taobao_shop#bd

11.jpg



步进电机的接法

22.jpg

驱动板的接法如图
33.jpg

口述如下
int1->gpio0
int2->gpio1
int3->gpio2
int4->gpio3
电源要接5v和接地

程序如下(抄别人的)



/* moto.c
* A program to control a stepper motor through the GPIO on Raspberry Pi.
*
* Author: Darran Zhang
*/

#include <wiringPi.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#define CLOCKWISE 1
#define COUNTER_CLOCKWISE 2

void delayMS(int x);
void rotate(int* pins, int direction);

int main(int argc,char* argv[]) {
  if (argc < 4) {
    printf("Usage example: ./motor 0 1 2 3 \n");
    return 1;
  }

  /* number of the pins which connected to the stepper motor driver board */
  int pinA = atoi(argv[1]);
  int pinB = atoi(argv[2]);
  int pinC = atoi(argv[3]);
  int pinD = atoi(argv[4]);

  int pins[4] = {pinA, pinB, pinC, pinD};

  if (-1 == wiringPiSetup()) {
    printf("Setup wiringPi failed!");
    return 1;
  }

  /* set mode to output */
  pinMode(pinA, OUTPUT);
  pinMode(pinB, OUTPUT);
  pinMode(pinC, OUTPUT);
  pinMode(pinD, OUTPUT);

  delayMS(50);    // wait for a stable status
  for (int i = 0; i < 500; i++) {
    rotate(pins, CLOCKWISE);
  }

  return 0;
}

/* Suspend execution for x milliseconds intervals.
*  @param ms Milliseconds to sleep.
*/
void delayMS(int x) {
  usleep(x * 1000);
}

/* Rotate the motor.
*  @param pins     A pointer which points to the pins number array.
*  @param direction  CLOCKWISE for clockwise rotation, COUNTER_CLOCKWISE for counter clockwise rotation.
*/
void rotate(int* pins, int direction) {
  for (int i = 0; i < 4; i++) {
    if (CLOCKWISE == direction) {
      for (int j = 0; j < 4; j++) {
        if (j == i) {
          digitalWrite(pins[3 - j], 1); // output a high level
        } else {
          digitalWrite(pins[3 - j], 0); // output a low level
        }
      }
    } else if (COUNTER_CLOCKWISE == direction) {
      for (int j = 0; j < 4; j++) {
        if (j == i) {
          digitalWrite(pins[j], 1); // output a high level
        } else {
          digitalWrite(pins[j], 0); // output a low level
        }
      }
    }
    delayMS(4);
  }
}
然后编译
g++ motor.c -o motor -lwiringPi
最后运行
./motor 0 1 2 3
马达就转起来了

评分

参与人数 2 +1 +5 收起 理由
Tomcat猫纸 + 1 教程很全面哦!wjb711
树老大 + 5

查看全部评分

回复

使用道具 举报

2013-8-19 20:56:24 | 显示全部楼层
前来支持。最好是树莓派使用不进店的全部过程,包括驱动起来的教程哦。貌似还没有这样的文章,有的也是杂七杂八不详细的。
回复 支持 反对

使用道具 举报

2013-8-19 21:54:10 | 显示全部楼层
看楼主的图上 好像一个驱动板就只能带一个电机??好像驱动板上就一个插口
回复 支持 反对

使用道具 举报

 楼主| 2013-8-19 23:04:18 | 显示全部楼层
对,一个驱动板一个电机,或者用扩展版来实现两个电机的连接,不过我没试过
回复 支持 反对

使用道具 举报

 楼主| 2013-8-20 10:58:34 | 显示全部楼层
老外写的脚本过于复杂难懂, 我来写个简单的
原理就是分别赋予GPIO0,1,2,3 这4个脚高电平
举例
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
然后无限循环, 就可以让步进电机持续转下去
还有要注意的事项就是
设置高电平的时候至少要3毫秒以上 也就是我写的x=3
设置低电平还原的时候, 最好是1毫秒 也就是我写的y=1
最后发现其实步进电机转的没有想象的那么快, 不想马达一样快, 差不多两秒转一圈吧
/********** motor1.c ***************/
#include <wiringPi.h>
#include <stdlib.h>


int main()
{
if( wiringPiSetup() == -1 )
        exit( 1 );



    pinMode( 0, OUTPUT );
    pinMode( 1, OUTPUT );
    pinMode( 2, OUTPUT );
    pinMode( 3, OUTPUT );       
int x=3;
int y=1;
for (;;)
{
digitalWrite( 0, HIGH );
delay( x );
digitalWrite( 0, LOW );
delay( y );
digitalWrite( 1, HIGH );
delay( x );
digitalWrite( 1, LOW );
delay( y );
digitalWrite( 2, HIGH );
delay( x );
digitalWrite( 2, LOW );
delay( y );
digitalWrite( 3, HIGH );
delay( x );
digitalWrite( 3, LOW );
delay( y );


}
}
/********** motor1.c ***************/
回复 支持 反对

使用道具 举报

 楼主| 2013-8-20 12:05:38 | 显示全部楼层
控制步进电机正传和反转
通过侦测GPIO4的高低电平来实现
脚本如下
/********** motor2.c ***************/
#include <wiringPi.h>
#include <stdlib.h>


int main()
{
if( wiringPiSetup() == -1 )
        exit( 1 );



    pinMode( 0, OUTPUT );
    pinMode( 1, OUTPUT );
    pinMode( 2, OUTPUT );
    pinMode( 3, OUTPUT );     
    pinMode( 4, INPUT );
//增加GPIO4为判断引脚   
int x=3;
int y=1;
for (;;)
{
if (digitalRead(4) == 0)
//如果是低电平,默认还是原来的转法
{
digitalWrite( 0, HIGH );
delay( x );
digitalWrite( 0, LOW );
delay( y );
digitalWrite( 1, HIGH );
delay( x );
digitalWrite( 1, LOW );
delay( y );
digitalWrite( 2, HIGH );
delay( x );
digitalWrite( 2, LOW );
delay( y );
digitalWrite( 3, HIGH );
delay( x );
digitalWrite( 3, LOW );
delay( y );
}
else
//如果是高电平, 就反转, 把顺序倒转过来,3,2,1,0 分别接高电平
{
digitalWrite( 3, HIGH );
delay( x );
digitalWrite( 3, LOW );
delay( y );
digitalWrite( 2, HIGH );
delay( x );
digitalWrite( 2, LOW );
delay( y );
digitalWrite( 1, HIGH );
delay( x );
digitalWrite( 1, LOW );
delay( y );
digitalWrite( 0, HIGH );
delay( x );
digitalWrite( 0, LOW );
delay( y );
}

}
}
/********** motor2.c ***************/

实验结果是 如果GPIO4未接, 顺时针旋转
如果GPIO4接高3.3伏,逆时针旋转。
回复 支持 反对

使用道具 举报

2013-8-22 09:36:06 | 显示全部楼层
高级,转速如何,可以做小车吗
回复 支持 反对

使用道具 举报

 楼主| 2013-8-22 10:00:52 | 显示全部楼层
Chuyue 发表于 2013-8-22 09:36
高级,转速如何,可以做小车吗

转速约两秒一圈, 相当的慢, 不过扭力很大, 直接做小车的的话, 需要加齿轮放大转速
回复 支持 反对

使用道具 举报

2013-8-22 10:08:06 | 显示全部楼层
wjb711 发表于 2013-8-22 10:00
转速约两秒一圈, 相当的慢, 不过扭力很大, 直接做小车的的话, 需要加齿轮放大转速

了解!补字补字
回复 支持 反对

使用道具 举报

2013-8-22 14:08:41 | 显示全部楼层
多谢楼主分享
回复 支持 反对

使用道具 举报

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

本版积分规则

关注我们,了解更多

官方微信

服务时间:10:00-16:00

13714503811

公司地址:深圳市龙岗区南湾街道东门头路8号

Copyright © 2012-2020 Powered by 树莓派论坛 2019.4  粤ICP备15075382号-1
快速回复 返回列表 返回顶部