Thanks Thanks:  0
Needs Pictures Needs Pictures:  0
Picture(s) thanks Picture(s) thanks:  0
Page 9 of 11 FirstFirst ... 4567891011 LastLast
Results 121 to 135 of 163
  1. #121
    Join Date
    Nov 2006
    Location
    Heidelberg, Victoria
    Age
    79
    Posts
    2,251

    Default Camera slider update

    Hi foob,

    burning the midnight oil, eh?

    At least the stepper now responds to the fwd and rev push buttons by ramping up then down in both directions.

    Pressing either limit switch or stop button produces some strange effects. The motor does a bit of a jiggle dance, forward and reverse then stops.

    The other thing I've noticed is that if the fwd or rev button is depressed, and held down, the motor rotates very, very, slowly until the button is released.

    I hope this project is not keeping you awake at night, too much!

    Ken

  2. # ADS
    Google Adsense Advertisement
    Join Date
    Always
    Location
    Advertising world
    Age
    2010
    Posts
    Many





     
  3. #122
    Join Date
    Jun 2010
    Location
    Canberra
    Posts
    769

    Default

    Quote Originally Posted by neksmerj View Post
    Pressing either limit switch or stop button produces some strange effects. The motor does a bit of a jiggle dance, forward and reverse then stops.
    That, I expect, is due to this bit of code:

    Code:
    if (dir==2)  // stop button hit
      {
        stepper1.moveTo(stepper1.currentPosition());
        motor1->release();
        dir=0;
      }
    I expect setting the "moveTo" value to the current position probably doesn't do exactly what you might expect.

    The other thing I've noticed is that if the fwd or rev button is depressed, and held down, the motor rotates very, very, slowly until the button is released.
    That's because Foob hasn't implemented a state machine, so if you hold down a direction key, on each iteration of the main loop the code does this:
    Code:
    if (dir==1)
      {
        newpos=stepper1.currentPosition() + 500;
        stepper1.moveTo(newpos);
        Serial.print("Updating position to: ");
        Serial.println(newpos);
      }
    Then lets the stepper run *very* briefly with this:
    Code:
    stepper1.run();
    Before looping around and adding another 500 to the position setting and so on.

    A state machine (which Foob implemented in an earlier version) would "remember" that a movement routine had been initiated and thus would ignore the same direction button being pressed down on subsequent checks.

  4. #123
    Join Date
    Nov 2008
    Location
    somewhere
    Posts
    152

    Default

    yup, thats basically it. this iteration of code was just to get us to the point where we had the motor working reliably, with some basic button stuff added on. We'll move forward from here, but not tonight because I'm feeling snoozy.

  5. #124
    Join Date
    Nov 2006
    Location
    Heidelberg, Victoria
    Age
    79
    Posts
    2,251

    Default Camera slider update

    Rusty,

    PM sent.

    Ken

  6. #125
    Join Date
    Nov 2008
    Location
    somewhere
    Posts
    152

    Default

    Hi guys,

    I'm about at the point where I'm ready to put the distance and speed pots back into the program, but I've run into a little bug that has been driving me a little nuts and I'm not sure whats causing it. The bug is that I'm winding up with a 30 second delay before the motor moves to its next position. not sure why. I'm going to take a break and let my head settle, before I come back and try and figure out why.

    In the meantime if you guys want to play with it, feel free.

    *EDIT* damn it, always the way. two seconds after you post and you figure it out. bug fixed, but I'm still taking a break. Ken if I can be stuffed I'll get the distance / speed pots set up tonight. Now that I've learnt there is a map function built into arduino it'll be easier to understand than last time. Oh, and code below has been fixed too, though i'd recommend setting MAX_IDLE to about 1000

    Code:
    /* Ken's Camera Slider 
    ** -------------------------------------------------------------------------------------------
    ** Overview:
    **   Uses a stepper motor to move a camera along a slider, stopping at regular distance 
    **   intervals.
    **   When the movement stops the camera should take a number of photographs, triggered by pin
    **   13 on the arduino going high for a brief pulse.
    **   Direction of the stepper movement is controlled by 2 buttons, Left and Right (or forwards,
    **   backwards).
    **   The stepper motor can be stopped at any time by hitting a stop button, or by one of the
    **   limit switches triggering.  Movement will not continue until a direction button is hit
    **   and the corresponding limit switch is not active.
    **   Distance for each interval is controlled by a potentiometer, as is speed of rotation.
    **
    ** Hardware:
    **   Stepper Motor is connected to an Adafruit Motorshield V2, on motor connector 2
    **   Camera trigger is connected to Digital output 13
    **   All switches use the internal pullup and are connected between the corresponding pin and
    **   ground
              Switch        Digital Pin
              Left      -    2
              Right     -    4
              Stop      -    3
              Left Limit-    5
              Right Limit-   6
    **
    **  Required Libraries:
    **  Adafruit MotorShield V2  src:
    **  AccelStepper             src:
    */
    
    #include <Wire.h>
    #include <Adafruit_MotorShield.h>
    #include <AccelStepper.h>
    
    // Definitions:
    #define LEFT_PIN       2            //  swap this with the RIGHT_PIN value if direction of rotation is not as intended
    #define RIGHT_PIN      4
    #define STOP_PIN       3
    #define LEFT_LIM_PIN   5
    #define RIGHT_LIM_PIN  6
    
    #define SPEED_PIN     A0
    #define DISTANCE_PIN  A1
    #define MAX_DISTANCE 2000
    #define MAX_SPEED    250
    #define MAX_ACCEL    150
    
    #define CAMERA_PIN    13
    #define CAMERA_PULSE 100
    #define MAX_SHOTS      2        // define the maximum number of shots to be taken while stopped
    
    #define STEPS_PER_REV 200
    #define STEP_TYPE SINGLE         // you can change this to DOUBLE, SINGLE, INTERLEAVE or MICROSTEP to control the step type
    
    #define MAX_IDLE    500
    
    // Global Variables:
    Adafruit_MotorShield AFMS=Adafruit_MotorShield();  // access to the motor shield using the default I2C address
    
    Adafruit_StepperMotor *motor1=AFMS.getStepper(STEPS_PER_REV,2); // connect to a stepper motor
    AccelStepper stepper1(forwardstep,backwardstep);  // use functions to step
    
    // setup function just configures the hardware to be operating the way we want it to operate
    void setup()
    {
      Serial.begin(9600);          // serial library will only be used for debug purposes - can safely be removed later
      Serial.println("Ken's Camera Slider");
      
      AFMS.begin();                // connect to the motorshield using the default 1.6kHz frequency
            
      stepper1.setMaxSpeed(MAX_SPEED); // set default speed and acceleration for the accelstepper library
      stepper1.setAcceleration(MAX_ACCEL);
      
      pinMode(LEFT_PIN,INPUT_PULLUP); // set up the input pins
      pinMode(STOP_PIN,INPUT_PULLUP);
      pinMode(RIGHT_PIN,INPUT_PULLUP);
      pinMode(LEFT_LIM_PIN,INPUT_PULLUP);
      pinMode(RIGHT_LIM_PIN,INPUT_PULLUP);
      pinMode(CAMERA_PIN,OUTPUT);    // and the output pin
    }
    
    void loop()
    {
      static int cnt=MAX_IDLE;
      static char shotstaken=0;
      static char dir=0;
      static char lastdirtaken=0;
      
      cnt++;
      if (stepper1.distanceToGo()==0)          // check to see if we are sitting around doing nothing
      {
        motor1->release();                     // yep, make sure the motor is not drawing current
        if (cnt>MAX_IDLE)                          // output debug information, if its been long enough
        {
            Serial.print("Waiting on input - ");
            Serial.print("Current position: ");
            Serial.print(stepper1.currentPosition());
            Serial.print(" last dir: ");
            Serial.println((long)lastdirtaken);
            cnt=0;
            
            shotstaken=TrigCamera(shotstaken);    // take photos, until we have taken the max / stop
            if (shotstaken>=MAX_SHOTS)
            {
              dir=UpdateDistance(lastdirtaken);            // we have been idling long enough and can move on.
              if (dir!=0)
                shotstaken=0;
            }
        }
        
        dir=CheckInputs(dir);
        if (dir!=0)
        {
          lastdirtaken=UpdateDistance(dir);
          shotstaken=0;
          cnt=0;
        }
      }
      if (CheckStopEvent())                            // a stop command has happened, either via the stop buttons or the limits
      {
        if (cnt>MAX_IDLE)
        {
          Serial.println("Stop event occurred");
          cnt=0;
        }
        motor1->release();
        stepper1.moveTo(stepper1.currentPosition());
        lastdirtaken=0;
        dir=0;
      }
      else
        stepper1.run();
    
      if (cnt>MAX_IDLE)
          cnt=0;
    }
    
    
    // This function checks to see if we've taken enough shots yet. If we haven't then the camera pin is set HIGH for
    // CAMERA_PULSE number of milliseconds before being set low again.
    // TODO: Change the behaviour here to do whatever your camera requires to trigger it.
    char TrigCamera(char shots)
    {
      if (shots>=MAX_SHOTS)
        return MAX_SHOTS;
      
      shots++;
      digitalWrite(CAMERA_PIN,HIGH);
      delay(CAMERA_PULSE);
      digitalWrite(CAMERA_PIN,LOW);
      
      Serial.println("shot taken");
      return shots;
    }
    
    char Debounce(char pin,char last)
    {
      char current=digitalRead(pin);
      if (last!=current)
      {
        delay(1);
        current=digitalRead(pin);
      }
      return current;
    }
    
    // this function checks our inputs and returns a number indicating if the motor will turn LEFT (1), RIGHT (-1)
    // or if it should stop (2).  0 will indicate no input was received
    char CheckInputs(char dir)
    {
      static char lleft=1;
      static char lright=1;
      char left,right;
    
      left=Debounce(LEFT_PIN,lleft);
      right=Debounce(RIGHT_PIN,lright);
      
      if (left==0 && digitalRead(LEFT_LIM_PIN)==1)
          dir=1;
      else if (right==0 && digitalRead(RIGHT_LIM_PIN)==1)
          dir=-1;
      else dir=0;
      
      lleft=digitalRead(LEFT_PIN);
      lright=digitalRead(RIGHT_PIN);
    
      return dir;
    }
    
    char CheckStopEvent()
    {
      if (digitalRead(STOP_PIN)==0 || digitalRead(RIGHT_LIM_PIN)==0 || digitalRead(STOP_PIN)==0)
        return 1;
      return 0;
    }
    
    char UpdateDistance(char dir)
    {
      long newpos;
      
      newpos=stepper1.currentPosition() + (500*dir);
      stepper1.moveTo(newpos);
      Serial.print("Updating position to: ");
      Serial.println(newpos);
      return dir;
    }
    
    // this function will be called by the AccelStepper every step the motor turns forward
    void forwardstep()
    {
        motor1->onestep(FORWARD, STEP_TYPE);
    }
    
    // this function will be called by the AccelStepper every step the motor turns backward
    void backwardstep()
    {
        motor1->onestep(BACKWARD, STEP_TYPE);
    }
    Ken this should be getting pretty close to what you want. Sits and does nothing until a movement switch is hit, then moves, pauses for a bit, takes two shots, moves on, etc until a limit switch or stop is hit.

  7. #126
    Join Date
    Nov 2006
    Location
    Heidelberg, Victoria
    Age
    79
    Posts
    2,251

    Default Camera slider update

    Hi foob, wow, well done.

    It's almost perfect except for the distance control.

    One little thing, with the motor rotating and a limit switch or stop button is pressed, the motor does not stop dead but carries on and backs up a bit.

    If the stop button is held down, the motor stops dead. When released, the motor behaves as above.

    Foob, you have spent an awful amount of time on my project, for which I'm extremely great-full.

    I can only hope that you love writing code.

    Ken

    edit: the speed control pot has no effect on the speed, however, I don't think it really matters

  8. #127
    Join Date
    Nov 2006
    Location
    Heidelberg, Victoria
    Age
    79
    Posts
    2,251

    Default Camera slider update

    Hi foob,

    Would you be kind enough to check the action of your limit switches.

    Depressing my fwd limit switch, connected to D5, does not stop the motor. I know the limit switch works, it checks out with my digital probe.

    The other limit switch, connected to D6, does work.

    Also let's assume the fwd limit switch is hit and stays depressed, as will be the case when struck by the carriage. Pressing the rev push button should drive the motor in the opposite direction, but it does not.

    Ken

  9. #128
    Join Date
    Nov 2008
    Location
    somewhere
    Posts
    152

    Default

    Hi Ken,

    I'll check it out tonight when I get home. I only actually checked the stop switch was working properly.

    With regard to the strange action when the stop button is hit, I'm not seeing that behaviour at all. Its a dead stop for me. Rusty if you receive your bits would you be able to check that out and advise what the behaviour is your end? it might be something different about my stepper or something.

  10. #129
    Join Date
    Jun 2010
    Location
    Canberra
    Posts
    769

    Default

    I thought the stepper1.moveTo(stepper1.currentPosition()) might be causing the slight wobble, but was hoping to try it out myself on hardware. However for some odd reason it hasn't turned up yet. I'm planning to wait for today's parcel deliveries and then head off to the Post Office in case it's been waiting there.

  11. #130
    Join Date
    Jun 2010
    Location
    Canberra
    Posts
    769

    Default

    Ken's board just arrived in the post

    I'll have a whack at it later this evening.

  12. #131
    Join Date
    Nov 2008
    Location
    somewhere
    Posts
    152

    Default

    sweet.

    Minor update to the code, which fixs the limit switch problem. Turned out to be a typo (was checking STOP_PIN twice). right limit switch worked, but not the left.

    Yeah, you could be right about that, but its almost exactly the way the stop() function in the accelstepper library works. The only difference is the stop() function will actually slow down before stopping. I'll try changing that code over to the stop() later on. Your welcome to try it first (its just a straight swap of the line stepper1.moveTo(stepper1.currentPosition()) to stepper1.stop())

    anyway, limits fixed, the slight movement after the stop button is hit still isn't.
    Code:
    /* Ken's Camera Slider 
    ** -------------------------------------------------------------------------------------------
    ** Overview:
    **   Uses a stepper motor to move a camera along a slider, stopping at regular distance 
    **   intervals.
    **   When the movement stops the camera should take a number of photographs, triggered by pin
    **   13 on the arduino going high for a brief pulse.
    **   Direction of the stepper movement is controlled by 2 buttons, Left and Right (or forwards,
    **   backwards).
    **   The stepper motor can be stopped at any time by hitting a stop button, or by one of the
    **   limit switches triggering.  Movement will not continue until a direction button is hit
    **   and the corresponding limit switch is not active.
    **   Distance for each interval is controlled by a potentiometer, as is speed of rotation.
    **
    ** Hardware:
    **   Stepper Motor is connected to an Adafruit Motorshield V2, on motor connector 2
    **   Camera trigger is connected to Digital output 13
    **   All switches use the internal pullup and are connected between the corresponding pin and
    **   ground
              Switch        Digital Pin
              Left      -    2
              Right     -    4
              Stop      -    3
              Left Limit-    5
              Right Limit-   6
    **
    **  Required Libraries:
    **  Adafruit MotorShield V2  src:
    **  AccelStepper             src:
    */
    
    #include <Wire.h>
    #include <Adafruit_MotorShield.h>
    #include <AccelStepper.h>
    
    // Definitions:
    #define LEFT_PIN       2            //  swap this with the RIGHT_PIN value if direction of rotation is not as intended
    #define RIGHT_PIN      4
    #define STOP_PIN       3
    #define LEFT_LIM_PIN   5
    #define RIGHT_LIM_PIN  6
    
    #define SPEED_PIN     A0
    #define DISTANCE_PIN  A1
    #define MAX_DISTANCE 2000
    #define MAX_SPEED    250
    #define MAX_ACCEL    150
    
    #define CAMERA_PIN    13
    #define CAMERA_PULSE 100
    #define MAX_SHOTS      2        // define the maximum number of shots to be taken while stopped
    
    #define STEPS_PER_REV 200
    #define STEP_TYPE SINGLE         // you can change this to DOUBLE, SINGLE, INTERLEAVE or MICROSTEP to control the step type
    
    #define MAX_IDLE    1000
    
    // Global Variables:
    Adafruit_MotorShield AFMS=Adafruit_MotorShield();  // access to the motor shield using the default I2C address
    
    Adafruit_StepperMotor *motor1=AFMS.getStepper(STEPS_PER_REV,2); // connect to a stepper motor
    AccelStepper stepper1(forwardstep,backwardstep);  // use functions to step
    
    // setup function just configures the hardware to be operating the way we want it to operate
    void setup()
    {
      Serial.begin(9600);          // serial library will only be used for debug purposes - can safely be removed later
      Serial.println("Ken's Camera Slider");
      
      AFMS.begin();                // connect to the motorshield using the default 1.6kHz frequency
            
      stepper1.setMaxSpeed(MAX_SPEED); // set default speed and acceleration for the accelstepper library
      stepper1.setAcceleration(MAX_ACCEL);
      
      pinMode(LEFT_PIN,INPUT_PULLUP); // set up the input pins
      pinMode(STOP_PIN,INPUT_PULLUP);
      pinMode(RIGHT_PIN,INPUT_PULLUP);
      pinMode(LEFT_LIM_PIN,INPUT_PULLUP);
      pinMode(RIGHT_LIM_PIN,INPUT_PULLUP);
      pinMode(CAMERA_PIN,OUTPUT);    // and the output pin
    }
    
    void loop()
    {
      static int cnt=MAX_IDLE;
      static char shotstaken=0;
      static char dir=0;
      static char lastdirtaken=0;
      
      cnt++;
      if (stepper1.distanceToGo()==0)          // check to see if we are sitting around doing nothing
      {
        motor1->release();                     // yep, make sure the motor is not drawing current
        if (cnt>MAX_IDLE)                          // output debug information, if its been long enough
        {
            Serial.print("Waiting on input - ");
            Serial.print("Current position: ");
            Serial.print(stepper1.currentPosition());
            Serial.print(" last dir: ");
            Serial.println((long)lastdirtaken);
            cnt=0;
            
            shotstaken=TrigCamera(shotstaken);    // take photos, until we have taken the max / stop
            if (shotstaken>=MAX_SHOTS)
            {
              dir=UpdateDistance(lastdirtaken);            // we have been idling long enough and can move on.
              if (dir!=0)
                shotstaken=0;
            }
        }
        
        dir=CheckInputs(dir);
        if (dir!=0)
        {
          lastdirtaken=UpdateDistance(dir);
          shotstaken=0;
          cnt=0;
        }
      }
      if (CheckStopEvent())                            // a stop command has happened, either via the stop buttons or the limits
      {
        if (cnt>MAX_IDLE)
        {
          Serial.println("Stop event occurred");
          cnt=0;
        }
        motor1->release();
        stepper1.moveTo(stepper1.currentPosition());
        lastdirtaken=0;
        dir=0;
      }
      else
        stepper1.run();
        
        if (cnt>MAX_IDLE)
          cnt=0;
    }
    
    
    // This function checks to see if we've taken enough shots yet. If we haven't then the camera pin is set HIGH for
    // CAMERA_PULSE number of milliseconds before being set low again.
    // TODO: Change the behaviour here to do whatever your camera requires to trigger it.
    char TrigCamera(char shots)
    {
      if (shots>=MAX_SHOTS)
        return MAX_SHOTS;
      
      shots++;
      digitalWrite(CAMERA_PIN,HIGH);
      delay(CAMERA_PULSE);
      digitalWrite(CAMERA_PIN,LOW);
      
      Serial.println("shot taken");
      return shots;
    }
    
    char Debounce(char pin,char last)
    {
      char current=digitalRead(pin);
      if (last!=current)
      {
        delay(1);
        current=digitalRead(pin);
      }
      return current;
    }
    
    // this function checks our inputs and returns a number indicating if the motor will turn LEFT (1), RIGHT (-1)
    // or if it should stop (2).  0 will indicate no input was received
    char CheckInputs(char dir)
    {
      static char lleft=1;
      static char lright=1;
      char left,right;
    
      left=Debounce(LEFT_PIN,lleft);
      right=Debounce(RIGHT_PIN,lright);
      
      if (left==0 && digitalRead(LEFT_LIM_PIN)==1)
          dir=1;
      else if (right==0 && digitalRead(RIGHT_LIM_PIN)==1)
          dir=-1;
      else dir=0;
      
      lleft=digitalRead(LEFT_PIN);
      lright=digitalRead(RIGHT_PIN);
    
      return dir;
    }
    
    char CheckStopEvent()
    {
      if (digitalRead(STOP_PIN)==0 || digitalRead(RIGHT_LIM_PIN)==0 || digitalRead(LEFT_LIM_PIN)==0)
        return 1;
      return 0;
    }
    
    char UpdateDistance(char dir)
    {
      long newpos;
      
      newpos=stepper1.currentPosition() + (500*dir);
      stepper1.moveTo(newpos);
      Serial.print("Updating position to: ");
      Serial.println(newpos);
      return dir;
    }
    
    // this function will be called by the AccelStepper every step the motor turns forward
    void forwardstep()
    {
        motor1->onestep(FORWARD, STEP_TYPE);
    }
    
    // this function will be called by the AccelStepper every step the motor turns backward
    void backwardstep()
    {
        motor1->onestep(BACKWARD, STEP_TYPE);
    }

  13. #132
    Join Date
    Nov 2006
    Location
    Heidelberg, Victoria
    Age
    79
    Posts
    2,251

    Default Camera slider update

    Hi foob,

    Thanks for fixing the code. I have two concerns.

    1. When the motor rotates and stops, it remains stopped for about four seconds, I reckon it need only stop for about one second.
    2. There seems a long delay, about three seconds, before the shutter fires after the motor stops, and it fires again just as the motor starts up again.

    Your efforts are much appreciated.

    Ken

  14. #133
    Join Date
    Jun 2010
    Location
    Canberra
    Posts
    769

    Default

    Quote Originally Posted by neksmerj View Post
    1. When the motor rotates and stops, it remains stopped for about four seconds, I reckon it need only stop for about one second.
    2. There seems a long delay, about three seconds, before the shutter fires after the motor stops, and it fires again just as the motor starts up again.
    Change:
    #define MAX_SHOTS 1
    #define MAX_IDLE 500

  15. #134
    Join Date
    Jun 2010
    Location
    Canberra
    Posts
    769

    Default

    So the pause before shoot time is a bit random. That's because it's using the "cnt" variable as a kind of timer, except that it increments asynchronously with the stepper turning, so the pause after stopping is variable.

    It might be better to use the millis() function and a defined delay-before-shoot period.

  16. #135
    Join Date
    Nov 2006
    Location
    Heidelberg, Victoria
    Age
    79
    Posts
    2,251

    Default Camera slider update

    Hi Rusty & foob.

    Rusty, I took your advice and all seems fine. I don't know anything about millis, a little example code might help.

    Is there any way of achieving a smooth speed up from MICROSTEP. MICROSTEP is just a tad slow, INTERLEAVE is just a tad fast, but until I actually try it out, I won't really know.

    Regards,

    Ken

    edit. disregard the step type, I'm sure once the speed control is up and running, I'll be sweet.

    edit2. I've just noticed that the camera pin still pulses even when the motor is stopped.

Page 9 of 11 FirstFirst ... 4567891011 LastLast

Similar Threads

  1. Limit/home switches
    By warrick in forum CNC Machines
    Replies: 2
    Last Post: 7th July 2012, 08:11 PM
  2. Limit switches
    By Bob Willson in forum CNC Machines
    Replies: 0
    Last Post: 24th August 2011, 12:31 PM
  3. CNC Mill Limit Switches
    By electrosteam in forum METALWORK FORUM
    Replies: 8
    Last Post: 10th September 2010, 07:31 PM
  4. How to Wire Limit switches on Xylotex board.
    By Ch4iS in forum CNC Machines
    Replies: 19
    Last Post: 19th October 2008, 09:58 PM
  5. Xylotex & limit/home switches
    By John H in forum CNC Machines
    Replies: 3
    Last Post: 31st July 2008, 08:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •