1. For downloading SimTools plugins you need a Download Package. Get it with virtual coins that you receive for forum activity or Buy Download Package - We have a zero Spam tolerance so read our forum rules first.

    Buy Now a Download Plan!
  2. Do not try to cheat our system and do not post an unnecessary amount of useless posts only to earn credits here. We have a zero spam tolerance policy and this will cause a ban of your user account. Otherwise we wish you a pleasant stay here! Read the forum rules
  3. We have a few rules which you need to read and accept before posting anything here! Following these rules will keep the forum clean and your stay pleasant. Do not follow these rules can lead to permanent exclusion from this website: Read the forum rules.
    Are you a company? Read our company rules

My 2DOF with stepper motors

Discussion in 'DIY Motion Simulator Projects' started by Lebois, Jun 28, 2019.

  1. cityparker

    cityparker Member

    Joined:
    Monday
    Messages:
    28
    Balance:
    234Coins
    Ratings:
    +2 / 0 / -0
    My Motion Simulator:
    2DOF, DC motor
    Looks like a great project and its fantastic that you are sharing your stepper code! Big props!
  2. Trigen

    Trigen Active Member

    Joined:
    Nov 25, 2018
    Messages:
    398
    Balance:
    2,602Coins
    Ratings:
    +127 / 0 / -0
    My Motion Simulator:
    2DOF, 3DOF, DC motor, Arduino
    So would i :)
  3. dr.Evil

    dr.Evil New Member Gold Contributor

    Joined:
    Aug 26, 2020
    Messages:
    16
    Balance:
    66Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    2DOF
    @Lebois ....any stepper motor could be a set up as a transducer.
    Did you use the same motors and the same drivers as in the first page? I mean, on your OP you said "Arduino sends pulse and dir signals to two HBS86h stepper drivers. I had a hard time trying to make them work. I learned a lot doing this project, but it also means that it took quite a long time... It's a closed loop driver. that means that an position encoder is fitted on the motor shaft and indicates to the driver where the motor is, and the driver can correct the position. " and to my question "the encoders are integrated into the stepper motors."

    Sorry for so many questions. I have a 2DOF rig that i bought from a bankrupt VR shop, it's working only on analogs inputs eg. turn of the steering wheel or accel/brake. it's made from quite good quality steppers and drivers. I need to make it work on the feedback from Simtools. You are the only soul that succeeded in making something similar.
  4. Lebois

    Lebois (maybe I am wrong, but who knows...)

    Joined:
    Dec 10, 2018
    Messages:
    220
    Occupation:
    Math teacher
    Location:
    France
    Balance:
    1,706Coins
    Ratings:
    +103 / 2 / -0
    My Motion Simulator:
    2DOF
    I already tried to wire the stepper motor encoder directly to the arduino. Problem is that the arduino needs to run extremely fast to read the encoder position while driving the motor at the same time. Or you need a great code... Everything is possible, but it was clearly simpler to use it the way I used it.

    My new stepper based actuators are almost ready. I can't describe the enjoyment they gave me. 3DOF is the way to go if you really want a good experience :)
    [​IMG]
  5. dr.Evil

    dr.Evil New Member Gold Contributor

    Joined:
    Aug 26, 2020
    Messages:
    16
    Balance:
    66Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    2DOF
    @Lebois ....any stepper motor could be a set up as a transducer.
    did you use the same motors and the same drivers as in the first page? I mean, on your OP you said "Arduino sends pulse and dir signals to two HBS86h stepper drivers. I had a hard time trying to make them work. I learned a lot doing this project, but it also means that it took quite a long time... It's a closed loop driver. that means that an position encoder is fitted on the motor shaft and indicates to the driver where the motor is, and the driver can correct the position. " and to my question
    with no means of feedback other that the motors?
  6. Lebois

    Lebois (maybe I am wrong, but who knows...)

    Joined:
    Dec 10, 2018
    Messages:
    220
    Occupation:
    Math teacher
    Location:
    France
    Balance:
    1,706Coins
    Ratings:
    +103 / 2 / -0
    My Motion Simulator:
    2DOF
    yes, same motor and same driver. The feedback comes from the motor encoder, and is only sent to the driver.
  7. yellofella

    yellofella Member

    Joined:
    Aug 8, 2011
    Messages:
    90
    Occupation:
    builder
    Location:
    UK
    Balance:
    707Coins
    Ratings:
    +66 / 0 / -0
    My Motion Simulator:
    3DOF, SimforceGT
    Hi guys. You mentioned that you would like to see the code im using but i have not got the complete code to control all the motors finished yet as it is the project that i am currently working on so is a work in progress at the moment. As i build/expand and improve the project the code that it uses changes. Once the project is finished i will do a build log and post all the files on the site but i am a long way from that.
    @Lebois i looked up the datasheet for your driver and it does say that it has auto correction for missed steps. I did some testing with the hybrid stepper driver that i have and found it better than a standard driver for missed steps but it did loose its home point at high rpm with no acceleration ramping. The real problem and the main reason i abandoned the driver was that you cant change the micro stepping dynamically. You set it using dip switches on the side of the driver. This means you have a choice between full steps with massive cogging while trying to hold a position or using a high micro step value to stop the cogging but have low rpm output. I ordered some drv8825 drivers to test with as they are cheap and found that being able to change the step rate in the arduino code allowed full step high rpm and 1/32 micro steps to sit on the target exactly. I'm using a 4000cpr encoder on the 1.8 degree motor so need at least 1/20 microsteps to hold target without any cogging. This works well for small motors but i need more power than the little driver can deliver so now im using the pololu drv8711. This can drive the 4A that i need but is controlled using spi which has changed the code im using again and in no way suits your driver.
    You could give this a go though if you want to.
    To read the encoder on the arduino i use this example https://github.com/Saeterncj/QuadratureEncoder
    Install the library and connect your encoder to the arduino. For your encoder i would wire red to 5v through a 1k resistor, white to ground, blue to pin 2 and green to pin 3. Open the serial monitor and you should see the encoder count change when you turn the motor. It uses interrupts so will always catch the pulses.
    Your motors have a 1000ppr encoder on them with 2 encoder outputs (a & b). I assume that you should get 4000cpr in the serial monitor (4000 pulses per revolution as 1 quad encoder).
    So an example for you might be that you want 180 degrees or rotation at the final drive of your 5:1 gearbox for your rig. So 4000cpr x 5 / 2 = 10000 pulses for 180 degrees. If you set the simtools output to 13 bit you get an output of 0 - 8192. Read the value into the arduino via serial and this will be the target position. The encoder count will be the current position. Then do some simple integer math to calculate the difference between the two which i will call delta position for this example. I will use 1 arduino pin for the micro stepping to keep it simple. Something like this.

    if (delta_position < 500) {
    interval = map(delta_position, 0, 499, speedMin, speedMax); // speedmin 2000 - speedmax 600
    }
    else {
    interval = speedMax;
    }
    if (target_pos < current_pos) {
    delta_position = current_pos - target_pos;
    digitalWrite(dirPin, HIGH); // Set the spinning direction CCW
    servoStep();
    }
    else if (target_pos > current_pos) {
    delta_position = target_pos - current_pos;
    digitalWrite(dirPin, LOW); // Set the spinning direction CCW
    servoStep();
    }
    }
    void servoStep() {
    if (delta_position < 20) {
    digitalWrite(microStepPin, HIGH); //1/32 step
    }
    else {
    digitalWrite(microStepPin, LOW); //full step
    }

    if (micros() - previous_micros >= interval) {
    previous_micros = micros();
    if (step_state == LOW) {
    step_state = HIGH;
    } else {
    step_state = LOW;
    }
    digitalWrite(stepPin, !digitalRead(stepPin));
    }
    }
    You could count the pulses from the encoder, control the stepping and use the other two interrupts for limit switches to setup a home position all on 1 arduino. Then use a second arduino with the same setup for motor two and a third for motor 3. I guess that would be the simplest way but i cant do this for six motors as simtools has a limit of 3 interfaces i think so im using a fast controller as a master to interface all six motor drivers and encoder counters via spi.
    Not sure if this helps guys as its not the code that im using now but gives you the idea. You would have to modify the code for you particular driver input as this example uses 1 step and direction pins. It will move the motors at full speed until it is 24 steps from the target then ramp the speed down in full step mode the closer it gets to the target then switches to 1/32 microsteping when 1 full step from the target. If you manually force the motor out of position or it stalls it will always try to return to the target set by sim tools.
    • Like Like x 2
    • Informative Informative x 1
  8. Lebois

    Lebois (maybe I am wrong, but who knows...)

    Joined:
    Dec 10, 2018
    Messages:
    220
    Occupation:
    Math teacher
    Location:
    France
    Balance:
    1,706Coins
    Ratings:
    +103 / 2 / -0
    My Motion Simulator:
    2DOF
    "The real problem and the main reason i abandoned the driver was that you cant change the micro stepping dynamically" Yes, it can be a big improvement !
    Your explanation is clear, thank you !
    I will think about it, and try it later. For the moment, I will focus on releasing the first version of this actuators.
  9. dr.Evil

    dr.Evil New Member Gold Contributor

    Joined:
    Aug 26, 2020
    Messages:
    16
    Balance:
    66Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    2DOF
    Good advice, I'll try it as soon as I get free.