查看: 19564|回复: 9

[教程] 【分享】通过Raspberry Pi(树莓派)的GPIO接口控制步进电机

  [复制链接]
  • TA的每日心情
    开心
    2024-3-15 14:34
  • 签到天数: 651 天

    连续签到: 1 天

    [LV.9]以坛为家II

    发表于 2012-12-31 10:42:55 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 xinxincaijq 于 2012-12-31 10:54 编辑

    Raspberry Pi是什么?
    引用维基百科的一句话:

    The Raspberry Pi is a credit card sized single-board computer developed in the UK by the Raspberry Pi Foundation with the intention of stimulating the teaching of basic computer science in schools.
    简单地说,它就是一个基于ARM CPU的、信用卡那么大的迷你计算机。
    In short, Pi is an ARM-based mini computer which has a credit card size.

    在阅读本文之前,请确保你已经阅读过我的另外几篇关于Pi的文章,因为本文与它们有或多或少的关系。
    Before reading this article, please make sure that you've read some other articles of mine on Pi, because this article is more or less based on them.

    【1】实验目的 / Experiment purpose
    在树莓派上编写一个C程序,通过其GPIO口控制步进电机的转动方向以及速度。
    Write a C program to control the direction & speed of rotation of a stepper motor through the GPIO on Raspberry Pi.

    【2】为什么要用树莓派来控制步进电机 / Why doing this
    在我的概念中,无论是控制接在树莓派上的摄像头来拍照,还是通过树莓派控制LED发光,我都还是在“虚拟世界”中折腾树莓派,因为它没有向外界输出任何动作。而在构建一个稍微复杂的系统时,这种能力可能是很重要的,于是,用树莓派来控制步进电机是我必须要做的事情。
    In my view, no matter using Pi to control webcam, or using Pi to light LED, the Raspberry Pi was in a "virtual" world all the time because it didn't output any movement to the real world. Having this ability may be very important to build a slightly more complex system, so I decided to use Pi to control a stepper motor.

    【3】实现方案 / The implementation
    怎样通过Raspberry Pi的GPIO口来控制步进电机?已经有很多人这样做了,例如Youtube上的这个例子。
    How to control a stepper motor through the GPIO on Pi? Lots of people have tried that before, such as this example on Youtube.
    由于步进电机通常需要一块驱动板,而这样的产品已经很容易买到并且极其廉价(肯定比你自己做要便宜),因此,我们没有必要自己去做。我在淘宝上不断地查啊查,终于找到了白菜价的步进电机和配套驱动板,加起来才一共8块多钱。
    Because we need a driver circuit board to drive the stepper motor, and it's really easy to buy this kind of product with super low price nowadays, so it's no need to make it ourselves. I searched taobao.com(the largest e-commerce website in China) and found a super cheap stepper motor & it's matched driver board, they cost me only about 8 RMB, that is, less than $1.5!

    下面来看看我买的步进电机和驱动板的样子:
    The stepper motor & it's driver board:


    步进电机和驱动板的接线:
    Connect the stepper motor with it's driver board:


    该驱动板配有一个原理图,根据它我们可以知道如何把整个系统连接起来:
    The driver board has a schematic diagram and we can learn from it how to connect the whole system together:


    可见,驱动板上有4个输入口:IN1~IN4,这4个口用来接树莓派的4个GPIO口。同时,我们需要为驱动板提供5V的供电,这从哪里来?当然是从树莓派引出来。从下面的GPIO口分布图可以得知,“2”口就是+5V,正好就利用它。
    We can see that there are total 4 input pins: IN1~IN4, these 4 pins will be connected to 4 GPIO pins on Raspberry Pi. And we also need to provide a 5V power supply to the board, so where can we get this? Use the pin "2"(5V) next to pin "1"(3.3V) on Pi is the most convenient way. Refer to following GPIO pinouts for details:


    我将树莓派上的GPIO 17、18、21、22口(用树莓派的命名方式就是0、1、2、3口)分别接到步进电机驱动板上的IN1、IN2、IN3、IN4口,最后接好线的效果如下:
    I connected GPIO 17, 18, 21, 22(namely GPIO 0, 1, 2, 3 in Raspberry Pi name) on Pi to IN1, IN2, IN3, IN4 on the driver board separately, after it's done, the whole system looks like this:


    【4】编写程序 / Write the program
    在参考了这款步进电机配套的51单片机的示例代码后,我知道了依次把驱动板的IN1~IN4置为高电平,就可以驱动步进电机,也就是说,要把树莓派的4个GPIO输出口依次置为高电平。例如,假设用0代表低电平,1代表高电平的话,GPIO 17、18、21、22口的电平第一次被置为1、0、0、0,第二次被置为0、1、0、0,第三次被置为0、0、1、0,第四次被置为0、0、0、1。
    After checking the code(for MCS51 singlechip) provided by the product dealer I knew that it's possible to drive the motor by set IN1~IN4 high level in turn, namely set the 4 GPIO pins high level in turn. For example, if we use 0 to represent low level and 1 to represent high level, then GPIO 17, 18, 21, 22 on Raspberry Pi will be set to 1,0,0,0 in the 1st round, 0,1,0,0 in the 2nd round, 0,0,1,0 in the 3rd round and 0,0,0,1 in the 4th round.

    于是我写出了下面的代码(仍然使用WiringPi这个库来操作GPIO):
    So I wrote the follow code(still use WiringPi to manipulate the GPIO):
    1. /* moto.c
    2. * A program to control a stepper motor through the GPIO on Raspberry Pi.
    3. *
    4. * Author: Darran Zhang
    5. */

    6. #include <wiringPi.h>
    7. #include <stdio.h>
    8. #include <unistd.h>
    9. #include <stdlib.h>

    10. #define CLOCKWISE 1
    11. #define COUNTER_CLOCKWISE 2

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

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

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

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

    25.   if (-1 == wiringPiSetup()) {
    26.     printf("Setup wiringPi failed!");
    27.     return 1;
    28.   }

    29.   /* set mode to output */
    30.   pinMode(pinA, OUTPUT);
    31.   pinMode(pinB, OUTPUT);
    32.   pinMode(pinC, OUTPUT);
    33.   pinMode(pinD, OUTPUT);

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

    38.   return 0;
    39. }

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

    46. /* Rotate the motor.
    47. *  @param pins     A pointer which points to the pins number array.
    48. *  @param direction  CLOCKWISE for clockwise rotation, COUNTER_CLOCKWISE for counter clockwise rotation.
    49. */
    50. void rotate(int* pins, int direction) {
    51.   for (int i = 0; i < 4; i++) {
    52.     if (CLOCKWISE == direction) {
    53.       for (int j = 0; j < 4; j++) {
    54.         if (j == i) {
    55.           digitalWrite(pins[3 - j], 1); // output a high level
    56.         } else {
    57.           digitalWrite(pins[3 - j], 0); // output a low level
    58.         }
    59.       }
    60.     } else if (COUNTER_CLOCKWISE == direction) {
    61.       for (int j = 0; j < 4; j++) {
    62.         if (j == i) {
    63.           digitalWrite(pins[j], 1); // output a high level
    64.         } else {
    65.           digitalWrite(pins[j], 0); // output a low level
    66.         }
    67.       }
    68.     }
    69.     delayMS(4);
    70.   }
    71. }
    复制代码
    编译程序:
    Compile the code:

    1
    g++ motor.c -o motor -lwiringPi
    运行程序:
    Run the program:

    1
    ./motor 0 1 2 3
    这里向程序传入了4个参数,它们分别代表要控制的树莓派的GPIO口。切记,由于使用了WiringPi库,所以要参考上面的GPIO分布图的左边那部分来确定这些数字。
    The four arguments passed to the program are the GPIO pin numbers on Pi. Be sure to keep in mind that because of using WiringPi, we need to refer to the left part of the GPIO pinouts image describe above to identify the numbers.

    可以看到步进电机已经转动了(如下面的Youku视频所示)。如果你发现转动方向(顺/逆时针)反了,你可以把传入的参数顺序调整一下,就可以让它转对方向。
    Then we'll see the stepper motor start to rotate shown as this Youtube video. If you find that the direction of rotation is wrong, just adjust the order of the arguments passed to the program and it will be able to make the direction right.

    从前面的代码可见,如果要改变步进电机的转速,只需要改变rotate()函数中每次delay的时间即可。因此,如果我们把delay的时间逐渐由大变小,就会导致步进电机呈加速状态。让步进电机周期性加速的完整代码如下:
    From the code above we know that we can change the rotation speed of the motor by changing the delay time in function rotate(). So if we reduce the delay time gradually, the motor will be in an accelerating state. The full source code to make the motor speeding up periodically is shown below:
    1. /* motor_speed_up.c
    2. * A program to control a stepper motor(speed up) through the GPIO on Raspberry Pi.
    3. *
    4. * Author: Darran Zhang (http://www.codelast.com)
    5. */

    6. #include <wiringPi.h>
    7. #include <stdio.h>
    8. #include <unistd.h>
    9. #include <stdlib.h>

    10. #define CLOCKWISE 1
    11. #define COUNTER_CLOCKWISE 2

    12. void delayMS(int x);
    13. void rotate(int* pins, int direction, int delay);
    14. void stop(int* pins);

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

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

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

    26.   if (-1 == wiringPiSetup()) {
    27.     printf("Setup wiringPi failed!");
    28.     return 1;
    29.   }

    30.   /* set mode to output */
    31.   pinMode(pinA, OUTPUT);
    32.   pinMode(pinB, OUTPUT);
    33.   pinMode(pinC, OUTPUT);
    34.   pinMode(pinD, OUTPUT);

    35.   delayMS(50);    // wait for a stable status
    36.   int delay = 25;
    37.   while (true) {
    38.     for (int i = 0; i < 10; i++) {
    39.       rotate(pins, CLOCKWISE, delay);
    40.     }
    41.     delay--;
    42.     if (delay < 4) {
    43.       delay = 25;
    44.       stop(pins);
    45.       delayMS(500);
    46.     }
    47.   }

    48.   return 0;
    49. }

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

    56. /* Rotate the motor.
    57. *  @param pins     A pointer which points to the pins number array.
    58. *  @param direction  CLOCKWISE for clockwise rotation, COUNTER_CLOCKWISE for counter clockwise rotation.
    59. *  @param delay    The time intervals(in ms) to delay, and if the value is smaller, the motor rotates faster.
    60. */
    61. void rotate(int* pins, int direction, int delay) {
    62.   for (int i = 0; i < 4; i++) {
    63.     if (CLOCKWISE == direction) {
    64.       for (int j = 0; j < 4; j++) {
    65.         if (j == i) {
    66.           digitalWrite(pins[3 - j], 1); // output a high level
    67.         } else {
    68.           digitalWrite(pins[3 - j], 0); // output a low level
    69.         }
    70.       }
    71.     } else if (COUNTER_CLOCKWISE == direction) {
    72.       for (int j = 0; j < 4; j++) {
    73.         if (j == i) {
    74.           digitalWrite(pins[j], 1); // output a high level
    75.         } else {
    76.           digitalWrite(pins[j], 0); // output a low level
    77.         }
    78.       }
    79.     }
    80.     delayMS(delay);
    81.   }
    82. }

    83. /* Stop the motor.
    84. *  @param pins     A pointer which points to the pins number array.
    85. */
    86. void stop(int* pins) {
    87.   for (int i = 0; i < 4; i++) {
    88.     digitalWrite(pins, 0); // output a low level
    89.   }
    90. }
    复制代码

    程序运行效果如下面的视频(Youku,Youtube)所示:
    Run the program and then you'll see the speeding up effect, as the video(Youku, Youtube) shown below:


    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    2020-9-28 10:10
  • 签到天数: 1018 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2012-12-31 10:48:43 | 显示全部楼层
    楼主,你这是直接复制粘贴过来的吧?!这样的帖子没什么质量!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2024-3-15 14:34
  • 签到天数: 651 天

    连续签到: 1 天

    [LV.9]以坛为家II

    发表于 2012-12-31 10:59:50 | 显示全部楼层
    是转的, 如果不合格就算助助人气吧
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2015-8-7 21:35
  • 签到天数: 340 天

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2012-12-31 11:06:39 | 显示全部楼层
    唉……我还以为是自己的呢……
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2024-3-15 14:34
  • 签到天数: 651 天

    连续签到: 1 天

    [LV.9]以坛为家II

    发表于 2012-12-31 12:10:09 | 显示全部楼层
    学会了就是自己的,努力啊
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2013-6-21 12:27
  • 签到天数: 26 天

    连续签到: 1 天

    [LV.4]偶尔看看III

    发表于 2012-12-31 13:48:14 | 显示全部楼层
    我来看看。
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2012-12-26 19:31
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    发表于 2012-12-31 14:37:03 | 显示全部楼层
    不错啊,怎么写起来和Arduino一样,表示看到pinMode很亲切。。。
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2012-12-30 19:57
  • 签到天数: 3 天

    连续签到: 1 天

    [LV.2]偶尔看看I

    发表于 2013-2-13 02:36:00 | 显示全部楼层
    http://www.codelast.com/?p=5232
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    发表于 2013-3-4 17:18:58 | 显示全部楼层
    学习一下,但是没有图啊
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    慵懒
    2013-9-4 11:33
  • 签到天数: 13 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    发表于 2013-3-5 22:28:25 | 显示全部楼层
    楼主我只想问下这个你试了么,到底能不能用~~
    回复 支持 反对

    使用道具 举报

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

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-11-19 06:20 , Processed in 0.196891 second(s), 34 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.