步进电机库函数

This library allows you to control unipolar or bipolar stepper motors. To use it you will need a stepper motor, and the appropriate hardware to control it. For more on that, see Tom Igoe‘s notes on steppers.

函数

Stepper(steps, pin1, pin2)            步进电机定义

Stepper(steps, pin1, pin2, pin3, pin4)   步进电机定义

Description  解释

This function creates a new instance of the Stepper class that represents a particular stepper motor attached to your Arduino board. Use it at the top of your sketch, above setup() and loop(). The number of parameters depends on how you‘ve wired your motor - either using two or four pins of the Arduino board.

Parameters  参数

steps: the number of steps in one revolution of your motor. If your motor gives the number of degrees per step, divide that number into 360 to get the number of steps (e.g. 360 / 3.6 gives 100 steps). (int)  一圈对应的步数

pin1, pin2: two pins that are attached to the motor (int)

pin3, pin4: optional the last two pins attached to the motor, if it‘s connected to four pins (int)

Returns  A new instance of the Stepper motor class.

setSpeed(rpm)  速度设定

Description  解释

Sets the motor speed in rotations per minute (RPMs). This function doesn‘t make the motor turn, just sets the speed at which it will when you call step().

Parameters  参数

rpms: the speed at which the motor should turn in rotations per minute - a positive number (long)

Returns None 无返回值

step(steps)   步数执行

Description

Turns the motor a specific number of steps, at a speed determined by the most recent call to setSpeed(). This function is blocking; that is, it will wait until the motor has finished moving to pass control to the next line in your sketch. For example, if you set the speed to, say, 1 RPM and called step(100) on a 100-step motor, this function would take a full minute to run. For better control, keep the speed high and only go a few steps with each call to step().

Parameters

steps: the number of steps to turn the motor - positive to turn one direction, negative to turn the other (int)

Returns  None

举例

1  Stepper Motor Knob  用电位器控制步进电机步数

Control a highly accurate stepper motor using a potentiometer

Stepper motors, due to their unique design, can be controlled to a high degree of accuracy without any feedback mechanisms. The shaft of a stepper, mounted with a series of magnets, is controlled by a series of electromagnetic coils that are charged positively and negatively in a specific sequence, precisely moving it forward or backward in small "steps".

There are two types of steppers, Unipolars and Bipolars, and it is very important to know which type you are working with. For each of the motors, there is a different circuit. The example code will control both kinds of motors. See the unipolar and bipolar motor schematics for information on how to wire up your motor.

In this example, a potentiometer (or other sensor) on analog input 0 is used to control the movement of a stepper motor using the Arduino Stepper Library. The stepper is controlled by with digital pins 8, 9, 10, and 11 for either unipolar or bipolar motors.

The Arduino or Genuino board will connect to a U2004 Darlington Array if you‘re using a unipolar stepper or a SN754410NE H-Bridge if you have a bipolar motor.

For more information about the differences of the two types, please take a look at Tom Igoe‘s page on stepper motors.

Hardware Required  硬件

Arduino or Genuino Board

10k ohm potentiometer

stepper motor

U2004 Darlington Array (if using a unipolar stepper)

SN754410ne H-Bridge (if using a bipolar stepper)

power supply appropriate for your particular stepper

hook-up wires

breadboard

Circuits  电路

Below you‘ll find circuits for both unipolar and bipolar steppers. In either case, it is best to power your stepper motors from an external supply, as they draw too much to be powered directly from your Arduino board.

In both circuits, connect a 10k pot to power and ground, with it‘s wiper outputting to analog pin 0.

Note: Both circuits below are four wire configurations. Two wire configurations will not work with the code provided.

Unipolar Stepper Circuit and schematic:

Bipolar Stepper Circuit and schematic:

Code  程序

For both unipolar and bipolar steppers

 1 /*
 2 * MotorKnob
 3 ** A stepper motor follows the turns of a potentiometer (or other sensor) on analog input 0.
 4 ** http://www.arduino.cc/en/Reference/Stepper
 5 * This example code is in the public domain.
 6 */
 7
 8 #include <Stepper.h>
 9 #define STEPS 100     // change this to the number of steps on your motor
10   // create an instance of the stepper class, specifying
11   // the number of steps of the motor and the pins it‘s attached to
12 Stepper stepper(STEPS, 8, 9, 10, 11);
13 int previous = 0;    // the previous reading from the analog input
14
15 void setup()
16 {
17     stepper.setSpeed(30);   // set the speed of the motor to 30 RPMs
18 }
19
20 void loop()
21 {
22     int val = analogRead(0);   // get the sensor value
23   // move a number of steps equal to the change in the sensor reading
24     stepper.step(val - previous);   // remember the previous value of the sensor
25     previous = val;
26 }

2  Stepper Speed Control  用电位器控制步进电机速度

Control the stepping speed with a potentiometer

硬件与电路部分,与上例同

Code  程序

For both unipolar and bipolar steppers

 1 /*
 2 Stepper Motor Control - speed control
 3 This program drives a unipolar or bipolar stepper motor.
 4 The motor is attached to digital pins 8 - 11 of the Arduino.
 5 A potentiometer is connected to analog input 0.
 6     The motor will rotate in a clockwise direction. The higher the potentiometer value,the faster the motor speed. Because setSpeed() sets the delay between steps,you may notice the motor is less responsive to changes in the sensor value at low speeds.
 7
 8 Created 30 Nov. 2009,Modified 28 Oct 2010,by Tom Igoe
 9 */
10
11 #include <Stepper.h>
12 const int stepsPerRevolution = 200;
13 // change this to fit the number of steps per revolution
14   // for your motor initialize the stepper library on pins 8 through 11:
15 Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
16 int stepCount = 0;   // number of steps the motor has taken
17
18 void setup()
19 {
20     // nothing to do inside the setup
21 }
22
23 void loop()
24  {
25     int sensorReading = analogRead(A0); // read the sensor value:
26     int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
27     // map it to a range from 0 to 100
28     if (motorSpeed > 0) // set the motor speed:
29     {
30         myStepper.setSpeed(motorSpeed);
31         myStepper.step(stepsPerRevolution / 100); // step 1/100 of a revolution:
32     }
33 }

3  Stepper One Revolution    正转一圈反转一圈

Turn the shaft one revolution clockwise and one counterclockwise.

硬件与电路部分,与上例同

Code  程序

For both unipolar and bipolar steppers

 1 /*
 2 Stepper Motor Control - one revolution
 3 This program drives a unipolar or bipolar stepper motor.
 4 The motor is attached to digital pins 8 - 11 of the Arduino.
 5     The motor should revolve one revolution in one direction, then one revolution in the other direction.
 6
 7 Created 11 Mar. 2007,Modified 30 Nov. 2009,by Tom Igoe
 8 */
 9 #include <Stepper.h>
10 const int stepsPerRevolution = 200;
11 // change this to fit the number of steps per revolution
12  // for your motor initialize the stepper library on pins 8 through 11:
13 Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
14
15 void setup()
16 {
17     myStepper.setSpeed(60);   // set the speed at 60 rpm:
18     Serial.begin(9600);   // initialize the serial port:
19 }
20
21 void loop()
22 {
23     Serial.println("clockwise");   // step one revolution in one direction:
24     myStepper.step(stepsPerRevolution);
25     delay(500);
26     Serial.println("counterclockwise");   // step one revolution in the other         direction:
27     myStepper.step(-stepsPerRevolution);
28     delay(500);
29 }

4  One step at a time  一次只走一步

Turn the shaft step by step to check the proper wiring of the motor.

硬件与电路部分,与上例同

Code  程序

For both unipolar and bipolar steppers

 1 /*
 2 Stepper Motor Control - one step at a time
 3 This program drives a unipolar or bipolar stepper motor.
 4 The motor is attached to digital pins 8 - 11 of the Arduino.
 5     The motor will step one step at a time, very slowly. You can use this to test that you‘ve got the four wires of your stepper wired to the correct pins. If wired correctly, all steps should be in the same direction.
 6     Use this also to count the number of steps per revolution of your motor,if you don‘t know it. Then plug that number into the one Revolution example to see if you got it right.
 7
 8 Created 30 Nov. 2009,by Tom Igoe
 9 */
10
11 #include <Stepper.h>
12 const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
13     // for your motor initialize the stepper library on pins 8 through 11:
14 Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
15 int stepCount = 0; // number of steps the motor has taken
16
17 void setup()
18 {
19     Serial.begin(9600);   // initialize the serial port:
20 }
21
22 void loop()
23 {
24     myStepper.step(1);   // step one step:
25     Serial.print("steps:");
26     Serial.println(stepCount);
27     stepCount++;
28     delay(500);
29 }

原文地址:https://www.cnblogs.com/MyAutomation/p/9290105.html

时间: 2024-10-31 11:18:12

步进电机库函数的相关文章

STM32在定时器中控制步进电机

写WEB程序的时候,发现如果加上步进电机控制的话,就会出现问题.原因是如果把电机控制放在uip循环中处理的话,因为控制电机涉及到时间问题.所以必须解决,想到的方法就是多任务的思想.把步进电机的处理放在定时器中断里面.这样的话就能避免问题了.顺便学习了库函数操作定时器.这里用到的定时器功能简单,只需要产生更新中断. 下面是定时器初始化代码 //定时器3初始化 //arr:定时重装值 //psc:分频值 void TIM3_Init(uint16_t arr, uint16_t psc) { TIM

使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用

使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用 选择调用的进程为 24 i386 getuid sys_getuid1647 i386 getgid sys_getgid16 使用库函数API方式 使用C代码中嵌入汇编代码方式

C语言标准库函数qsort详解

1 函数简介 功 能: 使用快速排序例程进行排序 头文件:stdlib.h 用 法: void qsort(void *base,int nelem,int width,int (*fcmp)(const void *,const void *)); 参数: 1 待排序数组首地址 2 数组中待排序元素数量 3 各元素的占用空间大小 4 指向函数的指针,用于确定排序的顺序 2 基本用法 使用qsort()排序并用 bsearch()搜索是一个比较常用的组合,使用方便快捷. qsort 的函数原型是

步进电机

一.步进电机的构造 步进电动机构造上大致分为定子与转子两部分. 转子由转子 1.转子 2.永久磁钢等 3 部分构成.而且转子朝轴方向 已经磁化,转子 1 为 N 极时,转子 2 则为 S 极. 定子拥有小齿状的磁极,共有 10 个,皆绕有线圈. 其线圈的对角位置的磁极相互连接着,电流流通后,线圈即会被磁 化成同一极性.(例如某一线圈经由电流的流通后,对角线的磁极将 同化成 S 极或 N 极.) 对角线的 2 个磁极形成 1 个相,而由于有 A 相至 E 相等 5 个相位, 因此称为 5 相步进电

STM32定时器输出PWM频率和步进电机控制速度计算

1.STM32F4系列定时器输出PWM频率计算 第一步,了解定时器的时钟多少: 我们知道AHP总线是168Mhz的频率,而APB1和APB2都是挂在AHP总线上的. (1)高级定时器timer1, timer8以及通用定时器timer9, timer10, timer11的时钟来源是APB2总线(2)通用定时器timer2~timer5,通用定时器timer12~timer14以及基本定时器timer6,timer7的时钟来源是APB1总线 从STM32F4的内部时钟树可知: 当APB1和APB

第二篇:库函数和系统调用的区别

前言 这是一对非常容易混淆的概念.对于用户( 应用程序开发者 )来说,并不一定要严格区分其意义.因为在用户看来,它们都是以C函数的形式出现的.但了解二者的区别对我们掌握整个计算机系统有很大帮助. 区别 1. 一部分库函数实现需要使用系统调用( 如 printf 库函数需要调用 write 系统调用 ) 2. 另一部分库函数实现不需要使用系统调用( 如strcpy 库函数不需要使用系统调用因为它不需要使用CPU特权指令 ) 3. 系统调用一定在内核空间执行,而库函数只有其中的系统调用部分执行时才会

单片机与控制实验(4)——步进电机原理及应用

一.实验目的和要求 了解步进电机的工作原理,学习用单片机的步进电机控制系统的硬件设计方法,掌握定时器和中断系统的应用,熟悉单片机应用系统的设计与调试方法. 二.实验设备 单片机测控实验系统 步进电机控制实验模块 Keil开发环境 STC-ISP程序下载工具 三.实验内容 编制MCS-51程序使步进电机按照规定的转速和方向进行旋转,并将已转动的步数显示在数码管上. 步进电机的转速分为两档,当按下S1开关时,加速旋转,速度从10转/分加速到60转/分.当松开开关时,减速旋转,速度恢复为10转/分.当

系统调用和标准库函数的关联

1. 首先,现在的OS内核主要采用两种模式,整体的单内核模式(linux)和分层的微内核模式(Windows).单内核 模式的特点就是代码紧凑,执行速度快,各个模块之间是直接的调用关系,可以说最后一点既是优点,也是缺 点...有点就是执行速度快,缺点是内核看起来很乱,维护起来困难. 2. 无论是单内核,还是微内核,立体的体系结构从下到上大概都是分成这样几层:物理硬件,OS内核,OS服务, 应用程序.这四层结构中,OS内核起到一个“承上启下”的作用,向下管理物理硬件:向上为OS服务和应用程序 提供

实验--使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用(杨光)

使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用 攥写人:杨光  学号:20135233 ( *原创作品转载请注明出处*) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验要求: 选择一个系统调用(13号系统调用time除外),系统调用列表参见http://codelab.shiyanlou.com/xref/linux-3.18.6/arch/x86/syscalls/sys