类实例化仅适用于全局但不适用于设置 [英] Class Instantiation only works globally but not in set up

查看:64
本文介绍了类实例化仅适用于全局但不适用于设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,对象的实例化顺序会影响我系统的功能.以下所有代码都编译,但只有一个驱动步进器.我还想指出 Arduino Stepper library 指定在设置之前实例化类向上.我认为同样的原因适用于 TMC26XStepper.

I have an issue where the order of an object's instantiation effects the functionality of my system. All the following code compile but only one actuates the stepper. I also want to point out that the Arduino Stepper library specifies the class be instantiated before set up. I assume the same reason applies for the TMC26XStepper.

我自己的研究让我认为事实正好相反.我的理解是在调用 Arduino 运行时环境 init() 之前调用在 setup() 之前实例化的对象.这将导致 pinMode() 之类的方法尚不可用.再次反映默认的 Arduino 步进库,构造函数调用 pinMode() 似乎是错误的.

Research on my own makes me think the opposite would be true. My understanding is that objects instantiated before setup() are called before the Arduino runtime environment init() is called. This would cause methods like pinMode() to not be available yet. Reflecting on the default Arduino stepper library again the constructor calls pinMode() which seems wrong.

为什么一定要按这个顺序?

Why does it have to be in this order?

在 setup() 之前实例化 TMC26XStepper 类的工作代码:

Working code that instantiates the TMC26XStepper class before setup():

#include <SPI.h>
#include <TMC26XStepper.h>
TMC26XStepper tmc26xStepper = TMC26XStepper(200, 2, 6, 7, 800);
void setup() {
  Serial.begin(9600);
  tmc26xStepper.setSpreadCycleChopper(0, 24, 8, 6, 0);
  tmc26xStepper.setRandomOffTime(0);
  tmc26xStepper.setMicrosteps(16);
  tmc26xStepper.setStallGuardThreshold(2, 0);
  tmc26xStepper.setSpeed(400);
  tmc26xStepper.start();
  moveNSteps(tmc26xStepper, 1000);
  moveNSteps(tmc26xStepper, -1000);
}
void loop() {
}

void moveNSteps(TMC26XStepper stepper, int steps) {
  int microSteps = 16;
  stepper.step(steps * microSteps);
  while (stepper.getStepsLeft() != 0 && stepper.isMoving()) {
    stepper.move();
  }
}

此代码编译但步进电机不旋转.唯一的区别是 TMC26XStepper 对象的实例化位置:

This code compiles however the stepper motor does not spin. The only difference is where the TMC26XStepper object is instantiated:

#include <SPI.h>
#include <TMC26XStepper.h>

void setup() {
  TMC26XStepper tmc26xStepper = TMC26XStepper(200, 2, 6, 7, 800);
  Serial.begin(9600);
  tmc26xStepper.setSpreadCycleChopper(0, 24, 8, 6, 0);
  tmc26xStepper.setRandomOffTime(0);
  tmc26xStepper.setMicrosteps(16);
  tmc26xStepper.setStallGuardThreshold(2, 0);
  tmc26xStepper.setSpeed(400);
  tmc26xStepper.start();
  moveNSteps(tmc26xStepper, 1000);
  moveNSteps(tmc26xStepper, -1000);
}
void loop() {
}

void moveNSteps(TMC26XStepper stepper, int steps) {
  int microSteps = 16;
  stepper.step(steps * microSteps);
  while (stepper.getStepsLeft() != 0 && stepper.isMoving()) {
    stepper.move();
  }
}

这也编译但不转动电机:

This also compiles but does not turn the motor:

#include <SPI.h>
#include <TMC26XStepper.h>
TMC26XStepper *tmc26xStepper
void setup() {
  tmc26XStepper= new TMC26XStepper(200, 2, 6, 7, 800);
  Serial.begin(9600);
  tmc26xStepper->setSpreadCycleChopper(0, 24, 8, 6, 0);
  tmc26xStepper->setRandomOffTime(0);
  tmc26xStepper->setMicrosteps(16);
  tmc26xStepper->setStallGuardThreshold(2, 0);
  tmc26xStepper->setSpeed(400);
  tmc26xStepper->start();
  moveNSteps(tmc26xStepper, 1000);
  moveNSteps(tmc26xStepper, -1000);
}
void loop() {
}
void moveNSteps(TMC26XStepper* stepper, int steps) {
  int microSteps = 16;
  stepper->step(steps * microSteps);
  while (stepper->getStepsLeft() != 0 && stepper->isMoving()) {
    stepper->move();
  }
}

使用的硬件

Arduino uno

Arduino uno

TOS100 步进屏蔽

屏蔽上的 tmc260a 芯片用户手册

推荐答案

有趣的问题.. 即使在第二种情况下 stepper 是 setup 内部的局部变量,考虑到变量(根据我有限的知识).指针也是一样..

Interesting question.. even if in the second case the stepper is a local variable inside setup, calling the function from inside the setup should work considering the scope of the variables (according to my limited knowledges). The same for the pointer..

我使用模拟器做了一些尝试,因为我现在没有 Arduino 和步进器/伺服器.这 3 个案例在 wokwi Arduino 模拟器 上对我有用,但我不确定模拟器真正代表了真实的 Arduino 行为.这是我使用指针模拟的示例代码:

I did some tries using the simulator as I do not have an Arduino and a stepper/servo here right now. The 3 cases worked for me on wokwi Arduino simulator, but I am not sure if the simulator really represents the real Arduino behavior. Here is the example code I simulated using pointers:

#include <Servo.h>
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  Servo * myservo = new Servo();  // create servo object to control a servo
  myservo->attach(9);  // attaches the servo on pin 9 to the servo object
  move(myservo);
}

void loop() {
  delay(50);
}

void move(Servo * myfbservo){
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myfbservo->write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

这篇关于类实例化仅适用于全局但不适用于设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