Two servos at once?

Status
Not open for further replies.
Can the teensy 3.2 control two continuous micro servos at once?
can someone please give me the example code?
I am a noob, sorry!

THANKS!
 
I believe they are referring to a servo with no end points.. it will continually turn CW or CCW.

If I recall, they stop at a normal servo centerpoint and go faster one direction with long pulses and faster the opposite direction with the short pulses.

Used for simple robotic tank like steering for one example.
 
Indeed continuous servos have no stops ... I found this :: rg-continous-rotation-servo

PWM control
These servos can be controlled via a standard PWM signal, the same one used in most hobby servos. Instead of controlling servo position, you are able to control servo speed (i.e. a 0° signal = full speed counter clockwise, 180° signal = full speed clockwise, and a 90° signal = stopped). The servo even has an external potentiometer for adjusting the deadband on the servo so you can calibrate it.
This signal is easy to understand and can easily be generated by most microcontroller systems. The Arduino environment even has a built in library for easily working with PWM servos.

And from arduino.cc/en/Reference/Serv this note suggesting they should work as indicated above: ... Continuous rotation servos allow the rotation of the shaft to be set to various speeds. ...
 
Last edited:
This is the code I use on the ProMicro for a basic autonomous Rover. It uses two Continuous servos and knock off parallax range sensor. For the servos the pins must be PWM pins. I have not tested this on the teensy. I originally ported the code and made very small modification from trinket platform to the Arduino Pro Micro (leonardo).

Code:
//ROVER code orginally copied from Adafruit.com.  Code modified 
//by Chris Rees for Robotics class


#include <Servo.h>

#define SERVO1PIN 9   //Servo control line (orange) on Pro Micro Pin #0
#define SERVO2PIN 10   //Servo control line (orange) on Pro Micro Pin #1

Servo servo_left, servo_rght;

// Connect the trig signal pin to this pin on the Trinket.
const int trig = 7; //(Gray or Orange)
const int echo = 8; //(Purple or Blue)

// Moderate speed forward for both servos. Given the orientation of the servos
// one will be going forward, and the other backward. You may need to adjust 
// these slightly to get the rover to move straight forward.
const int left_speed = 90;
const int rght_speed = 90; //increase if rover is going left 

// Number in cm when the rover will reverse and try to navigate around.
const int obstacle = 12;

// Multiplier used to determine how far the rover will back-up.
const int back_track = 200;

// Duration of a ping, distance in inches, distance converted to cm.
long duration, inches, cm;

// Setup Correction values
int correction = 0;

void setup() 
{  

  delay(2000);
   
  Serial.begin(9600); //This pipes to the serial monitor
  
 
    // Attach servos... and off we go!
  servo_left.attach(SERVO1PIN);
  servo_rght.attach(SERVO2PIN);
} 

void loop() 
{ 
correction++;
  if (correction == 25){
    correction = 0;
    servo_rght.write(75);
    servo_left.write(105);
    delay(25);
  }
  // Setting servos in forward motion.

  servo_left.write(left_speed + cm);
  //servo_left.refresh();
  servo_rght.write(rght_speed - cm);
  //servo_rght.refresh();
  delay(15);  


  // establish variables for duration of the ping, and the distance 
  // result in inches and centimeters.
  duration = 0;
  inches = 0;
  cm = 0;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trig, OUTPUT);
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(5);
  digitalWrite(trig, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echo, INPUT);
  duration = pulseIn(echo, HIGH);

  if ( duration == 0 ) {
  // SR04 unlock
  pinMode(echo, OUTPUT);
  digitalWrite(trig, LOW);
  delay(50);
  pinMode(echo, INPUT);
}
  // convert the time into a distance.
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);  

  // Long distances will cause the servos to misbehave... cap at 50 cm.
  if ( cm > 50 ) {
    cm = 50;
  }

  // text display tests
 
  Serial.println("Distance in cm is ");
Serial.println(cm);



  

  if ( cm < obstacle ) {
    // back_track * delay(15) = distance the rover will back-up during 
    // obstacle avoidance.
    for (int i = 0; i < back_track; i++) {
      // Magic numbers... will always backup the same direction. Can you 
      // think of a better way to navigate obstacles?
      
      servo_left.write(80);
      //servo_left.refresh();
      servo_rght.write(150);
      //servo_rght.refresh(); 
      
      delay(15);
    }
  }
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
 
Last edited:
Status
Not open for further replies.
Back
Top