极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10317|回复: 4

关于步进电机加减速的考虑

[复制链接]
发表于 2016-7-25 19:44:42 | 显示全部楼层 |阅读模式
本帖最后由 wolfcolorful 于 2016-7-27 15:57 编辑
  1. /*
  2. *       Filename:  my_stepper.cpp
  3. *
  4. *    Description:  
  5. *
  6. *        Version:  1.0
  7. *        Created:  2014-1-13 11:40:42
  8. *       Revision:  none
  9. *       Compiler:  gcc
  10. *
  11. *         Author:  YOUR NAME (V__KING__),
  12. *        Company:         sogworks
  13. *
  14. * =====================================================================================
  15. */

  16. #include"my_stepper.h"
  17. #include"Arduino.h"
  18. My_stepper::My_stepper(int p1,int p2,int p3,int p4)
  19. {
  20.         this->Pin0=p1;       
  21.         this->Pin1=p2;
  22.         this->Pin2=p3;
  23.         this->Pin3=p4;
  24.         pinMode(this->Pin0,OUTPUT);
  25.         pinMode(this->Pin1,OUTPUT);
  26.         pinMode(this->Pin2,OUTPUT);
  27.         pinMode(this->Pin3,OUTPUT);
  28.        
  29. }
  30. void My_stepper::my_stepMotor(int thisStep)
  31. {
  32.         switch (thisStep) {
  33.                 case 0:
  34.                         digitalWrite(this->Pin0, LOW);
  35.                         digitalWrite(this->Pin1, LOW);
  36.                         digitalWrite(this->Pin2, LOW);
  37.                         digitalWrite(this->Pin3, HIGH);
  38.                         break;
  39.                 case 1:
  40.                         digitalWrite(this->Pin0, LOW);
  41.                         digitalWrite(this->Pin1, LOW);
  42.                         digitalWrite(this->Pin2, HIGH);
  43.                         digitalWrite(this->Pin3, HIGH);
  44.                         break;
  45.                 case 2:
  46.                         digitalWrite(this->Pin0, LOW);
  47.                         digitalWrite(this->Pin1, LOW);
  48.                         digitalWrite(this->Pin2, HIGH);
  49.                         digitalWrite(this->Pin3, LOW);
  50.                         break;
  51.                 case 3:
  52.                         digitalWrite(this->Pin0, LOW);
  53.                         digitalWrite(this->Pin1, HIGH);
  54.                         digitalWrite(this->Pin2, HIGH);
  55.                         digitalWrite(this->Pin3, LOW);
  56.                         break;
  57.                 case 4:
  58.                         digitalWrite(this->Pin0, LOW);
  59.                         digitalWrite(this->Pin1, HIGH);
  60.                         digitalWrite(this->Pin2, LOW);
  61.                         digitalWrite(this->Pin3, LOW);
  62.                         break;
  63.                 case 5:
  64.                         digitalWrite(this->Pin0, HIGH);
  65.                         digitalWrite(this->Pin1, HIGH);
  66.                         digitalWrite(this->Pin2, LOW);
  67.                         digitalWrite(this->Pin3, LOW);
  68.                         break;
  69.                 case 6:
  70.                         digitalWrite(this->Pin0, HIGH);
  71.                         digitalWrite(this->Pin1, LOW);
  72.                         digitalWrite(this->Pin2, LOW);
  73.                         digitalWrite(this->Pin3, LOW);
  74.                         break;
  75.                 case 7:
  76.                         digitalWrite(this->Pin0, HIGH);
  77.                         digitalWrite(this->Pin1, LOW);
  78.                         digitalWrite(this->Pin2, LOW);
  79.                         digitalWrite(this->Pin3, HIGH);
  80.                         break;
  81.                 default:
  82.                         digitalWrite(this->Pin0, LOW);
  83.                         digitalWrite(this->Pin1, LOW);
  84.                         digitalWrite(this->Pin2, LOW);
  85.                         digitalWrite(this->Pin3, LOW);
  86.                         break;
  87.         }
  88. }

  89. void My_stepper::my_setSpeed(long whatSpeed)
  90. {
  91.         this->number_of_steps=64;
  92.   this->step_delay = 60L * 1000L / this->number_of_steps / whatSpeed;
  93. }


  94. void My_stepper::my_step(int steps_to_move,int direction)
  95. {  
  96.         int steps_left = abs(steps_to_move);  // how many steps to take

  97.         // decrement the number of steps, moving one step each time:
  98.         while(steps_left > 0) {
  99.                 // move only if the appropriate delay has passed:examples
  100.                 if (millis() - this->last_step_time >= this->step_delay) {
  101.                         // get the timeStamp of when you stepped:
  102.                         this->last_step_time = millis();
  103.                         // increment or decrement the step number,
  104.                         // depending on direction:
  105.                         if (direction == 1) {
  106.                                 this->step_number++;
  107.                                 if (this->step_number == this->number_of_steps) {
  108.                                         this->step_number = 0;
  109.                                 }
  110.                         }
  111.                         else if(direction==-1){
  112.                                 if (this->step_number == 0) {
  113.                                         this->step_number = this->number_of_steps;
  114.                                 }
  115.                                 this->step_number--;
  116.                                 //Serial.println(this->step_number);
  117.                         }
  118.                         // decrement the steps left:
  119.                         steps_left--;
  120.                         // step the motor to step number 0, 1, 2, or 3:
  121.                         my_stepMotor(this->step_number % 8);
  122.                 }
  123.         }
  124. }
复制代码

以上为网上分享的资料,地址http://blog.csdn.net/v__king__/article/details/18222323。亲测可用!
按照个人的理解,规律性的改变电平(此程序为四相八拍)即可完成步进电机的匀速运转。如果需要加速或者减速,那是否可以直接在改变电平的间隙加上所需的等待时间(不用delay,见笔者的前一个求助帖http://www.geek-workshop.com/thread-27206-1-1.html)即可达到目的!
另:关于舵机的加减速,在下使用中这种方式曾经做过简单的试验。当然这种试验很不成熟,那时尚未试图去了解delay和millis之间的区别。不过也的确可以明显的感受到加减速的存在。
  1. void servo1_run(void)
  2. {
  3.   for(i=0;i<=180;i++)
  4.   {
  5.     servo1.write(i);
  6.     delay(180-i);
  7.   }
复制代码
回复

使用道具 举报

发表于 2016-7-26 10:17:29 | 显示全部楼层
百度 “S曲线 加减速”
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-7-26 10:46:09 | 显示全部楼层
328522073 发表于 2016-7-26 10:17
百度 “S曲线 加减速”

没太明白你的意思,能说详细点吗?
回复 支持 反对

使用道具 举报

发表于 2016-7-26 16:49:27 | 显示全部楼层
AccelStepper 这个库支持加速度   http://www.airspayce.com/mikem/arduino/AccelStepper/
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-7-27 10:05:29 | 显示全部楼层
nick_zm 发表于 2016-7-26 16:49
AccelStepper 这个库支持加速度   http://www.airspayce.com/mikem/arduino/AccelStepper/

多谢指点,雪中送炭!
回复 支持 反对

使用道具 举报

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

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

Archiver|联系我们|极客工坊

GMT+8, 2024-4-20 06:54 , Processed in 0.038550 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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