📜 ⬆️ ⬇️

Tracking focus for the SLR. Raspberry Pi + HC-SR04 + SG90 servo

This post describes how in one evening the tracking focus for the mirror was made of shit and sticks. Video of what happened:


Foreword


Raspberries are redundant here, indisputably. But if we are talking about a more complex device whose tracking focus is just one of the functions, then why not.

I did not invent anything new, I just took it and did it in one evening. I liked the result, so I want to share with the community.
')

Mechanics, mount


For me, the mechanic is always the stumbling block. Finding shit and sticks under the arms that are compatible with each other, while at the same time allowing you to achieve your goal, can be difficult. But this time I was lucky.

The prototype looks like this:



As a gear on the axis of the server, I used the lid of the milk carton on which I glued the rest of the yoke-gear for the lens of the DSLR. The milk cap in my case turned out to be the optimal diameter for adjustment in a 50mm lens. "Gear" glued with cyanoacrylate with soda to one of the rocker arms in the kit to the servo and fastened with a screw. To attach the structure to the DSLR, you need an inch-threaded screw, which I found from an octopus tripod.

In general, the design somehow somehow became reality:



Without screeds now difficult. The blue electrical tape ended and I had to mount the sensor on paper tape.

Electronics


My brain is the Raspberry Pi 3. Servo I power it with a separate 24VDC power supply with a DC-DC Step Down converter on an LM2596 microcircuit whose output is 7.2V.
The algorithm is simple to ugliness. There is feedback on the distance to the object from the ultrasonic rangefinder HC-SR04, which is converted into the angle of rotation of the SG-90 servo. On the servo axis, there is a “gear” that rotates the lens through the yoke-gear setting the focal length.

Code


The connection of the ultrasonic sensor is described here . Servo control is done using hardware PWM, the setting was taken here .

The code is very modest, but this is only the beginning and there is no limit to perfection.

#Libraries import RPi.GPIO as GPIO import time from subprocess import call from RPIO import PWM servo = PWM.Servo() #GPIO Mode (BOARD / BCM) GPIO.setmode(GPIO.BCM) #set GPIO Pins GPIO_TRIGGER = 18 GPIO_ECHO = 24 #set GPIO direction (IN / OUT) GPIO.setup(GPIO_TRIGGER, GPIO.OUT) GPIO.setup(GPIO_ECHO, GPIO.IN) def distance(): # set Trigger to HIGH GPIO.output(GPIO_TRIGGER, True) # set Trigger after 0.01ms to LOW time.sleep(0.00001) GPIO.output(GPIO_TRIGGER, False) StartTime = time.time() StopTime = time.time() # save StartTime while GPIO.input(GPIO_ECHO) == 0: StartTime = time.time() # save time of arrival while GPIO.input(GPIO_ECHO) == 1: StopTime = time.time() # time difference between start and arrival TimeElapsed = StopTime - StartTime # multiply with the sonic speed (34300 cm/s) # and divide by 2, because there and back distance = (TimeElapsed * 34300) / 2 return distance if __name__ == '__main__': try: while True: dist = distance() if dist <=40: focus = 900 elif dist <= 70: focus = 900+int(dist-40)*20 else: focus = 2000 servo.set_servo(23, focus) print ("Measured Distance = %.1f cm, focus = %3i" % (dist, focus)) time.sleep(0.1) # Reset by pressing CTRL + C except KeyboardInterrupt: print("Measurement stopped by User") servo.stop_servo(23) GPIO.cleanup() 

To determine the correspondence of the angle of rotation to the servo focal length, I made several calibration measurements, as a result I obtained a linear relationship in the region of interest (40–70 cm), the rest was taken as a focus at infinity.

Conclusion


In my opinion, even the cheapest SG-90 servers are capable of a lot, 1.2kg / cm stock for such babies, and even at such a speed - this is cool, especially for the price of 1.5 dollars. The same applies to the ultrasonic rangefinder.

P.S. A server can be controlled separately from the sensor, for example, to make quick transitions from one object to another with different focal lengths. Also, to improve the performance of the system, you can add a filter that will compensate for the shortcomings of the measurement of the ultrasonic sensor.

Source: https://habr.com/ru/post/410917/


All Articles