4 Replies Latest reply on Apr 22, 2015 11:04 AM by martinianodl

    Arduino + PWM + Sabertooth 2x25

    martinianodl

        Hi all, I'm building a line follower robot and I was using a Arduino Mega 2560 and a Sabertooth 2x25 to control my motors, it worked perfect, but now I changed to the Intel Edison and my code is acting totally crazy.

      First I thought using my PID algorithm to control the robot on the line would work, but was not. Then I tested each motor independently and surprise!

      When I was using the Arduino Mega I could write 0 degrees to be full reverse, 180 degrees to be full forward and sending a servo command of 90 would stop the motor. But using the Edison my range dropped 20 degrees, so now I can't do full reverse because it's -20, full forward is now 160 and to stop the motor I have to send 70.

       

      Could someone explain to me what is happening?

       

      I don't know if it can change anything but I was using the ports 12 and 13 of the Arduino Mega and on the Edison I changed the PWM swizzler and now I'm using the ports 10 and 11. I'm also attaching the code that I'm using to test the motors and the picture of the configuration of the motor controller (Lithium mode, micro controller R/C input, independent mode with exponential control).

      exponential.jpg

       

      #include <Servo.h>
      
      
      Servo ST1, ST2; // We'll name the Sabertooth servo channel objects ST1 and ST2.
                      // For how to configure the Sabertooth, see the DIP Switch Wizard for
                      //  http://www.dimensionengineering.com/datasheets/SabertoothDIPWizard/start.htm
                      // Be sure to select RC Microcontroller Mode for use with this sample.
                      //
                      // Connections to make:
                      //  Arduino Pin 9  ->  Sabertooth S1
                      //  Arduino Pin 10 ->  Sabertooth S2
                      //  Arduino GND    ->  Sabertooth 0V
                      //  Arduino VIN    ->  Sabertooth 5V (OPTIONAL, if you want the Sabertooth to power the Arduino)
                      //
                      // Sabertooth accepts servo pulses from 1000 us to 2000 us.
                      // We need to specify the pulse widths in attach(). 0 degrees will be full reverse, 180 degrees will be
                      // full forward. Sending a servo command of 90 will stop the motor. Whether the servo pulses control
                      // the motors individually or control throttle and turning depends on your mixed mode setting.
      
      
      // Notice these attach() calls. The second and third arguments are important.
      // With a single argument, the range is 44 to 141 degrees, with 92 being stopped.
      // With all three arguments, we can use 0 to 180 degrees, with 90 being stopped.
      void setup()
      {
        setPwmSwizzler(3, 9, 10, 11);
        ST1.attach(11, 1000, 2000);
        ST2.attach(10, 1000, 2000);
      }
      
      
      void loop()
      {
          ST1.write(180);
          ST2.write(180);
          delay(1000);
          ST1.write(90);
          ST2.write(90);
          delay(1000);
          ST1.write(0);
          ST2.write(0);
          delay(1000);
      }
      

       

      Thanks!

        • 1. Re: Arduino + PWM + Sabertooth 2x25
          DiegoV_Intel

          Hi martinianodl,

           

          I have tested your code with a mini-servo I have and I didn't get a 0 - 180 degrees movement with the attach() calls you are using in your code so I changed them as below:

               ST1.attach(11); 

               ST2.attach(10);

           

          This is the default way to call the attach() method and it uses the default limits (544 and 2400). Using these calls the servo has been able to move from 0 to 180 degrees.

           

          Regards,

          Diego.

          • 2. Re: Arduino + PWM + Sabertooth 2x25
            martinianodl

            Hi DiegoV_Intel,

            I tried to use the code like you said

                ST1.attach(11);

                ST2.attach(10);

            but still not working. I ran some tests today with the oscilloscope and the Edison using the configuration

                ST1.attach(11, 1000, 2000);

                ST2.attach(10, 1000, 2000);

            The pulse width supposed to be 1000us for ST1.write(0); and 2000us for ST1.write(180), but it was a little bit bigger, so I tuned the 'attach' to output the same width as the Arduino and now I can send the same degrees as I was sending to Arduino. I thought I had solved my problems but when I ran a code to the motors output from 0 to 180, I could realize that they were increasing the speed in steps, so I ran in the Arduino and it works perfectly. I attached the code and a video comparing both Arduino and Edison, you can hear and see in the oscilloscope that the Edison is outputting the PWM in steps not smooth like the Arduino.

             

            Do you have any ideia how to solve this problem?

             

            The code:

            #include <Servo.h>
            
            
            Servo ST1, ST2;
            
            
            void setup()
            {
              setPwmSwizzler(3, 9, 10, 11);
              ST1.attach(11, 900, 1920);
              ST2.attach(10, 900, 1920);
            }
            
            
            void loop()
            {
            
              int power;
            
              // Ramp both servo channels from 0 to 180 (full reverse to full forward),
              for (power = 0; power <= 180; power ++)
              {
                ST1.write(power);
                ST2.write(power);
                delay(30);
              }
            
              // Now go back the way we came.
              for (power = 180; power >= 0; power --)
              {
                ST1.write(power);
                ST2.write(power);
                delay(30);
              }
            }
            
            
            
            
            
            
            

             

             

            Thanks!

             

            - Lucas Martiniano

            • 3. Re: Arduino + PWM + Sabertooth 2x25
              DiegoV_Intel

              Hi martinianodl,

               

              I have just tested your code but I am not able to get the 0 - 180 degrees movement unless I re-define the attach() line for

               

                   ST1.attach(11);

               

              without additional arguments.

               

              However, I got the same behavior as you with the speed in steps. That is because of the PWM resolution. Here is a thread with a discussion about the PWM resolution that you might find interesting: Edison PWM accuracy. I think it will respond your question.

               

              The Edison PWM accuracy is not as good as the Arduino board and it is reflected in the servo movement and the speed in steps.

               

              Regards,

              Diego.

              • 4. Re: Arduino + PWM + Sabertooth 2x25
                martinianodl

                Hi DiegoV_Intel,

                 

                Thank you for your help, I also found the thread Multiple servo controlling issues on Edison Arduino board which was very helpful.

                I ordered a I2C PWM controller made by Adafruit, I will test it and see if it can control the motors properly. I'm just worried about the delay because of the I2C communication.

                 

                Thank you again for your help, hopefully Intel can fix this problem in future revisions on the Edison.

                 

                - Lucas Martiniano de Oliveira