Quick target change

79 views
Skip to first unread message

GitShipRekt Online

unread,
Apr 9, 2024, 5:30:42 PMApr 9
to accelstepper
I'm basically doing a object tracking turret with the camera mounted on it. I am trying to make it follow my object but I can't seem to get the arduino code right. Best I can get using moveTo/move functions is for my robot to jump side to side from my object(trying to put the object in the center of the ball) I want it to have a smooth follow and be able to change directions but I can't get it right. any suggestions?

gregor christandl

unread,
Apr 9, 2024, 5:33:10 PMApr 9
to accels...@googlegroups.com
Hi,
can you show us some code?
the runSpeed() function may be better for your use case:
https://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html#aa4a6bdf99f698284faaeb5542b0b7514
> --
> You received this message because you are subscribed to the Google
> Groups "accelstepper" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to accelstepper...@googlegroups.com
> <mailto:accelstepper...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/accelstepper/36c6deec-3e18-451d-9dbb-78bb00093da5n%40googlegroups.com <https://groups.google.com/d/msgid/accelstepper/36c6deec-3e18-451d-9dbb-78bb00093da5n%40googlegroups.com?utm_medium=email&utm_source=footer>.
Message has been deleted
Message has been deleted

Jim Larson

unread,
Apr 10, 2024, 8:25:03 PMApr 10
to accels...@googlegroups.com
Yes, if you can post your code, I'll be glad to look at it. The
AccelStepper library works great, but sometimes using it properly isn't
obvious. I see you have posted to that forum. I'll respond there.

     -jim
--
This email has been checked for viruses by Avast antivirus software.
www.avast.com

GitShipRekt Online

unread,
Apr 10, 2024, 8:25:04 PMApr 10
to accels...@googlegroups.com
Ok so I've tried runSpeed(). but I still get the same old issue. The annoying bouncing Left RIght from the axis
image.png
void loop() {

  if (Serial.available() > 0)
  {
 
    if(Serial.read()=='X')
      nx1=Serial.parseInt();
      //double currentAngle=stp_1.currentPosition();
      error=-nx1*16;

  }
  if(error>10)
    {
      stp_1.setSpeed(-1000);
    }
  else if(error<10)
    {
      stp_1.setSpeed(1000);
    }
  else
     stp_1.stop();
  stp_1.runSpeed();
 
  //stp_2.run();
}
and this is the loop in which I'm running it



To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/accelstepper/778bfb60-fa84-4d79-b3b4-6b9e640967f8%40gmail.com.

GitShipRekt Online

unread,
Apr 10, 2024, 8:25:04 PMApr 10
to accels...@googlegroups.com
Obviously I'm expecting some jerking around but the higher the speed the higher the jumps. What I want it to be able to do is have a smooth follow which I would get at low speeds. but at high speed it all goes poopie

Ralph Hulslander

unread,
Apr 10, 2024, 10:44:27 PMApr 10
to accels...@googlegroups.com
Thanks for posting, I thought the AccellStepper forum was still blocked.

Ralph

To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/accelstepper/bee30b5a-8eba-4c79-8e3b-3ca548719b61%40pacifier.com.

Jim Larson

unread,
Apr 11, 2024, 12:38:30 AMApr 11
to accelstepper
The code for the rest of your loop and for setup and initialization seems to be missing. Could you please post your complete program? Then maybe we can help you.

                 -jim

GitShipRekt Online

unread,
Apr 11, 2024, 7:48:18 AMApr 11
to accelstepper
#include <AccelStepper.h>
#include <MultiStepper.h>

const int dirPin=5;
const int stepPin=2;
const int x1_ls=11;
const int y1_ls=10;
int x1_state;
int x1_lr=0;
int x1_lastState;
int y1_state;
int y1_lr=0;
int y1_lastState;
int ny1=0;
int nx1=0;
int sx1=0;
int sy1=0;
int val1;
int val2;
int G_x1=350;
int G_y1=250;
int e_x1=0;
int e_y1=0;
int e_lx1=0;
int vx1;

int non1=0;
int non2=0;
float last_coordinate=0;

const double Kp=0.001;
const double Ki=0.1;
const double Kd=0.0;
double integral=0;
double previousError=0;
double error=0;

AccelStepper stp_1(AccelStepper:: DRIVER, stepPin, dirPin);
AccelStepper stp_2(AccelStepper:: DRIVER, 3, 6);


void zeroing1()
{
  while(1)
  {
 
  stp_1.setMaxSpeed(10000);
  stp_1.setSpeed(100);
  if(non1==0)
  stp_1.runSpeed();
  stp_2.setMaxSpeed(10000);
  stp_2.setSpeed(100);
  if(non2==0)
  stp_2.runSpeed();
  x1_lr=digitalRead(x1_ls);
  y1_lr=digitalRead(y1_ls);
  if(x1_lr!=x1_lastState)
  {
    if(x1_lr==LOW)
  {
    stp_1.stop();
    stp_1.setCurrentPosition(0);
    non1++;
  }
  }
  x1_lastState=x1_lr;

  if(y1_lr!=y1_lastState)
  {
    if(y1_lr==LOW)
    {
    stp_2.stop();
    stp_2.setCurrentPosition(0);
    non2++;
    }
  }
  y1_lastState=y1_lr;
  if(stp_1.currentPosition()==0 && stp_2.currentPosition()==0)
  break;
   
}
}

void setup() {
  delay(10);
  pinMode(x1_ls,INPUT_PULLUP);
  pinMode(y1_ls,INPUT_PULLUP);
  Serial.begin(9600);
  //zeroing1();
  stp_1.setMaxSpeed(10000);
  stp_1.setAcceleration(10000);
  stp_2.setMaxSpeed(1000000);
  stp_2.setSpeed(1000000);
  stp_2.setAcceleration(1000000);
  stp_1.setCurrentPosition(0);
  stp_1.moveTo(-100*16);
  while(stp_1.currentPosition()!=-(100*16))
    stp_1.run();
  delay(50);
 
}

void loop() {

  if (Serial.available() > 0)
  {
 
    if(Serial.read()=='X')
      nx1=Serial.parseInt();
      //double currentAngle=stp_1.currentPosition();
      error=-nx1;

  }
  if(error>10)
    {
      stp_1.setSpeed(-1000);
    }
  else if(error<10)
    {
      stp_1.setSpeed(1000);
    }
  else
     stp_1.stop();
  stp_1.runSpeed();
 
  //stp_2.run();
}
 
This is all the code. I didn't upload it initially cuz I thought at least half of the code here is irrelevant to my issue so far. Like zeroing does not need fixing. And really the only part would be in the void loop function. As for future research I've done I came to the conclusion that for 1 thing Serial.parseInt() may be a huge problem as it is a blocking function and my teacher said that I may have to figure out a small PID control for it due to the nature of my constant correction and overshoot of positioning
 


Jim Larson

unread,
Apr 11, 2024, 7:39:56 PMApr 11
to accelstepper
What is the position sensor you are using? Does it continually calculate and report error from the ideal position? If so, you need to be able to slow it down so you can read and process the input. I suggest you only read it every 20 to 50 milliseconds. See what kind of response you get. Can you set up the sensor to only send data when prompted?

You might also try slowing the setSpeed value to 100 or less and see if the response slows. You may be driving the stepper too fast.

Finally, you may find it helpful to read the Missing Manual for the AccelStepper library. Note that you don't need to call stop() when you are using runSpeed().

Note that I am assuming that zeroing1() is never called and that setup() works the way you want it to.

HTH

                               -jim

GitShipRekt Online

unread,
Apr 12, 2024, 12:44:01 AMApr 12
to accels...@googlegroups.com
Well the sensor is a bad camera mounted on the motor. I send constant data when something is detected from a python script running on my pc hence why I  have to use Serial.parseInt(). Yes zeroing works perfectly fine despite the not needed stop as u mentioned, i comented it out since rn, while trying to fix the tracking it only hinders my ability to quick test stuff. Yes if I reduce the speed to like 100. It will stop overshooting but I'm trying to make it actually have a nice tracking. Also as prev stated I need to rid myaelf of that parseInt and use a algorithm to do it without blocking. After that I'll also attempt what u suggested to reduce the reading time.

Reply all
Reply to author
Forward
0 new messages