1. Do not share user accounts! Any account that is shared by another person will be blocked and closed. This means: we will close not only the account that is shared, but also the main account of the user who uses another person's account. We have the ability to detect account sharing, so please do not try to cheat the system. This action will take place on 04/18/2023. Read all forum rules.
    Dismiss Notice
  2. 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!
  3. 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
  4. 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

Tutorial SPS - Sabertooth Packet Serial PID motor driver sketch w/SoftStart

Discussion in 'SimTools compatible interfaces' started by BlazinH, Nov 10, 2016.

  1. Pit

    Pit - - - - - - - - - - - - - - - - Gold Contributor

    Joined:
    Oct 2, 2013
    Messages:
    3,012
    Location:
    Switzerland
    Balance:
    30,451Coins
    Ratings:
    +3,091 / 31 / -0
    My Motion Simulator:
    DC motor, Arduino, 6DOF
    you have to edit the sketch before uploading
  2. SilentChill

    SilentChill Problem Maker

    Joined:
    Jul 19, 2014
    Messages:
    2,643
    Occupation:
    Railway Maintenance
    Location:
    Morecambe, Lancashire, England
    Balance:
    20,560Coins
    Ratings:
    +3,488 / 34 / -0
    My Motion Simulator:
    DC motor, Arduino, Motion platform, 6DOF
    Yes lol but where in the sketch

    edit: I found it however there are a lot, do i change them all ?

    upload_2016-11-12_13-9-9.png
  3. Pit

    Pit - - - - - - - - - - - - - - - - Gold Contributor

    Joined:
    Oct 2, 2013
    Messages:
    3,012
    Location:
    Switzerland
    Balance:
    30,451Coins
    Ratings:
    +3,091 / 31 / -0
    My Motion Simulator:
    DC motor, Arduino, 6DOF
    #ifdef SECOND_SERIAL
    mySerial.begin(115200);
    #endif

    to

    #ifdef SECOND_SERIAL
    mySerial.begin(38400);
    #endif
  4. BlazinH

    BlazinH Well-Known Member

    Joined:
    Oct 19, 2013
    Messages:
    2,145
    Location:
    Oklahoma City, USA
    Balance:
    16,621Coins
    Ratings:
    +1,835 / 32 / -1
    I just sent you a PM with a new sketch to try with instructions how to change the sabertooths baud.
  5. RufusDufus

    RufusDufus Well-Known Member

    Joined:
    Jul 21, 2013
    Messages:
    681
    Location:
    Adelaide Australia
    Balance:
    15,952Coins
    Ratings:
    +1,016 / 10 / -1
    @BlazinH, I only had a quick look so this isn't a comprehensive response but I notice the packet serial requires about 4bytes minimum per motor. At 115200 baud that's at least 1ms to send all the data to 3 motors without allowing for any processing in between.

    I would get rid of the ST[1] commands for motor 3 and just make it a two motor controller. That will give you a bit extra headroom.

    It also means that you don't want the PID loop running at its full speed. I note in SilentChill's screenshot Fpid is set to divide by 5 which is good - maybe even go divide 6 or more if I allowed it? If you run faster than that I expect it will be overrunning the tx buffer to the sabretooth.

    As for the baud rate to the sabretooth, this also means you want the fastest baud rate possible to get the commands flushed before the next time round the loop.

    You may also find that adding #pragma GCC optimize ("-fast") at the top of the code might help speed the code a bit (sometimes it can have negative effects though). Although the main limiting factor is the rate at which commands can be sent to the sabretooth this might at least ensure the rest of the code is as fast as possible.
    • Like Like x 1
    • Informative Informative x 1
  6. SilentChill

    SilentChill Problem Maker

    Joined:
    Jul 19, 2014
    Messages:
    2,643
    Occupation:
    Railway Maintenance
    Location:
    Morecambe, Lancashire, England
    Balance:
    20,560Coins
    Ratings:
    +3,488 / 34 / -0
    My Motion Simulator:
    DC motor, Arduino, Motion platform, 6DOF
    @RufusDufus I have just tried from 5-10 Fpid and it didnt make any difference still each motor twitching in turn and then goes crazy when I try to move the sliders in simtools
  7. BlazinH

    BlazinH Well-Known Member

    Joined:
    Oct 19, 2013
    Messages:
    2,145
    Location:
    Oklahoma City, USA
    Balance:
    16,621Coins
    Ratings:
    +1,835 / 32 / -1
    Thanks for your sharing your insight @RufusDufus. I must not be figuring the baud rate correctly; 4bytes x 8 = 32 x 3 motors = 96 x 820 (fpid/5) = 78720?

    Also, I just added motor 3 for the hell of it but it has never been tested so removing it is a good idea.

    Btw, if the sabertooths tx buffer is overrun it will just ignore additional data. Sabertooths can handle up to 2000 updates per second though.
    Last edited: Nov 12, 2016
  8. BlazinH

    BlazinH Well-Known Member

    Joined:
    Oct 19, 2013
    Messages:
    2,145
    Location:
    Oklahoma City, USA
    Balance:
    16,621Coins
    Ratings:
    +1,835 / 32 / -1
    Probably don't want to use larger than Fpid /6. That will give 410 motor updates per second and I don't think less than that would be good. You could try using 4 too just for kicks.

    CORRECTION: I was incorrectly thinking in binary terms. Fpid /10 = 410 updates per second and Fpid /6 = 683.
    Last edited: Nov 13, 2016
  9. RufusDufus

    RufusDufus Well-Known Member

    Joined:
    Jul 21, 2013
    Messages:
    681
    Location:
    Adelaide Australia
    Balance:
    15,952Coins
    Ratings:
    +1,016 / 10 / -1
    There are 10bits for a serial byte at the hardware level (includes the start and stop bit)
    so that is 1/115200 * 10 * 4 * 3 for three motors with 4byte packets = 1.04ms assuming the bytes are sent without any interbyte delay which might be optimistic for softwareserial?

    The PID loop is about 4000Hz at full speed from memory so Fpid/5 = 1.25ms round loop assuming nothing else slows it down.
    • Informative Informative x 1
  10. Pit

    Pit - - - - - - - - - - - - - - - - Gold Contributor

    Joined:
    Oct 2, 2013
    Messages:
    3,012
    Location:
    Switzerland
    Balance:
    30,451Coins
    Ratings:
    +3,091 / 31 / -0
    My Motion Simulator:
    DC motor, Arduino, 6DOF
    @RufusDufus today I tested again the UNO with the latest sketch from @BlazinH and I can confirm that 1ms packet rate works flawlessly on the UNO. Maybe there was a bug earlier when testing different betas so at that time I came to this conclusion.
  11. BlazinH

    BlazinH Well-Known Member

    Joined:
    Oct 19, 2013
    Messages:
    2,145
    Location:
    Oklahoma City, USA
    Balance:
    16,621Coins
    Ratings:
    +1,835 / 32 / -1
    Updated sketch posted.
    • Like Like x 1
    Last edited: Nov 13, 2016
  12. SilentChill

    SilentChill Problem Maker

    Joined:
    Jul 19, 2014
    Messages:
    2,643
    Occupation:
    Railway Maintenance
    Location:
    Morecambe, Lancashire, England
    Balance:
    20,560Coins
    Ratings:
    +3,488 / 34 / -0
    My Motion Simulator:
    DC motor, Arduino, Motion platform, 6DOF
    Quick update, I have been using the latest sketch now for a few weeks and it is working had to set Fpid 4 to get rid of an annoying twitching that was going on but other than that it works great.

    Big thanks to @BlazinH for all your hard work making this and helping me set everything up and @RufusDufus for his amazing SMC3 :)

    Think this should possibly get a sticky @Historiker @value1 ??
    • Like Like x 1
    • Friendly Friendly x 1
  13. Historiker

    Historiker Dramamine Adict Gold Contributor

    Joined:
    Dec 16, 2010
    Messages:
    2,161
    Occupation:
    Retired
    Location:
    Michigan USA
    Balance:
    9,201Coins
    Ratings:
    +2,164 / 19 / -1
    My Motion Simulator:
    3DOF, DC motor, Arduino, Motion platform, 6DOF
    Agreed and done.
    • Winner Winner x 1
    • Friendly Friendly x 1
  14. BlazinH

    BlazinH Well-Known Member

    Joined:
    Oct 19, 2013
    Messages:
    2,145
    Location:
    Oklahoma City, USA
    Balance:
    16,621Coins
    Ratings:
    +1,835 / 32 / -1
    UPDATE: This sketch is further optimized for speed and can now use an Fpid setting of 1. If you experience motor twitching or issues at Fpid = 1 then try increasing it.

    Btw, added a SOFT START motor routine too so I need some testers to let me know if it needs any tweaking. At the top of the sketch look for
    //****************************************************************************************************************
    //SET SOFTSTART SPEED
    const int SSloop = 8;
    //****************************************************************************************************************
    I estimate SSloop = 8 to be around 2 seconds to achieve full motor power. Increasing SSloop will slow it down more and decreasing it will speed it up.
    • Like Like x 2
    • Informative Informative x 1
    Last edited: Jan 4, 2017
  15. SilentChill

    SilentChill Problem Maker

    Joined:
    Jul 19, 2014
    Messages:
    2,643
    Occupation:
    Railway Maintenance
    Location:
    Morecambe, Lancashire, England
    Balance:
    20,560Coins
    Ratings:
    +3,488 / 34 / -0
    My Motion Simulator:
    DC motor, Arduino, Motion platform, 6DOF

    Is this so that the when it starts up it moves slowly into position ?
  16. BlazinH

    BlazinH Well-Known Member

    Joined:
    Oct 19, 2013
    Messages:
    2,145
    Location:
    Oklahoma City, USA
    Balance:
    16,621Coins
    Ratings:
    +1,835 / 32 / -1
    Yes, on initial start.
  17. MajorClod

    MajorClod New Member

    Joined:
    Oct 25, 2010
    Messages:
    12
    Balance:
    - 14Coins
    Ratings:
    +5 / 0 / -0
    Set this up on my test bench with a new Arduino and 2x32 today. Everything went smoothly. Looking forward to trying it with some bigger DC motors on the weekend. Great work guys.
  18. baykah

    baykah Active Member

    Joined:
    Sep 1, 2016
    Messages:
    137
    Location:
    Toulouse - France
    Balance:
    414Coins
    Ratings:
    +133 / 0 / -0
    My Motion Simulator:
    6DOF
    Great work !, working great with my 2x32
  19. MWB

    MWB Drive it like you stole it!!

    Joined:
    Jan 2, 2016
    Messages:
    131
    Occupation:
    Commissioning Engineer
    Location:
    U.K.
    Balance:
    424Coins
    Ratings:
    +148 / 0 / -0
    My Motion Simulator:
    2DOF, DC motor, Arduino, Motion platform, 4DOF, 6DOF
    Just ordered the 2x60 Sabertooth, so this is really helpful! Thanks :thumbs
    • Agree Agree x 1
  20. MajorClod

    MajorClod New Member

    Joined:
    Oct 25, 2010
    Messages:
    12
    Balance:
    - 14Coins
    Ratings:
    +5 / 0 / -0
    FYI - if using the new Simtools 2.0, your interface output should be: [A<Axis1a>][B<Axis2a>][C<Axis3a>]

    Note the Axis1a not just Axis1

    Might be worth updating the original post.
    • Agree Agree x 2