以下代码来自Accelstepper库的examples ,就是想弄清各个函数怎么实际应用。

实例清单如下:

1 AFMotor_ConstantSpeed

演示如何非常简单的运行AccelStepper
 固定速度模式没有加速度
 需要AFMotor库
 注意:Adafruit电机shield V2不兼容
浏览 https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library 
 来获取如何在Adafruit电机shield V2运行的实例

// AFMotor_ConstantSpeed.pde
// -*- mode: C++ -*-
//
// Shows how to run AccelStepper in the simplest,
// fixed speed mode with no accelerations
// Requires the AFMotor library
// (https://github.com/adafruit/Adafruit-Motor-Shield-library)
// Caution, does not work with Adafruit Motor Shield V2
// See https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library
// for examples that work with Adafruit Motor Shield V2.#include <AccelStepper.h>
#include <AFMotor.h>AF_Stepper motor1(200, 1);// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
void forwardstep() {  motor1.onestep(FORWARD, SINGLE);
}
void backwardstep() {  motor1.onestep(BACKWARD, SINGLE);
}AccelStepper stepper(forwardstep, backwardstep); // use functions to stepvoid setup()
{  Serial.begin(9600);           // set up Serial library at 9600 bpsSerial.println("Stepper test!");stepper.setMaxSpeed(50); stepper.setSpeed(50);
}void loop()
{  stepper.runSpeed();
}

2  AFMotor_MultiStepper

控制两个电机以不同的速度和加速度同时运动

// AFMotor_MultiStepper.pde
// -*- mode: C++ -*-
//
// Control both Stepper motors at the same time with different speeds
// and accelerations.
// Requires the AFMotor library (https://github.com/adafruit/Adafruit-Motor-Shield-library)
// Caution, does not work with Adafruit Motor Shield V2
// See https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library
// for examples that work with Adafruit Motor Shield V2.#include <AccelStepper.h>
#include <AFMotor.h>// two stepper motors one on each port
AF_Stepper motor1(200, 1);
AF_Stepper motor2(200, 2);// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
// wrappers for the first motor!
void forwardstep1() {  motor1.onestep(FORWARD, SINGLE);
}
void backwardstep1() {  motor1.onestep(BACKWARD, SINGLE);
}
// wrappers for the second motor!
void forwardstep2() {  motor2.onestep(FORWARD, SINGLE);
}
void backwardstep2() {  motor2.onestep(BACKWARD, SINGLE);
}// Motor shield has two motor ports, now we'll wrap them in an AccelStepper object
AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);void setup()
{  stepper1.setMaxSpeed(200.0);stepper1.setAcceleration(100.0);stepper1.moveTo(24);stepper2.setMaxSpeed(300.0);stepper2.setAcceleration(100.0);stepper2.moveTo(1000000);}void loop()
{// Change direction at the limitsif (stepper1.distanceToGo() == 0)stepper1.moveTo(-stepper1.currentPosition());stepper1.run();stepper2.run();
}

3 Blocking

演示如何使用阻塞调用runToNewPosition,该调用设置一个新目标位置,然后等待,直到步进电机运行到指定位置。

// Blocking.pde
// -*- mode: C++ -*-
//
// Shows how to use the blocking call runToNewPosition
// Which sets a new target position and then waits until the stepper has
// achieved it.
//
// Copyright (C) 2009 Mike McCauley
// $Id: Blocking.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $#include <AccelStepper.h>// Define a stepper and the pins it will use
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5void setup()
{  stepper.setMaxSpeed(200.0);stepper.setAcceleration(100.0);
}void loop()
{    stepper.runToNewPosition(0);stepper.runToNewPosition(500);stepper.runToNewPosition(100);stepper.runToNewPosition(120);
}

4  Bounce

从一个限位跳跃到另一个限位

// Bounce.pde
// -*- mode: C++ -*-
//
// Make a single stepper bounce from one limit to another
//
// Copyright (C) 2012 Mike McCauley
// $Id: Random.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $#include <AccelStepper.h>// Define a stepper and the pins it will use
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5void setup()
{  // Change these to suit your stepper if you wantstepper.setMaxSpeed(100);stepper.setAcceleration(20);stepper.moveTo(500);
}void loop()
{// If at the end of travel go to the other endif (stepper.distanceToGo() == 0)stepper.moveTo(-stepper.currentPosition());stepper.run();
}

5   ConstantSpeed

演示如何固定速度模式没有加速度方式非常简单的运行AccelStepper

// ConstantSpeed.pde
// -*- mode: C++ -*-
//
// Shows how to run AccelStepper in the simplest,
// fixed speed mode with no accelerations
/// \author  Mike McCauley (mikem@airspayce.com)
// Copyright (C) 2009 Mike McCauley
// $Id: ConstantSpeed.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $#include <AccelStepper.h>AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5void setup()
{  stepper.setMaxSpeed(1000);stepper.setSpeed(50);
}void loop()
{  stepper.runSpeed();
}

6   DualMotorShield

使用Itead Studio 双电机驱动板同时运行两个步进电机 实现两个电机同时来回移动

// DualMotorShield.pde
// -*- mode: C++ -*-
//
// Shows how to run 2 simultaneous steppers
// using the Itead Studio Arduino Dual Stepper Motor Driver Shield
// model IM120417015
// This shield is capable of driving 2 steppers at
// currents of up to 750mA
// and voltages up to 30V
// Runs both steppers forwards and backwards, accelerating and decelerating
// at the limits.
//
// Copyright (C) 2014 Mike McCauley
// $Id:  $#include <AccelStepper.h>// The X Stepper pins
#define STEPPER1_DIR_PIN 3
#define STEPPER1_STEP_PIN 2
// The Y stepper pins
#define STEPPER2_DIR_PIN 7
#define STEPPER2_STEP_PIN 6// Define some steppers and the pins the will use
AccelStepper stepper1(AccelStepper::DRIVER, STEPPER1_STEP_PIN, STEPPER1_DIR_PIN);
AccelStepper stepper2(AccelStepper::DRIVER, STEPPER2_STEP_PIN, STEPPER2_DIR_PIN);void setup()
{  stepper1.setMaxSpeed(200.0);stepper1.setAcceleration(200.0);stepper1.moveTo(100);stepper2.setMaxSpeed(100.0);stepper2.setAcceleration(100.0);stepper2.moveTo(100);
}void loop()
{// Change direction at the limitsif (stepper1.distanceToGo() == 0)stepper1.moveTo(-stepper1.currentPosition());if (stepper2.distanceToGo() == 0)stepper2.moveTo(-stepper2.currentPosition());stepper1.run();stepper2.run();
}

7  AFMotor_ConstantSpeed

控制三相电机,例如硬盘主轴电机

// AFMotor_ConstantSpeed.pde
// -*- mode: C++ -*-
//
// Shows how to use AccelStepper to control a 3-phase motor, such as a HDD spindle motor
// using the Adafruit Motor Shield
// http://www.ladyada.net/make/mshield/index.html.
// Create a subclass of AccelStepper which controls the motor  pins via the
// Motor Shield serial-to-parallel interface#include <AccelStepper.h>// Arduino pin names for interface to 74HCT595 latch
// on Adafruit Motor Shield
#define MOTORLATCH   12
#define MOTORCLK     4
#define MOTORENABLE  7
#define MOTORDATA    8// PWM pins, also used to enable motor outputs
#define PWM0A        5
#define PWM0B        6
#define PWM1A        9
#define PWM1B        10
#define PWM2A        11
#define PWM2B        3// The main purpose of this class is to override setOutputPins to work with Adafruit Motor Shield
class AFMotorShield : public AccelStepper
{public:AFMotorShield(uint8_t interface = AccelStepper::FULL4WIRE, uint8_t pin1 = 2, uint8_t pin2 = 3, uint8_t pin3 = 4, uint8_t pin4 = 5); virtual void   setOutputPins(uint8_t mask);
};AFMotorShield::AFMotorShield(uint8_t interface, uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4): AccelStepper(interface, pin1, pin2, pin3, pin4)
{// Enable motor control serial to parallel latchpinMode(MOTORLATCH, OUTPUT);pinMode(MOTORENABLE, OUTPUT);pinMode(MOTORDATA, OUTPUT);pinMode(MOTORCLK, OUTPUT);digitalWrite(MOTORENABLE, LOW);// enable both H bridges on motor 1pinMode(PWM2A, OUTPUT);pinMode(PWM2B, OUTPUT);pinMode(PWM0A, OUTPUT);pinMode(PWM0B, OUTPUT);digitalWrite(PWM2A, HIGH);digitalWrite(PWM2B, HIGH);digitalWrite(PWM0A, HIGH);digitalWrite(PWM0B, HIGH);setOutputPins(0); // Reset
};// Use the AF Motor Shield serial-to-parallel to set the state of the motor pins
// Caution: the mapping of AccelStepper pins to AF motor outputs is not
// obvious:
// AccelStepper     Motor Shield output
// pin1                M4A
// pin2                M1A
// pin3                M2A
// pin4                M3A
// Caution this is pretty slow and limits the max speed of the motor to about 500/3 rpm
void AFMotorShield::setOutputPins(uint8_t mask)
{uint8_t i;digitalWrite(MOTORLATCH, LOW);digitalWrite(MOTORDATA, LOW);for (i=0; i<8; i++) {digitalWrite(MOTORCLK, LOW);if (mask & _BV(7-i))digitalWrite(MOTORDATA, HIGH);elsedigitalWrite(MOTORDATA, LOW);digitalWrite(MOTORCLK, HIGH);}digitalWrite(MOTORLATCH, HIGH);
}AFMotorShield stepper(AccelStepper::HALF3WIRE, 0, 0, 0, 0); // 3 phase HDD spindle drivevoid setup()
{  stepper.setMaxSpeed(500);    // divide by 3 to get rpmstepper.setAcceleration(80);stepper.moveTo(10000000);
}void loop()
{  stepper.run();
}

8 MultiStepper

演示多电机同时控制

// MultiStepper.pde
// -*- mode: C++ -*-
//
// Shows how to multiple simultaneous steppers
// Runs one stepper forwards and backwards, accelerating and decelerating
// at the limits. Runs other steppers at the same time
//
// Copyright (C) 2009 Mike McCauley
// $Id: MultiStepper.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $#include <AccelStepper.h>// Define some steppers and the pins the will use
AccelStepper stepper1; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
AccelStepper stepper2(AccelStepper::FULL4WIRE, 6, 7, 8, 9);
AccelStepper stepper3(AccelStepper::FULL2WIRE, 10, 11);void setup()
{  stepper1.setMaxSpeed(200.0);stepper1.setAcceleration(100.0);stepper1.moveTo(24);stepper2.setMaxSpeed(300.0);stepper2.setAcceleration(100.0);stepper2.moveTo(1000000);stepper3.setMaxSpeed(300.0);stepper3.setAcceleration(100.0);stepper3.moveTo(1000000);
}void loop()
{// Change direction at the limitsif (stepper1.distanceToGo() == 0)stepper1.moveTo(-stepper1.currentPosition());stepper1.run();stepper2.run();stepper3.run();
}

9  MultiStepper

使用 MultiStepper类管理多个电机,使它们移动到相同的位置在相同的时间,达到二维或三维的运动

// MultiStepper.pde
// -*- mode: C++ -*-
// Use MultiStepper class to manage multiple steppers and make them all move to
// the same position at the same time for linear 2d (or 3d) motion.#include <AccelStepper.h>
#include <MultiStepper.h>// EG X-Y position bed driven by 2 steppers
// Alas its not possible to build an array of these with different pins for each :-(
AccelStepper stepper1(AccelStepper::FULL4WIRE, 2, 3, 4, 5);
AccelStepper stepper2(AccelStepper::FULL4WIRE, 8, 9, 10, 11);// Up to 10 steppers can be handled as a group by MultiStepper
MultiStepper steppers;void setup() {Serial.begin(9600);// Configure each stepperstepper1.setMaxSpeed(100);stepper2.setMaxSpeed(100);// Then give them to MultiStepper to managesteppers.addStepper(stepper1);steppers.addStepper(stepper2);
}void loop() {long positions[2]; // Array of desired stepper positionspositions[0] = 1000;positions[1] = 50;steppers.moveTo(positions);steppers.runSpeedToPosition(); // Blocks until all are in positiondelay(1000);// Move to a different coordinatepositions[0] = -100;positions[1] = 100;steppers.moveTo(positions);steppers.runSpeedToPosition(); // Blocks until all are in positiondelay(1000);
}

10  Overshoot

检查超调处理,它设置一个新的目标位置,然后等待,直到步进电机实现它。这用于测试超调的处理(overshoot 过冲,超调?)

// Overshoot.pde
// -*- mode: C++ -*-
//
// Check overshoot handling
// which sets a new target position and then waits until the stepper has
// achieved it. This is used for testing the handling of overshoots
//
// Copyright (C) 2009 Mike McCauley
// $Id: Overshoot.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $#include <AccelStepper.h>// Define a stepper and the pins it will use
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5void setup()
{  stepper.setMaxSpeed(150);stepper.setAcceleration(100);
}void loop()
{    stepper.moveTo(500);while (stepper.currentPosition() != 300) // Full speed up to 300stepper.run();stepper.runToNewPosition(0); // Cause an overshoot then back to 0
}

11  ProportionalControl

让一个步进电机跟随从一个旋钮中读取的模拟值,或者步进电机将根据旋钮中的值以恒定的速度移动到每个新设置的位置。

// ProportionalControl.pde
// -*- mode: C++ -*-
//
// Make a single stepper follow the analog value read from a pot or whatever
// The stepper will move at a constant speed to each newly set posiiton,
// depending on the value of the pot.
//
// Copyright (C) 2012 Mike McCauley
// $Id: ProportionalControl.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $#include <AccelStepper.h>// Define a stepper and the pins it will use
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5// This defines the analog input pin for reading the control voltage
// Tested with a 10k linear pot between 5v and GND
#define ANALOG_IN A0void setup()
{  stepper.setMaxSpeed(1000);
}void loop()
{// Read new positionint analog_in = analogRead(ANALOG_IN);stepper.moveTo(analog_in);stepper.setSpeed(100);stepper.runSpeedToPosition();
}

12 Quickstop

查看停止处理。当步进电机全速运行时调用stop(),使步进电机在当前加速度的约束下尽可能快地停止。

// Quickstop.pde
// -*- mode: C++ -*-
//
// Check stop handling.
// Calls stop() while the stepper is travelling at full speed, causing
// the stepper to stop as quickly as possible, within the constraints of the
// current acceleration.
//
// Copyright (C) 2012 Mike McCauley
// $Id:  $#include <AccelStepper.h>// Define a stepper and the pins it will use
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5void setup()
{  stepper.setMaxSpeed(150);stepper.setAcceleration(100);
}void loop()
{    stepper.moveTo(500);while (stepper.currentPosition() != 300) // Full speed up to 300stepper.run();stepper.stop(); // Stop as fast as possible: sets new targetstepper.runToPosition(); // Now stopped after quickstop// Now go backwardsstepper.moveTo(-500);while (stepper.currentPosition() != 0) // Full speed basck to 0stepper.run();stepper.stop(); // Stop as fast as possible: sets new targetstepper.runToPosition(); // Now stopped after quickstop}

13 Random

使单个步进电机执行速度、位置和加速度的随机变化

// Random.pde
// -*- mode: C++ -*-
//
// Make a single stepper perform random changes in speed, position and acceleration
//
// Copyright (C) 2009 Mike McCauley
// $Id: Random.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $#include <AccelStepper.h>// Define a stepper and the pins it will use
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5void setup()
{
}void loop()
{if (stepper.distanceToGo() == 0){// Random change to speed, position and acceleration// Make sure we dont get 0 speed or accelerationsdelay(1000);stepper.moveTo(rand() % 200);stepper.setMaxSpeed((rand() % 200) + 1);stepper.setAcceleration((rand() % 200) + 1);}stepper.run();
}

ACCELSTEPPER库实例分析相关推荐

  1. python图片解析库_python用来获得图片exif信息的库实例分析

    本文实例讲述了python用来获得图片exif信息的库用法.分享给大家供大家参考.具体分析如下: exif-py是一个纯python实现的获取图片元数据的python库,官方下载地址: http:// ...

  2. AccelStepper库

    文章目录 Accelstepper 库中的参数计算公式 ACCELSTEPPER库函数解析(一) moveTo runToNewPosition runSpeed runToPosition stop ...

  3. DPDK 跟踪库tracepoint源码实例分析

    DPDK笔记 DPDK 跟踪库tracepoint源码实例分析 RToax 2021年4月 注意: 跟踪库 基于DPDK 20.05 DPDK跟踪库:trace library 1. trace流程源 ...

  4. linux程序卸载动态库,Intel平台下linux中ELF文件动态链接的加载、解析及实例分析(二): 函数解析与卸载...

    在 IBM Bluemix 云平台上开发并部署您的下一个应用. 相信读者已经看过了 Intel平台下Linux中ELF文件动态链接的加载.解析及实例分析(一): 加载的内容了,了解了ELF文件被加载的 ...

  5. gpgpu-sim卡分配程序设计实例分析

    gpgpu-sim卡分配程序设计实例分析 运行代码地址:https://github.com/gpgpu-sim/gpgpu-sim_distribution 一.概述 此文件包含有关安装.生成和运行 ...

  6. python asyncio教程_python中使用asyncio实现异步IO实例分析

    1.说明 Python实现异步IO非常简单,asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持. asyncio的编程模型就是一个消息循环.我们从asyncio模块中直接 ...

  7. Android10.0 Binder通信原理(四)-Native-C\C++实例分析

    摘要:本节主要来讲解Android10.0 Binder的Native层实例流程 阅读本文大约需要花费35分钟. 文章首发微信公众号:IngresGe 专注于Android系统级源码分析,Androi ...

  8. linux驱动由浅入深系列:高通sensor架构实例分析之一

    点击打开链接 本系列导航: linux驱动由浅入深系列:高通sensor架构实例分析之一(整体概览+AP侧代码分析) linux驱动由浅入深系列:高通sensor架构实例分析之二(adsp驱动代码结构 ...

  9. JPEG 原理详细实例分析及其在嵌入式 Linux 中的应用

    http://www.ibm.com/developerworks/cn/linux/l-cn-jpeg/index.html 一.系统架构 本文以一个实际的产品为例,来说明 JPEG 在其中的应用. ...

最新文章

  1. sqlserver转oracle后nvchar2(char),Oracle(二)老师
  2. C#获取和设置环境变量
  3. 【Arduino】Nano功能引脚 PWM IIC UART 中断
  4. clone的fork与pthread_create创建线程有何不同pthread多线程编程的学习小结(转)
  5. selenium自动化测试-1.selenium介绍及环境安装
  6. cmd 取消点击锁定功能
  7. 埃森哲:AI成新生产要素,2035年将中国经济增速提高1.6% | 附下载
  8. python 制作聊天程序-python实现点对点聊天程序
  9. c++如何让类对象只能在堆(栈)上分配空间
  10. 平面设计师学习指南,平面设计都要学什么
  11. 相关系数excel_利用【相关系数】理解数据之间的关系
  12. dwcs6 php 教程,初学者如何使用Dreamweaver CS6 (Dreamweaver CS6详细使用教程)
  13. regedit 导入注册表
  14. 网络接口 FE GE 10GE SFP
  15. CSAPP : Arch Lab 解题报告
  16. DWcs6+AppServ快速搭建PHP环境
  17. 2022.04.15【单细胞】|Seurat安装,C++ compiler supports the long long type... no解决方法
  18. 腾讯云通信WebIM事件回调的坑~
  19. STM32F4之按键(二)
  20. springboot整合ActiveMQ(点对点和发布订阅)

热门文章

  1. SQL取整、四舍五入
  2. 黑夜给了我黑色的眼睛,我却用它研究CV
  3. android 效果集合
  4. WPF—WrapPanel布局
  5. Chrome浏览器自动填充的表单如何去掉淡黄色背景???
  6. CMake:CMAKE_SOURCE_DIR,PROJECT_SOURCE_DIR,PROJECT_BINARY_DIR
  7. Android 仿QQ个性标签显示
  8. Android项目Ant命令与问题记录
  9. Matplotlib figure图形对象
  10. Qt QSlider双滑块