Go Down

### Topic: AccelStepper Library-Copy-Problem (Read 625 times)previous topic - next topic

#### RWTH_MASCHI

##### Dec 15, 2017, 11:33 pm
hey guys its me again-

i kinda wrote my own AccelStepper Lib cuz i wanna change things up and add some funktions for later projects.

Accelstepper is based on basic math and there are a lot of things i dont need in my case.

Im only using stepperdrivers so I left out a lot of things.

now the problem: my motor is running, yet its weirdly ticking. its the same code as in Accelstepper so it should work   yet it doesnt run as smooth as it does in accelstepper and the question is why..

any advice?`

heres the code:

Code: [Select]
`#include "StepperControl.h"void StepperControl::moveAbs( long absolute) {  _targetPos = absolute;  computeNewSpeed();}void StepperControl::moveRel(long relative) {  moveAbs(_currentPos + relative);}boolean StepperControl::runGeschw() {  unsigned long zeit = millis();  if (zeit > _lastStepTime + _stepInterval)  {    if ( _speed > 0 )    {      _currentPos += 1;    }    else if (  _speed < 0 )    {      _currentPos -= 1;    }    schritt(_currentPos & 0x3);    _lastStepTime = zeit;    return true;  }  else    return false;}long StepperControl::distToGo() {  return _targetPos - _currentPos;}long StepperControl::targetPos() {  return _targetPos;}long StepperControl::currentPos() {  return _currentPos;}void StepperControl::setCurrentPos(long _position) {  _currentPos = _position;}void StepperControl::computeNewSpeed() {  setGeschw(desiredSpeed());}float StepperControl::desiredSpeed() {  long distanceTo = distToGo();  float requiredSpeed;  if (distanceTo == 0) return 0.0;  else if (distanceTo > 0) requiredSpeed = sqrt(2.0 * distanceTo * _acceleration);  else requiredSpeed = -sqrt(2.0 * -distanceTo * _acceleration);  if (requiredSpeed > _speed)  {    if (_speed == 0) requiredSpeed = sqrt(2.0 * _acceleration);    else requiredSpeed = _speed + abs(_acceleration / _speed);    if (requiredSpeed > _maxSpeed) requiredSpeed = _maxSpeed;  }  else if (requiredSpeed < _speed) {    if (_speed == 0) requiredSpeed = -sqrt(2.0 * _acceleration);    else requiredSpeed = _speed - abs(_acceleration / _speed);    if (requiredSpeed < -_maxSpeed) requiredSpeed = -_maxSpeed;  }  return requiredSpeed;}boolean StepperControl::go() {  if (_targetPos == _currentPos) return false;  if (runGeschw()) {    computeNewSpeed();    return true;  }}StepperControl::StepperControl(uint8_t pin_1, uint8_t pin_2) {  _pins=1;  _pin1 = pin_1;  _pin2 = pin_2;  _currentPos = 0;  _targetPos = 0;  _speed = 0;  _maxSpeed = 1;  _acceleration = 1;  _stepInterval = 0;  _lastStepTime = 0;}void StepperControl::setMaxGeschw(float geschw) {  _maxSpeed = geschw;  computeNewSpeed();}void StepperControl::setBeschl(float acceleration) {  _acceleration = acceleration;  computeNewSpeed();}void StepperControl::setGeschw( float geschw) {  _speed = geschw;  _stepInterval = abs(1000.0 / _speed);}float StepperControl::getGeschw() {  return _speed;}void StepperControl::schritt(uint8_t schritt){  switch (_pins)  {    case 0:      //step0();      break;    case 1:      schritt1(schritt);      break;    case 2:      //step2(schritt);      break;    case 4:     // step4(schritt);      break;  }}void StepperControl::schritt1(uint8_t schritt) {  digitalWrite(_pin2, _speed > 0);  digitalWrite(_pin1, HIGH);  delayMicroseconds(1);  digitalWrite(_pin1, LOW);}`

#### RWTH_MASCHI

#1
##### Dec 15, 2017, 11:39 pm
Or is there a way I can add my funktions to the AccelStepperLibrary directly?

its just weird it doesnt work. all of the methods I didnt copy arent called i checked that and yet it doesnt work the same. its just weird

thanks a lot for ur help

#### pert

#2
##### Dec 16, 2017, 01:26 am
Sounds like the problem was that the code was in fact not the same:
okay I solved the problem. the AccelStepperLibrary code posted in GitHub is vastly different from the actual one and just appears to not work. when i get the one from the actual used library it works.
sad that he just posted the wrong one for no reason, it annoyed the heck out of me!
Would you mind posting a link to the working and the non-working versions of the library to help out anyone else who might encounter the same problem?

#### Robin2

#3
##### Dec 16, 2017, 10:39 am
any advice?`

It seems a bit much to expect someone to take the time to figure out the fine details of the AccelStepper library and then figure out the fine details of your program so they can tell you where you have gone wrong.

You haven't even told us what you want done that is not provided for in the AccelStepper library?

It may be simpler just to write your own code from the ground up.

...R
Stepper Motor Basics
Simple Stepper Code
Simple acceleration suggestion
Two or three hours spent thinking and reading documentation solves most programming problems.

#### pert

#4
##### Dec 16, 2017, 11:06 am
In @RWTH_MASCHI's other thread (http://forum.arduino.cc/index.php?topic=517331), where I got the quote in my last reply, they explained that they had found the problem was that they were using two different versions of the library, thus the different behavior.

#### RWTH_MASCHI

#5
##### Dec 16, 2017, 11:39 am
I guess its even a bit much to explain what i want to implement. its a PID to control missbehaviour and missed steps of my inverse Kinematics for 6 axis robot ( im studying engineering )

its done with some AMS AS5048a encoders etc. its all working fine now.

the wrong code is this: https://github.com/adafruit/AccelStepper/blob/master/AccelStepper.cpp

the correct one is the one i got directly out of the arduino/library folder
i dont know if its preinstalled or u have to install it, yet its not the one from github

greetings

#### pert

#6
##### Dec 16, 2017, 11:57 am
The Arduino IDE doesn't come with AccelStepper library and I'm not aware that any of the hardware packages do either. The adafruit repository says "A small fork of AccelStepper v1.3" but they're not kind enough to put a link to the parent library in the readme. From looking at the comments in the source I found this link:
http://www.airspayce.com/mikem/arduino/AccelStepper/
If you look at the changelog on that page you'll see the version is currently at 1.57 so the Adafruit fork is years out of date.

I think it's actually quite likely that you used Library Manager (Sketch > Include Library > Manage Libraries) to install the library, which gets you yet another fork!
https://github.com/waspinator/AccelStepper
That repository says that it follows the upstream version but has been modified to Arduino library format and the version shown is 1.57 so it appears to be up to date with the parent repo.

#### Robin2

#7
##### Dec 16, 2017, 01:01 pm
In @RWTH_MASCHI's other thread (http://forum.arduino.cc/index.php?topic=517331), where I got the quote in my last reply, they explained that they had found the problem was that they were using two different versions of the library, thus the different behavior.
That's why it is a real PITA when there are multiple Threads about the same project.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

#### RWTH_MASCHI

#8
##### Dec 16, 2017, 01:06 pm
Version 1.57 is the working one yes..

Go Up