pageauc / face-track-demo

Raspberry Pi python PiCamera, OpenCV Face and Motion Tracking using pan/tilt assembly via servo controller or gpiozero pwm
40 stars 25 forks source link

how to invert servo direction #4

Closed WWE-Corey closed 2 years ago

WWE-Corey commented 2 years ago

Love the work you have done. I built a face tracker and guess my design swapped the direction of the servo motion (horizontal in particular). Where in the code could I invert this?

pageauc commented 2 years ago

The best way is to change over the servo wires on the controller

If you are using gpiozero simply switch the gpio pin numbers in the config.py so in the example below 17 and 23 are reversed. Your pins may be different.

GPIOZERO_ON = True # False = use pipan servo driver, True = gpiozero

pan/tilt servo driver GPIOZERO_PAN_PIN = 17 GPIOZERO_TILT_PIN = 23

If you are using pi-pan you could also try reversing the wiring Alternatively you could change the code at line 174 and 180 of face-track-pipan.py

so at line 174 p.do_pan(int(x)) becomes p.do_tilt(iint(x)) and at line 180 p.do_tilt(int(y)) becomes p.do_pan(int(y))

I have not tested this so not sure if it will screw things up in the program but worth a try.

Long term the best way would be to add a new option eg SERVO_FLIP boolean variable to config.py and write code to implement the servo changes above based on whether SERVO_FLIP is True or False. This is a little more involved and depends on the weather changes to 174 and 180 work OK.

eg code at line 174 if SERVO_FLIP: # is True so flip pan and tile p.do_tilt(int(x)) else: p.do_pan(int(x))

and similar logic for y variable at line 180

Also I now use a pimoroni or waveshare pantilt hat. This is done in the pi-timolo code.

This code is very old and I have not looked at it in 4 years so code could be spruced up.

Let me know how you make out.

Claude ...

On Tue, Jun 28, 2022 at 3:19 PM Corey Johnson @.***> wrote:

Love the work you have done. I built a face tracker and guess my design swapped the direction of the servo motion (horizontal in particular). Where in the code could I invert this?

— Reply to this email directly, view it on GitHub https://github.com/pageauc/face-track-demo/issues/4, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABNPKZDDRSH6PMWSWGPP3EDVRNF4PANCNFSM52DHXWOA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

-- YouTube Channel at https://www.youtube.com/user/pageaucp http://www.youtube.com/user/pageaucp GitHub Repository at https://github.com/pageauc

WWE-Corey commented 2 years ago

Thank you! I will tinker with it and see where I end up.

pageauc commented 2 years ago

FYI

I have a repo for waveshare pantilt hat https://github.com/pageauc/waveshare.pantilthat

https://www.amazon.ca/Waveshare-Pan-Tilt-Raspberry-Onboard-Intensity/dp/B07Q5W6P3N/ref=sr_1_1?crid=8GF1LNGAGHXX&keywords=waveshare+pan+tilt+hat&qid=1656496614&sprefix=waveshare+pantilt+hat%2Caps%2C65&sr=8-1

Some assembly required. The test programs also works with the pimoroni pantilt hat but it is more expensive. These work on pan -90 to +90 deg tilt -90 to +90 deg So center position is 0,0. This is different than facetrack repo where servo pan tilt positions are 0 to 180 degrees so center position is 90,90 To make compatible with waveshare and pimoroni you would need to subtract 90 deg to get position based on -90 to + 90

https://www.amazon.ca/Pimoroni-Pan-Tilt-Full-Micro-Servos/dp/B01N5E3I3W/ref=sr_1_1?crid=3KIPST5UGMY35&keywords=pimoroni+pan+tilt+hat&qid=1656496782&sprefix=pimoroni+pan+tilt+hat%2Caps%2C52&sr=8-1

These are Canadian Amazon links.

Claude ...

On Tue, Jun 28, 2022 at 10:22 PM Corey Johnson @.***> wrote:

Thank you! I will tinker with it and see where I end up.

— Reply to this email directly, view it on GitHub https://github.com/pageauc/face-track-demo/issues/4#issuecomment-1169462320, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABNPKZGWK5EYFU32QZZTTYDVROXOPANCNFSM52DHXWOA . You are receiving this because you commented.Message ID: @.***>

-- YouTube Channel at https://www.youtube.com/user/pageaucp http://www.youtube.com/user/pageaucp GitHub Repository at https://github.com/pageauc

WWE-Corey commented 2 years ago

So my starting point was https://core-electronics.com.au/guides/Face-Tracking-Raspberry-Pi/ which was based on your code.

How I got it to work was some trial and error of WHERE to place the "-". Finally found it outside the int call parameter.

Thank you for your code and help!

def pan_goto( x, y):
    """ Move the pan/tilt to a specific location."""
    if x < pan_max_left:
        x = pan_max_left
    elif x > pan_max_right:
        x = pan_max_right
    pan(-(int(x-90))) # added - to invert
    time.sleep(pan_servo_delay)
    # give the servo's some time to move
    if y < pan_max_top:
        y = pan_max_top
    elif y > pan_max_bottom:
        y = pan_max_bottom
    tilt(-(int(y-90))) # added - to invert
    time.sleep(pan_servo_delay)  # give the servo's some time to move
    if verbose:
        print(("pan_goto - Moved Camera to pan_cx=%i pan_cy=%i" % (x, y)))
    return x, y