Controlling multiple steppers synchronous using C++












0















I am currently working on a project on a atmega 2560 where I need to control multiple stepper motors synchronous. Meaning sometimes only one motor turns at a time and sometimes multiple motors need to turn at the exact same time for the exact same angle. While planning out my approach I ran into a problem I am not certain how to solve. So, I decided to see if anyone can give me valuable advice.
I am writing the program in C++ and I wanted to create a class for the stepper motors(see attached UML) which has a method rotate() to, u guessed it, rotate the stepper for a specific angle in a defined direction. Now of course this presents a timing issue because I need to implement the function in such a way that it does not block any other code and enables me to turn multiple motors at the same time. I came up with two possible approaches:




  1. Using the timers of the atmega and interrupts to create a PWM signal which turns the motors one step per interrupt. In order to be able to turn multiple motors at once I thought about creating a table for active steppers. Each time the ISR is entered it checks what steppers are supposed to move and transfers the PWM signal to the corresponding pins. Of course, I would somehow need to keep track of the number of steps still left to turn (maybe by introducing another member variable).

  2. Not using interrupts at all and simply avoid blocking the code as much as possible, by not using while loops and using the system time for delays.


Now my question to all of you is whatever possibility you think would work best or if there is something else, I could try? I am pretty confident that I could implement either one of the above, but I am just not quite happy with both of them. It would feel right to use counters and interrupts in order to guarantee exact timing. However, the approach of having an external table for the active steppers makes the program less easy to understand. In addition, I had my problems of fitting ISRs into the object-oriented world.



Any ideas or contributions are highly appreciated.



Best regards
Axodarap










share|improve this question























  • Sounds like you want a state machine.

    – user4581301
    Jan 1 at 18:16











  • @user4581301 I dont't see how a state machine would silve my timing issue. :/

    – Axodarap
    Jan 1 at 20:43











  • Option 2 sounds like the AccelStepper library.

    – David Grayson
    Jan 2 at 18:35


















0















I am currently working on a project on a atmega 2560 where I need to control multiple stepper motors synchronous. Meaning sometimes only one motor turns at a time and sometimes multiple motors need to turn at the exact same time for the exact same angle. While planning out my approach I ran into a problem I am not certain how to solve. So, I decided to see if anyone can give me valuable advice.
I am writing the program in C++ and I wanted to create a class for the stepper motors(see attached UML) which has a method rotate() to, u guessed it, rotate the stepper for a specific angle in a defined direction. Now of course this presents a timing issue because I need to implement the function in such a way that it does not block any other code and enables me to turn multiple motors at the same time. I came up with two possible approaches:




  1. Using the timers of the atmega and interrupts to create a PWM signal which turns the motors one step per interrupt. In order to be able to turn multiple motors at once I thought about creating a table for active steppers. Each time the ISR is entered it checks what steppers are supposed to move and transfers the PWM signal to the corresponding pins. Of course, I would somehow need to keep track of the number of steps still left to turn (maybe by introducing another member variable).

  2. Not using interrupts at all and simply avoid blocking the code as much as possible, by not using while loops and using the system time for delays.


Now my question to all of you is whatever possibility you think would work best or if there is something else, I could try? I am pretty confident that I could implement either one of the above, but I am just not quite happy with both of them. It would feel right to use counters and interrupts in order to guarantee exact timing. However, the approach of having an external table for the active steppers makes the program less easy to understand. In addition, I had my problems of fitting ISRs into the object-oriented world.



Any ideas or contributions are highly appreciated.



Best regards
Axodarap










share|improve this question























  • Sounds like you want a state machine.

    – user4581301
    Jan 1 at 18:16











  • @user4581301 I dont't see how a state machine would silve my timing issue. :/

    – Axodarap
    Jan 1 at 20:43











  • Option 2 sounds like the AccelStepper library.

    – David Grayson
    Jan 2 at 18:35
















0












0








0








I am currently working on a project on a atmega 2560 where I need to control multiple stepper motors synchronous. Meaning sometimes only one motor turns at a time and sometimes multiple motors need to turn at the exact same time for the exact same angle. While planning out my approach I ran into a problem I am not certain how to solve. So, I decided to see if anyone can give me valuable advice.
I am writing the program in C++ and I wanted to create a class for the stepper motors(see attached UML) which has a method rotate() to, u guessed it, rotate the stepper for a specific angle in a defined direction. Now of course this presents a timing issue because I need to implement the function in such a way that it does not block any other code and enables me to turn multiple motors at the same time. I came up with two possible approaches:




  1. Using the timers of the atmega and interrupts to create a PWM signal which turns the motors one step per interrupt. In order to be able to turn multiple motors at once I thought about creating a table for active steppers. Each time the ISR is entered it checks what steppers are supposed to move and transfers the PWM signal to the corresponding pins. Of course, I would somehow need to keep track of the number of steps still left to turn (maybe by introducing another member variable).

  2. Not using interrupts at all and simply avoid blocking the code as much as possible, by not using while loops and using the system time for delays.


Now my question to all of you is whatever possibility you think would work best or if there is something else, I could try? I am pretty confident that I could implement either one of the above, but I am just not quite happy with both of them. It would feel right to use counters and interrupts in order to guarantee exact timing. However, the approach of having an external table for the active steppers makes the program less easy to understand. In addition, I had my problems of fitting ISRs into the object-oriented world.



Any ideas or contributions are highly appreciated.



Best regards
Axodarap










share|improve this question














I am currently working on a project on a atmega 2560 where I need to control multiple stepper motors synchronous. Meaning sometimes only one motor turns at a time and sometimes multiple motors need to turn at the exact same time for the exact same angle. While planning out my approach I ran into a problem I am not certain how to solve. So, I decided to see if anyone can give me valuable advice.
I am writing the program in C++ and I wanted to create a class for the stepper motors(see attached UML) which has a method rotate() to, u guessed it, rotate the stepper for a specific angle in a defined direction. Now of course this presents a timing issue because I need to implement the function in such a way that it does not block any other code and enables me to turn multiple motors at the same time. I came up with two possible approaches:




  1. Using the timers of the atmega and interrupts to create a PWM signal which turns the motors one step per interrupt. In order to be able to turn multiple motors at once I thought about creating a table for active steppers. Each time the ISR is entered it checks what steppers are supposed to move and transfers the PWM signal to the corresponding pins. Of course, I would somehow need to keep track of the number of steps still left to turn (maybe by introducing another member variable).

  2. Not using interrupts at all and simply avoid blocking the code as much as possible, by not using while loops and using the system time for delays.


Now my question to all of you is whatever possibility you think would work best or if there is something else, I could try? I am pretty confident that I could implement either one of the above, but I am just not quite happy with both of them. It would feel right to use counters and interrupts in order to guarantee exact timing. However, the approach of having an external table for the active steppers makes the program less easy to understand. In addition, I had my problems of fitting ISRs into the object-oriented world.



Any ideas or contributions are highly appreciated.



Best regards
Axodarap







c++ atmega stepper






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 17:54









AxodarapAxodarap

11




11













  • Sounds like you want a state machine.

    – user4581301
    Jan 1 at 18:16











  • @user4581301 I dont't see how a state machine would silve my timing issue. :/

    – Axodarap
    Jan 1 at 20:43











  • Option 2 sounds like the AccelStepper library.

    – David Grayson
    Jan 2 at 18:35





















  • Sounds like you want a state machine.

    – user4581301
    Jan 1 at 18:16











  • @user4581301 I dont't see how a state machine would silve my timing issue. :/

    – Axodarap
    Jan 1 at 20:43











  • Option 2 sounds like the AccelStepper library.

    – David Grayson
    Jan 2 at 18:35



















Sounds like you want a state machine.

– user4581301
Jan 1 at 18:16





Sounds like you want a state machine.

– user4581301
Jan 1 at 18:16













@user4581301 I dont't see how a state machine would silve my timing issue. :/

– Axodarap
Jan 1 at 20:43





@user4581301 I dont't see how a state machine would silve my timing issue. :/

– Axodarap
Jan 1 at 20:43













Option 2 sounds like the AccelStepper library.

– David Grayson
Jan 2 at 18:35







Option 2 sounds like the AccelStepper library.

– David Grayson
Jan 2 at 18:35














1 Answer
1






active

oldest

votes


















0














I would like to recommend you to use ISR.




  1. Using ISRs let you adding new feature easier. Whenever you want to add new functionality, you write it and your ISRs still execute at the same situation/time/synchronus/asynchronus.

  2. Working with motors force you to use time reliant system, especially when there is no feedback from position/angle.


Whats the problem with storing data about steppers? You don't need external table for active steppers or I misunderstand you. You can use static variables in ISR to prevent them from disappear after executing.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997662%2fcontrolling-multiple-steppers-synchronous-using-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I would like to recommend you to use ISR.




    1. Using ISRs let you adding new feature easier. Whenever you want to add new functionality, you write it and your ISRs still execute at the same situation/time/synchronus/asynchronus.

    2. Working with motors force you to use time reliant system, especially when there is no feedback from position/angle.


    Whats the problem with storing data about steppers? You don't need external table for active steppers or I misunderstand you. You can use static variables in ISR to prevent them from disappear after executing.






    share|improve this answer






























      0














      I would like to recommend you to use ISR.




      1. Using ISRs let you adding new feature easier. Whenever you want to add new functionality, you write it and your ISRs still execute at the same situation/time/synchronus/asynchronus.

      2. Working with motors force you to use time reliant system, especially when there is no feedback from position/angle.


      Whats the problem with storing data about steppers? You don't need external table for active steppers or I misunderstand you. You can use static variables in ISR to prevent them from disappear after executing.






      share|improve this answer




























        0












        0








        0







        I would like to recommend you to use ISR.




        1. Using ISRs let you adding new feature easier. Whenever you want to add new functionality, you write it and your ISRs still execute at the same situation/time/synchronus/asynchronus.

        2. Working with motors force you to use time reliant system, especially when there is no feedback from position/angle.


        Whats the problem with storing data about steppers? You don't need external table for active steppers or I misunderstand you. You can use static variables in ISR to prevent them from disappear after executing.






        share|improve this answer















        I would like to recommend you to use ISR.




        1. Using ISRs let you adding new feature easier. Whenever you want to add new functionality, you write it and your ISRs still execute at the same situation/time/synchronus/asynchronus.

        2. Working with motors force you to use time reliant system, especially when there is no feedback from position/angle.


        Whats the problem with storing data about steppers? You don't need external table for active steppers or I misunderstand you. You can use static variables in ISR to prevent them from disappear after executing.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 22 at 6:42

























        answered Jan 22 at 6:35









        dunajskidunajski

        11416




        11416
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997662%2fcontrolling-multiple-steppers-synchronous-using-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith