Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4094

Interfacing (DSI, CSI, I2C, etc.) • Re: Raspberry Pi Joystick examples

$
0
0
Hum ADC1115 isn't a servo control but an A/D converter.
Sorry for ambiguous post. I meant ADC + Servo. In other words: Analog to Digital Converter + Servo motor driven via PCA9685 driver)

Thanks for your elaboration, but this part seems confusing because 500-2500 is value range PCA9685 accepts, but it seems we need joystick values, also it's hard to understand if you refer `max` and `min` python functions, or it's a part of the variable, in a code below:
min AdC VALUE = 500
max ADC VALUE = 2500
From them calculate the slope and offset

slope = (2500-500) / (max-min ADC Value)
offset = 500 - ( slope ( min ADC VALUE))

Then the function is Servo(ADC1115Value) = ( ADC1115Value * slope) + offset;

et voila! High school math!

Of course if it isn't linear you could create a curve fit but you need more point then.
Can you be so kind to adjust example to an actual code below? :

Code:

#!/usr/bin/pythonimport timeimport Adafruit_ADS1x15import RPi.GPIO as GPIO# https://github.com/v-thiennp12/palt-tilt-cam/blob/master/PCA9685.pyfrom PCA9685 import PCA9685# Create an ADS1115 instanceADC = Adafruit_ADS1x15.ADS1115()# Set the gain to ±4.096V (adjust if needed)GAIN = 1# PCA9685 intended pulse rangeMIN_PULSE_VALUE = 500MAX_PULSE_VALUE = 2500# Joystick X values, received through ADC1115MIN_X_VALUE = 18CENTER_X_VALUE = 19773MAX_X_VALUE = 32767# Joystick Y values, received through ADC1115MIN_Y_VALUE = 25CENTER_Y_VALUE = 19587MAX_Y_VALUE = 32767try:    # Create PWM driver object and set both servos to initial position    pwm = PCA9685()    pwm.setPWMFreq(50)        # Required PWM frequency    pwm.setServoPulse(0, 500) # PAN    pwm.setServoPulse(1, 500) # TILT    while True:        X_pos = int(ADC.read_adc(2, GAIN))        Y_pos = int(ADC.read_adc(3, GAIN))        print(f"X: {X_pos} Y:{Y_pos}")except:    pwm.setServoPulse(0, 500)    pwm.setServoPulse(1, 500)       pwm.exit_PCA9685()    print("\nTerminating")    exit()
Best regards, Sergio.

Statistics: Posted by xnemox1 — Wed Jan 31, 2024 10:16 am



Viewing all articles
Browse latest Browse all 4094

Trending Articles