Go Down

Topic: Help with coding  (Read 310 times) previous topic - next topic

SewTech

First let me say that I am not a programmer as of yet. I've watched some tube and can replicate code that way, sure. But I can't wrap my head around code.

I have always been more mechanical than electronic.

With that being said, I need help to make a machine do what I want it to do. It's all pretty straight forward as it's all known "off the shelf" hardware, nothing fancy.

Think Y axis and Z axis linear rails with steppers already attached and functioning.

2 Sanyo Step-Syn motors
Mega 2560 Arduino
2 TB6600 drivers.
2 Home switch
2 limit switch
1 momentary "control switch"

At startup I would like both motors to move CW to reach their home switch and back off until switch de-activates and then hold.

At push of momentary switch Y axis moves to limit switch and holds. At second push from momentary switch (or second switch?) Z axis engages down to limit switch with adjustable delay here, and then returns to home, once home the Y axis should also return to home until loop starts over again.

I need help with the code. I was planning on using accel stepper library unless another suggested. I basically need the sketch written, I can do the pin-outs and the delays, speeds etc. once I have the bare bones of code.

Any help would be appreciated.

What info would you need to get started? Pics? datasheets? Like I stated, it's all off the shelf items. Pictures enclosed of the gantry but the z axis is not on at the moment.


PaulS

Quote
At startup I would like both motors to move CW to reach their home switch and back off until switch de-activates and then hold.
How does that differ from what your code does now?

Quote
At push of momentary switch Y axis moves to limit switch and holds.
After backing off to release the limit switch, or not? If that requirement includes backing off, I don't see how it differs from the startup requirement. So, you need to develop only one function.

Quote
At second push from momentary switch (or second switch?)
Make up your mind...

Quote
Z axis engages down to limit switch with adjustable delay here, and then returns to home
How will the delay be adjusted? Upload code again, or will there be a config file on an SD card or a potentiometer or encoder? Am I correct in understanding that the Z travel has two limit switches - one at each end?

Quote
I need help with the code. I was planning on using accel stepper library unless another suggested.
AccelStepper allows two or more steppers to move at the same time. But, it does not ensure that they stepping is coordinated. The MultiStepper library does, and it includes acceleration/deceleration.

Quote
What info would you need to get started?
A lot more than you've posted so far.

Quote
Pics?
Pics are always good.

Quote
datasheets?
Facts are always good.

Quote
Like I stated, it's all off the shelf items.
Links to the hardware are better than vague verbiage. I can, for instance, imagine that Step-Syn sells more than one motor.

Quote
Pictures enclosed of the gantry
I must have missed them, somehow.
The art of getting good answers lies in asking good questions.

luisilva

#2
Jun 21, 2018, 05:37 pm Last Edit: Jun 21, 2018, 05:38 pm by luisilva
Did you think about use one of the many versions of 3D printers firmware (one of those are Marlin)? The home function is similar to what you want. Take a look, and if you can't do anything with that send me a PM and we talk about it (I'm not a professional programmer but I think I cant help).

PaulMurrayCbr

#3
Jun 26, 2018, 04:31 am Last Edit: Jun 26, 2018, 04:45 am by PaulMurrayCbr
So, something a bit like this:

Code: [Select]

#include <AccelStepper.h>

/*
   An accelstepper with an associated home and limit pin.
*/

class LimitedStepper {
  public:
    enum State {
      GOING_HOME, GOING_LIMIT, STOPPED
    };

  private:
    AccelStepper& stepper;
    const byte homePin;
    const byte limitPin;
    const int homeSpeed;
    const int limitSpeed;
    State state;

  public:
    LimitedStepper(AccelStepper &attach_stepper,
                   byte attach_homePin,
                   byte attach_limitPin,
                   int homeSpeed,
                   int limitSpeed) :
      stepper(attach_stepper),
      homePin(attach_homePin),
      limitPin(attach_limitPin),
      homeSpeed(homeSpeed),
      limitSpeed(limitSpeed)
    {}

    void setup() {
      pinMode(homePin, INPUT_PULLUP);
      pinMode(limitPin, INPUT_PULLUP);
    }

    void run() {
      stepper.run();

      switch (state) {
        case GOING_HOME:
          if (digitalRead(homePin) == LOW) {
            stepper.setSpeed(0);
            state = STOPPED;
          }
          break;

        case GOING_LIMIT: if (digitalRead(limitPin) == LOW) {
            stepper.setSpeed(0);
            state = STOPPED;
          }
          break;

        case STOPPED:
          break;
      }
    }

    void go_home() {
      if (digitalRead(homePin) != LOW) {
        stepper.setSpeed(homeSpeed);
        state = GOING_HOME;
      }
    }

    void go_limit() {
      if (digitalRead(homePin) != LOW) {
        stepper.setSpeed(limitSpeed);
        state = GOING_LIMIT;
      }
    }

    boolean stopped() {
      return state == STOPPED;
    }
};

// this depends on the details of what steppers you have.
// see the AccelStepper library docs for details - there are a couple of options, here
AccelStepper accelsteppery; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
AccelStepper accelstepperz(AccelStepper::FULL2WIRE, 6, 7);

LimitedStepper ystepper(accelsteppery,
                        8, 9, // home and limit pins
                        50, -50 // speed to move when going to the home and limit, for example
                       );

LimitedStepper zstepper(accelstepperz,
                        10, 11, // home and limit pins
                        -20, 20 // speed to move when going to the home and limit, for example
                       );

const byte controlPin = 12;
byte controlValue;

// it might be an idea to give these meaningful names.
// if the Y stepper is a cutting arm, rename 'Y_TO_LIMIT' to
// 'EXTEND_CUTTING_ARM', or something

enum State {
  INIT,
  HOME,
  Y_TO_LIMIT,
  Y_HOLDING,
  Z_TO_LIMIT,
  Z_HOLDING,
  Z_TO_HOME,
  Y_TO_HOME
} state;

const uint32_t Z_HOLDING_ms = 3000; // three seconds z delay, for example
uint32_t z_holding_start_ms;

void setup() {
  // I don't know if the pinmodes for the stepper need to be set to output
  // none of the accelstepper examples have that, so I assume not.

  pinMode(controlPin, INPUT_PULLUP);
  controlValue = digitalRead(controlPin);

  ystepper.setup();
  zstepper.setup();

  ystepper.go_home();
  zstepper.go_home();

  state = INIT;
}

void loop() {
  byte prevControlValue = controlValue;
  controlValue = digitalRead(controlPin);
  boolean controlPressed = (prevControlValue == HIGH && controlValue == LOW);

  ystepper.run();
  zstepper.run();

  switch (state) {
    case INIT:
      if (ystepper.stopped() && zstepper.stopped()) {
        state = HOME;
      }
      break;

    case HOME:
      if (controlPressed) {
        ystepper.go_limit();
        state = Y_TO_LIMIT;
      }
      break;

    case Y_TO_LIMIT:
      if (ystepper.stopped()) {
        state = Y_HOLDING;
      }
      break;

    case Y_HOLDING:
      if (controlPressed) {
        zstepper.go_limit();
        state = Z_TO_LIMIT;
      }
      break;

    case Z_TO_LIMIT:
      if (zstepper.stopped()) {
        z_holding_start_ms = millis();
        state = Z_HOLDING;
      }
      break;

    case Z_HOLDING:
      if (millis() - z_holding_start_ms > Z_HOLDING_ms) {
        zstepper.go_home();
        state = Z_TO_HOME;
      }
      break;

    case Z_TO_HOME:
      if (zstepper.stopped()) {
        ystepper.go_home();
        state = Y_TO_HOME;
      }
      break;

    case Y_TO_HOME:
      if (ystepper.stopped()) {
        state = HOME;
      }
      break;
  }
}


You will need to install the accelstepper library.
http://paulmurraycbr.github.io/ArduinoTheOOWay.html

Go Up