Help with Servo Stop Command Upon Jam

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jb3422
 
Posts: 12
Joined: Sat Aug 17, 2013 4:04 pm

Help with Servo Stop Command Upon Jam

Post by jb3422 »

Hi,
I am trying to figure out how to have the arduino stop trying to increment the angle of a servo position if the desired position cannot be obtained within a time frame of 2 or 3 seconds. Basically, the servo in this project is used to open and close a door to let dog food flow through the system. When the closing procedure happens, the servo will get jammed with food and never reach the angle I set it for. When this happens, no other functions for the feeder can happen because of the jam. However, stopping the servo at the point of the jam is sufficient enough as no food will flow at this point. I just need to know how to get the servo to stop at this position so it doesn't burn out and also that the rest of the functions I have set for the system can run. My thought process for handling the jam is the following pseudo code:
- Start the servo close process
- If the close process does not reach desired angle in set time frame then stop servo from trying to reach the angle, and continue to other functions.
- else continue to other functions.

I've been trying to achieve this by modifying the basic servo example below.
Any help is greatly appreciated as I am a junior programmer and this is for a school project.

Code: Select all

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
int curAngle; 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    curAngle = myservo.read();
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(20);                       // waits 15ms for the servo to reach the position 
    
  } 
  
  if(curAngle <= 170){
    myservo.detach();
    digitalWrite(9, LOW);
  }else{
    for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
    {                                
      myservo.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(15);                       // waits 15ms for the servo to reach the position 
    }
  }
} // end setup
 
void loop() 
{ 
  
} 

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Help with Servo Stop Command Upon Jam

Post by Franklin97355 »

You will need something to tell the Arduino when it has reached the position you want and if it does not stop sending pulses. You could use a limit switch, either mechanical or optical.

User avatar
jb3422
 
Posts: 12
Joined: Sat Aug 17, 2013 4:04 pm

Re: Help with Servo Stop Command Upon Jam

Post by jb3422 »

Thank you for the advice franklin97355.
However, I would like to stay away from adding anymore hardware to the system.
I've been looking into the millis() command, but unclear how to use it in this context and not even sure if that would work.
Also, thought about maybe switching from using the servo library and just doing it the old fashion way with digitalWrite HIGH and LOW with delayMicroseconds(), but again, still reading into these options.

Do either of these sound like the might be viable options?

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Help with Servo Stop Command Upon Jam

Post by Franklin97355 »

Do either of these sound like the might be viable options?
No, a regular servo has no feedback from the servo. You could use these http://www.adafruit.com/products/1404 or a circuit that keeps track of the power used to drive the servo and stop it if the power level indicates the servo is stalled but that too will require additional hardware.

User avatar
jb3422
 
Posts: 12
Joined: Sat Aug 17, 2013 4:04 pm

Re: Help with Servo Stop Command Upon Jam

Post by jb3422 »

Thanks again for your advice.
I have one other thought before I commit to purchasing a new servo
that I'd like your feedback on.
What if I used time instead of position as the interrupting factor?

My next line of thought would be to do the following without using the servo library, because I am almost certain after doing a little digging that the servo library uses interrupts, so that would be the issue with trying to measure time against this unless I modify the library (which I have very little C experience).

However, if I manually controlled the servo, I'm wondering if I could then just power it down after a set amount of time. By powering it down, this would leave it in the position it was in last.

For Example something added to this code:

digitalWrite(servoPin, HIGH);
delayMicroseconds(1500);
digitalWrite(servoPin, LOW);

Thanks again for your help.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Help with Servo Stop Command Upon Jam

Post by adafruit_support_mike »

The code you posted above doesn't work the way you probably think it does.

The `servo.read()` function doesn't report the actual position of the servo. It just returns the value sent as a parameter for the last call to `servo.write()`.

The last value sent in the loop is 179, so your conditional will never see a value below 171. Therefore the `detach()` operation will never execute.

You don't have any feedback from the motor that reliably tells your code the actual position of the horn, so you're running what's called an 'open loop' control system. You send the commands telling the system what to do, and hope like heck that the results match the instructions.

That being the case, just remove everything after the first `for()` loop and you should get the behavior you want.

User avatar
jb3422
 
Posts: 12
Joined: Sat Aug 17, 2013 4:04 pm

Re: Help with Servo Stop Command Upon Jam

Post by jb3422 »

Thanks Mike.
I did get it figured out and it was the same that you said.
Just needed to understand the functions a little bit better.
Everything works as expected now.

Thanks again for your help guys.

When the project is finished, I'd love to share it with everyone.
Should be done in two weeks, barring no other major issues (fingers crossed).

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Help with Servo Stop Command Upon Jam

Post by adafruit_support_mike »

We love seeing photos of what people have made! ;-)

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”