Paul, I fully understand and thank you for your code. I didn't post up my code as it is too long. Christoph has been very helpful and gave me an example which is nearly there but I just cannot achieve my goal. I attach the relevant section of my code. The main purpose of this is to get acceleration and deceleration between 2 points. It is unidirectional at the moment.. The code will receive target updates over Ethernet (I appreciate there is a problem with new updates coming in whilst servo is moving) Also I can't find a way to put my code in an attachment, sorry.
int Ramp = 20;
int MaxDelay = 10;
int MinDelay= 2;
int diffPos=0;
int moveStart = 0;
int JMdelay=0;
boolean ServoMoving=false;
int CalcDelay(int i){
if(diffPos>=(2*Ramp)){ // ################## ie LONG DURATION ######################
if ( i<= diffPos-(MaxDelay-MinDelay) ) {dly=max(MinDelay,MaxDelay-i);}
else {dly=min(dly+1,MaxDelay);}
}
else if (diffPos<(2*Ramp)){ // ########### This gives linear ramp up and down again on short duration ie less than 2 * Ramps
if ( i <= max( diffPos/2, diffPos-(MaxDelay-MinDelay) ) ){dly=max(MaxDelay-i,MinDelay);}//
else
{dly=min(dly+1,MaxDelay);}
}
return dly;
}
void loop() {
CheckNewTarget()
MoveServo()
}
void CheckNewTarget(){
if (((targetPos-currPos) >0) && (!ServoMoving))
{
diffPos=targetPos-currPos;
moveStart=currPos;
Serial.print("######new target specified is : ");Serial.print(targetPos);
JMdelay=0;
ServoMoving=true;
}
}
void MoveServo(){
//static uint8_t JMdelay = 0; // static makes sure that these are not re-initialized to zero in every main loop.
static elapsedMillis servoTimer;
//Serial.print("time now : ");Serial.print(servoTimer);Serial.print(" JM delay: ");Serial.printLN(nextDelay);
if((targetPos > currPos) && (servoTimer >= JMdelay))
{
JMdelay=CalcDelay(currPos-moveStart);
Serial.print("Current Position: ");Serial.print(currPos);Serial.print(" steps into move : ");Serial.print(currPos-moveStart);Serial.print(" JMdelay: ");Serial.printLN(JMdelay);
servoTimer = 0;
currPos+=1;
//move servo to currentPos
JMservo.writeMicroseconds( currPos+900 );
Serial.print("Servo moved to: ");Serial.print(currPos);Serial.print(" Target: ");Serial.printLN(targetPos);
if (currPos = targetPos) {
JMdelay = 0; // we need this to properly restart the "loop" upon the next target update
ServoMoving=false;
}
}
}
int Ramp = 20;
int MaxDelay = 10;
int MinDelay= 2;
int diffPos=0;
int moveStart = 0;
int JMdelay=0;
boolean ServoMoving=false;
int CalcDelay(int i){
if(diffPos>=(2*Ramp)){ // ################## ie LONG DURATION ######################
if ( i<= diffPos-(MaxDelay-MinDelay) ) {dly=max(MinDelay,MaxDelay-i);}
else {dly=min(dly+1,MaxDelay);}
}
else if (diffPos<(2*Ramp)){ // ########### This gives linear ramp up and down again on short duration ie less than 2 * Ramps
if ( i <= max( diffPos/2, diffPos-(MaxDelay-MinDelay) ) ){dly=max(MaxDelay-i,MinDelay);}//
else
{dly=min(dly+1,MaxDelay);}
}
return dly;
}
void loop() {
CheckNewTarget()
MoveServo()
}
void CheckNewTarget(){
if (((targetPos-currPos) >0) && (!ServoMoving))
{
diffPos=targetPos-currPos;
moveStart=currPos;
Serial.print("######new target specified is : ");Serial.print(targetPos);
JMdelay=0;
ServoMoving=true;
}
}
void MoveServo(){
//static uint8_t JMdelay = 0; // static makes sure that these are not re-initialized to zero in every main loop.
static elapsedMillis servoTimer;
//Serial.print("time now : ");Serial.print(servoTimer);Serial.print(" JM delay: ");Serial.printLN(nextDelay);
if((targetPos > currPos) && (servoTimer >= JMdelay))
{
JMdelay=CalcDelay(currPos-moveStart);
Serial.print("Current Position: ");Serial.print(currPos);Serial.print(" steps into move : ");Serial.print(currPos-moveStart);Serial.print(" JMdelay: ");Serial.printLN(JMdelay);
servoTimer = 0;
currPos+=1;
//move servo to currentPos
JMservo.writeMicroseconds( currPos+900 );
Serial.print("Servo moved to: ");Serial.print(currPos);Serial.print(" Target: ");Serial.printLN(targetPos);
if (currPos = targetPos) {
JMdelay = 0; // we need this to properly restart the "loop" upon the next target update
ServoMoving=false;
}
}
}
Last edited: