You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to use the motor in a way that it constantly receives new target data and that it simply tries to follow the target position. When using the StepControl class I have an easy way to move to an absolute position, but it doesn't allow for a graceful response when giving a new position while it is in the process of going somewhere. RotateControl does allow for dynamic shifts in direction with overrideSpeed(), though then I have to rebuilt the libraries ability to keep track of where it is on my side. Is there any way I could still use StepControl but allow for dynamically changing the absolute target?
This is my current test sketch, it works, but I have the feeling this could be done in a better way.
#include "Arduino.h"
#include "TeensyStep.h"
constexpr unsigned motorStpPerRev = 200 * 16; // e.g. fullstep/rev * microstepping
constexpr unsigned motorAcceleration = 15000;
float targetMotorSpeed, oldMotorSpeed;
int motorTargetPosition = 0;
int motorTargetDirection = 2; // 0 == up 1 == down 2 == none
int prevMotorPosition = 0;
constexpr unsigned maxSpeed = 1000;
RotateControl controller;
Stepper motor(4, 5);
void setup() {
Serial.begin(9600);
while(!Serial) {
;
}
motor.setMaxSpeed(1); // -> parameter of overrideSpeed equals real speed
motor.setAcceleration(motorAcceleration);
// startup controller
controller.rotateAsync(motor);
controller.overrideSpeed(0.0f);
// setMotorTarget(-4000);
}
void setMotorTarget(int t) {
motorTargetPosition = t;
if(t > motor.getPosition()) {
motorTargetDirection = 0;
} else if(t < motor.getPosition()){
motorTargetDirection = 1;
} else {
motorTargetDirection = 2; //none
}
}
void loop() {
int p = motor.getPosition();
int pp = prevMotorPosition;
int t = motorTargetPosition;
if(motorTargetDirection == 0) {
// we go up
setMotorSpeed(maxSpeed);//up
if(p > t) motorTargetDirection = 2;
} else if(motorTargetDirection == 1){
// we go down
setMotorSpeed(-maxSpeed);//down
if(p < t) motorTargetDirection = 2;
} else {
// nowhere
setMotorSpeed(0);
}
prevMotorPosition = p;
updateSpeeds();
int sinT = sin(millis()*0.0001)*9000; // send it a changing target (sine wave for testing)
setMotorTarget(sinT);
}
void setMotorSpeed(float rpm)
{
targetMotorSpeed = rpm / 60.0f * motorStpPerRev;
}
void updateSpeeds()
{
if(targetMotorSpeed != oldMotorSpeed) // if target speed changed -> update all
{
oldMotorSpeed = targetMotorSpeed;
controller.overrideSpeed(targetMotorSpeed);
}
}
inline int getCurMotorSpeed() { return controller.isRunning() ? controller.getCurrentSpeed() : 0; }
The text was updated successfully, but these errors were encountered:
You currently can not change the target while the motor is moving. but there is a simple way to do this. Here some more information and a test sketch: #58 (comment)
(You need to take the code fromthe DevTimer branch for this to work).
Edit:
...though then I have to rebuilt the libraries ability to keep track of where it is on my side. Is there any way I could still use StepControl but allow for dynamically changing the absolute target?
BTW: It keeps track of the position whichever controller you use. The current position is handled by the Stepper class not the controlller. You can also switch controllers if needed. Just make sure that you don't have 2 controllers driving the same motor at the same time...
I want to use the motor in a way that it constantly receives new target data and that it simply tries to follow the target position. When using the StepControl class I have an easy way to move to an absolute position, but it doesn't allow for a graceful response when giving a new position while it is in the process of going somewhere. RotateControl does allow for dynamic shifts in direction with overrideSpeed(), though then I have to rebuilt the libraries ability to keep track of where it is on my side. Is there any way I could still use StepControl but allow for dynamically changing the absolute target?
This is my current test sketch, it works, but I have the feeling this could be done in a better way.
The text was updated successfully, but these errors were encountered: