关于Arduino 步进电机Stepper库的一些想法
官方提供了一些库,使Arduino入门起来更加快速,我们连原理都不用懂,就能通过函数控制终端。但是,这样也带来了很多的缺陷,比如,库函数的功能有限,有些无法实现。然后还有库函数因为要考虑其他的情况,你是四线的还是两线的,于是整个程序就会写的很麻烦。
我想用Sony无线手柄控制电机停止、顺时针、逆时针转动,按Start键能启动。但是库里根本没有这个功能。
还有我发现,一旦我的无线手柄里面加入了电机的相关程序,无线手柄与接收器的通信就会变迟钝,往往需要按着才能等到电机反向转动,而且有时候我改变方向,结果它转了一圈又回到原来方向了,完全就是乱套了。不过,中断还是能照常反应,但是还是会出现延迟现象。
后来看了一下代码,库文件只有在转完一圈才会跳出循环,所以按键的消息根本没办法马上反应。
后来就直接根据库文件写了函数,反应很迅速!!!!
下面是库文件的函数
1: #ifndef Stepper_h
2: #define Stepper_h
3:
4: // library interface description
5: class Stepper {
6: public:
7: // constructors:
8: Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2);
9: Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4);
10:
11: // speed setter method:
12: void setSpeed(long whatSpeed);
13:
14: // mover method:
15: void step(int number_of_steps);
16:
17: int version(void);
18:
19: private:
20: void stepMotor(int this_step);
21:
22: int direction; // Direction of rotation
23: int speed; // Speed in RPMs
24: unsigned long step_delay; // delay between steps, in ms, based on speed
25: int number_of_steps; // total number of steps this motor can take
26: int pin_count; // whether you're driving the motor with 2 or 4 pins
27: int step_number; // which step the motor is on
28:
29: // motor pin numbers:
30: int motor_pin_1;
31: int motor_pin_2;
32: int motor_pin_3;
33: int motor_pin_4;
34:
35: long last_step_time; // time stamp in ms of when the last step was taken
36: };
37:
38: #endif
39:
1: #include "Arduino.h"
2: #include "Stepper.h"
3:
4: /*
5: * two-wire constructor.
6: * Sets which wires should control the motor.
7: */
8: Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2)
9: {
10: this->step_number = 0; // which step the motor is on
11: this->speed = 0; // the motor speed, in revolutions per minute
12: this->direction = 0; // motor direction
13: this->last_step_time = 0; // time stamp in ms of the last step taken
14: this->number_of_steps = number_of_steps; // total number of steps for this motor
15:
16: // Arduino pins for the motor control connection:
17: this->motor_pin_1 = motor_pin_1;
18: this->motor_pin_2 = motor_pin_2;
19:
20: // setup the pins on the microcontroller:
21: pinMode(this->motor_pin_1, OUTPUT);
22: pinMode(this->motor_pin_2, OUTPUT);
23:
24: // When there are only 2 pins, set the other two to 0:
25: this->motor_pin_3 = 0;
26: this->motor_pin_4 = 0;
27:
28: // pin_count is used by the stepMotor() method:
29: this->pin_count = 2;
30: }
31:
32:
33: /*
34: * constructor for four-pin version
35: * Sets which wires should control the motor.
36: */
37:
38: Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4)
39: {
40: this->step_number = 0; // which step the motor is on
41: this->speed = 0; // the motor speed, in revolutions per minute
42: this->direction = 0; // motor direction
43: this->last_step_time = 0; // time stamp in ms of the last step taken
44: this->number_of_steps = number_of_steps; // total number of steps for this motor
45:
46: // Arduino pins for the motor control connection:
47: this->motor_pin_1 = motor_pin_1;
48: this->motor_pin_2 = motor_pin_2;
49: this->motor_pin_3 = motor_pin_3;
50: this->motor_pin_4 = motor_pin_4;
51:
52: // setup the pins on the microcontroller:
53: pinMode(this->motor_pin_1, OUTPUT);
54: pinMode(this->motor_pin_2, OUTPUT);
55: pinMode(this->motor_pin_3, OUTPUT);
56: pinMode(this->motor_pin_4, OUTPUT);
57:
58: // pin_count is used by the stepMotor() method:
59: this->pin_count = 4;
60: }
61:
62: /*
63: Sets the speed in revs per minute
64:
65: */
66: void Stepper::setSpeed(long whatSpeed)
67: {
68: this->step_delay = 60L * 1000L / this->number_of_steps / whatSpeed;
69: }
70:
71: /*
72: Moves the motor steps_to_move steps. If the number is negative,
73: the motor moves in the reverse direction.
74: */
75: void Stepper::step(int steps_to_move)
76: {
77: int steps_left = abs(steps_to_move); // how many steps to take
78:
79: // determine direction based on whether steps_to_mode is + or -:
80: if (steps_to_move > 0) {this->direction = 1;}
81: if (steps_to_move < 0) {this->direction = 0;}
82:
83:
84: // decrement the number of steps, moving one step each time:
85: while(steps_left > 0) {
86: // move only if the appropriate delay has passed:
87: if (millis() - this->last_step_time >= this->step_delay) {
88: // get the timeStamp of when you stepped:
89: this->last_step_time = millis();
90: // increment or decrement the step number,
91: // depending on direction:
92: if (this->direction == 1) {
93: this->step_number++;
94: if (this->step_number == this->number_of_steps) {
95: this->step_number = 0;
96: }
97: }
98: else {
99: if (this->step_number == 0) {
100: this->step_number = this->number_of_steps;
101: }
102: this->step_number--;
103: }
104: // decrement the steps left:
105: steps_left--;
106: // step the motor to step number 0, 1, 2, or 3:
107: stepMotor(this->step_number % 4);
108: }
109: }
110: }
111:
112: /*
113: * Moves the motor forward or backwards.
114: */
115: void Stepper::stepMotor(int thisStep)
116: {
117: if (this->pin_count == 2) {
118: switch (thisStep) {
119: case 0: /* 01 */
120: digitalWrite(motor_pin_1, LOW);
121: digitalWrite(motor_pin_2, HIGH);
122: break;
123: case 1: /* 11 */
124: digitalWrite(motor_pin_1, HIGH);
125: digitalWrite(motor_pin_2, HIGH);
126: break;
127: case 2: /* 10 */
128: digitalWrite(motor_pin_1, HIGH);
129: digitalWrite(motor_pin_2, LOW);
130: break;
131: case 3: /* 00 */
132: digitalWrite(motor_pin_1, LOW);
133: digitalWrite(motor_pin_2, LOW);
134: break;
135: }
136: }
137: if (this->pin_count == 4) {
138: switch (thisStep) {
139: case 0: // 1010
140: digitalWrite(motor_pin_1, HIGH);
141: digitalWrite(motor_pin_2, LOW);
142: digitalWrite(motor_pin_3, HIGH);
143: digitalWrite(motor_pin_4, LOW);
144: break;
145: case 1: // 0110
146: digitalWrite(motor_pin_1, LOW);
147: digitalWrite(motor_pin_2, HIGH);
148: digitalWrite(motor_pin_3, HIGH);
149: digitalWrite(motor_pin_4, LOW);
150: break;
151: case 2: //0101
152: digitalWrite(motor_pin_1, LOW);
153: digitalWrite(motor_pin_2, HIGH);
154: digitalWrite(motor_pin_3, LOW);
155: digitalWrite(motor_pin_4, HIGH);
156: break;
157: case 3: //1001
158: digitalWrite(motor_pin_1, HIGH);
159: digitalWrite(motor_pin_2, LOW);
160: digitalWrite(motor_pin_3, LOW);
161: digitalWrite(motor_pin_4, HIGH);
162: break;
163: }
164: }
165: }
166:
167: /*
168: version() returns the version of the library:
169: */
170: int Stepper::version(void)
171: {
172: return 4;
173: }
转载于:https://www.cnblogs.com/zjjsxuqiang/p/3485484.html
关于Arduino 步进电机Stepper库的一些想法相关推荐
- Arduino 调用Stepper库驱动28BYJ-48步进电机,电机振动不转、无法反方向转的解决办法
电机堵转 首先检查杜邦线是否正常联通,如GND线接触不良,电机也会导通,但无法正常工作. 检查Arduino和驱动板接线是否正确,正确硬件接线如下图,即板子IO 8.9.10.11分别对应 驱动IN ...
- 【Proteus仿真】Arduino UNO利用Stepper库实现uln2003驱动步进电机转动
[Proteus仿真]Arduino UNO利用Stepper库实现uln2003驱动步进电机转动 Proteus仿真 Proteus说明 Proteus软件里面的步进电机的步距角默认是90,和代码中 ...
- arduino步进电机程序库_Arduino入门教程15(步进电机驱动库的使用):Arduino Uno R3+ULN2003+步进电机 使用Stepper驱动库,控制步进电机转动角度...
本篇介绍步进电机驱动库的使用,通过读取电位器输入,控制步进电机转动相应角度. Stepper库是官方提供的驱动库,我们启动Arduino IDE,点击「文件」-「示例」就能找到Stepper库,官方提 ...
- arduino步进电机程序库_Arduino基础入门篇27—步进电机驱动库的使用
本篇介绍步进电机驱动库的使用,通过读取电位器输入,控制步进电机转动相应角度. Stepper库是官方提供的驱动库,我们启动Arduino IDE,点击「文件」-「示例」就能找到Stepper库,官方提 ...
- Arduino UNO使用库实现步进电机指定角度旋转
Arduino UNO使用库实现步进电机指定角度旋转 步进电机和驱动器相关的资料 https://pan.baidu.com/s/1udb4MyEOXk4CTO7TKRHj6w 提取码: fuea 步 ...
- 手把手教你看懂并理解Arduino PID控制库——调参改变
2019独角兽企业重金招聘Python工程师标准>>> 引子 本文将分析<手把手教你看懂并理解Arduino PID控制库>中第三个问题:PID控制参数突变对系统的影响. ...
- arduino红外遥控库IRremote的IRsend类sendRaw函数溢出问题及其解决方法
arduino红外遥控库IRremote的IRsend类sendRaw函数溢出问题及其解决方法 参考文章: (1)arduino红外遥控库IRremote的IRsend类sendRaw函数溢出问题及其 ...
- 关于多库操作一些想法
关于多库操作一些想法 /// <summary> /// 功能描述:sql连接方法 /// </summary> /// < ...
- Arduino ESP32 第三方库读取SD卡信息(三)
Arduino ESP32 第三方库读取SD卡信息(三) 相关篇<Arduino ESP32 第三方库读取SD卡信息(一)> <Arduino ESP32 第三方库读取SD卡信息(二 ...
最新文章
- oracle如果为空替换为0,oracle 如何把0转为null
- Python学习之变量、对象和引用
- iOS 11.4.1 正式版越狱
- JavaScript 音频处理库 pico.js
- mysql注册slave_创建slave库?spm=a2c4e.11155472的搜索结果-阿里云开发者社区
- Adobe 2022软件安装错误代码107解决办法
- 文档安全管理系统服务器地址是什么,一种文档安全管理系统登录方法及装置
- Java 异常的捕获与处理详解 (一)
- iOS UIImageView设置为圆形
- Spring SAS 0.2.0 上手教程
- 2023计算机毕业设计SSM最新选题之javaAI学院教务信息管理系统lx9v9
- Shell脚本编程30分钟入门学习
- java nio和io的区别_Java NIO和IO的区别
- ubuntu linux修改ip地址命令,永久修改ubuntu系统MAC和IP地址的方法命令
- 服务器server2012重置开机密码
- Keras.metrics中的 accuracy 总结
- 验证性因子分析(二)
- asp毕业设计——基于asp+access的网上投票系统设计与实现(毕业论文+程序源码)——网上投票系统
- ubuntu服务器安装可视化桌面(Gnome)
- 淘宝分布式数据库是如何实现高可用的
热门文章
- linux 抓包工具_03-Python爬虫工程师-抓包工具
- ManyToManyField的注意事项和如何建立索引
- optional用法_还在重复写空指针检查代码?考虑使用 Optional 吧!
- linux三并发进程,linux下用进度条显示三个进程的并发
- element table多选表格_关于layui表格obj.update();无法重渲模板引擎问题
- 比特币支付接口php,比特币支付php类
- java调试时监视_Java监控工具、调优、调试辅助函数
- 计算机底纹不起作用,CSS - 背景颜色在IE11中不起作用(CSS - background-color not working in IE11)...
- linux内存报警,linux 邮件报警,监控内存cup
- mysql5.6.22编译安装教程_Linux CentOS6.0下编译安装MySQL 5.6.22