Go Down

Topic: Using constrain() to limit servo movement (Read 1 time) previous topic - next topic

After fiddling around with my dysfunctional, bloated Servo limit function, I decided to scrap it entirely and go with something else that was more intuitive. I knew that there must be something that would serve my purposes without using a bunch of if statements and switch statements. As it turns out--that function was constrain(). It's really easy to use if you have specific functions to adjust their respective servos. Here's how I did it:

Code: [Select]

void Plane::Aileron(byte desiredDir, int increase) {
  int deg;
  switch(desiredDir) {     
  case RIGHT: 
      deg = aileronServo.read()-increase;
      deg = constrain(deg, minAileronServoLimit, maxAileronServoLimit);
      aileronServo.write(deg);
    break;

  case LEFT:
...


In this case, I calculate the original servo command, constrain it to my 2 global variables, minAileronServoLimit and maxAileronServoLimit, and then Servo.write() the resulting integer.
RadarProject - Arduino Autopilot for RC http://radarproject.wordpress.com/

PeterH

You didn't ask a question, so I will: what are RIGHT and LEFT for?
I only provide help via the forum - please do not contact me for private consultancy.

RIGHT, LEFT, STABLE, and RESET are the byte inputs from the parameter "desiredDirection". They control the direction/behavior of the aileron servos.

Something like Aileron(RIGHT, 5); would increment the aileron servos in the direction to make the plane roll to the right. The function is meant to be used in a loop.
RadarProject - Arduino Autopilot for RC http://radarproject.wordpress.com/

PeterH

That's a curious way to do it, but if it works for you then fair enough.
I only provide help via the forum - please do not contact me for private consultancy.

RadarProject - Arduino Autopilot for RC http://radarproject.wordpress.com/

PeterH

I can't guess what the 'stable' and 'reset' functions are intended to do. Left and right appear to be relative moves. If I was controlling the aileron positions directly, I'd define separate methods to move the ailerons by a relative amount, to an absolute position, or whatever other type of movement I needed. I wouldn't lump all these different functions together into a single method with flags to say what functionality I was trying to access. Moving left and right are IMO essentially the same thing - moving by an amount which might be positive or negative - so I would expect one function to handle both directions.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy