极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 15751|回复: 1

在AccelStepper库中如何实现两个步进电机同步工作

[复制链接]
发表于 2015-8-4 12:52:31 | 显示全部楼层 |阅读模式
本帖最后由 xboycy 于 2015-8-4 12:54 编辑

[pre lang="arduino" line="1" file="Accel_MultiStepper"]// Shows how to run three Steppers at once with varying speeds
//
// Requires the Adafruit_Motorshield v2 library
//   https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library
// And AccelStepper with AFMotor support
//   https://github.com/adafruit/AccelStepper

// This tutorial is for Adafruit Motorshield v2 only!
// Will not work with v1 shields

#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMSbot(0x61); // Rightmost jumper closed
Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers

// Connect two steppers with 200 steps per revolution (1.8 degree)
// to the top shield
Adafruit_StepperMotor *myStepper1 = AFMStop.getStepper(400, 1);
Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(400, 2);

// Connect one stepper with 200 steps per revolution (1.8 degree)
// to the bottom shield
Adafruit_StepperMotor *myStepper3 = AFMSbot.getStepper(400, 2);

// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
// wrappers for the first motor!
void forwardstep1() {  
  myStepper1->onestep(FORWARD, DOUBLE);
}
void backwardstep1() {  
  myStepper1->onestep(BACKWARD, DOUBLE);
}
// wrappers for the second motor!
void forwardstep2() {  
  myStepper2->onestep(FORWARD, DOUBLE);
}
void backwardstep2() {  
  myStepper2->onestep(BACKWARD, DOUBLE);
}
// wrappers for the third motor!
void forwardstep3() {  
  myStepper3->onestep(FORWARD, INTERLEAVE);
}
void backwardstep3() {  
  myStepper3->onestep(BACKWARD, INTERLEAVE);
}

// Now we'll wrap the 3 steppers in an AccelStepper object
AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);
AccelStepper stepper3(forwardstep3, backwardstep3);

void setup()
{  
  AFMSbot.begin(); // Start the bottom shield
  AFMStop.begin(); // Start the top shield
   
  stepper1.setMaxSpeed(200.0);
  stepper1.setAcceleration(100.0);
  stepper1.moveTo(600);
  delay(1000);
  
  stepper2.setMaxSpeed(200.0);
  stepper2.setAcceleration(100.0);
  stepper2.moveTo(600);

  stepper3.setMaxSpeed(300.0);
  stepper3.setAcceleration(100.0);
  stepper3.moveTo(100);
}

void loop()
{
    // Change direction at the limits
   if (stepper1.distanceToGo() == 0)
   {
    stepper1.moveTo(400);
    if (stepper1.currentPosition() == 400)
    {
      stepper1.moveTo(200);
    }
   }

   if (stepper2.distanceToGo() == 0)
   {
    stepper2.moveTo(400);
    if (stepper2.currentPosition() == 400)
    {
      stepper2.moveTo(200);
    }
   }
  
    stepper1.run();
    stepper2.run();
//    stepper3.run();
}
[/code]

请问,在这个程序中,如何来实现stepper2电机比stepper1电机延迟启动5秒钟啊。我试过加入delay()命令在其中多个地方,都达不到效果。(意思就是当stepper1号电机启动5秒钟之后,stepper2号电机开始启动,然后根据我的设定进入无限循环模式)。
回复

使用道具 举报

 楼主| 发表于 2015-8-6 16:56:18 | 显示全部楼层
自己顶一下,求助大神们,或者有没有其它更好的解决方案?
谢谢了……
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-7 19:29 , Processed in 0.034727 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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