Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Weird performance of a linear servo

Status
Not open for further replies.

mamech

Full Member level 3
Joined
Nov 9, 2010
Messages
176
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
3,135
Hello

I want to make a servo motor to move in sine wave motion. I believe that the code should be very easy, but strangely, the results are not as I expected.
first thing, I decided to use timer1 (the target is Arduino Uno) :
Code:
#include <Servo.h>
#include <TimerOne.h>            //Timer number one library
double t = 0;
float y = 0;
float f = 0.2; // frequency, and changing it will change speed of actuation
const double pi = 3.141593;
double w = 2 * pi * f;
float c = 0;   // phase shift
float d = 0;   // offset
float a = 10;  // max position amplitude
float resolution = 80;   // sine function number of samples per cycle
Servo myservo, myservo1, myservo2;  // create servo object to control a servo
boolean flag = false;
float sample_step;

float val = 0;   // variable to read the value from the analog pin
int i = 0;
void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo1.attach(10);  // attaches the servo on pin 10 to the servo object
  myservo2.attach(11);  // attaches the servo on pin 11 to the servo object
  Serial.begin(115200);
  sample_step = (1 / f) * 1000 / resolution;
  myservo.writeMicroseconds(1000);
  myservo1.writeMicroseconds(1000);
  myservo2.writeMicroseconds(1000);
  delay(3000);
  myservo.writeMicroseconds(1500);
  myservo1.writeMicroseconds(1500);
  myservo2.writeMicroseconds(1500);
  delay(3000);
}



void loop()
{

  y = a * sin(w * t + c) + d;
  y=y*100;
  int k = map(y, -1500, 1500, 1000, 2000);//(y + 10) * 100;
  myservo.writeMicroseconds(k);
  myservo1.writeMicroseconds(k);
  myservo2.writeMicroseconds(k);
  Serial.print(k);
  Serial.print("  ");
  Serial.println(t);
  while (i < sample_step)
  {
    delay(1);
    i++;
  }
  i = 0;
  t = t + (1 / f) / resolution;
}

The servo did not work at all. I believe that there is a problem in conflict between timer1 and servo library.
so I changed the code to be delay based.
Code:
#include <Servo.h>
#include <TimerOne.h>            //Timer number one library
double t = 0;
float y = 0;
float f = 0.2; // frequency, and changing it will change speed of actuation
const double pi = 3.141593;
double w = 2 * pi * f;
float c = 0;   // phase shift
float d = 0;   // offset
float a = 10;  // max position amplitude
float resolution = 80;   // sine function number of samples per cycle, do not exceed 1000
Servo myservo, myservo1, myservo2;  // create servo object to control a servo
boolean flag = false;
float sample_step;

float val = 0;   // variable to read the value from the analog pin
int i = 0;
void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo1.attach(10);  // attaches the servo on pin 10 to the servo object
  myservo2.attach(11);  // attaches the servo on pin 11 to the servo object
  Serial.begin(115200);
  sample_step = (1 / f) * 1000 / resolution;
  myservo.writeMicroseconds(1000);
  myservo1.writeMicroseconds(1000);
  myservo2.writeMicroseconds(1000);
  delay(3000);
  myservo.writeMicroseconds(1500);
  myservo1.writeMicroseconds(1500);
  myservo2.writeMicroseconds(1500);
  delay(3000);
}



void loop()
{

  y = a * sin(w * t + c) + d;
  y=y*100;
  int k = map(y, -1500, 1500, 1000, 2000);//(y + 10) * 100;
  myservo.writeMicroseconds(k);
  myservo1.writeMicroseconds(k);
  myservo2.writeMicroseconds(k);
  Serial.print(k);
  Serial.print("  ");
  Serial.println(t);
  while (i < sample_step)
  {
    delay(1);
    i++;
  }
  i = 0;
  t = t + (1 / f) / resolution;
}

this code worked, but this resulted in sluggish performance (motion is not smooth):
https://www.youtube.com/watch?v=m0AEOE2SOuY&feature=youtu.be

in the video only one motor is connected

what is wrong in my code? any suggestions?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top