Stepper Motor

Moderators: BertB, rtenklooster, Voyager, Stuntteam, Martinus

Message
Author
theju
Normal user
Posts: 4
Joined: 29 May 2016, 10:14

Stepper Motor

#1 Post by theju » 29 Nov 2016, 14:15

Is it possible to include control off a stepper motor ?

In earlier version, I use this in P001_switch :

Code: Select all

        if (command == F("stepper"))
        // Par1 = GPIO; Par2 = count ;Par3 = delay
        
        {
          success = true;
          if (event->Par1 >= 0 && event->Par1 <= 16)
          {
            int i = 0;
            for( i=1; i<event->Par2; i++){
              pinMode(event->Par1, OUTPUT);
              digitalWrite(event->Par1, 1);
              delay(event->Par3);
              digitalWrite(event->Par1, 0);
              delay(event->Par3);
            }
            
            setPinState(PLUGIN_ID_001, event->Par1, PIN_MODE_OUTPUT, 0);
            log = String(F("SW   : STEP ")) + String(event->Par2);
            addLog(LOG_LEVEL_INFO, log);
            SendStatus(event->Source, getPinStateJSON(SEARCH_PIN_STATE, PLUGIN_ID_001, event->Par1, log, 0));
          }
        }
If somebody better than me in prog can include something like this (maybe add direction ?) it would be very appreciate !

Thanks
Theju

majklovec
Normal user
Posts: 10
Joined: 12 Jul 2016, 07:47

Re: Stepper Motor

#2 Post by majklovec » 06 Dec 2016, 03:09

Code: Select all


#include <Arduino.h>

//#######################################################################################################
//############################# Plugin 106: Stepper <info@sensorio.cz####################################
//#######################################################################################################
#define PLUGIN_106
#define PLUGIN_ID_106        106
#define PLUGIN_NAME_106       "Aktuátory: Krokový motor"
#define PLUGIN_VALUENAME1_106 ""

#include <AccelStepper.h>
int max_stepper_speed = 1000000;
int stepper_speed = 40000;

int stepper_accel = 15000;
int timelapse_mins = 60;
long init_pos = 0;
long current_pos = 0;
long steps = 2000;
int sleepTime = 10; // sec
bool accel_enabled = true;
bool isStop = false;
int enable_pin = 12;
long actualTime;

AccelStepper *stepper;

boolean Plugin_106(byte function, struct EventStruct *event, String& string)
{
  boolean success = false;

  switch (function)
  {

    case PLUGIN_DEVICE_ADD:
      {
        Device[++deviceCount].Number = PLUGIN_ID_106;

        Device[deviceCount].Type = DEVICE_TYPE_TRIPLE;
        Device[deviceCount].VType = SENSOR_TYPE_LONG;
        Device[deviceCount].Ports = 0;
        Device[deviceCount].PullUpOption = false;
        Device[deviceCount].InverseLogicOption = false;
        Device[deviceCount].FormulaOption = false;
        Device[deviceCount].ValueCount = 1;
        Device[deviceCount].SendDataOption = true;
        Device[deviceCount].TimerOption = false;
        Device[deviceCount].GlobalSyncOption = true;

        break;
      }

    case PLUGIN_GET_DEVICENAME:
      {
        string = F(PLUGIN_NAME_106);
        break;
      }

    case PLUGIN_GET_DEVICEVALUENAMES:
      {
        strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_106));
        break;
      }
    case PLUGIN_WEBFORM_LOAD:
      {
        char tmpString[128];
        sprintf_P(tmpString, PSTR("<TR><TD>not ENABLE:<TD><input type='text' name='taskdevicepin1' value='%u'>"), Settings.TaskDevicePin1[event->TaskIndex]);
        string += tmpString;
        sprintf_P(tmpString, PSTR("<TR><TD>STEP:<TD><input type='text' name='taskdevicepin2' value='%u'>"), Settings.TaskDevicePin2[event->TaskIndex]);
        string += tmpString;
        sprintf_P(tmpString, PSTR("<TR><TD>DIR:<TD><input type='text' name='taskdevicepin3' value='%u'>"), Settings.TaskDevicePin3[event->TaskIndex]);
        string += tmpString;
        success = true;
        break;
      }

    case PLUGIN_TEN_PER_SECOND:
      {

        if (stepper) {
          if (isStop) {
            stepper->stop();
            //Serial.println("stopped");
          }
          else if (!isStop && stepper->distanceToGo() != 0) {
            //Serial.println("running");
            if (!accel_enabled)
              stepper->runSpeed();
            else if (accel_enabled)
              stepper->run();

            actualTime = millis();

            UserVar[event->BaseVarIndex] = stepper->currentPosition();
          }
        }

        if (millis() - actualTime > sleepTime * 1000) {
          disableStepper();
        }


        success = true;
        break;
      }

    case PLUGIN_INIT:
      {
        LoadTaskSettings(event->TaskIndex);

        Serial.print("pin1: ");
        Serial.print(Settings.TaskDevicePin1[event->TaskIndex]);
        Serial.print(", pin2: ");
        Serial.println(Settings.TaskDevicePin2[event->TaskIndex]);
        Serial.print(", pin3: ");
        Serial.println(Settings.TaskDevicePin3[event->TaskIndex]);

        enable_pin = Settings.TaskDevicePin1[event->TaskIndex];

        if (Settings.TaskDevicePin3[event->TaskIndex] && Settings.TaskDevicePin2[event->TaskIndex]) {
          stepper = new AccelStepper(1, Settings.TaskDevicePin2[event->TaskIndex], Settings.TaskDevicePin3[event->TaskIndex]);
          stepper->setMaxSpeed(max_stepper_speed);
          stepper->setSpeed(stepper_speed);
          stepper->setAcceleration(stepper_accel);
          stepper->setCurrentPosition(stepper->currentPosition ());
          Serial.println("current_position");
          Serial.println(stepper->currentPosition());
          success = true;
        }
        break;
      }
    case PLUGIN_WRITE:
      {
        String command  = string;
        int value = -1;

        int argIndex = command.indexOf(',');
        if (argIndex) {
          command = command.substring(0, argIndex);

          processGetRequest(command, event->Par1);
        }

        success = true;
        break;
      }

    case PLUGIN_READ:
      {
        success = true;
        break;
      }

  }
  return success;
}

void moveSlider(String direction) {
  enableStepper();
  isStop = false;
  if (direction.equals("left")) {
    Serial.println("moving left");
    //stepper->setSpeed(speed);
    stepper->setSpeed(-stepper_speed);
    stepper->move(-steps);
  }
  else if (direction.equals("right")) {
    long start_pos = stepper->currentPosition();
    Serial.println("moving right");
    stepper->setSpeed(stepper_speed);
    stepper->move(steps);
  }

  else if (direction.equals("reset")) {
    Serial.println("reset to 0");
    stepper->setSpeed(-stepper_speed);
    stepper->move(-stepper->currentPosition());
  }
}
void changeSpeed(float mot_speed) {
  stepper_speed = mot_speed;
  stepper->setAcceleration(mot_speed / 4);
  Serial.println(stepper_speed);
}

void timelapse(String direction) {
  float timelapse_speed =  36.0f / (timelapse_mins / 60.0f);
  Serial.println(timelapse_speed);
  if (timelapse_speed > max_stepper_speed)
    timelapse_speed = max_stepper_speed;

  isStop = false;
  enableStepper();
  if (direction.equals("left")) {
    stepper->setSpeed(timelapse_speed);
    stepper->move(steps);
  }

  else if (direction.equals("right")) {
    stepper->setSpeed(-timelapse_speed);
    stepper->move(-steps);
  }
}
void processGetRequest(String name, int value) {
  Serial.println(name);
  if (name.equals("left")) {
    moveSlider("left");
  }
  else if (name.equals("right")) {
    moveSlider("right");
  }
  else if (name.equals("speed"))
    changeSpeed(value);

  else if (name.equals("stop")) {
    Serial.println("stopping");
    isStop = true;
  }
  else if (name.equals("reset_pos")) {
    moveSlider("reset");
    isStop = false;
  }

  else if (name.equals("steps")) {
    steps = value;
  }
  else if (name.equals("recalibrate")) {
    stepper->setCurrentPosition(0);
    Serial.println(stepper->currentPosition());
  }
  else if (name.equals("timelapse_right")) {
    timelapse("right");
  }
  else if (name.equals("timelapse_left")) {
    timelapse("left");
  }
  else if (name.equals("mins")) {
    timelapse_mins = value;
  }

  else if (name.equals("accel_enabled")) {
    accel_enabled = value;
  }

  else if (name.equals("set_accel")) {
    stepper->setAcceleration(value);
  }
}

void disableStepper() {
  digitalWrite(enable_pin, 1);
}

void enableStepper() {
  digitalWrite(enable_pin, 0);
}


Last edited by majklovec on 12 Dec 2016, 06:39, edited 1 time in total.

theju
Normal user
Posts: 4
Joined: 29 May 2016, 10:14

Re: Stepper Motor

#3 Post by theju » 06 Dec 2016, 09:11

Hi majklovec,

Thank you for your answer

Unfortunately, I'm not able to compile your plugin. Maybe a definition missing in the main file ?

Code: Select all

ESPEasy/_P106_Stepper.ino: In function 'boolean Plugin_106(byte, EventStruct*, String&)':
_P106_Stepper:40: error: 'DEVICE_TYPE_TRIPLE' was not declared in this scope
         Device[deviceCount].Type = DEVICE_TYPE_TRIPLE;
                                    ^
_P106_Stepper:78: error: 'PLUGIN_BACKGROUND' was not declared in this scope
     case PLUGIN_BACKGROUND:
          ^
exit status 1
'DEVICE_TYPE_TRIPLE' was not declared in this scope
Regards,
Theju

timsson
Normal user
Posts: 76
Joined: 25 Mar 2016, 22:00

Re: Stepper Motor

#4 Post by timsson » 06 Dec 2016, 10:45

+1

majklovec
Normal user
Posts: 10
Joined: 12 Jul 2016, 07:47

Re: Stepper Motor

#5 Post by majklovec » 07 Dec 2016, 02:41

Yes, add

to espeasy.ino

Code: Select all

#define DEVICE_TYPE_TRIPLE 5
And to webserver.ino to function;

Code: Select all

void handle_devices() 
after

Code: Select all

      if (Device[DeviceIndex].Type == DEVICE_TYPE_DUAL)
      {
        Settings.TaskDevicePin1[index - 1] = taskdevicepin1.toInt();
        Settings.TaskDevicePin2[index - 1] = taskdevicepin2.toInt();
      }
ADD THIS:

Code: Select all

      if (Device[DeviceIndex].Type == DEVICE_TYPE_TRIPLE)
      {
        Settings.TaskDevicePin1[index - 1] = taskdevicepin1.toInt();
        Settings.TaskDevicePin2[index - 1] = taskdevicepin2.toInt();
        Settings.TaskDevicePin3[index - 1] = taskdevicepin3.toInt();
      }

theju
Normal user
Posts: 4
Joined: 29 May 2016, 10:14

Re: Stepper Motor

#6 Post by theju » 07 Dec 2016, 08:46

Thank you for your response.

I need to add

Code: Select all

#define PLUGIN_BACKGROUND                  20
in ESPEasy.ino and now compilation is correct.

I will try ASAP tu use it.

Regards,
Theju

majklovec
Normal user
Posts: 10
Joined: 12 Jul 2016, 07:47

Re: Stepper Motor

#7 Post by majklovec » 12 Dec 2016, 06:39

I believe that changing PLUGIN_BACKGROUND to PLUGIN_TEN_PER_SECOND in the source (in the case section) will do the job.

Just to get sure you need to have some stepper driver A4988, DRV8825 ... often used in RepRap 3D printers and connect "not ENable", STEP and DIR to the selected pins.

Image

DIYEd
Normal user
Posts: 1
Joined: 04 Jan 2017, 09:58

Re: Stepper Motor

#8 Post by DIYEd » 04 Jan 2017, 10:03

Compile did work without any errors. But what HTTP requets to send to control this steppermotor?

Domosapiens
Normal user
Posts: 125
Joined: 06 Nov 2016, 13:45

Re: Stepper Motor

#9 Post by Domosapiens » 29 Mar 2017, 00:45

YES!!
My baby makes its first steps!

DIYEd,
did you find HTTP request capabilities ?
Can you please share?

What I find out by code reading, are the following terminal commands:

Code: Select all

Adjustments:

accel_enabled,<boolean>		(0,1)
speed,<value> 			(call to changeSpeed, adjust speed to value, sets acceleration to <value>/4)
set_accel,<value> 		(overwritten by speed. so order is important)
recalibrate			(set the current position as 0 position)
steps,<value>			(prepare the number of steps)
mins,<value> 			(prepare timelaps rotation in minutes)

Execution:

left    			(call to moveSlider left, go, with -stepper_speed and #steps)
right  				(call to moveSlider right, with stepper_speed and #steps)
stop 				(break current operation?)
reset_pos 			(call to moveSlider reset: go to 0 position with  –stepper_speed)
timelapse_right	 		(timelaps rotation in #mins for #steps)
timelapse_left 			(timelaps rotation in #mins for #steps)
@majklovec,
can you shine your light?
Last edited by Domosapiens on 30 Mar 2017, 00:49, edited 1 time in total.
18+ ESP units for production and test. Ranging from production control of heating equipment, floor sensing, floor thermostat to a "fun box" for my grandson. Mainly using Wemos D1.

Domosapiens
Normal user
Posts: 125
Joined: 06 Nov 2016, 13:45

Re: Stepper Motor

#10 Post by Domosapiens » 29 Mar 2017, 23:20

HTTP request capabilities?
I checked a few ..and found:
http://<your_IP>/control?cmd=stop
http://<your_IP>/control?cmd=speed,<value>
http://<your_IP>/control?cmd=left
are working!

So no Device/Task name is involved.

That brings me to the question:
How to control 2 stepper motors?

Any help?
Domosapiens
18+ ESP units for production and test. Ranging from production control of heating equipment, floor sensing, floor thermostat to a "fun box" for my grandson. Mainly using Wemos D1.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests