如何修复我为 Arduino 制作的步进电机库?
How do I fix my stepper motor library that I made for Arduino?
我有一个 Arduino Mega,我正在使用 L298N,我的目标是制作一个成功的 Arduino 库,以使用步进电机制作音乐。我知道以前已经采用过这种方法,但我正在尝试自己做。我不能使用 Moppy 库,坦率地说,其他人的代码非常复杂。那么,我的问题是什么?我的问题是当我使用我制作的库(作为测试)时出现此错误:
"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\mega" "-IC:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary" "C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.cpp" -o "C:\Users\austin\AppData\Local\Temp\arduino_build_519425\libraries\stepperTestLibrary\stepperTestLibrary.cpp.o"
C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.cpp:13:1: error: prototype for 'stepperTest::stepperTest(int, int, int, int)' does not match any in class 'stepperTest'
stepperTest::stepperTest(int SMPin1, int SMPin2, int SMPin3, int SMPin4){
^~~~~~~~~~~
In file included from C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.cpp:11:0:
C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.h:15:7: error: candidates are: constexpr stepperTest::stepperTest(stepperTest&&)
class stepperTest{
^~~~~~~~~~~
C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.h:15:7: error: constexpr stepperTest::stepperTest(const stepperTest&)
C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.h:19:1: error: stepperTest::stepperTest(uint8_t, uint8_t, uint8_t, uint8_t)
stepperTest(uint8_t SMPin1, uint8_t SMPin2, uint8_t SMPin3, uint8_t SMPin4);
^~~~~~~~~~~
C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.cpp:29:6: error: prototype for 'void stepperTest::runStepper(int, int)' does not match any in class 'stepperTest'
void stepperTest::runStepper(int frequency, int duration){
^~~~~~~~~~~
In file included from C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.cpp:11:0:
C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.h:21:6: error: candidate is: void stepperTest::runStepper(uint8_t, uint8_t)
void runStepper(uint8_t frequency, uint8_t duration);
^~~~~~~~~~
还有这个:
exit status 1
Error compiling for board Arduino Mega or Mega 2560.
我希望库能够 运行 顺利,因为我正在关注其他一些步进电机库,但我遇到了这些错误。我试过多次修改代码,但我遇到了类似的问题,所以我不确定如何解决我自己的问题。我什至尝试过复制和粘贴一些代码来使库正常工作。没有什么。我正在使用其他人制作的 Arduino 步进器、Accelstepper 和双极步进器库,但没有结果。 https://create.arduino.cc/projecthub/ambhatt/bipolar-stepper-motor-library-b9d5e0
这是 .h 文件:
/*
This library is designed for testing purposing only. CC Public Domain
Austin Harris
3/4/2021
This library is designed to run a stepper motor at a certain frequency to
make musical notes and for a certain duration
*/
#ifndef stepperTestLibrary_h
#define stepperTestLibrary_h
#include "Arduino.h"
class stepperTest{
public:
stepperTest(int SMPin1, int SMPin2, int SMPin3, int SMPin4);
void runStepper(int frequency, int duration);
private:
int direction;
int SMPin1, intSMPin2, intSMPin3, intSMPin4;
};
#endif
这是 .cpp 文件:
/*
This library is designed for testing purposing only. CC Public Domain
Austin Harris
3/4/2021
This library is designed to run a stepper motor at a certain frequency to
make musical notes and for a cetain duration
*/
#include "Arduino.h"
#include "stepperTestLibrary.h"
stepperTest::stepperTest(int SMPin1, int SMPin2, int SMPin3, int SMPin4){
pinMode(SMPin1, OUTPUT);
pinMode(SMPin2, OUTPUT);
pinMode(SMPin3, OUTPUT);
pinMode(SMPin4, OUTPUT);
_SMPin1 = SMPin1;
_SMPin2 = SMPin2;
_SMPin3 = SMPin3;
_SMPin4 = SMPin4;
int direction = 0; // motor direction
}
void stepperTest::runStepper(int frequency, int duration){
int rpmCalculation = abs(1000.0 / frequency);
// determine direction based on whether steps_to_mode is + or -:
if (frequency > 0) { direction = 1; }
if (frequency < 0) { direction = 0; }
digitalWrite(SMPin1, HIGH);
digitalWrite(SMPin2, LOW);
digitalWrite(SMPin3, HIGH);
digitalWrite(SMPin4, LOW);
delay(1);
digitalWrite(SMPin1, LOW);
digitalWrite(SMPin2, HIGH);
digitalWrite(SMPin3, HIGH);
digitalWrite(SMPin4, LOW);
delay(1);
digitalWrite(SMPin1, LOW);
digitalWrite(SMPin2, HIGH);
digitalWrite(SMPin3, LOW);
digitalWrite(SMPin4, HIGH);
delay(1);
digitalWrite(SMPin1, HIGH);
digitalWrite(SMPin2, LOW);
digitalWrite(SMPin3, LOW);
digitalWrite(SMPin4, HIGH);
delay(duration);
}
这是keywords.txt:
stepperTest KEYWORD1
runStepper KEYWORD2
这是 .ino 示例:
#include <stepperTestLibrary.h>
stepperTest myStepper(22, 23, 24, 25);
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
myStepper.runStepper(440, 500);
}
接线如下:
This is the wiring image
这是步进器数据表的 link:
-谢谢,
奥斯汀
阅读一些关于 类 in C++ 的内容将是一个好的开始。也许变量名是如何工作的。以及阴影变量如何工作。 SMPin2、_SMPin2 和第三个变体是 intSMPin2 之间有很大区别(其他变体也搞砸了)
无论如何,如果你想在构造函数中使用与成员相同的参数名称,你必须使用构造函数初始化列表。
#ifndef stepperTestLibrary_h
#define stepperTestLibrary_h
#include "Arduino.h"
class stepperTest {
public:
stepperTest(uint8_t SMPin1, uint8_t SMPin2, uint8_t SMPin3, uint8_t SMPin4);
void runStepper(int frequency, int duration);
private:
// just give some space to it:
uint8_t SMPin1;
uint8_t SMPin2;
uint8_t SMPin3;
uint8_t SMPin4;
int8_t direction;
/// or you'll mess it up like this:
/// int SMPin1, --->>>>> intSMPin2, intSMPin3, intSMPin4 <<<<<---;
};
#endif
主要部分:
#include "Arduino.h"
#include "stepperTestLibrary.h"
stepperTest::stepperTest(uint8_t SMPin1, uint8_t SMPin2, uint8_t SMPin3, uint8_t SMPin4)
// constructor's initializer list:
: SMPin1{ SMPin1 }
, SMPin2{ SMPin2 }
, SMPin3{ SMPin3 }
, SMPin4{ SMPin4 }
, direction{ 0 }
{
pinMode(SMPin1, OUTPUT);
pinMode(SMPin2, OUTPUT);
pinMode(SMPin3, OUTPUT);
pinMode(SMPin4, OUTPUT);
}
void stepperTest::runStepper(int frequency, int duration) {
// // Never used anywhere:
// int rpmCalculation = abs(1000.0 / frequency);
// // determine direction based on whether steps_to_mode is + or -:
// if (frequency > 0) { direction = 1; }
// if (frequency < 0) { direction = 0; }
digitalWrite(SMPin1, HIGH);
digitalWrite(SMPin2, LOW);
digitalWrite(SMPin3, HIGH);
digitalWrite(SMPin4, LOW);
delay(1);
digitalWrite(SMPin1, LOW);
digitalWrite(SMPin2, HIGH);
digitalWrite(SMPin3, HIGH);
digitalWrite(SMPin4, LOW);
delay(1);
digitalWrite(SMPin1, LOW);
digitalWrite(SMPin2, HIGH);
digitalWrite(SMPin3, LOW);
digitalWrite(SMPin4, HIGH);
delay(1);
digitalWrite(SMPin1, HIGH);
digitalWrite(SMPin2, LOW);
digitalWrite(SMPin3, LOW);
digitalWrite(SMPin4, HIGH);
delay(duration); // what? why???
}
我有一个 Arduino Mega,我正在使用 L298N,我的目标是制作一个成功的 Arduino 库,以使用步进电机制作音乐。我知道以前已经采用过这种方法,但我正在尝试自己做。我不能使用 Moppy 库,坦率地说,其他人的代码非常复杂。那么,我的问题是什么?我的问题是当我使用我制作的库(作为测试)时出现此错误:
"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\mega" "-IC:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary" "C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.cpp" -o "C:\Users\austin\AppData\Local\Temp\arduino_build_519425\libraries\stepperTestLibrary\stepperTestLibrary.cpp.o"
C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.cpp:13:1: error: prototype for 'stepperTest::stepperTest(int, int, int, int)' does not match any in class 'stepperTest'
stepperTest::stepperTest(int SMPin1, int SMPin2, int SMPin3, int SMPin4){
^~~~~~~~~~~
In file included from C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.cpp:11:0:
C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.h:15:7: error: candidates are: constexpr stepperTest::stepperTest(stepperTest&&)
class stepperTest{
^~~~~~~~~~~
C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.h:15:7: error: constexpr stepperTest::stepperTest(const stepperTest&)
C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.h:19:1: error: stepperTest::stepperTest(uint8_t, uint8_t, uint8_t, uint8_t)
stepperTest(uint8_t SMPin1, uint8_t SMPin2, uint8_t SMPin3, uint8_t SMPin4);
^~~~~~~~~~~
C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.cpp:29:6: error: prototype for 'void stepperTest::runStepper(int, int)' does not match any in class 'stepperTest'
void stepperTest::runStepper(int frequency, int duration){
^~~~~~~~~~~
In file included from C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.cpp:11:0:
C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.h:21:6: error: candidate is: void stepperTest::runStepper(uint8_t, uint8_t)
void runStepper(uint8_t frequency, uint8_t duration);
^~~~~~~~~~
还有这个:
exit status 1
Error compiling for board Arduino Mega or Mega 2560.
我希望库能够 运行 顺利,因为我正在关注其他一些步进电机库,但我遇到了这些错误。我试过多次修改代码,但我遇到了类似的问题,所以我不确定如何解决我自己的问题。我什至尝试过复制和粘贴一些代码来使库正常工作。没有什么。我正在使用其他人制作的 Arduino 步进器、Accelstepper 和双极步进器库,但没有结果。 https://create.arduino.cc/projecthub/ambhatt/bipolar-stepper-motor-library-b9d5e0
这是 .h 文件:
/*
This library is designed for testing purposing only. CC Public Domain
Austin Harris
3/4/2021
This library is designed to run a stepper motor at a certain frequency to
make musical notes and for a certain duration
*/
#ifndef stepperTestLibrary_h
#define stepperTestLibrary_h
#include "Arduino.h"
class stepperTest{
public:
stepperTest(int SMPin1, int SMPin2, int SMPin3, int SMPin4);
void runStepper(int frequency, int duration);
private:
int direction;
int SMPin1, intSMPin2, intSMPin3, intSMPin4;
};
#endif
这是 .cpp 文件:
/*
This library is designed for testing purposing only. CC Public Domain
Austin Harris
3/4/2021
This library is designed to run a stepper motor at a certain frequency to
make musical notes and for a cetain duration
*/
#include "Arduino.h"
#include "stepperTestLibrary.h"
stepperTest::stepperTest(int SMPin1, int SMPin2, int SMPin3, int SMPin4){
pinMode(SMPin1, OUTPUT);
pinMode(SMPin2, OUTPUT);
pinMode(SMPin3, OUTPUT);
pinMode(SMPin4, OUTPUT);
_SMPin1 = SMPin1;
_SMPin2 = SMPin2;
_SMPin3 = SMPin3;
_SMPin4 = SMPin4;
int direction = 0; // motor direction
}
void stepperTest::runStepper(int frequency, int duration){
int rpmCalculation = abs(1000.0 / frequency);
// determine direction based on whether steps_to_mode is + or -:
if (frequency > 0) { direction = 1; }
if (frequency < 0) { direction = 0; }
digitalWrite(SMPin1, HIGH);
digitalWrite(SMPin2, LOW);
digitalWrite(SMPin3, HIGH);
digitalWrite(SMPin4, LOW);
delay(1);
digitalWrite(SMPin1, LOW);
digitalWrite(SMPin2, HIGH);
digitalWrite(SMPin3, HIGH);
digitalWrite(SMPin4, LOW);
delay(1);
digitalWrite(SMPin1, LOW);
digitalWrite(SMPin2, HIGH);
digitalWrite(SMPin3, LOW);
digitalWrite(SMPin4, HIGH);
delay(1);
digitalWrite(SMPin1, HIGH);
digitalWrite(SMPin2, LOW);
digitalWrite(SMPin3, LOW);
digitalWrite(SMPin4, HIGH);
delay(duration);
}
这是keywords.txt:
stepperTest KEYWORD1
runStepper KEYWORD2
这是 .ino 示例:
#include <stepperTestLibrary.h>
stepperTest myStepper(22, 23, 24, 25);
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
myStepper.runStepper(440, 500);
}
接线如下:
This is the wiring image
这是步进器数据表的 link:
-谢谢,
奥斯汀
阅读一些关于 类 in C++ 的内容将是一个好的开始。也许变量名是如何工作的。以及阴影变量如何工作。 SMPin2、_SMPin2 和第三个变体是 intSMPin2 之间有很大区别(其他变体也搞砸了)
无论如何,如果你想在构造函数中使用与成员相同的参数名称,你必须使用构造函数初始化列表。
#ifndef stepperTestLibrary_h
#define stepperTestLibrary_h
#include "Arduino.h"
class stepperTest {
public:
stepperTest(uint8_t SMPin1, uint8_t SMPin2, uint8_t SMPin3, uint8_t SMPin4);
void runStepper(int frequency, int duration);
private:
// just give some space to it:
uint8_t SMPin1;
uint8_t SMPin2;
uint8_t SMPin3;
uint8_t SMPin4;
int8_t direction;
/// or you'll mess it up like this:
/// int SMPin1, --->>>>> intSMPin2, intSMPin3, intSMPin4 <<<<<---;
};
#endif
主要部分:
#include "Arduino.h"
#include "stepperTestLibrary.h"
stepperTest::stepperTest(uint8_t SMPin1, uint8_t SMPin2, uint8_t SMPin3, uint8_t SMPin4)
// constructor's initializer list:
: SMPin1{ SMPin1 }
, SMPin2{ SMPin2 }
, SMPin3{ SMPin3 }
, SMPin4{ SMPin4 }
, direction{ 0 }
{
pinMode(SMPin1, OUTPUT);
pinMode(SMPin2, OUTPUT);
pinMode(SMPin3, OUTPUT);
pinMode(SMPin4, OUTPUT);
}
void stepperTest::runStepper(int frequency, int duration) {
// // Never used anywhere:
// int rpmCalculation = abs(1000.0 / frequency);
// // determine direction based on whether steps_to_mode is + or -:
// if (frequency > 0) { direction = 1; }
// if (frequency < 0) { direction = 0; }
digitalWrite(SMPin1, HIGH);
digitalWrite(SMPin2, LOW);
digitalWrite(SMPin3, HIGH);
digitalWrite(SMPin4, LOW);
delay(1);
digitalWrite(SMPin1, LOW);
digitalWrite(SMPin2, HIGH);
digitalWrite(SMPin3, HIGH);
digitalWrite(SMPin4, LOW);
delay(1);
digitalWrite(SMPin1, LOW);
digitalWrite(SMPin2, HIGH);
digitalWrite(SMPin3, LOW);
digitalWrite(SMPin4, HIGH);
delay(1);
digitalWrite(SMPin1, HIGH);
digitalWrite(SMPin2, LOW);
digitalWrite(SMPin3, LOW);
digitalWrite(SMPin4, HIGH);
delay(duration); // what? why???
}