Notifications
Clear all

Acceleration with accelstepper

8 Posts
2 Users
0 Likes
570 Views
(@tensecondsdown)
Member
Joined: 1 year ago
Posts: 19
Topic starter  

Hi, I have nema 23 on my mill and I am controlling it with a 4 way joystick. 2 of the positions move the motor at a speed set by a rotary encoder. 2 positions move the motor at a set higher speed. Everything works fine except for the high speed. The motor skips step because the code uses a function in accelstepper that does not use acceleration. I did not write the code myself, I found it online. I have tried other functions but they block. I know I am missing something, would somebody be willing to help. Here is my code:

Β 


   
Quote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 

@tensecondsdown

I notice that you still have lcd.init() called twice πŸ™‚

The sketch seems unnecessarily tangled to me, but that may be because I'm not fully understanding what it's supposed to be doing. Please let me know where my thinking is incorrect ...

loop() calls checkButtons() whose only purpose is to check the switches to see if we're moving ONE SINGLE STEP forward or backward and sets the target position accordingly.

If the button was the 'normal' speed button, then we move the single step in the desired direction at the speed currently set by the rotary encoder by calling runTheMotor();

If the button was the 'special' speed button then we move the single step in the desired direction at speed 1000 by calling runTheMotorFast().

Both runSpeed... modules start a while loop (which continues until the current location matches the target location, which is one step ahead or behind the current location) that resets the stepper step speed accordingly and then calls runSpeedToPosition (which, incidentally keeps running until the current position matches the target position independently of the while loop it's nested inside).

So, it seems to me that

1) putting that setSpeed inside the while loop is a total waste because it's only going to take a single step anyway. The speed may change if the user is twirling the rotary encoder while pressing the button but a speed cane of 1 or 2 (or even 5) steps/second won't be noticeable unless the stepper isn't turning at all.

2) since it's only taking a single step, calling runSpeed to position is unnecessary, just call stepper.run(), which will take the single step without the overhead of the runSpeedToPosition call.

3) the while is totally pointless here, the while and runSpeedToPosition only seem to chew up processing time.

4) Speaking of chewing up time, there is a tiny overhead in calling the checkButtons, runTheMotor and runTheMotorFast buttons, so they are also costing you some Β execution time.

5) It seems that you're not really using much of the functionality of the AccelStepper class and you might be better off running the stepper directly from your code.

I realize that you didn't write this code. I'm not sure I understand exactly what it's supposed to be doing, so first we have to come to some common ground about how you think it's supposed to work. If I'm correct in my speculation above, then I believe that the code can be simplified to a large extent, maybe enough to overcome your current problem.

Which leads me to my last part, can you explain better what you mean by "The motor skips step" ? Does that mean that it doesn't move, doesn't move faster or makes aΒ click/thumpy noise ?

Please let me know how I did with interpreting your requirements.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@tensecondsdown)
Member
Joined: 1 year ago
Posts: 19
Topic starter  

All I want is for the motor to act like a regular motor in that it moves my mill table back and forth so I am not constantly cranking the wheel by hand and to display the speed in inches per minute. I don't need it to move any number of steps or anything like that. I am using a stepper because I have a module that I use to cut gears and it requires a stepper motor so I am simply killing two birds with one stone. I would love to control the stepper just through the code and not use accelstepper but I do not know how to code for ramping up the speed so I does not skip steps. What I mean by that is it sounds like its grinding when first starting up and when I give it a push, it starts to turn normally. I wish I knew how to code better, which is why I have to use code that someone else has written that is usually not specific to what I need it for and try and edit it to my purpose. There are a bunch of people on youtube showing off their version of a stepper on their mill x-axis but for some reason they never share their code and when someone asks for it they either don't reply or make excuses.

This post was modified 7 months ago 3 times by tensecondsdown

   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 

@tensecondsdownΒ 

Sounds like you're trying to change speed too quickly and the NEMA can't manage enough torque to accomplish it.

Try setting your initial speed to something lower like like 50 or 100 to make it easier for the stepper; change the speed up/down by 5 or 10 per click on the encoder so that you get to your desired speed faster; and set acceleration to 25 or 50 so that you get from current to desired speed faster.

If that works better, then try experimenting with higher starting speed and accelerations so that you an get up/down to speed quicker.

Β 

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@tensecondsdown)
Member
Joined: 1 year ago
Posts: 19
Topic starter  

The acceleration isn't working, that the problem I am having with the code. RunSpeedToPosition is the only function that does not block but doesnt use acceleration. When I substitute it with a call that uses acceleration, when I move the joystick over, the motor starts turning and keeps turning even after the joystick is off and I have to turn the power off to get it to stop.

This post was modified 7 months ago 2 times by tensecondsdown

   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 

@tensecondsdownΒ 

You have set acceleration to 2 steps/sec/sec, which is essentially turning it off.

Substitute run() for runSpeedToPosition().

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@tensecondsdown)
Member
Joined: 1 year ago
Posts: 19
Topic starter  

I tried Run() and it just takes off and doesnt stop until i turn off the power. The acceleration was originally at 2000 but it wasnt working and i kept coming down by 500 but it still wasnt working but I wasnt sure so i put it to 2 to make sure.

This post was modified 7 months ago 2 times by tensecondsdown

   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 

@tensecondsdownΒ 

An acceleration of 2000 is far too high !

If the stepper keeps turning then it suggests that the stepper never gets to the case stepper.distanceToGo()==0 which terminates the while loop or the function is being called repeatedly regardless of the button state.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote