Home Arduino Code Controlling the Virtuabotix Pan & Tilt with your Arduino or Versalino
Controlling the Virtuabotix Pan & Tilt with your Arduino or Versalino

Controlling the Virtuabotix Pan & Tilt with your Arduino or Versalino

1

As you may have guessed, the guide and code I put together to position a servo with calibrated degrees was for a reason other than that I could. Today we will be using that basic principle of mapping our servo calibration to the range of degrees we would like to cover.

Basically what we will be doing is taking the sketch we developed for 1 servo and doing it twice. 😉

 

/*####################################################
FILE: panTiltBasicControl.ino
VERSION: 1S0A
PURPOSE: Control Virtuabotix Pan & Tilt with calibrate Degrees.
LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)

GET UPDATES: http://josephdattilo.com/

HISTORY:
Joseph Dattilo (Virtuabotix LLC) - Version 1S0A (10/02/13)
###################################################*/

First of all this code like almost everything else I have ever coded is licensed Open Source under the GPL V3 license, so please always include a link to this Article, or Virtuabotix.com if you use it.

#include
#include //this version of code is for the Versalino

Servo AzimuthServo, ElevationServo;
byte AzimuthCalibration[2] = {10,200};
byte ElevationCalibration[2] = {10,200};
//you can change the above calibration code to
//match your servos range

Okay so now we start getting into the real functional components of the sketch, and we load libraries Servo.h and Versalino.h to allow control of our servos, and easy access to bus structures in our code.

After that we declare to Servo objects, one for Azimuth and one for Elevation. Finally we declare our calibration data for each servo. The calibration data is what determines the mapping on the output side, so make sure it is accurate to the range you are using. The values I used are pretty safe for most servos because 10 to 200 is usually within the operational range of a servo, but I would check yours just to be sure.

void setup()
{

AzimuthServo.attach(BUSA.P2);
ElevationServo.attach(BUSA.P3);
//attach Azimuth servo to S2 & Elevation to S3 on the Sense & Move
//and assumes that you have the Sense & Move on BUSA
}

Next we attach our servos to the appropriate pulse wave modulation (PWM) pins on our Arduino or Versalino. In this case we are using the Versalino Sense & Move so we will choose from BUSA.P1 to BUSA.P3 (BUSA.P2 and BUSA.P3 are used above).

If you wanted to run this sketch on any Arduino all you would have to do would be to substitute the PWM pin number in place of what is currently BUSA.P2 and BUSA.P3. This project can be down on any Arduino or Arduino compatible microcontroller that has at least 2 PWM pins.

void WriteDegreesToServo(Servo servoInQuestion, byte* ServoCalibration, int myDegrees)
{
servoInQuestion.write(map(myDegrees, -90, 90, ServoCalibration[0], ServoCalibration[1]));
}

The function above is used to direct a Servo to a calibrated angle (it is the same function we wrote in our last Servo guide). The Function takes a Servo object, a Byte Array, and the degrees to which you wish to direct the Servo.

void loop()
{

for(int servoangle=-90; servoangle <=90; servoangle++) { WriteDegreesToServo(AzimuthServo, AzimuthCalibration, servoangle); WriteDegreesToServo(ElevationServo, ElevationCalibration, servoangle); delay(15);//we delay long enough for the motion to finish }}

Now we put it all together in the main loop by sweeping both Azimuth and Elevation motors from -90 to 90 degrees with a short delay between steps to make it nice and smooth.

Note: You may need to direct one or more motors to angle 0, and adjust the servo positions by removing the screws and making sure they are centered the way you would like them to be. I personally like to put the Elevation at -90 and make sure it is level at that extreme, while with Azimuth ensuring center on 0 degrees works quite nicely.

Now we have a great way to control our Pan & Tilt with degrees instead of raw delay values, which is going to prove very helpful in our next few projects, and I certainly hope it is helpful for yours. As always let me know if you come up with something better, or you have any requests for my next project!

Joseph Dattilo Writer, Electrical Engineer, CEO and founder of Virtuabotix LLC, and completely crazy in every way.

Comment(1)

Comments are closed.