I have created this code for a DOCYKE 350 kg hybrid servo motor. The motor is supposed to create a single sine wave, but for some reason the servo creates a sine wave within another. I have a tilt angle sensor that allows me to track the motion of the motor which is how I know that there is a sine wave inside od a sine wave. I am not sure if its creating a separate sine wave but it seems to be moving like one and the sine wave I am trying to make isnt consistent.
#include <Servo.h>
Servo servo_1;
int Ampl = 32.536;
int frequency = 1;
int pos1 = 90;
void setup()
{
servo_1.attach(9, 800, 2200);
Serial.begin(9600);
}
void loop()
{
float value = millis()*2;
pos1 = 90.0+((Ampl)*sin(2*3.14*frequency*value-0));
pos1 = constrain(pos1, 0, 180);
servo_1.write(pos1);
Serial.println(pos1);
}
The line that has millis() in it is multiplied by 2 because it gives me the most consistent line, if that is my issue please let me know.
Okay I will change the int to float thank you! I failed to mention that I am using a 10:1 gearbox so yes the values of the servo motor are 58-122 but the gearbox cuts that by 10.
The reason it goes into negative territory is due to the position of the tilt angle sensor, when the servo is at position 90 the tilt angle is 0 degrees. its not necessarily another sin wave it just looks like one, I'm more concerned about how inconsistent the wave is.
remember you are sampling a sine wave based on time and you take some time to move the servo and execute the code so you are not 100% guaranteed to always hit the top of the sine wave
if you really want to follow the sine wave, then don't depend on time and just walk the sine wave by incrementing time by a fixed fraction of a radian every step