bitsy-ai / rpi-object-tracking

Object tracking tutorial using TensorFlow / TensorFlow Lite, Raspberry Pi, Pi Camera, and a Pimoroni Pan-Tilt Hat.
https://medium.com/@grepLeigh/real-time-object-tracking-with-tensorflow-raspberry-pi-and-pan-tilt-hat-2aeaef47e134
MIT License
178 stars 70 forks source link

PID controller improvements #48

Open leigh-johnson opened 3 years ago

leigh-johnson commented 3 years ago

Summary

Currently, the PID controller implements basics from Adrian Rosebrock's blog post Pan/tilt face tracking with a Raspberry Pi and OpenCV. The PID gains are hard-coded, roughly tuned to track a desk-height object no further than a few meters.

Work Required

Martin2kid commented 3 years ago

Hello Leigh!

Great work & I really admire your extensive effort put into this project and shared with us and look forward PID fine tune capability(GUI would be great).

I was finally able to make my Pi4 work with RDP with Coral Edge TPU (Newbie's Milestone), although I just followed your instruction and it worked this time without Tf error! --As you may recall, I tried with V1.0 about six month ago & was not able to make it work due to tflite_run-time error & compatibility issues and probably gave you bunch of headache back then.

I'm back & tried again several times with install and uninstall process in last few days and finally was able to make it work(Github instruction, thought Tf runtime would be installed but realized that's additional step), I ran few times and I think it detect much better than Pi4's Movidius + Caffe+OpenCV+Openvino configuration with almost no sudden twitches or zitter like that of mentioned combination, very little minor back ground lighting issues and face detection at much better rate of about 8 feet, all in predictable smooth servo movement.

However, I noticed few things(I ran rpi-deep-pantilt track --edge-tpu face); 1: When Pi start(power on), servo position goes far left lower corner and stay there until RDP start and then it start oscillate unless face is presented closer to Picam.-- I guess this is more of Pimorini servo hat's issue that somehow sending high PWM signal prior to input signal of Pi4's processing output---(I think servo motor should be powered by external power source to prevent this & will experiment)

2: When in condition 1 & working, when face is too far(5') or farther and sudden face position changes, servo goes into oscillate and it's movement progresively amplifies and stops oslillate when face gets closer to camera (less than 12" from camera)---I guess this is PID issue and look forward your work around for ease of PID adjustment implementation here.---(I was able to change P of tilt servo to .10 and corrected this)

3: When RDP is running, tracking is pretty good and servo moves pretty smooth until subject(face) goes out of focus range(distance farther than about 8 feet and beyond servo's 90 degree maximum position --- camera slowly moves facing up or sometimes down and stop & stuck there(probably servo's maximum mechanical range and hangs, when I manually turn camera toward face it detect face but not resume tracking.---I would think this to be returned to pre-defined position in loop & resume tracking without hang there(in the center position)?

Is there setting that I can change for this(like 0 degree for pan & 60 degree for tilt)? if so, which file?

That small noisy RC-servo is notorious for fine positional control with quite bit of lagging & rotation limit and is not easy to set consistent working PID.

Have you thought about using direct driven brushless motor FOC control like here?https://youtu.be/TnECJG2mN-E or this at https://simplefoc.com

I like Pablo's 2 motor controller approach over SimpleFOC's single and ordered couple for experiment.

Also, Justine at https://youtu.be/OZvjfbpXpro has dual controller but not sure this support magnetic encoder(cheap & quite accurate) and what I wanted to use.

All of them shared their code & I hope someone can figure ways to drive PWM or I2c over 360 degree closed loop position control and multiple or continuous turn closed loop position control(Newbie here)

br0kej commented 3 years ago

Hello,

I came across this repo a couple of days ago and checked the issues! I am not sure how useful this will be but I saw a post on Reddit today of a Rpi/Arduino powered nerf gun aimbot which seems to be using similar ideas and seems very smooth. When I downloaded and setup some of the examples, the face tracking was pretty rough, especially at close range. Hopefully this could be of use to support the development of the features above - Github Link / Reddit Link

Martin2kid commented 3 years ago

Hi,

Have you tried with brushless motor with higher poll count over 12 & not stepper?

I actually tried with 32 poll motor with Simplebgc32 controller that uses same FOC control method here and it provided very smooth rotation in silence(Note this ran with intel Movidius+Caffe+OpenCV+Openvino); https://youtu.be/Ce-c9StqzsE

The problem that I had with such IMU based controller(Simplebgc32 & Storm32bgs) is that they all drift(slowly turn about couple or more degrees per an hour) due to gyro drift, even with magnetometer correction setup. and that's the main reason I was looking for one without IMU based controller but with FOC direct controller.

It appear conventional & readily available PWM input control method can only control 180 degree without scaling, although pan motor is capable of rotate over 360 degree --this limits tracking angle and I was hoping for alternatives. I'm also looking into I2c output control & still searching for closed loop position control over 360 degree(perhaps zeroing at each turn and start count over?)

As you may aware of, Haar-cascade method can only detect very limited facial detection & it's detection accuracy probably about 30% which naturally produce very jerky movement over PWM output.

From Newbie

Martin2kid commented 3 years ago

Leigh,

Per closed issue of #50, I've been thinking about how your setup & mine were producing different result, although we both have same setup of Pimorini pan tilt hat & same camera orientation.(assuming your setup were working with resume tracking of face when it return to FOV) but mine didn't, although I'm using Buster 8-20-2020.

I thought about typical servo motor that Pimorini used on this hat that batch of production run for my brushed motor servo may have different gear mesh tolerance, motor output torque or gear friction tolerance level that in-turn, may have affected PID rate and I suspected that may have produced totally different outcome (mine hangs).

Per your input, I've changed PID values hoping that will fix this behavior but it didn't.

Martin2kid commented 3 years ago

Leigh,

After experimenting with numerous PID setting with much faster FOC brushless motors, I'm beginning to think PID control loop may not needed at all for this particular application with FOC driven brushless motor type or perhaps PID control loop for this type application is actually causing random oscillation & other strange behavior during on & off object recognition processing output in random.

I think PID control loop works pretty well on inertia energy related servo motors such as Pimorini's or standard RC servo motors with higher reduction gear ratio type but not with FOC brushless motors with zero reduction gear ratio.

Thought about PID math formula to predict or smooth out this curve without oscillation during object on & off recognition and FOC direct driven brushless motors & that's just really beyond mine.

Can there be anyway to by-pass PID control loop? and hopefully that will also fix subject out of FOV related issue as well.

Martin2kid commented 3 years ago

Leigh,

I've tried to make FOC BLDC controller(not IMU based Gimbal controller such as; Simplebgc32 or Storm32 due to gyro drift) to work with rpi-deep-pantilt & none of developing platform based on Arduino or STM32 supporting PWM input except Odrive that is rather pricy ($120 plus)

After several month out of head scratching, I finally ordered couple of Drive which I'm sure is overkill, yet it support PWM input & also provide 360 degree magnetic absolute sensor support such as AS5048a.

I'll update outcome here after test drive with Software PID & Combined with both hardware PID control loop.

Hoping you would find solution for out of FOV behavior soon!