Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 11 of 11

Thread: Teensy 4.0 Servos

  1. #1
    Junior Member
    Join Date
    Aug 2019
    Posts
    4

    Teensy 4.0 Servos

    When using the servo library the Teensy 3.2 was limited to 12 servos.
    As far as I understand that‘s because it only has one 16-bit timer.
    Now what kind of timers are available on the new Teensy 4.0?

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,549
    @Wingman - I have not played around with RC servos for a long time (also in a partial answer to your PM on Trossen)

    Looking that the Teensy 4 First Beta Test thread - It looks like the servo library was marked as working, as also noted in the posting:
    https://forum.pjrc.com/threads/54711...l=1#post194446

    But could it be extended? Maybe? Would the PWMServo library work OK for your servos? Not sure, but there are lots of PWM pins on T4...

    Again I have not touched RC servos for several years now (Since Lynxmotion was taken over by RobotShop) so not sure how well things still work.

  3. #3
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    4,205
    @Wingman - when working with servos I tend to PWMServo. As @KurtE mentioned you have a lot more PWM pins to work with.

  4. #4
    Junior Member
    Join Date
    Aug 2019
    Posts
    4
    Thanks for your answer, KurtE and sorry for kind of cross-posting this!
    I'm just curious if there is more than one 16-bit timer for the new Teensy.
    It would have been perfect if the Teensy 4.0 worked with the standard servo library and 24 servos.
    PWMservo is of course an alternative but I would have to port your functions for grouped and timed movements.

  5. #5
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,549
    @PaullStoffregen updated the Servo library for T4 to use an IntervalTimer,

    I don't remember how many of these Interval Timers the T4 supports, but I am pretty sure more than one...

    So yes someone could probably update this library to optionally include a second interval timer to handle additional Servos. Not sure how hard, probably not that bad, but...

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,680
    16 bit timers aren't used for Servo on Teensy 4.0 (as they are on earlier Teensy and most Arduino boards). However, on your original question...

    Quote Originally Posted by Wingman View Post
    I'm just curious if there is more than one 16-bit timer for the new Teensy.
    Simple answer is there are 32 timers with 16 bit counters. NXP calls this 4 FlexPWM and 4 QuadTimer, so that's 8 total. But each one has 4 timers, which can be used separately or can sync together in special ways. The QuadTimer module has a number of very specific features, but isn't nearly as powerful as the FlexPWM timer. The details are all buried within the huge reference manual. But somehow I get the impression from the wording of your questions that you're not looking dive into the timer details, even though you are asking this narrowly focused question about how many exist. So, there you have it. The answer is 32 of them.

    As Kurt correctly pointed out, Servo on Teensy 4.0 now uses IntervalTimer (which is 32 bits, not 16). The code currently only supports using 1 of those timers, even though 4 of them exist in the hardware. I'm also getting the impression you're not looking to get into editing the Servo library, even those that would probably be fairly simple.

    PWMServo is almost certainly your path of least resistance.

    But the best (and easiest) thing you could do to help yourself is ask better questions. When you ask a narrowly focused question, the best we can do is give you technical answers which may or may not really apply. You can get much better help here if you step back from the tech details of timer and explain what you're really trying to accomplish and why that needs more servo motors and some context about the special ways you're trying to control them.

  7. #7
    Junior Member
    Join Date
    Aug 2019
    Posts
    4
    @PaulStoffregen You are right, that was a pretty unspecific question. Basically I am looking into how I can build an 18-servo hexapod with KurtE's Phoenix code. Using KurtE's modified servo library the Teensy 3.x can control up to 12 servos. Having twice as many like on the Atmega2650 would have been very convenient. Now I am thinking about whether it's worth the effort to port the code to PWMservo or if its best to just get rid of two legs on the robot.
    Anyway thank you for your help with this, you have answered my question!

  8. #8
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,549
    Hi @Paul and @wingman...

    I could sort of read between the lines of the question. As I knew he was referencing my OLD stuff, which is why I answered, that I have not looked at the stuff for several years now.

    What SerovEX did was to try to sort of act like a Lynxmotion SSC-32 servo controller. The main thing about it on Robots mainly Hexapods and Quads, was, while the robot was walking, as part of the walking gait you would want to tell all of the servos to move to a new position and all arrive that that new position in some specified time. So when you setup a group move, the code would compute how much each servo needed to move for each PWM output cycle.
    That is if you tell a servo to move from position 1500(us) to 1250 in 200ms and you know that servo cycle is 20ms, then you know that for this servo it needs to change 250 units in 10 cycles or 25 units per cycle...

    So nothing majorly complicated, it simply wants to go through at the right times and update the positions for the next servo update...

    Note: I think the old ServoEx did have some support for using PWMServo as well. I again don't remember how well those worked or not. That is I don't remember if those servos had as much granularity as the normal Servos.

    Again I don't think it would be hard to update the Servo library to handle the two sets of interval timers. My problem is lack of ambition to work with those servos again. But I do still have a few Lynxmotion robots sitting on the shelf..

  9. #9
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,549
    @PaulStoffregen and @wingman ...

    For the heck of it, I did make a version of Servo library for the T4, that does support up to 24 servos. It just felt right as well there are 24 easy to access IO pins on T4, so might make a nice servo controller.

    It will start up another Interval timer if you try to use more than 12 servos...
    It is up in a fork/branch: https://github.com/KurtE/Servo/tree/...port_24_servos

    Note: this was a quick and dirty extension, that for example duplicated the ISR function with it's static variables, to have another set of almost duplicate code for 2nd ISR again with it's own set of static variables.

    If I were to do it cleaner, I would probably have a set of data that each ISR owned and call of to common function with pointer/reference to the data...

    I did do a quick and dirty test to make sure it would work (hopefully)

    Code:
    /* Sweep
      by BARRAGAN <http://barraganstudio.com>
      This example code is in the public domain.
    
      modified 8 Nov 2013
      by Scott Fitzgerald
      http://arduino.cc/en/Tutorial/Sweep
    */
    
    #include <Servo.h>
    #define COUNT_SERVOS 18
    uint8_t pin_list[COUNT_SERVOS] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
                                      13, 14, 15, 16, 17
                                     };
    
    Servo servos[COUNT_SERVOS];
    int servo_pos[COUNT_SERVOS];
    int pos_changes[COUNT_SERVOS];
    void setup()
    {
    
      while (!Serial && (millis() < 4000)) ;
      Serial.begin(115200);
      for (uint8_t i = 0; i < COUNT_SERVOS; i++) {
        servos[i].attach(pin_list[i]);
        if (i & 1) {
          servo_pos[i] = 180-i;
          pos_changes[i] = -1;
        } else {
          servo_pos[i] = i;
          pos_changes[i] = 1;
        }
        Serial.printf("Index:%d Pin:%d Start:%d inc:%d\n", i, pin_list[i], 
          servo_pos[i],  pos_changes[i]);
      }
    //  Serial.printf("Active: %x  Allocated: %x\n",  servo_active_mask,servo_allocated_mask);
    }
    
    void loop()
    {
      for (uint8_t i = 0; i < COUNT_SERVOS; i++) {
        servo_pos[i] += pos_changes[i];
        if (servo_pos[i] >= 180) {
          pos_changes[i] = -1;
          //Serial.println(i, DEC);
        }  
        if (servo_pos[i] <= 0) pos_changes[i] = 1;
        servos[i].write(servo_pos[i]);
      }
      delay(15);                       // waits 15ms for the servo to reach the position
    }
    And verified with Logic Analyzer that all of the servos were doing something...

    Question is, to do a PR or not to do a PR...

  10. #10
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,549
    Now it is sort of an interesting question/comment to myself (and others).

    Ok So I made a version of the Servo library that works for up to 24 servos... The question to myself is do I have enough curiosity to actually try it out... Anyway I went to look to see if I had some of my earlier T3.2 Servo boards sitting around and if by chance I actually socket-ed the T3.2... The unfortunate answer is no... I do have a few T3.2/LC boards with 3 pin headers where I used a socket, but with these boards I hard wired either 3.3 or 5v to the power pins... (I did try out one of these boards with T4...) And the things I tried worked...
    Click image for larger version. 

Name:	Teensy-ax-board-bld.jpg 
Views:	2 
Size:	50.8 KB 
ID:	17257
    I did not try the sound or AX servos... I think I hacked up this board when the LC was in beta.

    But some of the earlier boards I did when I still was doing RC servos, all of the ones I found had the Teensy soldered into the board... If desperate, could probably unsolder... Also could maybe look in the different boxes and the like in my cabinet to see if I kept any of the unpopulated boards... But I were to do this, I would be more likely to want to simply order new boards...
    And if I were to order one, would I want one like below which I did for XBees or one with Arduino Shield like pinouts, like I did a version of later?

    Click image for larger version. 

Name:	Teensy-3.1x-breakout-brd-3D.jpg 
Views:	2 
Size:	70.5 KB 
ID:	17258

    And if I were going to order again what all should I change? Some possible maybe simple things include?
    a) This one is setup to use pins 9/10 for XBee. Why Serial2 works better on Serial3 on T3.x. BUT These pins are not Serial pins on T4, so logically should move to 7/8 which are. OR could just rely on Jumpers. OR sine 9/10 are FlexIO pins and I have a FlexIO library, which includes SerialIO, could put this to use...

    b) Sound - I just had simple buzzer and simple transister setup for sound, which is setup to jumper off of pin 6. Is this still a good choice?

    c) What to do with underlying pins? depending on usage I would maybe:
    1) add the 10 SMT type pins ...
    2) Probably hook up USB
    3) Probably punt on SD pins?

    d) Other Servo type connectors... This had one from of Robotis AX servos, not sure if level shifted or not... For this I might be tempted to:
    1) Either punt for These servos although they are what I mostly use. (Would probably use different level shifting including both pins 0/1...
    2) Maybe optionally have setup for Robotshop new servos with 4 pin connectors.

    Again hard to know where to draw the line?

  11. #11
    Junior Member
    Join Date
    Aug 2019
    Posts
    4
    @ KurtE It's great to see that you gained some interest in RC servos again. And that was a really fast fix for the library.
    I might have to look into this again on the weekend. Implementing the group move into you library should hopefully not be too complicated.

    And regarding the boards: Personally I am planning to make a board that's as slim as possible, whether it has 12 servos now or 18. With a micro servo robot there is just no room for anything wider than 40mm.
    Matt Denton's p.Brain is 35x68mm which would be my goal:
    Click image for larger version. 

Name:	MSR-uBug_Cable_rout_02.jpg 
Views:	3 
Size:	115.1 KB 
ID:	17259
    With the Teensy and two rows of servos there should be just enough room for voltage regulators and some peripherals:
    Click image for larger version. 

Name:	t4hex.PNG 
Views:	2 
Size:	72.9 KB 
ID:	17260

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •