mamech
Full Member level 3
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) :
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.
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?
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?