Please note: Friday June 18 is a holiday celebrating Juneteenth, please allow extra time for your order to arrive and plan accordingly.
0

Servo moves "randomly" on initialization of script?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Servo moves "randomly" on initialization of script?

by nickstiv on Sun Apr 04, 2021 4:20 pm

So I have this weird issue that i've replicated with a pretty simple script, but it happens randomly, I can't determine any pattern.

using a a raspberry pi 4, an "Adafruit 16-Channel PWM / Servo HAT for Raspberry Pi", and 3 of the mg92b micro servos. Using a 4Amp 5v power supply from adafruit as well.
File: Test.py
Code: Select all | TOGGLE FULL SIZE
import time
from adafruit_servokit import ServoKit
kit = ServoKit(channels=16)
servo1open=180
servo1close=165

time.sleep(3)
kit.servo[1].angle=servo1open
time.sleep(1)
kit.servo[1].angle=servo1close


If i run this code in a while loop via bash:
Code: Select all | TOGGLE FULL SIZE
x=0; while true; do echo $x; python3 Test.py; x=$((x+1)); done



my servo will randomly go towards zero degrees (i only want it operating between 165 and 180) when the script is first initialized, but ONLY intermittently. Sometimes it happens the first time the script is run, sometimes it happens the 20th time it's run or any times in between.

Whenever the servo moves too far outside of the parameters I have set it breaks the 3d printed mounts I have the servos in, which then take like 6 hours to reprint. What's going on here?

here's the version of all my adafruit python libs
Code: Select all | TOGGLE FULL SIZE
Adafruit-Blinka                  6.4.0   
adafruit-circuitpython-busdevice 5.0.6   
adafruit-circuitpython-motor     3.2.8   
adafruit-circuitpython-motorkit  1.6.2   
adafruit-circuitpython-pca9685   3.3.5   
adafruit-circuitpython-register  1.9.5   
adafruit-circuitpython-servokit  1.3.3   
Adafruit-PlatformDetect          3.3.0   
Adafruit-PureIO                  1.1.8   
adafruit-python-shell            1.3.1   


If i do a while loop within python I haven't detected any issues yet of the servo moving to a 'random pos'. Only seems to be when i initialize the python script.

I tried setting the servo to a close position at the start of the script (before the first leep) then running my script, and i was still getting intermittent wacky behavior.

nickstiv
 
Posts: 9
Joined: Tue Mar 30, 2021 2:12 pm

Re: Servo moves "randomly" on initialization of script?

by nickstiv on Sun Apr 04, 2021 5:17 pm

I think i may have found my own problem. The servo requires 5v logic, but the hat outputs 3.3v logic. Would that cause the issue i'm seeing? Sounds like my options are either to use a level shifter? or different servos like the mg90d?

nickstiv
 
Posts: 9
Joined: Tue Mar 30, 2021 2:12 pm

Re: Servo moves "randomly" on initialization of script?

by adafruit_support_carter on Mon Apr 05, 2021 2:11 pm

Since you are using the Servo HAT, you shouldn't have to worry about any signal level issues. To verify basic servo functionality, try running the library example:
https://github.com/adafruit/Adafruit_Ci ... pletest.py

And just run it directly, not in bash loop.

Does that generally work as expected?

adafruit_support_carter
 
Posts: 20354
Joined: Tue Nov 29, 2016 2:45 pm

Re: Servo moves "randomly" on initialization of script?

by nickstiv on Mon Apr 05, 2021 3:53 pm

Yes, if i run the test script once it works like i'd expect it to.

nickstiv
 
Posts: 9
Joined: Tue Mar 30, 2021 2:12 pm

Re: Servo moves "randomly" on initialization of script?

by adafruit_support_carter on Mon Apr 05, 2021 3:59 pm

Then it may have something to do with the initial 3 second pause in your code. For those 3 seconds, no specific angle has yet been set.

adafruit_support_carter
 
Posts: 20354
Joined: Tue Nov 29, 2016 2:45 pm

Re: Servo moves "randomly" on initialization of script?

by nickstiv on Mon Apr 05, 2021 4:52 pm

