arduino - Servo delay on direction change -
i'm using sg-90 servo , sweeping left , right between 2 values. problem there distinct delay of 500ms between direction changes. position of servo predictable, due @ direction changes not.
bool servodir = true; int servocnt = 90; int servoinc = 1; int servomin = servocnt - 5; int servomax = servocnt + 5; void loop() { if (servodir) { dx = servoinc; if (servocnt > servomax) { servodir = false; } } else { dx = -servoinc; if (servocnt < servomin) { servodir = true; } } servocnt += dx; servo.write(servocnt); // delay or other code here (mine 40ms) }
i've tried both arduino servo library , varspeedservo library. both show same thing.
what cause of , can it?
update
so if speed @ direction change, so:
int = 5; void loop() { if (servodir) { dx = servoinc; if (servocnt > servomax) { dx -= extra; servodir = false; } } else { dx = -servoinc; if (servocnt < servomin) { dx += extra; servodir = true; } } servocnt += dx; servo.write(servocnt); // delay or other code here (mine 40ms) }
the delay disappears, servo position become lot less predictable.
you experience these symptoms if servomin , servomax out of servo range.... are.
more seriously, these servos not precise. incrementing 1 @ time issue. experiencing form of backlash.
you should check , clamp count against range after incrementing/decrementing. that's 1 of basic rules of embedded programming. lives may @ stake, or equipment destroyed when sending out out of range values.
bool servodir = true; int servocnt = 90; int servoinc = 1; // <-- exclusively use control speed. int servomin = servocnt - 5; // <-- range bit short testing int servomax = servocnt + 5; void loop() { // 'jog' servo. servocnt += (servodir) ? servoinc : -servoinc; // past point, position jogging final if (servocnt > servomax) { servocnt = servomax; servodir = false; } else if (servocnt < servomin) { servocnt = servomin; servodir = true; } servo.write(servocnt); // 40ms sounds bit short tests. // 100 ms should give 2 seconds (0.5hz) oscillation delay(100); }
Comments
Post a Comment