adafruit / Adafruit_CircuitPython_ServoKit

CircuitPython helper library for the PWM/Servo FeatherWing, Shield and Pi HAT kits.
MIT License
70 stars 31 forks source link

Controlling ESC (Electronic Speed Controller) #54

Closed ugoi closed 2 weeks ago

ugoi commented 2 weeks ago

Can this library also be used to control an ESC? The goal is to remotely operate an RC car using a Raspberry Pi and the Adafruit 16-Channel PWM Servo Hat. I’ve successfully used this library to control the steering servos already.

ugoi commented 2 weeks ago

I found the solution. Using kit.continuous_servo[0].throttle directly allows control of the speed and direction of the ESC (Electronic Speed Controller) on channel 0 without additional setup. Positive throttle values move it forward, -1 applies the brake, and setting the throttle to 0 stops the motor. Here’s the code I used:

# Import the time module to allow for timed delays between actions
import time

# Import the ServoKit class from the adafruit_servokit library to control servo motors
from adafruit_servokit import ServoKit

# Set up the ServoKit instance with the number of channels (16 here for Shield/HAT/Bonnet)
kit = ServoKit(channels=16)

# Generic test sequence to test servo movement in different directions and speeds
print("Generic test")

# Stop the servo by setting throttle to 0 (neutral position)
print("Setting throttle to 0")
kit.continuous_servo[0].throttle = 0
time.sleep(2)  # Pause for 2 seconds to allow observation of the neutral position

# Set throttle to 1 (full forward)
print("Setting throttle to 1")
kit.continuous_servo[0].throttle = 0.13
time.sleep(2)  # Pause for 2 seconds to allow the servo to move forward

# Return throttle to 0 (neutral) to stop movement
print("Setting throttle to 0")
kit.continuous_servo[0].throttle = 0
time.sleep(2)  # Pause for 2 seconds to observe the stop

# Set throttle to -1 (full reverse)
print("Setting throttle to -1")
kit.continuous_servo[0].throttle = -0.13
time.sleep(2)  # Pause for 2 seconds to allow the servo to move in reverse

# Return throttle to 0 to stop movement again
print("Setting throttle to 0")
kit.continuous_servo[0].throttle = 0
time.sleep(2)  # Pause for 2 seconds to observe the stop

# Test the braking functionality with various throttle settings
print("Test break")

# Set throttle to 0 (neutral) before beginning brake test
print("Setting throttle to 0")
kit.continuous_servo[0].throttle = 0
time.sleep(2)

# Apply medium forward throttle (0.5)
print("Setting throttle to 0.5")
kit.continuous_servo[0].throttle = 0.5
time.sleep(2)  # Pause for 2 seconds to observe forward movement

# Immediately set throttle to -1 (full reverse) to simulate a braking effect
print("Setting throttle to -1")
kit.continuous_servo[0].throttle = -1
time.sleep(2)  # Pause for 2 seconds to observe quick reverse movement

# Return throttle to 0 to stop movement after braking
print("Setting throttle to 0")
kit.continuous_servo[0].throttle = 0
time.sleep(2)

# Apply medium reverse throttle (-0.5) for a slower reverse movement
print("Setting throttle to -0.5")
kit.continuous_servo[0].throttle = -0.5
time.sleep(2)  # Pause for 2 seconds to observe the slower reverse

# Finally, set throttle back to 0 to stop the servo
print("Setting throttle to 0")
kit.continuous_servo[0].throttle = 0
time.sleep(2)  # Final pause to observe the stop