Brushless Motorcontroll
Verfasst: Dienstag 24. Januar 2023, 19:14
Moin Moin,
ich bin da an so nem kleinen Projekt dran, aber komme irgendwie an meine Grenzen, da ich nicht aktiv am Coden bin.
Vielleicht kann mir jemand weiterhelfen? Ich zeige erstmal den Code.
import socket
import pigpio
import RPi.GPIO as GPIO
# Set up the pigpio library
pi = pigpio.pi()
servo_pin = 18 # Servo control pin
# Set up the GPIO pins for the brushless motor
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT) # ESC control pin
esc_pwm = GPIO.PWM(12, 50) # ESC PWM signal
esc_pwm.start(0)
# Connect to the Dualshock 4 controller
HOST = '0.0.0.0' # IP address of the Dualshock 4 controller
PORT = 1234 # Port number to connect to
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen()
print(f'Listening for incoming connections on {HOST}:{PORT}...')
conn, addr = s.accept()
print(f'Connection established with {addr}')
previous_speed = 0
while True:
data = conn.recv(1024).decode()
if not data:
break
data_list = data.split("\n")
for i in data_list:
# parse the received data to determine which axis the data corresponds to
if 'Axis 0' in i:
angle = float(i.split(':')[1])
print(f'Received angle for axis 0: {angle}')
# map the angle from 0 to 180 to a pulse width from 1000 to 2000
pulse_width = 1000 + (angle / 180) * 1000
pi.set_servo_pulsewidth(servo_pin, int(pulse_width))
elif 'Axis 5' in i:
speed = float(i.split(':')[1])
print(f'Received speed for axis 5: {speed}')
# map the speed from 0 to 100 to a duty cycle from 0 to 100
duty_cycle = speed
if speed > 0 and previous_speed == 0:
esc_pwm.start(duty_cycle)
elif speed == 0 and previous_speed > 0:
for i in range(previous_speed, 25, -1):
esc_pwm.ChangeDutyCycle(i)
time.sleep(0.01)
else:
esc_pwm.ChangeDutyCycle(duty_cycle)
previous_speed = duty_cycle
conn.close()
pi.stop()
GPIO.cleanup()
Nun zur Sache.
Ich hab ein RC Auto und möchte dies über ein ovpn steuern.
Das RC Auto ist ein WLTOYS 124017 V2.
Leider funktioniert mein Gaspedal nicht wirklich.
Ich hab die Range vom Sende eingestellt auf 0-10 für Axe5 leider reagiert dieser aber nicht.
Hättet ihr einen Plan warum das Ganze nicht funktioniert?
Teilweise reagiert der Motor teilweise nicht.
Über Tipps wäre ich mega glücklich !!!
Grüße,
Capo24
ich bin da an so nem kleinen Projekt dran, aber komme irgendwie an meine Grenzen, da ich nicht aktiv am Coden bin.
Vielleicht kann mir jemand weiterhelfen? Ich zeige erstmal den Code.
import socket
import pigpio
import RPi.GPIO as GPIO
# Set up the pigpio library
pi = pigpio.pi()
servo_pin = 18 # Servo control pin
# Set up the GPIO pins for the brushless motor
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT) # ESC control pin
esc_pwm = GPIO.PWM(12, 50) # ESC PWM signal
esc_pwm.start(0)
# Connect to the Dualshock 4 controller
HOST = '0.0.0.0' # IP address of the Dualshock 4 controller
PORT = 1234 # Port number to connect to
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen()
print(f'Listening for incoming connections on {HOST}:{PORT}...')
conn, addr = s.accept()
print(f'Connection established with {addr}')
previous_speed = 0
while True:
data = conn.recv(1024).decode()
if not data:
break
data_list = data.split("\n")
for i in data_list:
# parse the received data to determine which axis the data corresponds to
if 'Axis 0' in i:
angle = float(i.split(':')[1])
print(f'Received angle for axis 0: {angle}')
# map the angle from 0 to 180 to a pulse width from 1000 to 2000
pulse_width = 1000 + (angle / 180) * 1000
pi.set_servo_pulsewidth(servo_pin, int(pulse_width))
elif 'Axis 5' in i:
speed = float(i.split(':')[1])
print(f'Received speed for axis 5: {speed}')
# map the speed from 0 to 100 to a duty cycle from 0 to 100
duty_cycle = speed
if speed > 0 and previous_speed == 0:
esc_pwm.start(duty_cycle)
elif speed == 0 and previous_speed > 0:
for i in range(previous_speed, 25, -1):
esc_pwm.ChangeDutyCycle(i)
time.sleep(0.01)
else:
esc_pwm.ChangeDutyCycle(duty_cycle)
previous_speed = duty_cycle
conn.close()
pi.stop()
GPIO.cleanup()
Nun zur Sache.
Ich hab ein RC Auto und möchte dies über ein ovpn steuern.
Das RC Auto ist ein WLTOYS 124017 V2.
Leider funktioniert mein Gaspedal nicht wirklich.
Ich hab die Range vom Sende eingestellt auf 0-10 für Axe5 leider reagiert dieser aber nicht.
Hättet ihr einen Plan warum das Ganze nicht funktioniert?
Teilweise reagiert der Motor teilweise nicht.
Über Tipps wäre ich mega glücklich !!!
Grüße,
Capo24