adafruit_support_carter wrote:Then it may have something to do with the initial 3 second pause in your code. For those 3 seconds, no specific angle has yet been set.



that's what I thought when messing with it, but even if i set the angle on startup i get the same issue (see below) and here is a video, around iteration 11 (13seconds in) you see the top right servo wig out.

https://www.youtube.com/watch?v=wKl4Xdd43Os

Code: Select all | TOGGLE FULL SIZE
import time
from adafruit_servokit import ServoKit
kit = ServoKit(channels=16)
#kit.servo[0].actuation_range = 160
#kit.servo[0].set_pulse_width_range(1000, 2000)

#servo 0 left 
#servo 1 top right
#servo 2 bottom right

servo0open=0
servo0close=20
servo1open=180
servo1close=160
servo2open=0
servo2close=20

##Initiliaze servos set all to close to try an eliminate random behavior
kit.servo[0].angle=servo0close
kit.servo[1].angle=servo1close
kit.servo[2].angle=servo2close


time.sleep(1)
kit.servo[0].angle=servo0open
kit.servo[1].angle=servo1open
kit.servo[2].angle=servo2open
time.sleep(.1)
kit.servo[0].angle=servo0close
kit.servo[1].angle=servo1close
kit.servo[2].angle=servo2close


I've tried switching the servos and it follows the port which is defined to run between 180 and 160 instead of 0 and 20 like the other servos. I then set all my servos to run between 180 and 160 and they all had the same thing happen. I then set them all to run between 70 and 90 and the same behavior happened. To me it looks like the servos randomly want to go towards 0 degrees for some reason on initialization of the script. Since my other servos were at zero it wasn't happening. I only ever had this happen in practice when starting the script from bash which made me try this simplified script in a while loop.

I tried editing the code (see below) and running everything after defining the ServoKit in a while loop and I could not replicate it in python, only when the python script is initialized via bash. I did get it to wig out a couple times on instance 0 of running the loop in python, but never on subsequent instances, which also points to something weird happening on initialization.

Code: Select all | TOGGLE FULL SIZE
import time
from adafruit_servokit import ServoKit
kit = ServoKit(channels=16)
#kit.servo[0].actuation_range = 160
#kit.servo[0].set_pulse_width_range(1000, 2000)

#servo 0 left 
#servo 1 top right
#servo 2 bottom right
x=0

while True:
    print(x)
    servo0open=0
    servo0close=20
    servo1open=180
    servo1close=160
    servo2open=0
    servo2close=20
    ##Initiliaze servos set all to close to try an eliminate random behavior
    kit.servo[0].angle=servo0close
    kit.servo[1].angle=servo1close
    kit.servo[2].angle=servo2close


    time.sleep(1)
    kit.servo[0].angle=servo0open
    kit.servo[1].angle=servo1open
    kit.servo[2].angle=servo2open
    time.sleep(.1)
    kit.servo[0].angle=servo0close
    kit.servo[1].angle=servo1close
    kit.servo[2].angle=servo2close
    x+=1

nickstiv
 
Posts: 9
Joined: Tue Mar 30, 2021 2:12 pm

Re: Servo moves "randomly" on initialization of script?

by adafruit_support_carter on Mon Apr 05, 2021 5:16 pm

So it only happens when you try to drive the loop via bash? If so, that may be related to stuff being de-initialized with each completion of the code being run within the bash loop.

If you loop with within the Python code, it would be like:
Code: Select all | TOGGLE FULL SIZE
initialize stuff
loop forever
    move servos

vs. looping in bash, your Python code is:
Code: Select all | TOGGLE FULL SIZE
initialize stuff
move servos
de-initialize stuff

So you can get unexpected behavior with each bash loop.

adafruit_support_carter
 
Posts: 20354
Joined: Tue Nov 29, 2016 2:45 pm

Re: Servo moves "randomly" on initialization of script?

by nickstiv on Mon Apr 05, 2021 6:06 pm

Understood, but this loop in bash was a form of troubleshooting, not a way I actually want to run the code.

In practice I have program.py that controls my servos based on some triggering events. Every now and again (randomly) when i start program.py the servo would go to the wrong position and break my 3d printed parts (which take about 6 hours to print)! it happened a few times, and i reprinted parts a few times before i realized it was only happening when the code started, but not while it was running. So i wrote some test code and tried to replicate the issue, and i figured out it only happened on initiation of the code.


I understand that a bash loop is an artificial case, but it's replicating a problem that's happening in actual practical usage.

nickstiv
 
Posts: 9
Joined: Tue Mar 30, 2021 2:12 pm

Re: Servo moves "randomly" on initialization of script?

by adafruit_support_carter on Mon Apr 05, 2021 6:09 pm

Can you recreate the issue with a pure Python example? Where you could reliably and repeatably run:
Code: Select all | TOGGLE FULL SIZE
python3 problem_demo.py

and it would demonstrate the issue?

adafruit_support_carter
 
Posts: 20354
Joined: Tue Nov 29, 2016 2:45 pm

Re: Servo moves "randomly" on initialization of script?

by nickstiv on Mon Apr 05, 2021 6:18 pm

No. I think i said in a previous message, if i have a loop run purely in python, the issue does not occur while demo.py is running.

But if i start demo.py, there seems to be a 1 in 20 or so chance that it will result in the problem.

so the issue MAY occur when demo.py is first initialized, but never after that.

nickstiv
 
Posts: 9
Joined: Tue Mar 30, 2021 2:12 pm

Re: Servo moves "randomly" on initialization of script?

by adafruit_support_carter on Mon Apr 05, 2021 6:22 pm

Why is exiting the Python script necessary as part of an entire 3D print process?

adafruit_support_carter
 
Posts: 20354
Joined: Tue Nov 29, 2016 2:45 pm

Re: Servo moves "randomly" on initialization of script?

by nickstiv on Mon Apr 05, 2021 6:36 pm

I don't mean to be rude, but I'm not sure why the use case has any relevance to solving this unexpected behavior? Something is happening that shouldn't and i just want to try to find out why and come up with a solution.

Regardless I think there may have been a little miscommunication. The servos have nothing to do with a 3d print process.

The parts the servos are connected to are 3d printed. When the servo moves WAY outside of the parameters defined in the program it breaks those parts.

This is a script for a hobby device i'm making that isn't going to run continuously, always on isn't an option.

nickstiv
 
Posts: 9
Joined: Tue Mar 30, 2021 2:12 pm

Re: Servo moves "randomly" on initialization of script?

by adafruit_support_carter on Mon Apr 05, 2021 7:02 pm

Sorry, misread this:
Every now and again (randomly) when i start program.py the servo would go to the wrong position and break my 3d printed parts

as messing up an actual 3D print. You're just saying it's physically breaking the actual completed part.

Try setting all the angles to None before exiting the code. That still may not fix it since it might be caused by noise in the signal line happening when the code exits and the servos are still powered. Unfortunately, the OE (output enable) pin of the PCA is not broken out on the HAT, otherwise you could tie into that to disable the servos before exiting.

adafruit_support_carter
 
Posts: 20354
Joined: Tue Nov 29, 2016 2:45 pm

Re: Servo moves "randomly" on initialization of script?

by nickstiv on Tue Apr 06, 2021 10:45 am

Ok, I'll just go under the assumption that this is unavoidable and try to alter my design to be more forgiving of unexpected ranges.

Question, if I used the Adafruit 16-Channel 12-bit PWM/Servo Driver - I2C interface - PCA9685 PRODUCT ID: 815, it does have an OE pin available.

If OE is pin is activated: does it just disabled the signal wires? Or does it also disable power to the servos as well? If only the signal wire is disabled and it picks up noise, i'm assuming the same thing would happen as i'm seeing now?

nickstiv
 
Posts: 9
Joined: Tue Mar 30, 2021 2:12 pm

Re: Servo moves "randomly" on initialization of script?

by adafruit_support_carter on Tue Apr 06, 2021 11:18 am

The OE pin would disable power. The idea would then be to initialize the servo channels and set the initial angle so that a good signal is being sent. And then enable power. When done, disable power to insure you don't get odd behavior due to signal line glitches when the code exits.

adafruit_support_carter
 
Posts: 20354
Joined: Tue Nov 29, 2016 2:45 pm

Please be positive and constructive with your questions and comments.